-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathgrammar.jison
200 lines (166 loc) · 6.02 KB
/
grammar.jison
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
%lex
%x script
%%
// Inline JavaScript State (gobble up all tokens including whitespace)
"{{=" %{ this.begin("script"); return "SCRIPT_EVAL"; %}
"{{!" %{ this.begin("script"); return "SCRIPT_EXEC"; %}
<script>([^\}]|\n|\}[^}]) return "SCRIPT_TOKEN";
<script>"}}" this.popState()
// Assembler State
// Ignorables
([;][^\n]*\n) // Ignore comments
(\s+) // Ignore Whitespace
// Opcodes
([A-Za-z][A-Za-z0-9]*) return "ID"
// Lists
"(" return "OPEN_PAREN"
")" return "CLOSE_PAREN"
"," return "COMMA"
// Labelled Target Prefixes
([@][A-Za-z][A-Za-z0-9]*) return "AT_ID"
// Label References
([$](_|[A-Za-z][A-Za-z0-9]*)) return "DOLLAR_ID"
([#](_|[A-Za-z][A-Za-z0-9]*)) return "HASH_ID"
// Scope
"{" return "OPEN_BRACE"
"}" return "CLOSE_BRACE"
// Label
":" return "COLON"
// Data Segment
"[" return "OPEN_BRACKET"
"]" return "CLOSE_BRACKET"
// Literals
(0x([0-9a-fA-F][0-9a-fA-F])*) return "HEX"
(0b(0|1)+) return "BINARY"
([1-9][0-9]*|0) return "DECIMAL"
// Pop Placeholders
"$$" return "DOLLAR_DOLLAR"
([$][1-9][0-9]*) return "DOLLAR_INDEX"
// Special
<<EOF>> return "EOF"
return "INVALID"
/lex
%start program
%%
program
: statement_list EOF
{ return { type: "scope", name: "_", statements: $1, loc: getLoc(yy, null) }; }
;
javascript
: /* empty */
{ $$ = ""; }
| SCRIPT_TOKEN javascript
{ $$ = $1 + $2; }
;
opcode_list
: opcode
{ $$ = [ $1 ]; }
| opcode COMMA opcode_list
{ {
const opcodes = $3.slice();
opcodes.unshift($1);
$$ = opcodes;
} }
;
opcode
: ID
{ $$ = { type: "opcode", bare: true, mnemonic: $1, operands: [ ], loc: getLoc(yy, @1) }; }
| ID OPEN_PAREN CLOSE_PAREN
{ $$ = { type: "opcode", mnemonic: $1, operands: [ ], loc: getLoc(yy, @1, @3) }; }
| ID OPEN_PAREN opcode_list CLOSE_PAREN
{ $$ = { type: "opcode", mnemonic: $1, operands: $3, loc: getLoc(yy, @1, @4) }; }
| HASH_ID
{ $$ = { type: "length", label: $1.substring(1), loc: getLoc(yy, @1) }; }
| DOLLAR_ID
{ $$ = { type: "offset", label: $1.substring(1), loc: getLoc(yy, @1) }; }
| HEX
{ $$ = { type: "hex", value: $1, loc: getLoc(yy, @1) }; }
| DECIMAL
{ $$ = { type: "decimal", value: $1, loc: getLoc(yy, @1) }; }
| BINARY
{ {
const hex = "0x" + parseInt(("0" + ($1).substring(2)), 2).toString(16);
$$ = { type: "hex", value: hex, loc: getLoc(yy, @1) };
} }
| DOLLAR_DOLLAR
{ $$ = { type: "pop", index: 0, loc: getLoc(yy, @1) }; }
| DOLLAR_INDEX
{ $$ = { type: "pop", index: parseInt(($1).substring(1)), loc: getLoc(yy, @1) }; }
| SCRIPT_EVAL javascript
{ $$ = { type: "eval", script: $2, loc: getLoc(yy, @1, @2) }; }
;
hex_list
: /* empty */
{ $$ = [ ]; }
| hex hex_list
{ {
const hexes = $2.slice();;
hexes.unshift($1);
$$ = hexes;
} }
;
hex
: HEX
{ $$ = { type: "hex", verbatim: true, value: $1, loc: getLoc(yy, @1) }; }
| DECIMAL
{ {
const value = parseInt($1);
if (value >= 256) { throw new Error("decimal data values must be single bytes"); }
let hex = (value).toString(16);
while (hex.length < 2) { hex = "0" + hex; }
$$ = { type: "hex", verbatim: true, value: ("0x" + hex), loc: getLoc(yy, @1) };
} }
| BINARY
{ {
const value = parseInt(($1).substring(2), 2);
if (value >= 256) { throw new Error("binary data values must be single bytes"); }
const hex = ("0x" + (value).toString(16));
$$ = { type: "hex", verbatim: true, value: hex, loc: getLoc(yy, @1) };
} }
| SCRIPT_EVAL javascript
{ $$ = { type: "eval", verbatim: true, script: $2, loc: getLoc(yy, @1, @2) }; }
;
statement_list
: /* empty */
{ $$ = [ ]; }
| statement statement_list
{ {
const statements = $2.slice();
statements.unshift($1);
$$ = statements;
} }
;
statement
: opcode
{ {
const statement = $1;
statement.loc.statement = true;
$$ = statement;
} }
| AT_ID COLON
{ $$ = { type: "label", name: $1.substring(1), loc: getLoc(yy, @1, @2, true) }; }
| AT_ID OPEN_BRACE statement_list CLOSE_BRACE
{ $$ = { type: "scope", name: $1.substring(1), statements: $3, loc: getLoc(yy, @1, @4, true) }; }
| AT_ID OPEN_BRACKET hex_list CLOSE_BRACKET
{ $$ = { type: "data", name: $1.substring(1), data: $3, loc: getLoc(yy, @1, @4, true) }; }
| SCRIPT_EXEC javascript
{ $$ = { type: "exec", script: $2, loc: getLoc(yy, @1, @2, true) }; }
;
%%
function getLoc(yy, start, end, statement) {
if (end == null) { end = start; }
let result = null;
if (start) {
result = {
first_line: start.first_line,
first_column: start.first_column,
last_line: end.last_line,
last_column: end.last_column,
statement: !!statement
};
}
if (yy._ethersLocation) {
return yy._ethersLocation(result);
}
return result;
}