Skip to content

Commit

Permalink
Improve lexer for Solidity
Browse files Browse the repository at this point in the history
  • Loading branch information
iamvukasin authored and alecthomas committed Apr 27, 2021
1 parent cab6ebc commit 575caac
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
32 changes: 18 additions & 14 deletions lexers/s/solidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,16 @@ func solidityRules() Rules {
{`/[*][\w\W]*`, CommentMultiline, nil},
},
"keywords-other": {
{Words(``, `\b`, `for`, `in`, `while`, `do`, `break`, `return`, `returns`, `continue`, `if`, `else`, `throw`, `new`, `delete`), Keyword, nil},
{Words(``, `\b`, `for`, `in`, `while`, `do`, `break`, `return`, `returns`, `continue`, `if`, `else`, `try`, `catch`, `throw`, `_`, `new`, `delete`, `is`, `as`, `from`, `memory`, `storage`), Keyword, nil},
{`assembly\b`, Keyword, Push("assembly")},
{Words(``, `\b`, `contract`, `interface`, `enum`, `event`, `function`, `library`, `mapping`, `modifier`, `struct`, `var`), KeywordDeclaration, nil},
{`(contract|interface|enum|event|struct)(\s+)([a-zA-Z_]\w*)`, ByGroups(KeywordDeclaration, Text, NameClass), nil},
{`(function|modifier)(\s+)([a-zA-Z_]\w*)`, ByGroups(KeywordDeclaration, Text, NameFunction), nil},
{Words(``, `\b`, `contract`, `interface`, `enum`, `event`, `constructor`, `function`, `library`, `mapping`, `modifier`, `struct`, `var`), KeywordDeclaration, nil},
{Words(``, `\b`, `abstract`, `external`, `internal`, `private`, `public`), Keyword, nil},
{Words(``, `\b`, `anonymous`, `constant`, `immutable`, `indexed`, `override`, `payable`, `pure`, `view`, `virtual`), Keyword, nil},
{`(import|using)\b`, KeywordNamespace, nil},
{`pragma (solidity|experimental)\b`, KeywordReserved, nil},
{`(_|as|constant|default|from|is)\b`, KeywordReserved, nil},
{`payable\b`, KeywordReserved, nil},
{`(memory|storage)\b`, KeywordReserved, nil},
{`(external|internal|private|public)\b`, KeywordReserved, nil},
{`(anonymous|indexed)\b`, KeywordReserved, nil},
{`(abstract|pure|static|view)\b`, KeywordReserved, nil},
{`pragma (solidity|experimental)\b`, Keyword, nil},
{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},
{`(true|false)\b`, KeywordConstant, nil},
{`(wei|finney|szabo|ether)\b`, KeywordConstant, nil},
{`(seconds|minutes|hours|days|weeks|years)\b`, KeywordConstant, nil},
Expand Down Expand Up @@ -99,15 +98,20 @@ func solidityRules() Rules {
{`\+\+|--|\*\*|\?|:|~|&&|\|\||=>|==?|!=?|(<<|>>>?|[-<>+*%&|^/])=?`, Operator, nil},
{`[{(\[;,]`, Punctuation, nil},
{`[})\].]`, Punctuation, nil},
{`(block|msg|now|this|super|tx)\b`, NameBuiltin, nil},
{`(sender|origin)\b`, NameBuiltin, nil},
{`(gas|value)\b`, NameBuiltin, nil},
{`(abi|block|msg|tx)\b`, NameBuiltin, nil},
{`(?!abi\.)(decode|encode|encodePacked|encodeWithSelector|encodeWithSignature|encodeWithSelector)\b`, NameBuiltin, nil},
{`(?!block\.)(chainid|coinbase|difficulty|gaslimit|number|timestamp)\b`, NameBuiltin, nil},
{`(?!msg\.)(data|gas|sender|value)\b`, NameBuiltin, nil},
{`(?!tx\.)(gasprice|origin)\b`, NameBuiltin, nil},
{`(type)(\()([a-zA-Z_]\w*)(\))`, ByGroups(NameBuiltin, Punctuation, NameClass, Punctuation), nil},
{`(?!type\([a-zA-Z_]\w*\)\.)(creationCode|interfaceId|max|min|name|runtimeCode)\b`, NameBuiltin, nil},
{`(now|this|super|gasleft)\b`, NameBuiltin, nil},
{`(selfdestruct|suicide)\b`, NameBuiltin, nil},
{`(balance|send|transfer)\b`, NameBuiltin, nil},
{`(?!0x[0-9a-fA-F]+\.)(balance|code|codehash|send|transfer)\b`, NameBuiltin, nil},
{`(assert|revert|require)\b`, NameBuiltin, nil},
{`(call|callcode|delegatecall)\b`, NameBuiltin, nil},
{`selector\b`, NameBuiltin, nil},
{`(addmod|ecrecover|keccak256|mulmod|ripemd160|sha256|sha3)\b`, NameFunction, nil},
{`(addmod|blockhash|ecrecover|keccak256|mulmod|ripemd160|sha256|sha3)\b`, NameBuiltin, nil},
{`[a-zA-Z_]\w*`, Name, nil},
},
}
Expand Down
20 changes: 10 additions & 10 deletions lexers/testdata/solidity.expected
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[
{"type":"KeywordReserved","value":"pragma solidity"},
{"type":"Keyword","value":"pragma solidity"},
{"type":"Text","value":" "},
{"type":"Operator","value":"^"},
{"type":"LiteralNumberInteger","value":"0"},
Expand All @@ -11,30 +11,30 @@
{"type":"Text","value":"\n\n"},
{"type":"KeywordDeclaration","value":"contract"},
{"type":"Text","value":" "},
{"type":"Name","value":"ReactExample"},
{"type":"NameClass","value":"ReactExample"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordType","value":"address"},
{"type":"Text","value":" "},
{"type":"KeywordReserved","value":"private"},
{"type":"Keyword","value":"private"},
{"type":"Text","value":" "},
{"type":"Name","value":"owner"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"KeywordType","value":"string"},
{"type":"Text","value":" "},
{"type":"KeywordReserved","value":"public"},
{"type":"Keyword","value":"public"},
{"type":"Text","value":" "},
{"type":"Name","value":"you_awesome"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n \n "},
{"type":"KeywordDeclaration","value":"function"},
{"type":"Text","value":" "},
{"type":"Name","value":"ReactExample"},
{"type":"NameFunction","value":"ReactExample"},
{"type":"Punctuation","value":"()"},
{"type":"Text","value":" "},
{"type":"KeywordReserved","value":"public"},
{"type":"Keyword","value":"public"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
Expand All @@ -58,10 +58,10 @@
{"type":"Text","value":"\n \n "},
{"type":"KeywordDeclaration","value":"function"},
{"type":"Text","value":" "},
{"type":"Name","value":"kill"},
{"type":"NameFunction","value":"kill"},
{"type":"Punctuation","value":"()"},
{"type":"Text","value":" "},
{"type":"KeywordReserved","value":"public"},
{"type":"Keyword","value":"public"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
Expand All @@ -85,9 +85,9 @@
{"type":"Text","value":" "},
{"type":"Punctuation","value":"()"},
{"type":"Text","value":" "},
{"type":"KeywordReserved","value":"public"},
{"type":"Keyword","value":"public"},
{"type":"Text","value":" "},
{"type":"KeywordReserved","value":"payable"},
{"type":"Keyword","value":"payable"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
Expand Down

0 comments on commit 575caac

Please sign in to comment.