Fix/numeric literal parser#43
Open
exomo wants to merge 2 commits intoappsup-dart:masterfrom
Open
Conversation
The expression to parse numeric literals did not correctly match hex
numbers like "0xAB".
Also while analyzing the parser I found that it wrongfully accepted some
invalid expressions such as "123x456", which lead to an exception from
the num.parse call rather than the parser rejecting the input.
Updated the numericLiteral expression in the parser to
- match valid hex numbers by (digit() | anyOf('abcdefABCDEF')) instead
of just digit().plus()
- prevent matching illegal hex expressions by separating the hex part
always starting with "0x" and other numerical literal that may start
with several digits.
- Also allowed the x to be uppercase "anyOf('xX')" in hex expressions,
uppercase 0X123 is a valid literal for num.parse.
Extended the test case to include some more hex literals that were
previously not parsed correctly.
Another issue with the parser was that decimal point and "e" could not be used in the same expression as they were parsed in different "or" branches. Updated the parser to accept variations of valid scientific notation, such as ".5e2" and "1.23e+4" Notable exception are expression with trailing floating point such as "2." or "1.e3". Those would be accepted by num.parse, but are not valid numbers in dart.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request improves the parsing of numeric literals in the expression parser and expands the corresponding test coverage. The main focus is on supporting a wider range of numeric formats, including hexadecimal and scientific notation.
Fixes #42
Parser enhancements:
numericLiteralparser inparser.dartto support hexadecimal numbers (e.g.,0xAB,0X1b) and improved handling of scientific notation and decimal numbers.Test improvements:
expressions_test.dartto include more valid cases (such as.1e2,1.23e4,.6e-7,0xD3,0X1b) and additional invalid cases (such as5.,12x34), ensuring the parser behaves correctly for a wider set of inputs.