grist/dsl/ReportParser.g4
2025-07-16 12:13:39 +08:00

131 lines
3.2 KiB
ANTLR

grammar ReportParser;
import ReportLexer;
entry : expression+ EOF;
expression : exprComposite
| ifExpr
| caseExpr
| returnExpr
| variableAssign
;
exprComposite : expr #singleExprComposite
| ternaryExpr #ternaryExprComposite
| LeftParen exprComposite RightParen #parenExprComposite
| exprComposite Operator exprComposite #complexExprComposite
;
ternaryExpr : ifCondition (join ifCondition)* '?' block ':' block ;
caseExpr : 'case' '{' casePart (',' casePart)* '}' ;
casePart : ifCondition (join ifCondition)* ':'? block ;
ifExpr: ifPart elseIfPart* elsePart? ;
ifPart : 'if' '(' ifCondition (join ifCondition)* ')' '{' block '}';
elseIfPart : 'else if' '(' ifCondition (join ifCondition)* ')' '{' block '}' ;
elsePart : 'else' '{' block '}' ;
block : exprBlock* returnExpr? ;
exprBlock : variableAssign
| ifExpr
| caseExpr
;
returnExpr : 'return'? expr ';'?;
expr : item ;
ifCondition : expr OP expr ;
variableAssign : 'var'? variable '=' item ';'?;
// 算术表达式、逻辑表达式、连接表达式
item : operatorUnary* unit (Operator* OP* join* operatorUnary* unit)* #simpleJoin
| item (Operator* OP* join* item)+ #simpleItem
//| LeftParen item RightParen #singleParenJoin
| operatorUnary* LeftParen item RightParen #parenJoin
;
unit : dataset
| function
| set
| itemOfCollection
| cellPosition
| relativeCell
| currentCellValue
| currentCellData
| cell
| variable
| INTEGER
| BOOLEAN
| STRING
| NUMBER
| NULL
;
operatorUnary: 'not' | '-';
variable : Identifier ;
itemOfCollection: '@'; // 取集合中某项数据,多用于遍历过滤
cellPosition : '&'Cell ;//表示单元格位置
relativeCell : '$'Cell ; //表示当前引用对应的单元格的值
currentCellValue : '#' ;//表示当前单元格值
currentCellData : '#''.'property ;//表示取当前单元绑定对象的某个属性值
cell : 'cell' ('.'property)? ;
dataset : Identifier '.' aggregate '(' property? (',' conditions )? (',' ORDER)? ')';
function : Identifier '(' functionParameter? ')' ;
functionParameter : item (','? item)* ;
set : simpleValue #simpleData
| Cell #singleCell
| Cell '['']'('{' item '}')? #wholeCell
| Cell ':' Cell ('{' item '}')? #cellPair
| Cell '{' item '}' #singleCellCondition
| Cell '[' cellCoordinate ']' #singleCellCoordinate
| Cell '[' cellCoordinate ']' '{' item '}' #cellCoordinateCondition
| set 'to' set #range
;
cellCoordinate : coordinate (';' coordinate)? ;
coordinate : cellIndicator (',' cellIndicator)* ;
cellIndicator : Cell #relative
| Cell ':' EXCLAMATION? OFFSET? INTEGER #absolute
;
conditions : condition (join condition)* ;
condition : Cell OP expr #cellNameExprCondition
| property OP expr #propertyCondition
| currentValue OP expr #currentValueCondition
| expr OP expr #exprCondition
;
property : Identifier
| property '.' property
;
currentValue : '@@' ;
simpleValue : INTEGER|NUMBER|STRING|BOOLEAN|NULL;
join : AND | OR ;
aggregate : Identifier;