Skip to content

Commit

Permalink
fix: Define DecimalLiteral in DSL v2 using the DSL v1 rules (#643)
Browse files Browse the repository at this point in the history
Otherwise, the following rule:

```rust
// An integer (without a dot or a fraction) is enabled in all versions:
TokenDefinition(
    scanner = TrailingContext(
        scanner = Sequence([
            Fragment(DecimalDigits),
            Optional(Fragment(DecimalExponent))
        ]),
        not_followed_by = Fragment(IdentifierStart)
    )
),
```

was a catch-all rule that could successfully lex "1.2" as "1" in all
versions.

Instead, this mimicks the following version from the DSL v1:
```rust
scanner DecimalLiteral = (
        (
            (
                { removed in "0.5.0"    (DecimalDigits (("." (DecimalDigits ?) ) ?)) } |
                { introduced in "0.5.0" (DecimalDigits (("." DecimalDigits     ) ?)) } |
                ('.' DecimalDigits)
            )
            (DecimalExponent ?)
        ) not followed by IdentifierStart
    ) ;
```

and fixed a case in
https://github.com/Xanewok/slang/tree/codegen-use-dslv2 when using parts
of the DSLv2 for the codegen but I didn't open the PR yet.

Ref #638
  • Loading branch information
Xanewok authored Nov 9, 2023
1 parent eb3e2d0 commit f0cb998
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 92 deletions.
12 changes: 9 additions & 3 deletions crates/solidity/inputs/language/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3675,7 +3675,10 @@ codegen_language_macros::compile!(Language(
TokenDefinition(
scanner = TrailingContext(
scanner = Sequence([
Fragment(DecimalDigits),
TrailingContext(
scanner = Fragment(DecimalDigits),
not_followed_by = Atom(".")
),
Optional(Fragment(DecimalExponent))
]),
not_followed_by = Fragment(IdentifierStart)
Expand All @@ -3686,8 +3689,11 @@ codegen_language_macros::compile!(Language(
enabled = Till("0.5.0"),
scanner = TrailingContext(
scanner = Sequence([
Fragment(DecimalDigits),
Atom("."),
TrailingContext(
scanner =
Sequence([Fragment(DecimalDigits), Atom(".")]),
not_followed_by = Fragment(DecimalDigits)
),
Optional(Fragment(DecimalExponent))
]),
not_followed_by = Fragment(IdentifierStart)
Expand Down
7 changes: 4 additions & 3 deletions crates/solidity/inputs/language/src/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,9 +820,10 @@ slang_grammar! {
scanner DecimalLiteral = (
(
(
{ removed in "0.5.0" (DecimalDigits (("." (DecimalDigits ?) ) ?)) } |
{ introduced in "0.5.0" (DecimalDigits (("." DecimalDigits ) ?)) } |
('.' DecimalDigits)
(DecimalDigits not followed by '.') |
{ removed in "0.5.0" ((DecimalDigits '.') not followed by DecimalDigits) } |
('.' DecimalDigits) |
(DecimalDigits '.' DecimalDigits)
)
(DecimalExponent ?)
) not followed by IdentifierStart
Expand Down
36 changes: 15 additions & 21 deletions crates/solidity/outputs/cargo/crate/src/generated/language.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 15 additions & 21 deletions crates/solidity/outputs/npm/crate/src/generated/language.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ Source: >
Errors: # 1 total
- >
Error: Expected DaysKeyword or EtherKeyword or FinneyKeyword or HoursKeyword or MinutesKeyword or SecondsKeyword or SzaboKeyword or WeeksKeyword or WeiKeyword.
╭─[crates/solidity/testing/snapshots/cst_output/NumericExpression/float_no_fraction/input.sol:1:2]
Error: Expected DecimalLiteral or HexLiteral.
╭─[crates/solidity/testing/snapshots/cst_output/NumericExpression/float_no_fraction/input.sol:1:1]
1 │ 1.
│ ╰── Error occurred here.
───╯
Tree:
- NumericExpression (Rule): # 0..2 "1."
- DecimalLiteral (Token): "1" # 0..1
- SKIPPED (Token): "." # 1..2
- SKIPPED (Token): "1." # 0..2

This file was deleted.

This file was deleted.

0 comments on commit f0cb998

Please sign in to comment.