Skip to content

Commit a2aaa74

Browse files
committed
Add function statement
1 parent e759a18 commit a2aaa74

File tree

4 files changed

+28277
-26672
lines changed

4 files changed

+28277
-26672
lines changed

corpus/functions.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,35 @@ a.b.f = function(self, test, ...) return 10 end
3131
(variable_declaration (variable_declarator (identifier) (property_identifier) (property_identifier))
3232
(function_definition (parameters (self) (identifier) (spread))
3333
(return_statement (number)))))
34+
35+
============================================
36+
Function statements
37+
============================================
38+
39+
function f()
40+
return false
41+
end
42+
43+
f = function() return false end
44+
45+
function t.a.b.c.f()
46+
return true
47+
end
48+
49+
t.a.b.c.f = function() return true end
50+
51+
---
52+
53+
(lua
54+
(function (function_name (identifier)) (parameters)
55+
(return_statement (false)))
56+
57+
(variable_declaration (variable_declarator (identifier))
58+
(function_definition (parameters)
59+
(return_statement (false))))
60+
61+
(function (function_name (identifier) (property_identifier) (property_identifier) (property_identifier) (property_identifier)) (parameters)
62+
(return_statement (true)))
63+
64+
(variable_declaration (variable_declarator (identifier) (property_identifier) (property_identifier) (property_identifier) (property_identifier)) (function_definition (parameters)
65+
(return_statement (true)))))

grammar.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ module.exports = grammar({
6565
$.break_statement,
6666

6767
$.label_statement,
68-
$._empty_statement
68+
$._empty_statement,
69+
70+
alias($.function_statement, $.function)
6971
),
7072

7173
// Declarations
@@ -191,6 +193,18 @@ module.exports = grammar({
191193
_empty_statement: $ => ';',
192194

193195
// Functions
196+
function_statement: $ => seq(
197+
'function',
198+
$.function_name,
199+
$._function_body
200+
),
201+
202+
function_name: $ => seq(
203+
$.identifier,
204+
repeat(seq('.', alias($.identifier, $.property_identifier))),
205+
optional(seq(':', alias($.identifier, $.method)))
206+
),
207+
194208
parameters: $ => seq(
195209
'(',
196210
optional(choice(

src/grammar.json

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@
121121
{
122122
"type": "SYMBOL",
123123
"name": "_empty_statement"
124+
},
125+
{
126+
"type": "ALIAS",
127+
"content": {
128+
"type": "SYMBOL",
129+
"name": "function_statement"
130+
},
131+
"named": true,
132+
"value": "function"
124133
}
125134
]
126135
},
@@ -814,6 +823,79 @@
814823
"type": "STRING",
815824
"value": ";"
816825
},
826+
"function_statement": {
827+
"type": "SEQ",
828+
"members": [
829+
{
830+
"type": "STRING",
831+
"value": "function"
832+
},
833+
{
834+
"type": "SYMBOL",
835+
"name": "function_name"
836+
},
837+
{
838+
"type": "SYMBOL",
839+
"name": "_function_body"
840+
}
841+
]
842+
},
843+
"function_name": {
844+
"type": "SEQ",
845+
"members": [
846+
{
847+
"type": "SYMBOL",
848+
"name": "identifier"
849+
},
850+
{
851+
"type": "REPEAT",
852+
"content": {
853+
"type": "SEQ",
854+
"members": [
855+
{
856+
"type": "STRING",
857+
"value": "."
858+
},
859+
{
860+
"type": "ALIAS",
861+
"content": {
862+
"type": "SYMBOL",
863+
"name": "identifier"
864+
},
865+
"named": true,
866+
"value": "property_identifier"
867+
}
868+
]
869+
}
870+
},
871+
{
872+
"type": "CHOICE",
873+
"members": [
874+
{
875+
"type": "SEQ",
876+
"members": [
877+
{
878+
"type": "STRING",
879+
"value": ":"
880+
},
881+
{
882+
"type": "ALIAS",
883+
"content": {
884+
"type": "SYMBOL",
885+
"name": "identifier"
886+
},
887+
"named": true,
888+
"value": "method"
889+
}
890+
]
891+
},
892+
{
893+
"type": "BLANK"
894+
}
895+
]
896+
}
897+
]
898+
},
817899
"parameters": {
818900
"type": "SEQ",
819901
"members": [

0 commit comments

Comments
 (0)