-
Notifications
You must be signed in to change notification settings - Fork 64
Changed Parsing Method of Identifiers #138
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -348,12 +348,26 @@ fn partial_tokens_to_tokens(mut tokens: &[PartialToken]) -> EvalexprResult<Vec<T | |
| }, | ||
| PartialToken::Literal(literal) => { | ||
| cutoff = 1; | ||
| if let Ok(number) = parse_dec_or_hex(&literal) { | ||
| let starts_with_alphabet_or_underscore = | ||
| literal.starts_with(|x: char| x.is_alphabetic() || x == '_'); | ||
| // Alphanumeric || Underscore || Colon | ||
| let contains_only_valid_chars = literal | ||
| .chars() | ||
| .all(|x| x.is_alphanumeric() || x == '_' || x == ':'); | ||
| let is_not_underscore = literal != "_"; | ||
| let contains_alphabet = literal.contains(|x: char| x.is_alphabetic()); | ||
| if let Ok(boolean) = literal.parse::<bool>() { | ||
| Some(Token::Boolean(boolean)) | ||
| } else if starts_with_alphabet_or_underscore | ||
| && contains_only_valid_chars | ||
| && is_not_underscore | ||
| && contains_alphabet | ||
| { | ||
| Some(Token::Identifier(literal)) | ||
| } else if let Ok(number) = parse_dec_or_hex(&literal) { | ||
| Some(Token::Int(number)) | ||
| } else if let Ok(number) = literal.parse::<FloatType>() { | ||
| Some(Token::Float(number)) | ||
| } else if let Ok(boolean) = literal.parse::<bool>() { | ||
| Some(Token::Boolean(boolean)) | ||
| } else { | ||
| // If there are two tokens following this one, check if the next one is | ||
| // a plus or a minus. If so, then attempt to parse all three tokens as a | ||
|
|
@@ -370,10 +384,16 @@ fn partial_tokens_to_tokens(mut tokens: &[PartialToken]) -> EvalexprResult<Vec<T | |
| cutoff = 3; | ||
| Some(Token::Float(number)) | ||
| } else { | ||
| Some(Token::Identifier(literal.to_string())) | ||
| return Err(EvalexprError::IllegalIdentifierSequence( | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we are parsing the literal as a number, so this should be a new error variant like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. I will change this |
||
| literal.to_string(), | ||
| )); | ||
| } | ||
| }, | ||
| _ => Some(Token::Identifier(literal.to_string())), | ||
| _ => { | ||
| return Err(EvalexprError::IllegalIdentifierSequence( | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we are parsing the literal as a number, so this should be a new error variant like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
| literal.to_string(), | ||
| )) | ||
| }, | ||
| } | ||
| } | ||
| }, | ||
|
|
@@ -495,4 +515,32 @@ mod tests { | |
| ] | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn wrong_identifier_sequence() { | ||
| assert_eq!( | ||
| tokenize("1b + 1"), | ||
| Err(crate::EvalexprError::IllegalIdentifierSequence( | ||
| String::from("1b") | ||
| )) | ||
| ); | ||
| assert_eq!( | ||
| tokenize("_b + 1"), | ||
| Ok(vec![ | ||
| Token::Identifier(String::from("_b")), | ||
| Token::Plus, | ||
| Token::Int(1) | ||
| ]) | ||
| ); | ||
| assert_eq!( | ||
| tokenize("1.0 1e+5 _1a2 1"), | ||
| Ok(vec![ | ||
| Token::Float(1.0), | ||
| Token::Float(100000.0), | ||
| Token::Identifier(String::from("_1a2")), | ||
| Token::Int(1) | ||
| ]) | ||
| ); | ||
| // assert_eq!(tokenize("string")) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The decomposition of the conditions into separate variables is great!
Here we should first check only
starts_with_alphabet_or_underscore, and if it does but any of the other three are false, then return a the error variantIllegalIdentifierSequence.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did boolean first because true and false would both be interpreted as identifiers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean that the branch after boolean should only check for starts_with_alphabet_or_underscore, and then should have an inner branch that checks the rest.