Skip to content

Commit c1f7b15

Browse files
committed
Add local variable declaration
1 parent 3d419ee commit c1f7b15

File tree

5 files changed

+27118
-25308
lines changed

5 files changed

+27118
-25308
lines changed

corpus/expressions.txt

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ some_table = { 'pizza', 'favorite', "food", true }
88

99
(lua
1010
(variable_declaration (variable_declarator (identifier))
11-
(variable_expression
12-
(table
13-
(field (string))
14-
(field (string))
15-
(field (string))
16-
(field (true))))))
11+
(table
12+
(field (string))
13+
(field (string))
14+
(field (string))
15+
(field (true)))))
1716

1817
============================================
1918
Binary operations
@@ -29,22 +28,19 @@ c = i * 2 ^ j;
2928

3029
(lua
3130
(variable_declaration (variable_declarator (identifier))
32-
(variable_expression
33-
(binary_operation
34-
(binary_operation (identifier)
35-
(binary_operation (identifier) (number)))
36-
(binary_operation (identifier) (number)))))
31+
(binary_operation
32+
(binary_operation (identifier)
33+
(binary_operation (identifier) (number)))
34+
(binary_operation (identifier) (number))))
3735

3836
(variable_declaration (variable_declarator (identifier))
39-
(variable_expression
37+
(binary_operation (identifier)
4038
(binary_operation (identifier)
41-
(binary_operation (identifier)
42-
(binary_operation (number) (number))))))
39+
(binary_operation (number) (number)))))
4340

4441
(variable_declaration (variable_declarator (identifier))
45-
(variable_expression
46-
(binary_operation (identifier)
47-
(binary_operation (number) (identifier))))))
42+
(binary_operation (identifier)
43+
(binary_operation (number) (identifier)))))
4844

4945
============================================
5046
Unary operations
@@ -58,13 +54,11 @@ ternary = not condition and true or false;
5854

5955
(lua
6056
(variable_declaration (variable_declarator (identifier))
61-
(variable_expression
62-
(unary_operation (identifier))))
57+
(unary_operation (identifier)))
6358

6459
(variable_declaration (variable_declarator (identifier))
65-
(variable_expression
60+
(binary_operation
6661
(binary_operation
67-
(binary_operation
68-
(unary_operation (identifier))
69-
(true))
70-
(false)))))
62+
(unary_operation (identifier))
63+
(true))
64+
(false))))

corpus/statements.txt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22
Do statements
33
============================================
44

5+
local x, y = 0, 1, 9;
6+
57
do
68
return x == y and true or false;
79
end
810

911
---
1012

1113
(lua
12-
(do_statement
13-
(return_statement
14-
(binary_operation
15-
(binary_operation
16-
(binary_operation (identifier) (identifier))
17-
(true))
18-
(false)))))
14+
(local_variable_declaration (variable_declarator (identifier) (identifier))
15+
(number) (number) (number))
16+
(do_statement
17+
(return_statement
18+
(binary_operation
19+
(binary_operation
20+
(binary_operation (identifier) (identifier))
21+
(true))
22+
(false)))))
1923

2024
============================================
2125
If clauses
@@ -35,12 +39,12 @@ end
3539
(if_statement
3640
(condition_expression (binary_operation (identifier) (number)))
3741
(variable_declaration (variable_declarator (identifier))
38-
(variable_expression (string)))
42+
(string))
3943
(condition_expression (binary_operation (identifier) (number)))
4044
(variable_declaration (variable_declarator (identifier))
41-
(variable_expression (string)))
45+
(string))
4246
(variable_declaration (variable_declarator (identifier))
43-
(variable_expression (string)))))
47+
(string))))
4448

4549
============================================
4650
While statements

