-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar.lua
More file actions
392 lines (331 loc) · 13.5 KB
/
Copy pathgrammar.lua
File metadata and controls
392 lines (331 loc) · 13.5 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
-- MoonScript grammar for pgen. Produces the AST consumed by the
-- moonscript compiler.
--
-- The longer Cfn transform bodies live in moonscript_parser/tree.lua and
-- are required from the callback strings below.
local pgen = require "pgen"
local P, R, S, V, C, Ct, Cc, Cp, Cg, Cmb, Cmt, Cfn, L =
pgen.P, pgen.R, pgen.S, pgen.V, pgen.C, pgen.Ct, pgen.Cc, pgen.Cp,
pgen.Cg, pgen.Cmb, pgen.Cmt, pgen.Cfn, pgen.L
local ind = pgen.indenter{tab_width = 4, initial = 0}
-- `do` permission stack: the `do` expression is disabled while parsing the
-- header expression of while/with/switch/for, so the `do` in
-- `while x do ...` reads as the block keyword. 0 = disabled.
local dos = pgen.indenter{initial = 1}
local DisableDo = dos.cpush(0)
local PopDo = dos.pop
local CheckDo = dos.ctop("ne", 0)
local AlphaNum = R("az", "AZ", "09", "__")
-- Sorted longest-first: pgen's trie optimization requires longer strings
-- before their prefixes, and PEG ordered choice needs "elseif" tried
-- before "else" anyway.
local keyword_words = {
"continue", "extends",
"elseif", "export", "import", "return", "switch", "unless",
"break", "class", "local", "using", "while",
"else", "from", "then", "when", "with",
"and", "for", "not",
"do", "if", "in", "or",
}
local keyword_patt
for _, word in ipairs(keyword_words) do
keyword_patt = keyword_patt and (keyword_patt + P(word)) or P(word)
end
local Keyword = keyword_patt * -AlphaNum
local function mark(name, patt)
return Ct(Cc(name) * patt)
end
local function pos(patt)
return Cfn(Cp() * patt, [[return function(p, value)
if type(value) == "table" then
value[-1] = p
end
return value
end]])
end
-- Shared by Statement and WithExp; may raise error({node, msg}) for invalid
-- assignment targets, surfaced from parse() (see moonscript_parser/init.lua)
local assign_transform = [[
local tree = require("moonscript_parser.tree")
return function(lhs, assign)
return tree.format_assign(lhs, assign)
end]]
local function sym(chars)
return V"Space" * P(chars)
end
local function op(chars)
local patt = V"Space" * C(P(chars))
if chars:match("^%w*$") then
patt = patt * -AlphaNum
end
return patt
end
local function key(chars)
return V"Space" * P(chars) * -AlphaNum
end
return {
"Root",
Root = V"White" * V"File" * V"White" * P(-1),
White = S" \t\r\n"^0,
Break = P"\r"^-1 * P"\n",
Stop = V"Break" + P(-1),
Comment = P"--" * (P(1) - S"\r\n")^0 * L(V"Stop"),
Space = S" \t"^0 * V"Comment"^-1,
SomeSpace = S" \t"^1 * V"Comment"^-1,
SpaceBreak = V"Space" * V"Break",
EmptyLine = V"SpaceBreak",
Shebang = P"#!" * (P(1) - V"Stop")^0,
NameRaw = C(R("az", "AZ", "__") * AlphaNum^0),
Name = V"Space" * -Keyword * V"NameRaw",
Num = V"Space" * Ct(Cc"number" * C(
P"0x" * R("09", "af", "AF")^1 * (S"uU"^-1 * S"lL"^2)^-1 +
R"09"^1 * (S"uU"^-1 * S"lL"^2) +
(R"09"^1 * (P"." * R"09"^1)^-1 + P"." * R"09"^1) *
(S"eE" * P"-"^-1 * R"09"^1)^-1
)),
SelfName = V"Space" * P"@" * (
P"@" * (mark("self_class", V"NameRaw") + Cc"self.__class") +
mark("self", V"NameRaw") + Cc"self"),
KeyName = V"SelfName" + V"Space" * mark("key_literal", V"NameRaw"),
VarArg = V"Space" * C(P"..."),
File = V"Shebang"^-1 * (V"Block" + Ct(P"")),
Block = Ct(V"Line" * (V"Break"^1 * V"Line")^0),
CheckIndent = ind.check,
Line = V"CheckIndent" * V"Statement" + V"Space" * L(V"Stop"),
Statement = Cfn(
pos(
V"Import" + V"While" + V"With" + V"For" + V"ForEach" + V"Switch" +
V"Return" + V"Local" + V"Export" + V"BreakLoop" +
Cfn(Ct(V"ExpList") * (V"Update" + V"Assign")^-1, assign_transform)
) * V"Space" *
((mark("if", key"if" * V"Exp" * (key"else" * V"Exp")^-1 * V"Space") +
mark("unless", key"unless" * V"Exp") +
mark("comprehension", V"CompInner")) * V"Space")^-1,
[[return function(stm, dec)
if dec then
return {"decorated", stm, dec}
end
return stm
end]]),
Body = V"Space" * V"Break" * V"EmptyLine"^0 * V"InBlock" + Ct(V"Statement"),
Advance = ind.advance,
PushIndent = ind.push,
PreventIndent = ind.prevent,
PopIndent = ind.pop,
InBlock = V"Advance" * V"Block" * V"PopIndent",
Local = key"local" * (
mark("declare_glob", op"*" + op"^") +
mark("declare_with_shadows", Ct(V"NameList"))),
Import = mark("import",
key"import" * Ct(V"ImportNameList") * V"SpaceBreak"^0 * key"from" * V"Exp"),
ImportName = sym"\\" * Ct(Cc"colon" * V"Name") + V"Name",
ImportNameList = V"SpaceBreak"^0 * V"ImportName" *
((V"SpaceBreak"^1 + sym"," * V"SpaceBreak"^0) * V"ImportName")^0,
BreakLoop = Ct(key"break" * Cc"break") + Ct(key"continue" * Cc"continue"),
Return = mark("return",
key"return" * (mark("explist", V"ExpListLow") + C(P""))),
WithExp = Cfn(Ct(V"ExpList") * V"Assign"^-1, assign_transform),
With = mark("with",
key"with" * DisableDo * V"WithExp" * PopDo * key"do"^-1 * V"Body"),
Switch = mark("switch",
key"switch" * DisableDo * V"Exp" * PopDo * key"do"^-1 *
V"Space" * V"Break" * V"SwitchBlock"),
SwitchBlock = V"EmptyLine"^0 * V"Advance" *
Ct(V"SwitchCase" * (V"Break"^1 * V"SwitchCase")^0 *
(V"Break"^1 * V"SwitchElse")^-1) * V"PopIndent",
SwitchCase = mark("case",
key"when" * Ct(V"ExpList") * key"then"^-1 * V"Body"),
SwitchElse = mark("else", key"else" * V"Body"),
IfCond = Cfn(V"Exp" * V"Assign"^-1, [[
local tree = require("moonscript_parser.tree")
return function(lhs, assign)
return tree.format_single_assign(lhs, assign)
end]]),
IfElse = mark("else",
(V"Break" * V"EmptyLine"^0 * V"CheckIndent")^-1 * key"else" * V"Body"),
IfElseIf = mark("elseif",
(V"Break" * V"EmptyLine"^0 * V"CheckIndent")^-1 * key"elseif" *
pos(V"IfCond") * key"then"^-1 * V"Body"),
If = mark("if",
key"if" * V"IfCond" * key"then"^-1 * V"Body" *
V"IfElseIf"^0 * V"IfElse"^-1),
Unless = mark("unless",
key"unless" * V"IfCond" * key"then"^-1 * V"Body" *
V"IfElseIf"^0 * V"IfElse"^-1),
While = mark("while",
key"while" * DisableDo * V"Exp" * PopDo * key"do"^-1 * V"Body"),
For = mark("for",
key"for" * DisableDo * V"Name" * sym"=" *
Ct(V"Exp" * sym"," * V"Exp" * (sym"," * V"Exp")^-1) * PopDo *
key"do"^-1 * V"Body"),
ForEach = mark("foreach",
key"for" * Ct(V"AssignableNameList") * key"in" * DisableDo *
Ct(sym"*" * mark("unpack", V"Exp") + V"ExpList") * PopDo *
key"do"^-1 * V"Body"),
Do = mark("do", key"do" * V"Body"),
Comprehension = mark("comprehension",
sym"[" * V"Exp" * V"CompInner" * sym"]"),
TblComprehension = mark("tblcomprehension",
sym"{" * Ct(V"Exp" * (sym"," * V"Exp")^-1) * V"CompInner" * sym"}"),
CompInner = Ct((V"CompForEach" + V"CompFor") * V"CompClause"^0),
CompForEach = mark("foreach",
key"for" * Ct(V"AssignableNameList") * key"in" *
(sym"*" * mark("unpack", V"Exp") + V"Exp")),
CompFor = V"Space" * Ct(Cc"for" * P"for" * V"Name" * sym"=" *
Ct(V"Exp" * sym"," * V"Exp" * (sym"," * V"Exp")^-1)) * -AlphaNum,
CompClause = V"CompFor" + V"CompForEach" + mark("when", key"when" * V"Exp"),
Assign = mark("assign", sym"=" * (
Ct(V"With" + V"If" + V"Switch") +
Ct(V"TableBlock" + V"ExpListLow"))),
Update = mark("update",
V"Space" * C(
P"..=" + P"+=" + P"-=" + P"*=" + P"/=" + P"%=" +
P"or=" + P"and=" + P"&=" + P"|=" + P">>=" + P"<<=") * V"Exp"),
CharOperators = V"Space" * C(S"+-*/%^><|&"),
WordOperators =
op"or" + op"and" + op"<=" + op">=" + op"~=" + op"!=" + op"==" +
op".." + op"<<" + op">>" + op"//",
BinaryOperator = (V"WordOperators" + V"CharOperators") * V"SpaceBreak"^0,
Assignable = Cmt(V"Chain", [[
local subject, pos, node = ...
local last = node[#node]
local t = type(last) == "table" and last[1]
if t == "dot" or t == "index" or t == "slice" then
return pos, node
end
return false
]]) + V"Name" + V"SelfName",
Exp = Cfn(V"Value" * (V"BinaryOperator" * V"Value")^0, [[return function(...)
if select("#", ...) == 1 then
return ...
end
return {"exp", ...}
end]]),
SimpleValue =
V"If" + V"Unless" + V"Switch" + V"With" + V"ClassDecl" +
V"ForEach" + V"For" + V"While" +
CheckDo * V"Do" +
mark("minus", sym"-" * -V"SomeSpace" * V"Exp") +
mark("length", sym"#" * V"Exp") +
mark("bitnot", sym"~" * V"Exp") +
mark("not", key"not" * V"Exp") +
V"TblComprehension" + V"TableLit" + V"Comprehension" + V"FunLit" + V"Num",
ChainValue = Cfn((V"Chain" + V"Callable") * Ct(V"InvokeArgs"^-1), [[
local tree = require("moonscript_parser.tree")
return function(callee, args)
return tree.join_chain(callee, args)
end]]),
Value = pos(
V"SimpleValue" +
mark("table", Ct(V"KeyValueList")) +
V"ChainValue" + V"String"),
SliceValue = V"Exp",
String = V"Space" * V"DoubleString" + V"Space" * V"SingleString" + V"LuaString",
SingleString = mark("string",
C(P"'") * C((P"\\'" + P"\\\\" + (P(1) - P"'"))^0) * P"'"),
DoubleString = mark("string",
C(P'"') *
(C((V"DoubleStringInner" - P"#{")^1) + V"DoubleStringInterp")^0 *
P'"'),
DoubleStringInner = P'\\"' + P"\\\\" + (P(1) - P'"'),
DoubleStringInterp = mark("interpolate", P"#{" * V"Exp" * sym"}"),
-- The lua_eq group must sit at this Cfn's own level, not nested inside
-- another transform, or LuaStringClose's Cmb cannot see it.
LuaString = Cfn(
V"Space" * P"[" * Cp() * Cg(P"="^0, "lua_eq") * Cp() * P"[" *
V"Break"^-1 * C((P(1) - V"LuaStringClose")^0) * V"LuaStringClose",
[[return function(eq_start, eq_end, content)
return {"string", "[" .. ("="):rep(eq_end - eq_start) .. "[", content}
end]]),
LuaStringClose = P"]" * Cmb"lua_eq" * P"]",
Callable = pos(mark("ref", V"Name")) + V"SelfName" + V"VarArg" +
mark("parens", V"Parens"),
Parens = sym"(" * V"SpaceBreak"^0 * V"Exp" * V"SpaceBreak"^0 * sym")",
FnArgs = P"(" * V"SpaceBreak"^0 * Ct(V"FnArgsExpList"^-1) *
V"SpaceBreak"^0 * sym")" +
sym"!" * -P"=" * Ct(P""),
FnArgsExpList = V"Exp" * ((V"Break" + sym",") * V"White" * V"Exp")^0,
Chain = Ct(Cc"chain" *
(V"Callable" + V"String" + -S".\\") * V"ChainItems") +
Ct(Cc"chain" *
V"Space" * (V"DotChainItem" * V"ChainItems"^-1 + V"ColonChain")),
ChainItems = V"ChainItem"^1 * V"ColonChain"^-1 + V"ColonChain",
ChainItem = V"Invoke" + V"DotChainItem" + V"Slice" +
mark("index", P"[" * V"Exp") * sym"]",
DotChainItem = mark("dot", P"." * V"NameRaw"),
ColonChainItem = mark("colon", P"\\" * V"NameRaw"),
ColonChain = V"ColonChainItem" * (V"Invoke" * V"ChainItems"^-1)^-1,
Slice = mark("slice",
P"[" * (V"SliceValue" + Cc(1)) * sym"," * (V"SliceValue" + Cc"") *
(sym"," * V"SliceValue")^-1 * sym"]"),
Invoke = mark("call", V"FnArgs") +
Ct(Cc"call" * Ct(V"SingleString")) +
Ct(Cc"call" * Ct(V"DoubleString")) +
L(P"[") * Ct(Cc"call" * Ct(V"LuaString")),
TableValue = V"KeyValue" + Ct(V"Exp"),
TableLit = mark("table",
sym"{" * Ct(
V"TableValueList"^-1 * sym","^-1 *
(V"SpaceBreak" * V"TableLitLine" *
(sym","^-1 * V"SpaceBreak" * V"TableLitLine")^0 * sym","^-1)^-1
) * V"White" * sym"}"),
TableValueList = V"TableValue" * (sym"," * V"TableValue")^0,
TableLitLine = V"PushIndent" * V"TableValueList" * V"PopIndent" + V"Space",
TableBlockInner = Ct(V"KeyValueLine" * (V"SpaceBreak"^1 * V"KeyValueLine")^0),
TableBlock = mark("table",
V"SpaceBreak"^1 * V"Advance" * V"TableBlockInner" * V"PopIndent"),
ClassDecl = mark("class",
key"class" * -P":" *
(V"Assignable" + Cc(nil)) *
(key"extends" * V"PreventIndent" * V"Exp" * V"PopIndent" + C(P""))^-1 *
(V"ClassBlock" + Ct(P""))),
ClassBlock = V"SpaceBreak"^1 * V"Advance" *
Ct(V"ClassLine" * (V"SpaceBreak"^1 * V"ClassLine")^0) * V"PopIndent",
ClassLine = V"CheckIndent" * (
(mark("props", V"KeyValueList") +
mark("stm", V"Statement") +
mark("stm", V"Exp")) * sym","^-1),
Export = mark("export", key"export" * (
Cc"class" * V"ClassDecl" +
op"*" + op"^" +
Ct(V"NameList") * (sym"=" * Ct(V"ExpListLow"))^-1)),
KeyValue =
-- {:name} shorthand expands to the key/value pair
Cfn(sym":" * -V"SomeSpace" * V"Name" * Cp(), [[return function(name, p)
return {
{"key_literal", name},
{"ref", name, [-1] = p},
}
end]]) +
Ct((V"KeyName" + sym"[" * V"Exp" * sym"]" +
V"Space" * V"DoubleString" + V"Space" * V"SingleString") *
P":" *
(V"Exp" + V"TableBlock" + V"SpaceBreak"^1 * V"Exp")),
KeyValueList = V"KeyValue" * (sym"," * V"KeyValue")^0,
KeyValueLine = V"CheckIndent" * V"KeyValueList" * sym","^-1,
FnArgsDef = sym"(" * V"White" * Ct(V"FnArgDefList"^-1) *
(key"using" * Ct(V"NameList" + V"Space" * P"nil") + Ct(P"")) *
V"White" * sym")" +
Ct(P"") * Ct(P""),
FnArgDefList = V"FnArgDef" *
((sym"," + V"Break") * V"White" * V"FnArgDef")^0 *
((sym"," + V"Break") * V"White" * Ct(V"VarArg"))^0 +
Ct(V"VarArg"),
FnArgDef = Ct((V"Name" + V"SelfName" + V"TableLit") * (sym"=" * V"Exp")^-1),
FunLit = mark("fndef",
V"FnArgsDef" *
(sym"->" * Cc"slim" + sym"=>" * Cc"fat") *
(V"Body" + Ct(P""))),
NameList = V"Name" * (sym"," * V"Name")^0,
NameOrDestructure = V"Name" + V"TableLit",
AssignableNameList = V"NameOrDestructure" * (sym"," * V"NameOrDestructure")^0,
ExpList = V"Exp" * (sym"," * V"Exp")^0,
ExpListLow = V"Exp" * ((sym"," + sym";") * V"Exp")^0,
InvokeArgs = -P"-" * (
V"ExpList" *
(sym"," * (V"TableBlock" + V"SpaceBreak" * V"Advance" * V"ArgBlock" * V"TableBlock"^-1) +
V"TableBlock")^-1 +
V"TableBlock"),
ArgBlock = V"ArgLine" * (sym"," * V"SpaceBreak" * V"ArgLine")^0 * V"PopIndent",
ArgLine = V"CheckIndent" * V"ExpList",
}