Skip to content

Commit 79621af

Browse files
authored
Fix solidity lexer's handling of inline assembly (#1040)
1 parent 9da461e commit 79621af

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

lexers/embedded/solidity.xml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,20 @@
157157
<rule pattern="(continue|returns|storage|memory|delete|return|throw|break|catch|while|else|from|new|try|for|if|is|as|do|in|_)\b">
158158
<token type="Keyword"/>
159159
</rule>
160-
<rule pattern="assembly\b">
161-
<token type="Keyword"/>
160+
<rule pattern="(assembly)(\s+\()(.+)(\)\s+{)">
161+
<bygroups>
162+
<token type="Keyword"/>
163+
<token type="Text"/>
164+
<token type="LiteralString"/>
165+
<token type="Text"/>
166+
</bygroups>
167+
<push state="assembly"/>
168+
</rule>
169+
<rule pattern="(assembly)(\s+{)">
170+
<bygroups>
171+
<token type="Keyword"/>
172+
<token type="Text"/>
173+
</bygroups>
162174
<push state="assembly"/>
163175
</rule>
164176
<rule pattern="(contract|interface|enum|event|struct)(\s+)([a-zA-Z_]\w*)">
@@ -235,7 +247,7 @@
235247
<token type="Punctuation"/>
236248
<pop depth="1"/>
237249
</rule>
238-
<rule pattern="[(),]">
250+
<rule pattern="[(),.]">
239251
<token type="Punctuation"/>
240252
</rule>
241253
<rule pattern=":=|=:">
@@ -276,4 +288,4 @@
276288
</rule>
277289
</state>
278290
</rules>
279-
</lexer>
291+
</lexer>

lexers/testdata/solidity.actual

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ contract ReactExample {
99
you_awesome = "You're awesome";
1010
}
1111

12+
function AssemblyExample() public {
13+
assembly {
14+
let o := sload(0)
15+
call(gas(), o, 5, 0, 0, 0, 0)
16+
}
17+
require(success, "Failed to send ETH");
18+
}
19+
1220
function kill() public {
1321
require(msg.sender==owner);
1422
selfdestruct(owner);

lexers/testdata/solidity.expected

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,63 @@
5858
{"type":"Text","value":"\n \n "},
5959
{"type":"KeywordDeclaration","value":"function"},
6060
{"type":"Text","value":" "},
61+
{"type":"NameFunction","value":"AssemblyExample"},
62+
{"type":"Punctuation","value":"()"},
63+
{"type":"Text","value":" "},
64+
{"type":"Keyword","value":"public"},
65+
{"type":"Text","value":" "},
66+
{"type":"Punctuation","value":"{"},
67+
{"type":"Text","value":"\n "},
68+
{"type":"Keyword","value":"assembly"},
69+
{"type":"Text","value":" {\n "},
70+
{"type":"OperatorWord","value":"let"},
71+
{"type":"Text","value":" "},
72+
{"type":"NameVariable","value":"o"},
73+
{"type":"Text","value":" "},
74+
{"type":"Operator","value":":="},
75+
{"type":"Text","value":" "},
76+
{"type":"NameFunction","value":"sload"},
77+
{"type":"Punctuation","value":"("},
78+
{"type":"LiteralNumberInteger","value":"0"},
79+
{"type":"Punctuation","value":")"},
80+
{"type":"Text","value":"\n "},
81+
{"type":"NameFunction","value":"call"},
82+
{"type":"Punctuation","value":"("},
83+
{"type":"NameFunction","value":"gas"},
84+
{"type":"Punctuation","value":"(),"},
85+
{"type":"Text","value":" "},
86+
{"type":"Name","value":"o"},
87+
{"type":"Punctuation","value":","},
88+
{"type":"Text","value":" "},
89+
{"type":"LiteralNumberInteger","value":"5"},
90+
{"type":"Punctuation","value":","},
91+
{"type":"Text","value":" "},
92+
{"type":"LiteralNumberInteger","value":"0"},
93+
{"type":"Punctuation","value":","},
94+
{"type":"Text","value":" "},
95+
{"type":"LiteralNumberInteger","value":"0"},
96+
{"type":"Punctuation","value":","},
97+
{"type":"Text","value":" "},
98+
{"type":"LiteralNumberInteger","value":"0"},
99+
{"type":"Punctuation","value":","},
100+
{"type":"Text","value":" "},
101+
{"type":"LiteralNumberInteger","value":"0"},
102+
{"type":"Punctuation","value":")"},
103+
{"type":"Text","value":"\n "},
104+
{"type":"Punctuation","value":"}"},
105+
{"type":"Text","value":"\n "},
106+
{"type":"NameBuiltin","value":"require"},
107+
{"type":"Punctuation","value":"("},
108+
{"type":"Name","value":"success"},
109+
{"type":"Punctuation","value":","},
110+
{"type":"Text","value":" "},
111+
{"type":"LiteralString","value":"\"Failed to send ETH\""},
112+
{"type":"Punctuation","value":");"},
113+
{"type":"Text","value":"\n "},
114+
{"type":"Punctuation","value":"}"},
115+
{"type":"Text","value":"\n \n "},
116+
{"type":"KeywordDeclaration","value":"function"},
117+
{"type":"Text","value":" "},
61118
{"type":"NameFunction","value":"kill"},
62119
{"type":"Punctuation","value":"()"},
63120
{"type":"Text","value":" "},

0 commit comments

Comments
 (0)