Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypedIdentifier #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 67 additions & 31 deletions src/validator.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
use yul::*;

pub type Result<T> = std::result::Result<T, &'static str>;

pub trait Validator: Send + Sync {
fn validate(&self, typed: bool) -> Result<(), String>;
fn validate(&self, typed: bool) -> Result<()>;
}

impl Validator for Type {
fn validate(&self, typed: bool) -> Result<(), String> {
match *self {
// FIXME: validate that custom type name doesn't have space, etc.
Type::Custom(ref custom) => Ok(()),
fn validate(&self, typed: bool) -> Result<()> {
match self {
Type::Custom(custom) => custom.validate(typed),
_ => Ok(()),
}
}
}

impl Validator for Literal {
fn validate(&self, typed: bool) -> Result<(), String> {
fn validate(&self, typed: bool) -> Result<()> {
if typed {
if self.yultype == None {
return Err("Type must be set".to_string());
return Err("Type must be set");
}
if let Some(yultype) = &self.yultype {
yultype.validate(typed)?;
Expand All @@ -30,21 +31,44 @@ impl Validator for Literal {
}

impl Validator for Identifier {
fn validate(&self, typed: bool) -> Result<(), String> {
if typed {
if self.yultype == None {
return Err("Type must be set".to_string());
fn validate(&self, _: bool) -> Result<()> {
let mut bytes = self.0.bytes();

match bytes.next() {
None => return Err("Identifier label cannot be empty"),
Some(byte) => match byte {
b'a'...b'z' |
b'A'...b'Z' |
b'_' | b'$' => {},
_ => return Err("Identifier label must start with a letter, `_`, or `$` character")
}
if let Some(yultype) = &self.yultype {
yultype.validate(typed)?;
}

for byte in bytes {
match byte {
b'a'...b'z' |
b'A'...b'Z' |
b'0'...b'9' |
b'_' | b'$' => {},
_ => return Err("Identifier label can contain only letters, digits, `_`, or `$` characters")
}
}

Ok(())
}
}

impl Validator for TypedIdentifier {
fn validate(&self, typed: bool) -> Result<()> {
if typed {
self.yultype.validate(typed)?;
}
self.identifier.validate(typed)
}
}

impl Validator for Block {
fn validate(&self, typed: bool) -> Result<(), String> {
fn validate(&self, typed: bool) -> Result<()> {
for statement in &self.statements {
statement.validate(typed)?;
}
Expand All @@ -53,7 +77,7 @@ impl Validator for Block {
}

impl Validator for Statement {
fn validate(&self, typed: bool) -> Result<(), String> {
fn validate(&self, typed: bool) -> Result<()> {
match *self {
Statement::Switch(ref switch) => switch.validate(typed),
_ => Ok(()),
Expand All @@ -62,19 +86,19 @@ impl Validator for Statement {
}

impl Validator for Case {
fn validate(&self, typed: bool) -> Result<(), String> {
fn validate(&self, typed: bool) -> Result<()> {
if let Some(literal) = &self.literal {
literal.validate(typed)?;
if literal.literal.len() == 0 {
return Err("Case literal cannot be empty".to_string());
return Err("Case literal cannot be empty");
}
}
Ok(())
}
}

impl Validator for Switch {
fn validate(&self, typed: bool) -> Result<(), String> {
fn validate(&self, typed: bool) -> Result<()> {
for case in &self.cases {
case.validate(typed)?;
}
Expand All @@ -86,23 +110,38 @@ impl Validator for Switch {
mod tests {
use super::*;

#[test]
fn identifiers() {
assert!(Identifier::new("foo").validate(true).is_ok());
assert!(Identifier::new("Foo").validate(true).is_ok());
assert!(Identifier::new("_foo").validate(true).is_ok());
assert!(Identifier::new("$foo").validate(true).is_ok());
assert!(Identifier::new("f00").validate(true).is_ok());

assert!(Identifier::new("foo_bar").validate(true).is_ok());
assert!(Identifier::new("FooBar").validate(true).is_ok());

assert!(Identifier::new("1foo").validate(true).is_err());
assert!(Identifier::new("#foo").validate(true).is_err());
}

#[test]
fn basic_type() {
assert!(!Type::Bool.validate(true).is_err(), "");
assert!(Type::Bool.validate(true).is_ok(), "");
}

#[test]
fn custom_type() {
assert!(
!Type::Custom("test".to_string()).validate(true).is_err(),
Type::Custom("test".into()).validate(true).is_ok(),
""
);
}

#[test]
fn invalid_custom_type() {
assert!(
!Type::Custom("test invalid type".to_string())
Type::Custom("test invalid type".into())
.validate(true)
.is_err(),
""
Expand All @@ -112,11 +151,11 @@ mod tests {
#[test]
fn untyped_literal() {
assert!(
!Literal {
Literal {
literal: "test".to_string(),
yultype: None
}.validate(false)
.is_err(),
.is_ok(),
""
);
assert!(
Expand All @@ -132,11 +171,11 @@ mod tests {
#[test]
fn typed_literal() {
assert!(
!Literal {
Literal {
literal: "test".to_string(),
yultype: Some(Type::Bool)
}.validate(true)
.is_err(),
.is_ok(),
""
);
}
Expand All @@ -159,12 +198,9 @@ mod tests {
#[test]
fn complex_example() {
assert!(
!Block {
Block {
statements: vec![Statement::Switch(Switch {
expression: Expression::Identifier(Identifier {
identifier: "shouldbebool".to_string(),
yultype: Some(Type::Bool),
}),
expression: Expression::Identifier("shouldbebool".into()),
cases: vec![
Case {
literal: Some(Literal {
Expand All @@ -180,7 +216,7 @@ mod tests {
],
})],
}.validate(false)
.is_err(),
.is_ok(),
""
);
}
Expand Down
Loading