silverbullet/common/space_lua/lua.grammar

207 lines
4.3 KiB
Plaintext
Raw Permalink Normal View History

2024-09-12 03:17:56 +08:00
/* Based on: https://github.com/R167/lezer-lua */
@precedence {
call,
power @right,
prefix,
times @left,
plus @left,
concat @right,
shift @left,
bitand @left,
xor @left,
bitor @left,
compare @left,
and @left,
or @left
}
@top Chunk { Block }
Block { statement* ReturnStatement? }
2024-09-27 23:09:25 +08:00
ReturnStatement { kw<"return"> ExpList? ";"?}
2024-09-12 03:17:56 +08:00
@skip { newline | space | Comment }
statement[@isGroup=Statement] {
";" |
Label |
kw<"break"> |
Goto{ kw<"goto"> Name } |
Scope { kw<"do"> Block kw<"end"> } |
WhileStatement { kw<"while"> exp kw<"do"> Block kw<"end"> } |
RepeatStatement { kw<"repeat"> Block kw<"until"> exp } |
IfStatement |
ForStatement |
Function { kw<"function"> FuncName FuncBody } |
LocalFunction { kw<"local"> kw<"function"> Name FuncBody } |
Assign { VarList "=" ExpList } |
Local { kw<"local"> AttNameList ("=" ExpList)? } |
FunctionCall ~fcall
}
IfStatement {
kw<"if"> exp kw<"then"> Block
(kw<"elseif"> exp kw<"then"> Block)*
2024-09-12 19:40:43 +08:00
(kw<"else"> Block)?
2024-09-12 03:17:56 +08:00
kw<"end">
}
ForNumeric { Name "=" exp "," exp ("," exp)? }
ForGeneric { NameList kw<"in"> ExpList }
ForStatement {
kw<"for"> (ForNumeric | ForGeneric) kw<"do"> Block kw<"end">
}
FuncName { Name ("." Name)* (":" Name)? }
FuncBody { "(" ArgList ")" Block kw<"end"> }
list<term> { term ("," term)* }
NameList { list<Name> }
ExpList { list<exp> }
VarList { list<var> }
2024-09-12 19:40:43 +08:00
ArgList { (list<var | "...">)? }
2024-09-12 03:17:56 +08:00
2024-09-12 19:40:43 +08:00
AttNameList { list<AttName> }
AttName { Name Attrib }
2024-09-12 03:17:56 +08:00
Attrib { ( "<" Name ">" )? }
exp {
kw<"nil"> | kw<"true"> | kw<"false"> | "..." |
Number |
LiteralString |
prefixexp |
BinaryExpression |
UnaryExpression |
TableConstructor |
FunctionDef { kw<"function"> FuncBody }
2024-10-07 15:08:36 +08:00
// | Query
2024-09-12 03:17:56 +08:00
}
Query {
"[" exp QueryClause* "]"
}
QueryClause {
WhereClause |
OrderByClause |
SelectClause |
RenderClause |
LimitClause
}
WhereClause { kw<"where"> exp }
LimitClause { kw<"limit"> exp }
OrderByClause { kw<"order"> kw<"by"> exp kw<"desc">? }
SelectClause { kw<"select"> list<Select> }
RenderClause { kw<"render"> ( kw<"each"> | kw<"all"> )? simpleString }
Select { Name | exp kw<"as"> Name }
2024-09-12 03:17:56 +08:00
field[@isGroup=Field] {
FieldDynamic { "[" exp "]" "=" exp } |
FieldProp { Name "=" exp } |
FieldExp { exp }
}
prefixexp {
var |
Parens { "(" exp ")" ~parens } |
FunctionCall ~fcall
}
FunctionCall { prefixexp (":" Name)? !call args }
args {
LiteralString |
TableConstructor |
funcParams[@dynamicPrecedence=1] { "(" list<exp>? ")" ~parens }
}
var {
Name | Property { (prefixexp "." Name) } | MemberExpression { (prefixexp "[" exp "]") }
}
kw<term> { @specialize[@name={term}]<identifier, term> }
Name { identifier }
Label { "::" Name "::" }
LiteralString { simpleString }
BinaryExpression {
exp !or kw<"or"> exp |
exp !and kw<"and"> exp |
exp !compare CompareOp exp |
exp !bitor BitOp{"|"} exp |
exp !bitand BitOp{"&"} exp |
exp !xor BitOp{"~"} exp |
exp !shift BitOp{"<<" | ">>"} exp |
exp !concat ".." exp |
exp !plus ArithOp{"+" | minus} exp |
exp !times ArithOp{"*" | "/" | "%" | "//"} exp |
exp !power ArithOp{"^"} exp
}
UnaryExpression {
!prefix kw<"not"> exp |
2024-09-24 16:15:22 +08:00
!prefix (ArithOp{"+" | minus} | BitOp{"~"} | LenOp{"#"}) exp
2024-09-12 03:17:56 +08:00
}
TableConstructor { "{" (field (fieldsep field)* fieldsep?)? "}" }
@tokens {
2024-09-24 16:15:22 +08:00
CompareOp { "<" | ">" | $[<>=~/!] "=" }
2024-09-12 03:17:56 +08:00
TagIdentifier { @asciiLetter (@asciiLetter | @digit | "-" | "_" | "/" )* }
word { (std.asciiLetter | "_") (std.digit | std.asciiLetter | "_")* }
2024-09-12 03:17:56 +08:00
identifier { word }
stringEscape {
"\\" ($[abfnz"'\\] | digit digit? digit?) |
"\\x" hex hex |
"\\u{" hex+ "}"
}
2024-10-07 15:08:36 +08:00
// Any sequence of characters except two consecutive ]]
longStringContent { (![\]] | $[\]] ![\]])* }
2024-10-10 02:35:07 +08:00
longDelimStringContent {
(![\]] | $[\]] ![=]+ ![\]])*
}
2024-10-07 15:08:36 +08:00
simpleString {
"'" (stringEscape | ![\r\n\\'])* "'" |
'"' (stringEscape | ![\r\n\\"])* '"' |
2024-10-10 02:35:07 +08:00
'[[' longStringContent ']]' |
'[' '='+ '[' longDelimStringContent ']' '='+ ']'
2024-10-07 15:08:36 +08:00
}
2024-09-12 03:17:56 +08:00
hex { $[0-9a-fA-F] }
digit { std.digit }
Number {
digit+ ("." digit+)? ($[eE] $[+\-] digit+)? |
"0" $[xX] hex+ ("." hex+)? ($[pP] $[+/-] digit+)?
}
Comment { "--" ![\n\r]* }
space { ($[ \t\f] | "\\" $[\n\r])+ }
newline { $[\n\r] | "\n\r" | "\r\n" }
"..."[@name=Ellipsis]
".."[@name=Concat]
@precedence { Comment, minus }
minus {"-"}
fieldsep { $[,;] }
"(" ")" "[" "]" "{" "}"
"." "," ";" ":" "::"
}