From 3c9f22564e8a55c1a8f6a65078d54664253a91cd Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Tue, 19 Mar 2024 01:08:24 +0100 Subject: [PATCH] refactor: Use disjoint token definitions for DecimalLiteral --- .../inputs/language/src/definition.rs | 42 +++++++++--------- .../slang_solidity/src/generated/language.rs | 44 ++++++++++--------- .../outputs/spec/generated/grammar.ebnf | 9 ++-- .../public/05-expressions/04-numbers.md | 2 +- 4 files changed, 52 insertions(+), 45 deletions(-) diff --git a/crates/solidity/inputs/language/src/definition.rs b/crates/solidity/inputs/language/src/definition.rs index 452c284958..696130092d 100644 --- a/crates/solidity/inputs/language/src/definition.rs +++ b/crates/solidity/inputs/language/src/definition.rs @@ -3664,7 +3664,18 @@ codegen_language_macros::compile!(Language( Token( name = DecimalLiteral, definitions = [ - // An integer (without a dot or a fraction) is enabled in all versions: + // A dot and a fraction (without an integer) is enabled in all versions: + TokenDefinition( + scanner = TrailingContext( + scanner = Sequence([ + Atom("."), + Fragment(DecimalDigits), + Optional(Fragment(DecimalExponent)) + ]), + not_followed_by = Fragment(IdentifierStart) + ) + ), + // A bare integer (without a dot or a fraction) is enabled in all versions: TokenDefinition( scanner = TrailingContext( scanner = Sequence([ @@ -3677,7 +3688,7 @@ codegen_language_macros::compile!(Language( not_followed_by = Fragment(IdentifierStart) ) ), - // An integer and a dot (without a fraction) is disabled in "0.5.0" + // Till 0.5.0, the following lone dot was considered a part of the literal: TokenDefinition( enabled = Till("0.5.0"), scanner = TrailingContext( @@ -3692,25 +3703,12 @@ codegen_language_macros::compile!(Language( not_followed_by = Fragment(IdentifierStart) ) ), - // Since 0.5.0, only consume a dot if it's followed by a digit: + // As well as the full form of digits followed by a dot followed by digits... TokenDefinition( - enabled = From("0.5.0"), + enabled = Till("0.5.0"), scanner = TrailingContext( scanner = Sequence([ Fragment(DecimalDigits), - Optional(Sequence([ - Atom("."), - Fragment(DecimalDigits) - ])), - Optional(Fragment(DecimalExponent)) - ]), - not_followed_by = Fragment(IdentifierStart) - ) - ), - // A dot and a fraction (without an integer) is enabled in all versions: - TokenDefinition( - scanner = TrailingContext( - scanner = Sequence([ Atom("."), Fragment(DecimalDigits), Optional(Fragment(DecimalExponent)) @@ -3718,13 +3716,17 @@ codegen_language_macros::compile!(Language( not_followed_by = Fragment(IdentifierStart) ) ), - // An integer, a dot, and a fraction is enabled in all versions: + // ...both of which was subsumed by a more general form that only included + // the dot if it was followed by a decimal literal: TokenDefinition( + enabled = From("0.5.0"), scanner = TrailingContext( scanner = Sequence([ Fragment(DecimalDigits), - Atom("."), - Fragment(DecimalDigits), + Optional(Sequence([ + Atom("."), + Fragment(DecimalDigits) + ])), Optional(Fragment(DecimalExponent)) ]), not_followed_by = Fragment(IdentifierStart) diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/generated/language.rs b/crates/solidity/outputs/cargo/slang_solidity/src/generated/language.rs index 1f64945ff6..e02074dcae 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/generated/language.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/generated/language.rs @@ -6932,6 +6932,15 @@ impl Language { fn decimal_literal(&self, input: &mut ParserContext<'_>) -> bool { scan_choice!( input, + scan_not_followed_by!( + input, + scan_sequence!( + scan_chars!(input, '.'), + self.decimal_digits(input), + scan_optional!(input, self.decimal_exponent(input)) + ), + self.identifier_start(input) + ), scan_not_followed_by!( input, scan_sequence!( @@ -6960,6 +6969,20 @@ impl Language { } else { false }, + if !self.version_is_at_least_0_5_0 { + scan_not_followed_by!( + input, + scan_sequence!( + self.decimal_digits(input), + scan_chars!(input, '.'), + self.decimal_digits(input), + scan_optional!(input, self.decimal_exponent(input)) + ), + self.identifier_start(input) + ) + } else { + false + }, if self.version_is_at_least_0_5_0 { scan_not_followed_by!( input, @@ -6975,26 +6998,7 @@ impl Language { ) } else { false - }, - scan_not_followed_by!( - input, - scan_sequence!( - scan_chars!(input, '.'), - self.decimal_digits(input), - scan_optional!(input, self.decimal_exponent(input)) - ), - self.identifier_start(input) - ), - scan_not_followed_by!( - input, - scan_sequence!( - self.decimal_digits(input), - scan_chars!(input, '.'), - self.decimal_digits(input), - scan_optional!(input, self.decimal_exponent(input)) - ), - self.identifier_start(input) - ) + } ) } diff --git a/crates/solidity/outputs/spec/generated/grammar.ebnf b/crates/solidity/outputs/spec/generated/grammar.ebnf index 65da63b63f..3b3e3f3c1c 100644 --- a/crates/solidity/outputs/spec/generated/grammar.ebnf +++ b/crates/solidity/outputs/spec/generated/grammar.ebnf @@ -1211,18 +1211,19 @@ HEX_LITERAL = "0x" «HEX_CHARACTER»+ ("_" «HEX_CHARACTER»+)*; (* Deprecated in 0.5.0 *) HEX_LITERAL = "0X" «HEX_CHARACTER»+ ("_" «HEX_CHARACTER»+)*; +DECIMAL_LITERAL = "." «DECIMAL_DIGITS» «DECIMAL_EXPONENT»?; + DECIMAL_LITERAL = «DECIMAL_DIGITS» «DECIMAL_EXPONENT»?; (* Deprecated in 0.5.0 *) DECIMAL_LITERAL = «DECIMAL_DIGITS» "." «DECIMAL_EXPONENT»?; +(* Deprecated in 0.5.0 *) +DECIMAL_LITERAL = «DECIMAL_DIGITS» "." «DECIMAL_DIGITS» «DECIMAL_EXPONENT»?; + (* Introduced in 0.5.0 *) DECIMAL_LITERAL = «DECIMAL_DIGITS» ("." «DECIMAL_DIGITS»)? «DECIMAL_EXPONENT»?; -DECIMAL_LITERAL = "." «DECIMAL_DIGITS» «DECIMAL_EXPONENT»?; - -DECIMAL_LITERAL = «DECIMAL_DIGITS» "." «DECIMAL_DIGITS» «DECIMAL_EXPONENT»?; - «DECIMAL_DIGITS» = ("0"…"9")+ ("_" ("0"…"9")+)*; «DECIMAL_EXPONENT» = ("e" | "E") "-"? «DECIMAL_DIGITS»; diff --git a/crates/solidity/outputs/spec/generated/public/05-expressions/04-numbers.md b/crates/solidity/outputs/spec/generated/public/05-expressions/04-numbers.md index 4023817b21..aafa656187 100644 --- a/crates/solidity/outputs/spec/generated/public/05-expressions/04-numbers.md +++ b/crates/solidity/outputs/spec/generated/public/05-expressions/04-numbers.md @@ -26,7 +26,7 @@ ``` -
DECIMAL_LITERAL = «DECIMAL_DIGITS» «DECIMAL_EXPONENT»?;

(* Deprecated in 0.5.0 *)
DECIMAL_LITERAL = «DECIMAL_DIGITS» "." «DECIMAL_EXPONENT»?;

(* Introduced in 0.5.0 *)
DECIMAL_LITERAL = «DECIMAL_DIGITS» ("." «DECIMAL_DIGITS»)? «DECIMAL_EXPONENT»?;

DECIMAL_LITERAL = "." «DECIMAL_DIGITS» «DECIMAL_EXPONENT»?;

DECIMAL_LITERAL = «DECIMAL_DIGITS» "." «DECIMAL_DIGITS» «DECIMAL_EXPONENT»?;
+
DECIMAL_LITERAL = "." «DECIMAL_DIGITS» «DECIMAL_EXPONENT»?;

DECIMAL_LITERAL = «DECIMAL_DIGITS» «DECIMAL_EXPONENT»?;

(* Deprecated in 0.5.0 *)
DECIMAL_LITERAL = «DECIMAL_DIGITS» "." «DECIMAL_EXPONENT»?;

(* Deprecated in 0.5.0 *)
DECIMAL_LITERAL = «DECIMAL_DIGITS» "." «DECIMAL_DIGITS» «DECIMAL_EXPONENT»?;

(* Introduced in 0.5.0 *)
DECIMAL_LITERAL = «DECIMAL_DIGITS» ("." «DECIMAL_DIGITS»)? «DECIMAL_EXPONENT»?;
```{ .ebnf #DecimalDigits }