Skip to content

Commit

Permalink
Allow construction of only valid Identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
vaivaswatha committed Jun 12, 2024
1 parent fa53d87 commit 9b24832
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/builtin/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl FuncOp {
// Create an empty entry block.
let arg_types = ty.deref(ctx).get_inputs().clone();
let region = op.deref_mut(ctx).get_region(0).unwrap();
let body = BasicBlock::new(ctx, Some("entry".into()), arg_types);
let body = BasicBlock::new(ctx, Some("entry".try_into().unwrap()), arg_types);
body.insert_at_front(region, ctx);
{
let opref = &mut *op.deref_mut(ctx);
Expand Down
6 changes: 5 additions & 1 deletion src/debug_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ mod tests {
builtin::register(&mut ctx);

let i64_ty = IntegerType::get(&mut ctx, 64, Signedness::Signed);
let block = BasicBlock::new(&mut ctx, Some("entry".into()), vec![i64_ty.into()]);
let block = BasicBlock::new(
&mut ctx,
Some("entry".try_into().unwrap()),
vec![i64_ty.into()],
);
set_block_arg_name(&ctx, block, 0, "foo".to_string());
assert!(get_block_arg_name(&ctx, block, 0).unwrap() == "foo");
block.deref(&ctx).verify(&ctx)?;
Expand Down
2 changes: 1 addition & 1 deletion src/dialect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct DialectName(Identifier);
impl DialectName {
/// Create a new DialectName
pub fn new(name: &str) -> DialectName {
DialectName(name.into())
DialectName(name.try_into().expect("Invalid Identifier for DialectName"))
}
}

Expand Down
36 changes: 18 additions & 18 deletions src/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use combine::{token, Parser};
use thiserror::Error;

use crate::{
common_traits::Verify,
context::Context,
parsable::{self, Parsable, ParseResult},
printable::{self, Printable},
Expand Down Expand Up @@ -35,15 +34,23 @@ impl Display for Identifier {
}
}

impl From<String> for Identifier {
fn from(value: String) -> Self {
Identifier(value)
impl TryFrom<String> for Identifier {
type Error = result::Error;

fn try_from(value: String) -> Result<Self, Self::Error> {
let re = regex::Regex::new(r"[a-zA-Z_][a-zA-Z0-9_]*").unwrap();
if !(re.is_match(&value)) {
return verify_err_noloc!(MalformedIdentifierErr(value.clone()));
}
Ok(Identifier(value))
}
}

impl From<&str> for Identifier {
fn from(value: &str) -> Self {
Identifier(value.to_string())
impl TryFrom<&str> for Identifier {
type Error = result::Error;

fn try_from(value: &str) -> Result<Self, Self::Error> {
TryFrom::<String>::try_from(value.to_string())
}
}

Expand All @@ -65,16 +72,6 @@ impl Deref for Identifier {
#[error("Malformed identifier {0}")]
struct MalformedIdentifierErr(String);

impl Verify for Identifier {
fn verify(&self, _ctx: &Context) -> result::Result<()> {
let re = regex::Regex::new(r"[a-zA-Z_][a-zA-Z0-9_]*").unwrap();
if !(re.is_match(&self.0)) {
return verify_err_noloc!(MalformedIdentifierErr(self.0.clone()));
}
Ok(())
}
}

impl Parsable for Identifier {
type Arg = ();
type Parsed = Identifier;
Expand All @@ -89,7 +86,10 @@ impl Parsable for Identifier {
.map(|(c, rest)| c.to_string() + &rest);

parser
.map(|str| str.into())
.map(|str| {
str.try_into()
.expect("Something is wrong in our Identifier parser")
})
.parse_stream(state_stream)
.into()
}
Expand Down
2 changes: 1 addition & 1 deletion src/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ pub struct TypeName(Identifier);
impl TypeName {
/// Create a new TypeName.
pub fn new(name: &str) -> TypeName {
TypeName(name.into())
TypeName(name.try_into().expect("Invalid Identifier for TypeName"))
}
}

Expand Down

0 comments on commit 9b24832

Please sign in to comment.