Skip to content

Commit ac048c8

Browse files
committed
support lua5.5 grammar
1 parent de08dfd commit ac048c8

File tree

6 files changed

+6360
-4836
lines changed

6 files changed

+6360
-4836
lines changed

grammar.js

Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ export default grammar({
8484
for namelist in explist do block end |
8585
function funcname funcbody |
8686
local function Name funcbody |
87-
local namelist ['=' explist]
87+
global function Name funcbody |
88+
local attnamelist ['=' explist] |
89+
global attnamelist ['=' explist] |
90+
global [attrib] ‘*’
8891
*/
8992
statement: ($) =>
9093
choice(
@@ -99,7 +102,7 @@ export default grammar({
99102
$.repeat_statement,
100103
$.if_statement,
101104
$.for_statement,
102-
$.declaration
105+
$.declaration,
103106
),
104107

105108
// retstat ::= return [explist] [';']
@@ -207,25 +210,45 @@ export default grammar({
207210
field('end', $.expression),
208211
optional(seq(',', field('step', $.expression)))
209212
),
213+
// namelist ::= Name {',' Name}
214+
_name_list: ($) => name_list($),
210215

211216
// function funcname funcbody
212217
// local function Name funcbody
213-
// local namelist ['=' explist]
218+
// global function Name funcbody
219+
// local attnamelist [‘=’ explist]
220+
// global attnamelist [‘=’ explist]
221+
// global [attrib] ‘*’
214222
declaration: ($) =>
215223
choice(
216224
$.function_declaration,
217225
field(
218226
'local_declaration',
219227
alias($._local_function_declaration, $.function_declaration)
220228
),
221-
field('local_declaration', $.variable_declaration)
229+
field('local_declaration', $.variable_declaration),
230+
field(
231+
'global_declaration',
232+
alias($._global_function_declaration, $.function_declaration)
233+
),
234+
field(
235+
'global_declaration',
236+
alias($._global_variable_declaration, $.variable_declaration)
237+
),
238+
field(
239+
'global_declaration',
240+
alias($._global_implicit_variable_declaration, $.implicit_variable_declaration)
241+
)
222242
),
223243
// function funcname funcbody
224244
function_declaration: ($) =>
225245
seq('function', field('name', $._function_name), $._function_body),
226246
// local function Name funcbody
227247
_local_function_declaration: ($) =>
228248
seq('local', 'function', field('name', $.identifier), $._function_body),
249+
// global function Name funcbody
250+
_global_function_declaration: ($) =>
251+
seq('global', 'function', field('name', $.identifier), $._function_body),
229252
// funcname ::= Name {'.' Name} [':' Name]
230253
_function_name: ($) =>
231254
choice(
@@ -253,34 +276,52 @@ export default grammar({
253276
field('method', $.identifier)
254277
),
255278

256-
// local namelist ['=' explist]
279+
// local attnamelist [‘=’ explist]
257280
variable_declaration: ($) =>
258281
seq(
259282
'local',
260283
choice(
261284
alias($._att_name_list, $.variable_list),
262-
alias($._local_variable_assignment, $.assignment_statement)
285+
alias($._variable_assignment, $.assignment_statement)
286+
)
287+
),
288+
// global attnamelist [‘=’ explist]
289+
_global_variable_declaration: ($) =>
290+
seq(
291+
'global',
292+
choice(
293+
alias($._att_name_list, $.variable_list),
294+
alias($._variable_assignment, $.assignment_statement),
263295
)
264296
),
265-
_local_variable_assignment: ($) =>
297+
// attnamelist ‘=’ explist
298+
_variable_assignment: ($) =>
266299
seq(
267300
alias($._att_name_list, $.variable_list),
268301
field('operator', '='),
269302
alias($._variable_assignment_explist, $.expression_list)
270303
),
271-
// namelist ::= Name {',' Name}
272-
_name_list: ($) => name_list($),
273304

274-
// attnamelist ::= Name attrib {‘,’ Name attrib}
305+
// attnamelist ::= [attrib] Name [attrib] {‘,’ Name [attrib]}
275306
_att_name_list: ($) =>
276-
list_seq(
277-
seq(
278-
field('name', $.identifier),
279-
optional(field('attribute', alias($._attrib, $.attribute)))
307+
seq(
308+
optional(field('attribute', alias($._attrib, $.attribute))),
309+
list_seq(
310+
seq(
311+
field('name', $.identifier),
312+
optional(field('attribute', alias($._attrib, $.attribute)))
313+
),
314+
','
280315
),
281-
','
282316
),
283-
// attrib ::= [‘<’ Name ‘>’]
317+
// global [attrib] ‘*’
318+
_global_implicit_variable_declaration: ($) =>
319+
seq(
320+
'global',
321+
optional(field('attribute', alias($._attrib, $.attribute))),
322+
'*'
323+
),
324+
// attrib ::= ‘<’ Name ‘>’
284325
_attrib: ($) => seq('<', $.identifier, '>'),
285326

286327
// explist ::= exp {',' exp}
@@ -429,20 +470,23 @@ export default grammar({
429470
),
430471
// '(' [parlist] ')'
431472
parameters: ($) => seq('(', optional($._parameter_list), ')'),
432-
// parlist ::= namelist [',' '...'] | '...'
473+
// parlist ::= namelist [‘,’ varargparam] | varargparam
433474
_parameter_list: ($) =>
434475
choice(
435-
seq(name_list($), optional(seq(',', $.vararg_expression))),
436-
$.vararg_expression
476+
seq(name_list($), optional(seq(',', $._vararg_parameter))),
477+
$._vararg_parameter
437478
),
479+
// varargparam ::= ‘...’ [Name]
480+
_vararg_parameter: ($) =>
481+
seq($.vararg_expression, optional(field('name', $.identifier))),
438482

439483
// prefixexp ::= var | functioncall | '(' exp ')'
440484
_prefix_expression: ($) =>
441485
prec(1, choice($.variable, $.function_call, $.parenthesized_expression)),
442486

443487
// var ::= Name | prefixexp [ exp ] | prefixexp . Name
444488
variable: ($) =>
445-
choice($.identifier, $.bracket_index_expression, $.dot_index_expression),
489+
choice($._contextual_keyword, $.identifier, $.bracket_index_expression, $.dot_index_expression),
446490
// prefixexp [ exp ]
447491
bracket_index_expression: ($) =>
448492
seq(
@@ -499,7 +543,7 @@ export default grammar({
499543
field('operator', '='),
500544
field('value', $.expression)
501545
),
502-
seq(field('name', $.identifier), field('operator', '='), field('value', $.expression)),
546+
seq(field('name', choice($._contextual_keyword, $.identifier)), '=', field('value', $.expression)),
503547
field('value', $.expression)
504548
),
505549

@@ -583,5 +627,8 @@ export default grammar({
583627
field('end', alias($._block_comment_end, ']]'))
584628
)
585629
),
630+
631+
// only `global` for now
632+
_contextual_keyword: (_) => 'global',
586633
},
587634
});

queries/highlights.scm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"goto"
66
"in"
77
"local"
8+
"global"
89
] @keyword
910

1011
(label_statement) @label

0 commit comments

Comments
 (0)