Skip to content

Fix/numeric literal parser#43

Open
exomo wants to merge 2 commits intoappsup-dart:masterfrom
exomo:fix/numeric-literal-parser
Open

Fix/numeric literal parser#43
exomo wants to merge 2 commits intoappsup-dart:masterfrom
exomo:fix/numeric-literal-parser

Conversation

@exomo
Copy link
Copy Markdown

@exomo exomo commented Feb 14, 2026

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:

  • Updated the numericLiteral parser in parser.dart to support hexadecimal numbers (e.g., 0xAB, 0X1b) and improved handling of scientific notation and decimal numbers.

Test improvements:

  • Expanded the numeric literal tests in expressions_test.dart to include more valid cases (such as .1e2, 1.23e4, .6e-7, 0xD3, 0X1b) and additional invalid cases (such as 5., 12x34), ensuring the parser behaves correctly for a wider set of inputs.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parsing hex integer literal fails

1 participant