Skip to content

Commit ec58da4

Browse files
committed
Adjust grammar and add more invalid examples
1 parent edeb1cb commit ec58da4

File tree

1 file changed

+27
-49
lines changed

1 file changed

+27
-49
lines changed

standard/lexical-structure.md

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -626,76 +626,44 @@ Integer_Literal
626626
;
627627
628628
fragment Decimal_Integer_Literal
629-
: Decimal_Digits Integer_Type_Suffix?
629+
: Decimal_Digit Decorated_Decimal_Digit* Integer_Type_Suffix?
630630
;
631631
632-
fragment Decimal_Digits
633-
: Decimal_Digit
634-
| Decimal_Digit Decimal_Digits_And_Underscores? Decimal_Digit
632+
fragment Decorated_Decimal_Digit
633+
: '_'* Decimal_Digit
635634
;
636-
635+
637636
fragment Decimal_Digit
638637
: '0'..'9'
639638
;
640639
641-
fragment Decimal_Digits_And_Underscores
642-
: Decimal_Digits_And_Underscore+
643-
;
644-
645-
fragment Decimal_Digits_And_Underscore
646-
: Decimal_Digit
647-
| '_'
648-
;
649-
650640
fragment Integer_Type_Suffix
651641
: 'U' | 'u' | 'L' | 'l' |
652642
'UL' | 'Ul' | 'uL' | 'ul' | 'LU' | 'Lu' | 'lU' | 'lu'
653643
;
654644
655645
fragment Hexadecimal_Integer_Literal
656-
: ('0x' | '0X') Hex_Digits Integer_Type_Suffix?
646+
: ('0x' | '0X') Decorated_Hex_Digit+ Integer_Type_Suffix?
657647
;
658648
659-
fragment Hex_Digits
649+
fragment Decorated_Hex_Digit
660650
: '_'* Hex_Digit
661-
| '_'* Hex_Digit Hex_Digits_And_Underscores? Hex_Digit
662651
;
663-
652+
664653
fragment Hex_Digit
665654
: '0'..'9' | 'A'..'F' | 'a'..'f'
666655
;
667-
668-
fragment Hex_Digits_And_Underscores
669-
: Hex_Digits_And_Underscore+
670-
;
671-
672-
fragment Hex_Digits_And_Underscore
673-
: Hex_Digit
674-
| '_'
675-
;
676-
656+
677657
fragment Binary_Integer_Literal
678-
: '0b' Binary_Digits Integer_Type_Suffix?
679-
| '0B' Binary_Digits Integer_Type_Suffix?
658+
: ('0b' | '0B') Decorated_Binary_Digit+ Integer_Type_Suffix?
680659
;
681660
682-
fragment Binary_Digits
661+
fragment Decorated_Binary_Digit
683662
: '_'* Binary_Digit
684-
| '_'* Binary_Digit Binary_Digits_And_Underscores? Binary_Digit
685663
;
686-
664+
687665
fragment Binary_Digit
688-
: '0'
689-
| '1'
690-
;
691-
692-
fragment Binary_Digits_And_Underscores
693-
: Binary_Digits_And_Underscore+
694-
;
695-
696-
fragment Binary_Digits_And_Underscore
697-
: Binary_Digit
698-
| '_'
666+
: '0' | '1'
699667
;
700668
```
701669

@@ -721,17 +689,22 @@ To permit the smallest possible `int` and `long` values to be written as integer
721689
> 123 // decimal, int
722690
> 10_543_765Lu // decimal, ulong
723691
> 1_2__3___4____5 // decimal, int
692+
> _123 // invalid; no leading _ allowed
693+
> 123_ // invalid; no trailing _allowed
724694
>
725695
> 0xFf // hex, int
726696
> 0X1b_a0_44_fEL // hex, long
727697
> 0x1ade_3FE1_29AaUL // hex, ulong
728698
> 0x_abc // hex, int
699+
> _0x123 // invalid; no leading _ allowed
729700
> 0xabc_ // invalid; no trailing _ allowed
730701
>
731702
> 0b101 // binary, int
732703
> 0B1001_1010u // binary, uint
733704
> 0b1111_1111_0000UL // binary, ulong
734705
> 0B__111 // binary, int
706+
> __0B111 // invalid; no leading _ allowed
707+
> 0B111__ // invalid; no trailing _ allowed
735708
> ```
736709
>
737710
> *end example*
@@ -742,14 +715,15 @@ Real literals are used to write values of types `float`, `double`, and `decimal`
742715
743716
```ANTLR
744717
Real_Literal
745-
: Decimal_Digits '.' Decimal_Digits Exponent_Part? Real_Type_Suffix?
746-
| '.' Decimal_Digits Exponent_Part? Real_Type_Suffix?
747-
| Decimal_Digits Exponent_Part Real_Type_Suffix?
748-
| Decimal_Digits Real_Type_Suffix
718+
: Decimal_Digit Decorated_Decimal_Digit* '.'
719+
Decimal_Digit Decorated_Decimal_Digit* Exponent_Part? Real_Type_Suffix?
720+
| '.' Decimal_Digit Decorated_Decimal_Digit* Exponent_Part? Real_Type_Suffix?
721+
| Decimal_Digit Decorated_Decimal_Digit* Exponent_Part Real_Type_Suffix?
722+
| Decimal_Digit Decorated_Decimal_Digit* Real_Type_Suffix
749723
;
750724
751725
fragment Exponent_Part
752-
: ('e' | 'E') Sign? Decimal_Digits
726+
: ('e' | 'E') Sign? Decimal_Digit Decorated_Decimal_Digit*
753727
;
754728
755729
fragment Sign
@@ -789,7 +763,11 @@ The value of a real literal of type `float` or `double` is determined by using t
789763
> 15D // double
790764
> 19.73M // decimal
791765
> 1.F // invalid; ill-formed (parsed as "1." and "F")
766+
> _1.2F // invalid; no leading _ allowed
767+
> 1_.2F // invalid; no trailing _ allowed in integer part
768+
> 1._234 // invalid; no leading _ allowed in fraction
792769
> 1.234_ // invalid; no trailing _ allowed in fraction
770+
> .3e_5F // invalid; no leading _ allowed in exponent
793771
> .3e5_F // invalid; no trailing _ allowed in exponent
794772
> ```
795773
>

0 commit comments

Comments
 (0)