grammar.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ module.exports = grammar({
5252
alias($._expression, $.expression),
5353

5454
$.variable_declaration,
55+
$.local_variable_declaration,
5556

5657
$.do_statement,
5758
$.if_statement,
@@ -71,7 +72,13 @@ module.exports = grammar({
7172
variable_declaration: $ => seq(
7273
sequence(alias($._variable_declarator, $.variable_declarator)),
7374
'=',
74-
sequence(alias($._expression, $.variable_expression))
75+
sequence($._expression)
76+
),
77+
78+
local_variable_declaration: $ => seq(
79+
'local',
80+
alias($._local_variable_declarator, $.variable_declarator),
81+
optional(seq('=', sequence($._expression)))
7582
),
7683

7784
_variable_declarator: $ => choice(
@@ -80,6 +87,8 @@ module.exports = grammar({
8087
seq($._prefix, '.', alias($.identifier, $.property_identifier))
8188
),
8289

90+
_local_variable_declarator: $ => sequence($.identifier),
91+
8392
// Control statements
8493
do_statement: $ => seq(
8594
'do',

src/grammar.json

Lines changed: 93 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@
7878
"type": "SYMBOL",
7979
"name": "variable_declaration"
8080
},
81+
{
82+
"type": "SYMBOL",
83+
"name": "local_variable_declaration"
84+
},
8185
{
8286
"type": "SYMBOL",
8387
"name": "do_statement"
@@ -166,13 +170,8 @@
166170
"type": "SEQ",
167171
"members": [
168172
{
169-
"type": "ALIAS",
170-
"content": {
171-
"type": "SYMBOL",
172-
"name": "_expression"
173-
},
174-
"named": true,
175-
"value": "variable_expression"
173+
"type": "SYMBOL",
174+
"name": "_expression"
176175
},
177176
{
178177
"type": "REPEAT",
@@ -184,13 +183,8 @@
184183
"value": ","
185184
},
186185
{
187-
"type": "ALIAS",
188-
"content": {
189-
"type": "SYMBOL",
190-
"name": "_expression"
191-
},
192-
"named": true,
193-
"value": "variable_expression"
186+
"type": "SYMBOL",
187+
"name": "_expression"
194188
}
195189
]
196190
}
@@ -199,6 +193,66 @@
199193
}
200194
]
201195
},
196+
"local_variable_declaration": {
197+
"type": "SEQ",
198+
"members": [
199+
{
200+
"type": "STRING",
201+
"value": "local"
202+
},
203+
{
204+
"type": "ALIAS",
205+
"content": {
206+
"type": "SYMBOL",
207+
"name": "_local_variable_declarator"
208+
},
209+
"named": true,
210+
"value": "variable_declarator"
211+
},
212+
{
213+
"type": "CHOICE",
214+
"members": [
215+
{
216+
"type": "SEQ",
217+
"members": [
218+
{
219+
"type": "STRING",
220+
"value": "="
221+
},
222+
{
223+
"type": "SEQ",
224+
"members": [
225+
{
226+
"type": "SYMBOL",
227+
"name": "_expression"
228+
},
229+
{
230+
"type": "REPEAT",
231+
"content": {
232+
"type": "SEQ",
233+
"members": [
234+
{
235+
"type": "STRING",
236+
"value": ","
237+
},
238+
{
239+
"type": "SYMBOL",
240+
"name": "_expression"
241+
}
242+
]
243+
}
244+
}
245+
]
246+
}
247+
]
248+
},
249+
{
250+
"type": "BLANK"
251+
}
252+
]
253+
}
254+
]
255+
},
202256
"_variable_declarator": {
203257
"type": "CHOICE",
204258
"members": [
@@ -251,6 +305,31 @@
251305
}
252306
]
253307
},
308+
"_local_variable_declarator": {
309+
"type": "SEQ",
310+
"members": [
311+
{
312+
"type": "SYMBOL",
313+
"name": "identifier"
314+
},
315+
{
316+
"type": "REPEAT",
317+
"content": {
318+
"type": "SEQ",
319+
"members": [
320+
{
321+
"type": "STRING",
322+
"value": ","
323+
},
324+
{
325+
"type": "SYMBOL",
326+
"name": "identifier"
327+
}
328+
]
329+
}
330+
}
331+
]
332+
},
254333
"do_statement": {
255334
"type": "SEQ",
256335
"members": [

0 commit comments

Comments
 (0)