Skip to content

Commit

Permalink
Add reserved keywords to lexer and parser (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmzimmerman authored Sep 12, 2023
1 parent fe37f60 commit af26ea2
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/solidity-parser/dune
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -19,5 +24,3 @@

(documentation
(package solidity-parser))


37 changes: 36 additions & 1 deletion src/solidity-parser/solidity_lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -312,7 +347,7 @@ let init2 ?(list=[]) () =
"days", NUMBERUNIT (Days);
"weeks", NUMBERUNIT (Weeks);
"years", NUMBERUNIT (Years);
] @ list)
] @ reserved_keywords @ list)
end

let init ~freeton =
Expand Down
3 changes: 3 additions & 0 deletions src/solidity-parser/solidity_raw_parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@
%token <Solidity_common.Ident.t> IDENTIFIER
%token EOF

(* Reserved Keywords *)
%token RESERVEDKEYWORD

(* Some convenience precedences *)
%nonassoc below_IDENTIFIER
%nonassoc IDENTIFIER
Expand Down
33 changes: 33 additions & 0 deletions test/raw_tests/fails/reserved_kw_ko1.sol
Original file line number Diff line number Diff line change
@@ -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;
}
35 changes: 35 additions & 0 deletions test/raw_tests/fails/reserved_kw_ko2.sol
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit af26ea2

Please sign in to comment.