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

[Don't merge] TypedIdentifier, Box<str> and other tweaks #5

Open
wants to merge 1 commit 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
127 changes: 69 additions & 58 deletions src/validator.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,69 @@
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> {
fn validate(&self, typed: bool) -> Result<()> {
match *self {
// FIXME: validate that custom type name doesn't have space, etc.
Type::Custom(ref custom) => Ok(()),
Type::Custom(ref 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());
}
if let Some(yultype) = &self.yultype {
yultype.validate(typed)?;
}
self.yultype.validate(typed)?;
// FIXME: validate that the literal is valid based on the type
}
Ok(())
}
}

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 +72,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 +81,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,6 +105,21 @@ impl Validator for Switch {
mod tests {
use super::*;

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

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

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

#[test]
fn basic_type() {
assert!(!Type::Bool.validate(true).is_err(), "");
Expand All @@ -94,49 +128,29 @@ mod tests {
#[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(),
""
);
}

#[test]
fn untyped_literal() {
assert!(
!Literal {
literal: "test".to_string(),
yultype: None
}.validate(false)
.is_err(),
""
);
assert!(
Literal {
literal: "test".to_string(),
yultype: None
}.validate(true)
.is_err(),
""
);
}

#[test]
fn typed_literal() {
assert!(
!Literal {
literal: "test".to_string(),
yultype: Some(Type::Bool)
Literal {
literal: "true".into(),
yultype: Type::Bool
}.validate(true)
.is_err(),
.is_ok(),
""
);
}
Expand All @@ -146,8 +160,8 @@ mod tests {
assert!(
Case {
literal: Some(Literal {
literal: "".to_string(),
yultype: None,
literal: "".into(),
yultype: Type::Bool,
}),
block: Block { statements: vec![] },
}.validate(false)
Expand All @@ -159,17 +173,14 @@ 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 {
literal: "true".to_string(),
yultype: Some(Type::Bool),
literal: "true".into(),
yultype: Type::Bool,
}),
block: Block { statements: vec![] },
},
Expand All @@ -180,7 +191,7 @@ mod tests {
],
})],
}.validate(false)
.is_err(),
.is_ok(),
""
);
}
Expand Down
Loading