silverbullet/common/markdown_parser/query.grammar

150 lines
2.8 KiB
Plaintext
Raw Normal View History

2022-04-12 02:34:09 +08:00
@top Program { Query }
@precedence {
attr @left
unaryop
mulop @left
addop @left
binop @left
terop @left
and @left
or @left
}
@skip {
space | LineComment
2022-04-12 02:34:09 +08:00
}
commaSep<content> { content ("," content)* }
kw<term> { @specialize[@name={term}]<Identifier, term> }
Query {
TagIdentifier ( WhereClause | LimitClause | OrderClause | SelectClause | RenderClause )*
}
WhereClause { kw<"where"> Expression }
LimitClause { kw<"limit"> Expression }
OrderClause { Order commaSep<OrderBy> }
OrderBy { Expression OrderDirection? }
SelectClause { kw<"select"> commaSep<Select> }
2023-10-30 21:15:12 +08:00
RenderClause { kw<"render"> ( kw<"each"> | kw<"all"> )? PageRef }
Select { Identifier | Expression kw<"as"> Identifier }
2022-04-12 02:34:09 +08:00
Order {
kw<"order"> kw<"by">
}
OrderDirection {
kw<"asc"> | kw<"desc">
2022-04-12 02:34:09 +08:00
}
Value { Number | String | Bool | Regex | kw<"null"> | List }
Attribute {
Expression !attr "." Identifier
}
2022-04-12 02:34:09 +08:00
Call {
Identifier "(" commaSep<Expression> ")"
| Identifier "(" ")"
}
2022-04-12 02:34:09 +08:00
TopLevelVal { "." }
2022-04-12 02:34:09 +08:00
ParenthesizedExpression { "(" Expression ")" }
LogicalExpression {
Expression !and kw<"and"> Expression
| Expression !or kw<"or"> Expression
}
Expression {
Value
| Identifier
| GlobalIdentifier
| Attribute
| TopLevelVal
| ParenthesizedExpression
| LogicalExpression
| QueryExpression
| UnaryExpression
| BinExpression
| TernaryExpression
| Call
| PageRef
| Object
}
QueryExpression {
"{" Query "}"
}
UnaryExpression {
!unaryop kw<"not"> Expression
| !unaryop "!" Expression
| !unaryop "-" Expression
}
BinExpression {
Expression !binop "<" Expression
| Expression !binop "<=" Expression
| Expression !binop "=" Expression
| Expression !binop "!=" Expression
| Expression !binop ">=" Expression
| Expression !binop ">" Expression
| Expression !binop "=~" Expression
| Expression !binop "!=~" Expression
| Expression !binop kw<"in"> Expression
| Expression !mulop "*" Expression
| Expression !mulop "/" Expression
| Expression !mulop "%" Expression
| Expression !addop "+" Expression
| Expression !addop "-" Expression
| Expression !or "??" Expression
}
TernaryExpression {
Expression !terop "?" Expression !terop ":" Expression
}
List { "[" commaSep<Expression> "]" | "[" "]" }
KeyVal {
String ":" Expression
}
Object { "{" commaSep<KeyVal> "}" | "{" "}" }
2022-04-12 02:34:09 +08:00
Bool {
kw<"true"> | kw<"false">
2022-04-12 02:34:09 +08:00
}
@tokens {
space { std.whitespace+ }
LineComment { "#" ![\n]* }
TagIdentifier { @asciiLetter (@asciiLetter | @digit | "-" | "_" | "/" )* }
Identifier { @asciiLetter (@asciiLetter | @digit | "-" | "_")* | "`" ![`]* "`" }
GlobalIdentifier { "@" @asciiLetter (@asciiLetter | @digit | "-" | "_")* }
String {
"\"" ![\"]* "\""
| "'" ![']* "'"
}
2022-08-09 21:37:47 +08:00
PageRef {
"[[" ![\]]* "]]"
2022-08-09 21:37:47 +08:00
}
Regex { "/" ( ![/\\\n\r] | "\\" _ )+ "/" }
Number { std.digit+ | std.digit+ "." std.digit+ }
@precedence { Identifier, Number }
2022-04-12 02:34:09 +08:00
}