Skip to content

Commit

Permalink
support string literals in version pragmas
Browse files Browse the repository at this point in the history
- the new grammar makes sure the structure of string literals matches the parent productions, so that it can be parsed using the same parser for later analysis.
- also fixed a minor grammar bug where `||` was a precedence binary expression, instead of a separator of comparator sets (repeated list of base expressions).
- removed the internal `extract_version_pragmas` API for now, since it is not used. Later, we will introduce the same functionality as a public API, based on the Rust AST when is ready.
  • Loading branch information
OmarTawfik committed Apr 10, 2024
1 parent 9426eb5 commit 8f878e7
Show file tree
Hide file tree
Showing 54 changed files with 768 additions and 930 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-vans-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/slang": patch
---

support string literals in version pragmas
17 changes: 3 additions & 14 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ members = [
"crates/solidity/testing/sanctuary",
"crates/solidity/testing/snapshots",
"crates/solidity/testing/solc",
"crates/solidity/testing/utils",

"crates/testlang/inputs/language",
"crates/testlang/outputs/cargo/slang_testlang_node_addon",
Expand Down Expand Up @@ -64,7 +63,6 @@ solidity_spec = { path = "crates/solidity/outputs/spec" }
solidity_testing_sanctuary = { path = "crates/solidity/testing/sanctuary" }
solidity_testing_snapshots = { path = "crates/solidity/testing/snapshots" }
solidity_testing_solc = { path = "crates/solidity/testing/solc" }
solidity_testing_utils = { path = "crates/solidity/testing/utils" }

slang_testlang = { path = "crates/testlang/outputs/cargo/slang_testlang" }
slang_testlang_node_addon = { path = "crates/testlang/outputs/cargo/slang_testlang_node_addon" }
Expand Down
84 changes: 59 additions & 25 deletions crates/solidity/inputs/language/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,32 +122,27 @@ codegen_language_macros::compile!(Language(
name = VersionPragma,
fields = (
solidity_keyword = Required(SolidityKeyword),
expressions = Required(VersionPragmaExpressions)
sets = Required(VersionExpressionSets)
)
),
Repeated(
name = VersionPragmaExpressions,
reference = VersionPragmaExpression
Separated(
name = VersionExpressionSets,
reference = VersionExpressionSet,
separator = BarBar
),
Repeated(name = VersionExpressionSet, reference = VersionExpression),
Precedence(
name = VersionPragmaExpression,
name = VersionExpression,
precedence_expressions = [
PrecedenceExpression(
name = VersionPragmaOrExpression,
operators = [PrecedenceOperator(
model = BinaryLeftAssociative,
fields = (operator = Required(BarBar))
)]
),
PrecedenceExpression(
name = VersionPragmaRangeExpression,
name = VersionRange,
operators = [PrecedenceOperator(
model = BinaryLeftAssociative,
fields = (operator = Required(Minus))
)]
),
PrecedenceExpression(
name = VersionPragmaPrefixExpression,
name = VersionComparator,
operators = [
PrecedenceOperator(
model = Prefix,
Expand Down Expand Up @@ -180,25 +175,64 @@ codegen_language_macros::compile!(Language(
]
)
],
primary_expressions =
[PrimaryExpression(reference = VersionPragmaSpecifier)]
primary_expressions = [
PrimaryExpression(reference = VersionSpecifiers),
PrimaryExpression(reference = SingleQuotedVersionLiteral),
PrimaryExpression(reference = DoubleQuotedVersionLiteral)
]
),
Separated(
name = VersionPragmaSpecifier,
reference = VersionPragmaValue,
// __SLANG_VERSION_SPECIFIER_SYNTAX__ (keep in sync)
name = VersionSpecifiers,
reference = VersionSpecifier,
separator = Period
),
Token(
name = VersionPragmaValue,
// __SLANG_VERSION_SPECIFIER_SYNTAX__ (keep in sync)
name = VersionSpecifier,
definitions = [TokenDefinition(
scanner = OneOrMore(Choice([
Range(inclusive_start = '0', inclusive_end = '9'),
Atom("x"),
Atom("X"),
Atom("*")
]))
scanner = Fragment(VersionSpecifierFragment)
)]
),
Token(
// __SLANG_VERSION_SPECIFIER_SYNTAX__ (keep in sync)
name = SingleQuotedVersionLiteral,
definitions = [TokenDefinition(
scanner = Sequence([
Atom("'"),
Fragment(VersionSpecifierFragment),
ZeroOrMore(Sequence([
Atom("."),
Fragment(VersionSpecifierFragment)
])),
Atom("'")
])
)]
),
Token(
// __SLANG_VERSION_SPECIFIER_SYNTAX__ (keep in sync)
name = DoubleQuotedVersionLiteral,
definitions = [TokenDefinition(
scanner = Sequence([
Atom("\""),
Fragment(VersionSpecifierFragment),
ZeroOrMore(Sequence([
Atom("."),
Fragment(VersionSpecifierFragment)
])),
Atom("\"")
])
)]
),
Fragment(
name = VersionSpecifierFragment,
scanner = OneOrMore(Choice([
Range(inclusive_start = '0', inclusive_end = '9'),
Atom("x"),
Atom("X"),
Atom("*")
]))
),
Keyword(
name = AbicoderKeyword,
identifier = Identifier,
Expand Down

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

0 comments on commit 8f878e7

Please sign in to comment.