-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathluars.pest
117 lines (105 loc) · 3.15 KB
/
luars.pest
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
/*
Maybe do:
* Handle tabs and " " (non-breaking space)
* Handle comments
* Re-evaluate Pest Implicit whitepsace: https://pest.rs/book/grammars/syntax.html#implicit-whitespace
* Switch from "\n" to NEWLINE ("\n" | "\r\n" | "\r")
* Unicode support (not today satan)
* Better handling of accidential `))` end of function
*/
Document = { Statement* }
Statement = _{
(
WHITE_LINE
| ((Function | Global | Local) ~ ";" ~ W ~ "\n"?)
)
}
Fun_ = _{ "fun " }
Local_ = _{ "local " }
Global_ = _{ "global " }
Local = {
Local_ ~ Table
}
Global = {
Global_ ~ Table
}
Function = {
Fun_ ~ W
~ FunctionName ~ W
~ oParen ~ W
~ FunctionalParameters? ~ W
~ cParen
~ Return?
}
TableConstants = {
TableKey ~ W ~ ":" ~ W ~ CaptureType ~ W ~ ("=" ~ W ~ IntegerValue ~ W)?
~ ("," ~ W ~ TableKey ~ W ~ ":" ~ W ~ CaptureType ~ W ~ ("=" ~ W ~ IntegerValue ~W)?)*
~ W ~ ","?
}
Return = {
(
(":" ~ W ~ OptionalType ~ W)
| (":" ~ W ~ oParen ~ W ~ FunctionalParameters? ~ W ~ cParen ~ W)
)
~ Optional?
}
OptionalType = { TypeLua ~ Optional? }
Table = _{
Identifier ~ W
~ (":" ~ W ~ CaptureType ~ W)?
~ ("=" ~ W ~ "{" ~ W ~ TableConstants? ~ W ~ "}" ~ W)?
}
FunctionName = {
(Identifier ~ ":" ~ Identifier)
| Identifier
}
FunctionalParameters = {
(VariableParameter ~ W ~ ":" ~ W ~ OptionalType)
| ((FunctionalParameter ~ W ~ ("," ~ W ~ FunctionalParameter)*) ~ ("," ~ W ~ "...")? ~ ","?)
}
TableKey = !{
( DotDotDot | QuotedString | LuaIdentifier) ~ Optional?
| (LuaIdentifier ~ Optional? ~ W ~ ":" ~ W ~ (TypeLua))
}
DotDotDot = _{ "..." }
// these optionals and ... are overly friendly
// foo(bar?: integer): (baz:integer?, boop:string?)
FunctionalParameter = _{ ParameterIdentifier ~ W ~ ":" ~ W ~ OptionalType}
ParameterIdentifier = { ("..." | LuaIdentifier) ~ Optional? }
VariableParameter = { DotDotDot ~ Optional? }
oParen = _{ "(" }
cParen = _{ ")" }
Optional = _{ "?" }
Identifier = @{
LuaIdentifier ~ ("." ~ LuaIdentifier)*
}
LuaIdentifier = _{ ( "_" | ASCII_ALPHA ) ~ ("_" | ASCII_DIGIT | ASCII_ALPHA)* }
IntegerValue = { ("-"? ~ ASCII_DIGIT+) }
CaptureType = @{ TypeLua }
TypeLua = _{
LT
| "(" ~ W ~ LT ~ W ~ ("|" ~ W ~ LT ~ W)+ ~ ")"
}
LT = _{
| ("fun(" ~ W ~ FunctionalParameters? ~ W ~ ")" ~ W ~ (":" ~ W ~ UnparsedType ~ Optional? ~ W)?)
| ("table<" ~ W ~ Identifier ~ W ~ "," ~ W ~ TypeLua ~ W ~ ">" ~ W)
| UnparsedType
}
UnparsedType = _{
Identifier
~ ("[][][]" | "[][]" | "[]")?
}
QuotedString = _{ Quote ~ StringLiteral ~ Quote }
Quote = _{ "\"" }
StringLiteral = { QuotedChar* }
QuotedChar = _{ !"\"" ~ ANY }
// TypeLua = @{
// (LuaCatsBuiltIn ~ "?")
// | (LuaCatsBuiltIn ~ "[]")
// | (LuaCatsBuiltIn ~ ("|" ~ LuaCatsBuiltIn)*)
// }
// LuaCatsBuiltIn = { "any" | "nil" | "boolean" | "string" | "number" | "function" | "table" | "userdata" | "thread" | "integer" | "float" }
// LuaTypes_non_nil = @{ "boolean" | "string" | "number" | "function" | "table" | "userdata" | "thread" | "integer" | "float" }
// ConsType = { "boolean" | "string" | "number" | "integer" | "float" }
W = _{ (" " | "\n" )* }
WHITE_LINE = _{ " "* ~ "\n" }