1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-14 11:23:02 +02:00
ferret/pkg/parser/antlr/FqlParser.g4

305 lines
5.4 KiB
ANTLR

parser grammar FqlParser;
options { tokenVocab=FqlLexer; }
program
: body
;
body
: (bodyStatement)* bodyExpression
;
bodyStatement
: functionCallExpression
| variableDeclaration
;
bodyExpression
: returnExpression
| forExpression
;
returnExpression
: Return (Distinct)? expression
| Return (Distinct)? OpenParen forExpression CloseParen
| Return forTernaryExpression
;
forExpression
: For forExpressionValueVariable (Comma forExpressionKeyVariable)? In forExpressionSource
(forExpressionBody)*
forExpressionReturn
;
forExpressionValueVariable
: Identifier
;
forExpressionKeyVariable
: Identifier
;
forExpressionSource
: functionCallExpression
| arrayLiteral
| objectLiteral
| variable
| memberExpression
| rangeOperator
| param
;
forExpressionClause
: limitClause
| sortClause
| filterClause
| collectClause
;
forExpressionStatement
: variableDeclaration
| functionCallExpression
;
forExpressionBody
: forExpressionStatement
| forExpressionClause
;
forExpressionReturn
: returnExpression
| forExpression
;
filterClause
: Filter expression
;
limitClause
: Limit limitClauseValue (Comma limitClauseValue)?
;
limitClauseValue
: IntegerLiteral
| param
;
sortClause
: Sort sortClauseExpression (Comma sortClauseExpression)*
;
sortClauseExpression
: expression SortDirection?
;
collectClause
: Collect collectCounter
| Collect collectAggregator
| Collect collectGrouping collectAggregator
| Collect collectGrouping collectGroupVariable
| Collect collectGrouping collectCounter
| Collect collectGrouping
;
collectSelector
: Identifier Assign expression
;
collectGrouping
: collectSelector (Comma collectSelector)*
;
collectAggregator
: Aggregate collectAggregateSelector (Comma collectAggregateSelector)*
;
collectAggregateSelector
: Identifier Assign functionCallExpression
;
collectGroupVariable
: Into collectSelector
| Into Identifier (Keep Identifier)?
;
collectCounter
: With Count Into Identifier
;
variableDeclaration
: Let Identifier Assign expression
| Let Identifier Assign OpenParen forExpression CloseParen
| Let Identifier Assign forTernaryExpression
;
param
: Param Identifier
;
variable
: Identifier
;
rangeOperator
: (integerLiteral | variable | param) Range (integerLiteral | variable | param)
;
arrayLiteral
: OpenBracket arrayElementList? CloseBracket
;
objectLiteral
: OpenBrace (propertyAssignment (Comma propertyAssignment)*)? Comma? CloseBrace
;
booleanLiteral
: BooleanLiteral
;
stringLiteral
: StringLiteral
| TemplateStringLiteral
;
integerLiteral
: IntegerLiteral
;
floatLiteral
: FloatLiteral
;
noneLiteral
: Null
| None
;
arrayElementList
: expression (Comma + expression)*
;
propertyAssignment
: propertyName Colon expression
| computedPropertyName Colon expression
| shorthandPropertyName
;
shorthandPropertyName
: variable
;
computedPropertyName
: OpenBracket expression CloseBracket
;
propertyName
: Identifier
| stringLiteral
;
expressionGroup
: OpenParen expression CloseParen
;
namespace
: (NamespaceSegment)*
;
functionCallExpression
: namespace Identifier arguments
;
memberExpression
: Identifier (Dot propertyName (computedPropertyName)*)+
| Identifier computedPropertyName (Dot propertyName (computedPropertyName)*)* (computedPropertyName (Dot propertyName)*)*
| functionCallExpression (Dot propertyName (computedPropertyName)*)+
| functionCallExpression computedPropertyName (Dot propertyName (computedPropertyName)*)* (computedPropertyName (Dot propertyName)*)*
;
arguments
: OpenParen(expression (Comma expression)*)?CloseParen
;
expression
: unaryOperator expression
| expression multiplicativeOperator expression
| expression additiveOperator expression
| functionCallExpression
| expressionGroup
| expression arrayOperator (inOperator | equalityOperator) expression
| expression inOperator expression
| expression equalityOperator expression
| expression regexpOperator expression
| expression logicalAndOperator expression
| expression logicalOrOperator expression
| expression QuestionMark expression? Colon expression
| rangeOperator
| stringLiteral
| integerLiteral
| floatLiteral
| booleanLiteral
| arrayLiteral
| objectLiteral
| variable
| memberExpression
| noneLiteral
| param
;
forTernaryExpression
: expression QuestionMark expression? Colon OpenParen forExpression CloseParen
| expression QuestionMark OpenParen forExpression CloseParen Colon expression
| expression QuestionMark OpenParen forExpression CloseParen Colon OpenParen forExpression CloseParen
;
arrayOperator
: All
| Any
| None
;
inOperator
: In
| Not In
;
equalityOperator
: Gt
| Lt
| Eq
| Gte
| Lte
| Neq
;
regexpOperator
: RegexMatch
| RegexNotMatch
;
logicalAndOperator
: And
;
logicalOrOperator
: Or
;
multiplicativeOperator
: Multi
| Div
| Mod
;
additiveOperator
: Plus
| Minus
;
unaryOperator
: Not
| Plus
| Minus
| Like
;