Skip to content

Commit

Permalink
feat: support variadic types and variadic generic parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
amaanq committed May 21, 2024
1 parent 773eae8 commit 09df169
Show file tree
Hide file tree
Showing 5 changed files with 9,739 additions and 9,130 deletions.
23 changes: 18 additions & 5 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ const optional_block = $ => alias(optional($._block), $.block);
module.exports = grammar(lua, {
name: 'luau',

supertypes: ($, original) => original.concat([
$.type,
]),

rules: {
// Luau has no goto and label statements, and has continue statements
statement: ($, original) => choice(
Expand Down Expand Up @@ -121,13 +125,19 @@ module.exports = grammar(lua, {
field('body', optional_block($)),
'end',
),
generic_type_list: $ => seq('<', commaSep1($.identifier), '>'),
_parameter_list: $ => choice(
seq(commaSep1($.parameter), optional(seq(',', $.vararg_expression))),
$.vararg_expression,
generic_type_list: $ => seq(
'<',
commaSep1(
seq($.identifier, optional($.vararg_expression)),
),
'>',
),
_parameter_list: $ => choice(commaSep1($.parameter)),

parameter: $ => seq($.identifier, optional(seq(':', $.type))),
parameter: $ => seq(
choice($.identifier, $.vararg_expression),
optional(seq(':', $.type)),
),

_att_name_list: $ => sep1(
seq(
Expand All @@ -150,6 +160,7 @@ module.exports = grammar(lua, {
$.union_type,
$.optional_type,
$.literal_type,
$.variadic_type,
)),

builtin_type: _ => choice(
Expand Down Expand Up @@ -207,6 +218,8 @@ module.exports = grammar(lua, {

literal_type: $ => choice($.string, $.true, $.false),

variadic_type: $ => prec.right(seq('...', $.type)),

expression: ($, original) => choice(
original,
$.cast_expression,
Expand Down
2 changes: 2 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@
((identifier) @variable.builtin
(#eq? @variable.builtin "self"))

"..." @variable.builtin

((identifier) @module.builtin
(#any-of? @module.builtin "_G" "debug" "io" "jit" "math" "os" "package" "string" "table" "utf8"))

Expand Down
141 changes: 88 additions & 53 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -1963,56 +1963,26 @@
"type": "SEQ",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "parameter"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "parameter"
}
]
}
}
]
"type": "SYMBOL",
"name": "parameter"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "vararg_expression"
}
]
},
{
"type": "BLANK"
}
]
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "parameter"
}
]
}
}
]
},
{
"type": "SYMBOL",
"name": "vararg_expression"
}
]
},
Expand Down Expand Up @@ -3117,8 +3087,25 @@
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "vararg_expression"
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "REPEAT",
Expand All @@ -3130,8 +3117,25 @@
"value": ","
},
{
"type": "SYMBOL",
"name": "identifier"
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "vararg_expression"
},
{
"type": "BLANK"
}
]
}
]
}
]
}
Expand All @@ -3148,8 +3152,17 @@
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "vararg_expression"
}
]
},
{
"type": "CHOICE",
Expand Down Expand Up @@ -3223,6 +3236,10 @@
{
"type": "SYMBOL",
"name": "literal_type"
},
{
"type": "SYMBOL",
"name": "variadic_type"
}
]
}
Expand Down Expand Up @@ -3792,6 +3809,23 @@
}
]
},
"variadic_type": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "..."
},
{
"type": "SYMBOL",
"name": "type"
}
]
}
},
"cast_expression": {
"type": "PREC",
"value": 11,
Expand Down Expand Up @@ -4067,6 +4101,7 @@
"statement",
"expression",
"declaration",
"variable"
"variable",
"type"
]
}
Loading

0 comments on commit 09df169

Please sign in to comment.