Skip to content

Instantly share code, notes, and snippets.

@danbulant
Created February 13, 2024 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danbulant/df97700e41a1ef7d605a07a987c5e5a9 to your computer and use it in GitHub Desktop.
Save danbulant/df97700e41a1ef7d605a07a987c5e5a9 to your computer and use it in GitHub Desktop.
SurrealQL lezer grammar WIP
@top Script {
(statement SEMI)*
statement
}
@top SingleStatement {
statement
}
@precedence {
int,
array @left,
dot @left,
exp @left,
times @left,
plus @left,
comparison @left,
and @left,
or @left
}
@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 {
"$" identifier
}
StringPrefix {
ckw<"s"> | ckw<"r"> | ckw<"d"> | ckw<"u">
}
@skip {} {
String[isolate] {
StringPrefix?
('"' (stringContentDouble | Escape)* ('"') |
"'" (stringContentSingle | Escape)* ("'"))
}
}
Integer { int }
Decimal { int !int ("." int)? "dec" }
Float { int !int (("." int) "f"? | "f" ) }
Duration { int ("d" | "h" | "m" | "s" | "ms" | "us" | "ns") }
number { Decimal | Float | 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
}
Field {
expression ("as" identifier)? |
"*"
}
BinaryExpression {
expression !and ("&&" | "AND") expression |
expression !or ("||" | "OR") expression |
expression !comparison (
"??" | "?:" |
"=" | "IS" |
"!=" | "IS NOT"|
"==" |
"?=" | "*=" |
"~" | "!~" | "?~" | "*~" |
"IN" | "NOT IN" |
"CONTAINS" | "∋" |
"CONTAINSNOT" | "∌" |
"CONTAINSALL" | "⊇" |
"CONTAINSANY" | "⊃" |
"CONTAINSNONE" | "⊅" |
"INSIDE" | "∈" |
"NOTINSIDE" | "NOT IN" | "∉" |
"ALLINSIDE" | "⊆" |
"ANYINSIDE" | "⊂" |
"NONEINSIDE" | "⊄" |
"OUTSIDE" |
"INTERSECTS" |
"@@" | "@" identifier "@" |
"<" | ">" | "<=" | ">="
) expression |
expression !exp Raise expression |
expression !times (Divide | Multiply) expression |
expression !plus (Add | Subtract) expression |
expression !dot (".*" | ("." "*."*) expression) |
expression? !dot "->" expression |
expression !array ("[" expression "]")
}
ReturnStatement {
kw<"return"> expression
}
BeginStatement {
kw<"begin"> kw<"transaction">?
}
BreakStatement {
kw<"break">
}
CancelStatement {
kw<"cancel"> kw<"transaction">?
}
CommitStatement {
kw<"commit"> kw<"transaction">?
}
ContinueStatement {
kw<"continue">
}
UseStatement {
kw<"use">
(kw<"ns"> identifier)?
(kw<"db"> identifier)?
}
ThrowStatement {
kw<"throw"> expression
}
SleepStatement {
kw<"sleep"> expression
}
ShowStatement {
kw<"show"> ckw<"changes"> kw<"for"> kw<"table">
expression
("since" expression)?
("limit" expression)?
}
LetStatement {
kw<"let"> Variable "=" (expression | statement)
}
InfoStatement {
kw<"info"> kw<"for">
(
kw<"root"> |
kw<"ns"> | kw<"namespace"> |
kw<"db"> | kw<"database"> |
kw<"scope"> expression |
kw<"table"> expression
)
}
orderBy {
expression (kw<"collate"> | kw<"numeric">)? (kw<"asc"> | kw<"desc">)?
}
SelectStatement {
kw<"select">
kw<"value">?
(Field (COMMA Field)*)
("omit" Field (COMMA Field)*)?
"from" kw<"only">? expression (COMMA expression)*
("with" (kw<"noindex"> | kw<"index"> identifier (COMMA identifier)))?
("where" expression)?
("split" kw<"at">? expression)?
// ("group" kw<"by">? expression (COMMA expression)*)?
// ("order" kw<"by">? orderBy (COMMA orderBy)*)?
// ("limit" kw<"by">? expression)?
// ("start" kw<"at">? expression)?
("timeout" expression)?
"parallel"?
("explain" kw<"full">?)?
}
expression {
String |
number |
Duration |
Array |
Object |
FunctionCall |
Future |
Variable |
Cast |
RecordID |
Column |
ParenthesizedExpression |
BinaryExpression
}
ParenthesizedExpression {
"(" (expression | statement) ")"
}
statement[@isGroup=Statement] {
ReturnStatement |
BeginStatement |
BreakStatement |
CancelStatement |
CommitStatement |
ContinueStatement |
UseStatement |
ThrowStatement |
SleepStatement |
ShowStatement |
LetStatement |
InfoStatement |
SelectStatement
}
@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_]* }
columnIdentifier { identifier | "*" }
RIDDelim { "`" }
RIDStart { "⟨" }
RIDEnd { "⟩" }
RIDDelimContent { $[^`]+ }
RIDContent { $[^⟨⟩]+ }
Divide { "/" | "÷" }
Multiply { "*" | "×" }
Add { "+" }
Subtract { "-" }
Raise { "**" }
Escape {
"\\" ("x" hex hex | "u" ("{" hex+ "}" | hex hex hex hex) | ![xu])
}
hex { @digit | $[a-fA-F] }
stringContentSingle { ![\\']+ }
stringContentDouble { ![\\"]+ }
COMMA { "," }
SEMI { ";" }
"/*"
"(" ")" "[" "]" "{" "}"
"."
"->"
@precedence { "/*", LineComment, Divide }
@precedence { LineComment, Subtract }
}
@detectDelim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment