Skip to content

Commit 575caac

Browse files
iamvukasinalecthomas
authored andcommitted
Improve lexer for Solidity
1 parent cab6ebc commit 575caac

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

Diff for: lexers/s/solidity.go

+18-14
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,16 @@ func solidityRules() Rules {
3939
{`/[*][\w\W]*`, CommentMultiline, nil},
4040
},
4141
"keywords-other": {
42-
{Words(``, `\b`, `for`, `in`, `while`, `do`, `break`, `return`, `returns`, `continue`, `if`, `else`, `throw`, `new`, `delete`), Keyword, nil},
42+
{Words(``, `\b`, `for`, `in`, `while`, `do`, `break`, `return`, `returns`, `continue`, `if`, `else`, `try`, `catch`, `throw`, `_`, `new`, `delete`, `is`, `as`, `from`, `memory`, `storage`), Keyword, nil},
4343
{`assembly\b`, Keyword, Push("assembly")},
44-
{Words(``, `\b`, `contract`, `interface`, `enum`, `event`, `function`, `library`, `mapping`, `modifier`, `struct`, `var`), KeywordDeclaration, nil},
44+
{`(contract|interface|enum|event|struct)(\s+)([a-zA-Z_]\w*)`, ByGroups(KeywordDeclaration, Text, NameClass), nil},
45+
{`(function|modifier)(\s+)([a-zA-Z_]\w*)`, ByGroups(KeywordDeclaration, Text, NameFunction), nil},
46+
{Words(``, `\b`, `contract`, `interface`, `enum`, `event`, `constructor`, `function`, `library`, `mapping`, `modifier`, `struct`, `var`), KeywordDeclaration, nil},
47+
{Words(``, `\b`, `abstract`, `external`, `internal`, `private`, `public`), Keyword, nil},
48+
{Words(``, `\b`, `anonymous`, `constant`, `immutable`, `indexed`, `override`, `payable`, `pure`, `view`, `virtual`), Keyword, nil},
4549
{`(import|using)\b`, KeywordNamespace, nil},
46-
{`pragma (solidity|experimental)\b`, KeywordReserved, nil},
47-
{`(_|as|constant|default|from|is)\b`, KeywordReserved, nil},
48-
{`payable\b`, KeywordReserved, nil},
49-
{`(memory|storage)\b`, KeywordReserved, nil},
50-
{`(external|internal|private|public)\b`, KeywordReserved, nil},
51-
{`(anonymous|indexed)\b`, KeywordReserved, nil},
52-
{`(abstract|pure|static|view)\b`, KeywordReserved, nil},
50+
{`pragma (solidity|experimental)\b`, Keyword, nil},
51+
{Words(``, `\b`, `after`, `alias`, `apply`, `auto`, `case`, `copyof`, `default`, `define`, `final`, `implements`, `inline`, `let`, `macro`, `match`, `mutable`, `null`, `of`, `partial`, `promise`, `reference`, `relocatable`, `sealed`, `sizeof`, `static`, `supports`, `switch`, `typedef`, `typeof`, `unchecked`), KeywordReserved, nil},
5352
{`(true|false)\b`, KeywordConstant, nil},
5453
{`(wei|finney|szabo|ether)\b`, KeywordConstant, nil},
5554
{`(seconds|minutes|hours|days|weeks|years)\b`, KeywordConstant, nil},
@@ -99,15 +98,20 @@ func solidityRules() Rules {
9998
{`\+\+|--|\*\*|\?|:|~|&&|\|\||=>|==?|!=?|(<<|>>>?|[-<>+*%&|^/])=?`, Operator, nil},
10099
{`[{(\[;,]`, Punctuation, nil},
101100
{`[})\].]`, Punctuation, nil},
102-
{`(block|msg|now|this|super|tx)\b`, NameBuiltin, nil},
103-
{`(sender|origin)\b`, NameBuiltin, nil},
104-
{`(gas|value)\b`, NameBuiltin, nil},
101+
{`(abi|block|msg|tx)\b`, NameBuiltin, nil},
102+
{`(?!abi\.)(decode|encode|encodePacked|encodeWithSelector|encodeWithSignature|encodeWithSelector)\b`, NameBuiltin, nil},
103+
{`(?!block\.)(chainid|coinbase|difficulty|gaslimit|number|timestamp)\b`, NameBuiltin, nil},
104+
{`(?!msg\.)(data|gas|sender|value)\b`, NameBuiltin, nil},
105+
{`(?!tx\.)(gasprice|origin)\b`, NameBuiltin, nil},
106+
{`(type)(\()([a-zA-Z_]\w*)(\))`, ByGroups(NameBuiltin, Punctuation, NameClass, Punctuation), nil},
107+
{`(?!type\([a-zA-Z_]\w*\)\.)(creationCode|interfaceId|max|min|name|runtimeCode)\b`, NameBuiltin, nil},
108+
{`(now|this|super|gasleft)\b`, NameBuiltin, nil},
105109
{`(selfdestruct|suicide)\b`, NameBuiltin, nil},
106-
{`(balance|send|transfer)\b`, NameBuiltin, nil},
110+
{`(?!0x[0-9a-fA-F]+\.)(balance|code|codehash|send|transfer)\b`, NameBuiltin, nil},
107111
{`(assert|revert|require)\b`, NameBuiltin, nil},
108112
{`(call|callcode|delegatecall)\b`, NameBuiltin, nil},
109113
{`selector\b`, NameBuiltin, nil},
110-
{`(addmod|ecrecover|keccak256|mulmod|ripemd160|sha256|sha3)\b`, NameFunction, nil},
114+
{`(addmod|blockhash|ecrecover|keccak256|mulmod|ripemd160|sha256|sha3)\b`, NameBuiltin, nil},
111115
{`[a-zA-Z_]\w*`, Name, nil},
112116
},
113117
}

Diff for: lexers/testdata/solidity.expected

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[
2-
{"type":"KeywordReserved","value":"pragma solidity"},
2+
{"type":"Keyword","value":"pragma solidity"},
33
{"type":"Text","value":" "},
44
{"type":"Operator","value":"^"},
55
{"type":"LiteralNumberInteger","value":"0"},
@@ -11,30 +11,30 @@
1111
{"type":"Text","value":"\n\n"},
1212
{"type":"KeywordDeclaration","value":"contract"},
1313
{"type":"Text","value":" "},
14-
{"type":"Name","value":"ReactExample"},
14+
{"type":"NameClass","value":"ReactExample"},
1515
{"type":"Text","value":" "},
1616
{"type":"Punctuation","value":"{"},
1717
{"type":"Text","value":"\n "},
1818
{"type":"KeywordType","value":"address"},
1919
{"type":"Text","value":" "},
20-
{"type":"KeywordReserved","value":"private"},
20+
{"type":"Keyword","value":"private"},
2121
{"type":"Text","value":" "},
2222
{"type":"Name","value":"owner"},
2323
{"type":"Punctuation","value":";"},
2424
{"type":"Text","value":"\n "},
2525
{"type":"KeywordType","value":"string"},
2626
{"type":"Text","value":" "},
27-
{"type":"KeywordReserved","value":"public"},
27+
{"type":"Keyword","value":"public"},
2828
{"type":"Text","value":" "},
2929
{"type":"Name","value":"you_awesome"},
3030
{"type":"Punctuation","value":";"},
3131
{"type":"Text","value":"\n \n "},
3232
{"type":"KeywordDeclaration","value":"function"},
3333
{"type":"Text","value":" "},
34-
{"type":"Name","value":"ReactExample"},
34+
{"type":"NameFunction","value":"ReactExample"},
3535
{"type":"Punctuation","value":"()"},
3636
{"type":"Text","value":" "},
37-
{"type":"KeywordReserved","value":"public"},
37+
{"type":"Keyword","value":"public"},
3838
{"type":"Text","value":" "},
3939
{"type":"Punctuation","value":"{"},
4040
{"type":"Text","value":"\n "},
@@ -58,10 +58,10 @@
5858
{"type":"Text","value":"\n \n "},
5959
{"type":"KeywordDeclaration","value":"function"},
6060
{"type":"Text","value":" "},
61-
{"type":"Name","value":"kill"},
61+
{"type":"NameFunction","value":"kill"},
6262
{"type":"Punctuation","value":"()"},
6363
{"type":"Text","value":" "},
64-
{"type":"KeywordReserved","value":"public"},
64+
{"type":"Keyword","value":"public"},
6565
{"type":"Text","value":" "},
6666
{"type":"Punctuation","value":"{"},
6767
{"type":"Text","value":"\n "},
@@ -85,9 +85,9 @@
8585
{"type":"Text","value":" "},
8686
{"type":"Punctuation","value":"()"},
8787
{"type":"Text","value":" "},
88-
{"type":"KeywordReserved","value":"public"},
88+
{"type":"Keyword","value":"public"},
8989
{"type":"Text","value":" "},
90-
{"type":"KeywordReserved","value":"payable"},
90+
{"type":"Keyword","value":"payable"},
9191
{"type":"Text","value":" "},
9292
{"type":"Punctuation","value":"{"},
9393
{"type":"Text","value":"\n "},

0 commit comments

Comments
 (0)