From af26ea2c528efe7f2ee67ca8a8fcd566f3f08ade Mon Sep 17 00:00:00 2001 From: "Daniel M. Zimmerman" Date: Tue, 12 Sep 2023 07:14:56 -0700 Subject: [PATCH] Add reserved keywords to lexer and parser (#39) --- src/solidity-parser/dune | 9 +++-- src/solidity-parser/solidity_lexer.mll | 37 ++++++++++++++++++++- src/solidity-parser/solidity_raw_parser.mly | 3 ++ test/raw_tests/fails/reserved_kw_ko1.sol | 33 ++++++++++++++++++ test/raw_tests/fails/reserved_kw_ko2.sol | 35 +++++++++++++++++++ 5 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 test/raw_tests/fails/reserved_kw_ko1.sol create mode 100644 test/raw_tests/fails/reserved_kw_ko2.sol diff --git a/src/solidity-parser/dune b/src/solidity-parser/dune index 81bab3f..56215b3 100644 --- a/src/solidity-parser/dune +++ b/src/solidity-parser/dune @@ -10,7 +10,12 @@ ) (ocamllex solidity_lexer) -(menhir (modules solidity_raw_parser)) +(menhir + (modules solidity_raw_parser) + (flags + --explain + --strict + --unused-token RESERVEDKEYWORD)) (rule (targets version.ml) @@ -19,5 +24,3 @@ (documentation (package solidity-parser)) - - diff --git a/src/solidity-parser/solidity_lexer.mll b/src/solidity-parser/solidity_lexer.mll index 0934e91..ee4c04b 100644 --- a/src/solidity-parser/solidity_lexer.mll +++ b/src/solidity-parser/solidity_lexer.mll @@ -236,6 +236,41 @@ and end_pragma = parse { let initialized = ref false + +(* + * Reserved keywords that should be known to the parser, but are currently unused. + * https://docs.soliditylang.org/en/v0.6.0/miscellaneous.html#reserved-keywords + *) +let reserved_keywords = + [ + "after", RESERVEDKEYWORD; + "alias", RESERVEDKEYWORD; + "apply", RESERVEDKEYWORD; + "auto", RESERVEDKEYWORD; + "copyof", RESERVEDKEYWORD; + "define", RESERVEDKEYWORD; + "final", RESERVEDKEYWORD; + "implements", RESERVEDKEYWORD; + "in", RESERVEDKEYWORD; + "macro", RESERVEDKEYWORD; + "match", RESERVEDKEYWORD; + "mutable", RESERVEDKEYWORD; + "null", RESERVEDKEYWORD; + "of", RESERVEDKEYWORD; + "partial", RESERVEDKEYWORD; + "promise", RESERVEDKEYWORD; + "reference", RESERVEDKEYWORD; + "relocatable", RESERVEDKEYWORD; + "sealed", RESERVEDKEYWORD; + "sizeof", RESERVEDKEYWORD; + "supports", RESERVEDKEYWORD; + "typedef", RESERVEDKEYWORD; + "typeof", RESERVEDKEYWORD; + "unchecked", RESERVEDKEYWORD; + ] + (* Other keywords already handled: + * case, default, immutable, inline, let, static, switch *) + let init2 ?(list=[]) () = if not !initialized then begin initialized := true; @@ -312,7 +347,7 @@ let init2 ?(list=[]) () = "days", NUMBERUNIT (Days); "weeks", NUMBERUNIT (Weeks); "years", NUMBERUNIT (Years); - ] @ list) + ] @ reserved_keywords @ list) end let init ~freeton = diff --git a/src/solidity-parser/solidity_raw_parser.mly b/src/solidity-parser/solidity_raw_parser.mly index 160cd5e..2a42c95 100644 --- a/src/solidity-parser/solidity_raw_parser.mly +++ b/src/solidity-parser/solidity_raw_parser.mly @@ -375,6 +375,9 @@ %token IDENTIFIER %token EOF +(* Reserved Keywords *) +%token RESERVEDKEYWORD + (* Some convenience precedences *) %nonassoc below_IDENTIFIER %nonassoc IDENTIFIER diff --git a/test/raw_tests/fails/reserved_kw_ko1.sol b/test/raw_tests/fails/reserved_kw_ko1.sol new file mode 100644 index 0000000..52b081d --- /dev/null +++ b/test/raw_tests/fails/reserved_kw_ko1.sol @@ -0,0 +1,33 @@ +contract InterestingVars { + int after; + int alias; + int apply; + int auto; + int case; + int copyof; + int default; + int define; + int final; + int immutable; + int implements; + int in; + int inline; + int let; + int macro; + int match; + int mutable; + int null; + int of; + int partial; + int promise; + int reference; + int relocatable; + int sealed; + int sizeof; + int static; + int supports; + int switch; + int typedef; + int typeof; + int unchecked; +} diff --git a/test/raw_tests/fails/reserved_kw_ko2.sol b/test/raw_tests/fails/reserved_kw_ko2.sol new file mode 100644 index 0000000..0318d5a --- /dev/null +++ b/test/raw_tests/fails/reserved_kw_ko2.sol @@ -0,0 +1,35 @@ +contract InterestingLocalVars { + function f() { + int after; + int alias; + int apply; + int auto; + int case; + int copyof; + int default; + int define; + int final; + int immutable; + int implements; + int in; + int inline; + int let; + int macro; + int match; + int mutable; + int null; + int of; + int partial; + int promise; + int reference; + int relocatable; + int sealed; + int sizeof; + int static; + int supports; + int switch; + int typedef; + int typeof; + int unchecked; + } +}