lang-surrealql/src/surrealql.grammar
2024-02-13 22:20:51 +01:00

162 lines
No EOL
2.9 KiB
Text

@top Script {
(statement SEMI)*
statement
}
@top SingleStatement {
statement
}
@skip { whitespace | LineComment }
kw<term> { @specialize[@name={term}]<identifier, term> }
ckw<term> { @extend[@name={term}]<identifier, term> }
Table { identifier }
RecordID {
Table ":"
(identifier | RIDStart RIDContent RIDEnd | RIDDelim RIDDelimContent RIDDelim | Array | Object)
}
RecordRange {
Table ":"
(
number? (".." | "..=") number? |
Array? (".." | "..=") Array? |
Object? (".." | "..=") Object?
)
}
GeneratedRecordID {
Table ":" FunctionCall
}
Variable {
kw<"$"> identifier
}
StringPrefix {
ckw<"s"> | ckw<"r"> | ckw<"d"> | ckw<"u">
}
@skip {} {
String[isolate] {
StringPrefix?
('"' (stringContentDouble | Escape)* ('"' | "\n") |
"'" (stringContentSingle | Escape)* ("'" | "\n"))
}
}
@precedence { decimal @left, float @left, integer @left }
Integer { int }
Decimal { int ("." int)? "dec" }
Float { int (("." int) "f"? | "f" ) }
number { Decimal !decimal | Float !float | Integer !integer }
Array {
"[" (expression (COMMA expression)*)? "]"
}
Object {
"{" (identifier ":" expression (COMMA identifier ":" expression)*)? "}"
}
Namespace {
identifier
}
FunctionName {
identifier
}
FunctionCall {
(Namespace "::" )? FunctionName "(" (expression (COMMA expression)*)? ")"
}
Constant {
(Namespace "::")?
identifier
}
Cast {
"<" identifier ">" expression
}
Future {
"<future>" "{"
expression
"}"
}
Column {
identifier
}
ReturnStatement {
kw<"return"> expression
}
BeginStatement {
kw<"begin"> kw<"transaction">?
}
BreakStatement {
kw<"break">
}
CancelStatement {
kw<"cancel"> kw<"transaction">?
}
expression[@isGroup=Expression] {
String |
number |
Array |
Object |
FunctionCall |
Future |
Variable |
Cast |
RecordID |
Column
}
statement[@isGroup=Statement] {
ReturnStatement |
BeginStatement |
BreakStatement |
CancelStatement
}
@local tokens {
blockCommentEnd { "*/" }
blockCommentNewline { "\n" }
@else blockCommentContent
}
@skip {} { BlockComment { "/*" (blockCommentContent | blockCommentNewline)* blockCommentEnd } }
@tokens {
whitespace { @whitespace+ }
LineComment { ("//" | "-- ") ![\n]* }
int { @digit+ }
identifier { $[a-zA-Z] $[a-zA-Z0-9_]* }
RIDDelim { "`" }
RIDStart { "⟨" }
RIDEnd { "⟩" }
RIDDelimContent { $[^`]+ }
RIDContent { $[^⟨⟩]+ }
Divide { "/" }
Multiply { "*" }
Add { "+" }
Subtract { "-" }
LessThan { "<" }
GreaterThan { ">" }
Escape {
"\\" ("x" hex hex | "u" ("{" hex+ "}" | hex hex hex hex) | ![xu])
}
hex { @digit | $[a-fA-F] }
stringContentSingle { ![\\\n']+ }
stringContentDouble { ![\\\n"]+ }
EQ { "=" }
COMMA { "," }
SEMI { ";" }
"(" ")" "[" "]" "{" "}"
"."
}
@detectDelim