Skip to content

parser: read large real literals and report out-of-range literals - #51

Merged
rameel merged 1 commit into
mainfrom
parser/numeric-literal-range
Nov 20, 2026
Merged

rameel merged 1 commit into
mainfrom
parser/numeric-literal-range

Conversation

@rameel

@rameel rameel commented Nov 20, 2026

Copy link
Copy Markdown
Owner

A real literal whose integer part does not fit in ulong was not recognized at all. 100000000000000000000.0, 100000000000000000000d, and even 79228162514264337593543950335m (decimal.MaxValue) failed with Expected [!+\-~], ...: the integer scanner gave up on overflow before anything could see the fraction, exponent, or suffix after the digits.

This PR reads the digits to the end and decides afterwards whether the literal is real. As in C#, digits followed by d, f, or m form a real literal.

Literals out of range are now reported as C# reports them, at the position of the literal:

Literal Before After
100000000000000000000 (1:1) Expected [!+\-~], ... (1:1) Integral constant is too large. (CS1021)
0x10000000000000000, 65-bit 0b... (1:2) Expected '(', '[', ... (1:1) Integral constant is too large.
1e400, 1e39f Infinity (1:1) Floating-point constant is outside the range of type 'System.Double'. ('System.Single' for 1e39f, CS0594)
1e100m (1:1) System.OverflowException: ... with the stack trace (1:1) Floating-point constant is outside the range of type 'System.Decimal'.

A value too small for its type still becomes zero, as in C#: 1e-400 is 0d.

Expressions that relied on 1e400 evaluating to infinity are now rejected; double.PositiveInfinity and float.PositiveInfinity still give one.

A real literal whose integer part did not fit in ulong was not
recognized at all: 100000000000000000000.0, 100000000000000000000d,
and even 79228162514264337593543950335m (decimal.MaxValue) failed
with "Expected [!+\-~], ...". The integer scanner gave up on overflow
before anything could see the fraction, exponent, or suffix after the
digits.

Read the digits to the end and decide afterwards whether the literal
is real. As in C#, digits followed by d, f, or m form a real literal.

Report literals out of range as C# does, at the position of the
literal:

- an integer too large for ulong, in any base, with "Integral constant
  is too large." Decimal digits failed with "Expected ...", and
  hexadecimal and binary ones were read as 0 followed by an unexpected
  x or b;
- a real too large for its type, with "Floating-point constant is
  outside the range of type 'System.Double'." and the like. 1e400 and
  1e39f evaluated to infinity, which double.PositiveInfinity and
  float.PositiveInfinity still give, and 1e100m put an
  OverflowException with its stack trace into the error message.

A value too small for its type still becomes zero, as in C#.
@rameel
rameel merged commit 22f57d0 into main Nov 20, 2026
1 check passed
@rameel
rameel deleted the parser/numeric-literal-range branch November 21, 2026 11:55
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.

1 participant