-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: Add ANTLR grammar for BIDS Schema expressions
- Loading branch information
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
grammar BSExpressions; | ||
|
||
// Define tokens | ||
OR_OP : '||'; | ||
AND_OP : '&&'; | ||
NOT_OP : '!'; | ||
COMP_OP : '==' | '!=' | '<' | '<=' | '>' | '>='; | ||
ADD_OP : '+' | '-'; | ||
MUL_OP : '*' | '/'; | ||
EXP_OP : '**'; | ||
LPAR : '('; | ||
RPAR : ')'; | ||
LSQR : '['; | ||
RSQR : ']'; | ||
DOT : '.'; | ||
|
||
// Define literals | ||
LITERAL : (NUMBER | QUOTED_STRING); | ||
NUMBER : INT | FLOAT; | ||
INT : ('+' | '-')? DIGIT+; | ||
FLOAT : ('+' | '-')? DIGIT+ '.' DIGIT+; | ||
DIGIT : [0-9]; | ||
QUOTED_STRING : '"' (ESC | ~["\\])* '"'; | ||
ESC : '\\' ["\\/bfnrt]; | ||
BOOL : 'True' | 'False'; | ||
NULL : 'None'; | ||
// Define rules | ||
start : expression EOF; | ||
expression : test; | ||
test : andTest (OR_OP andTest)*; | ||
andTest : notTest (AND_OP notTest)*; | ||
notTest : NOT_OP notTest | comparison; | ||
comparison : expr (COMP_OP expr)*; | ||
expr : term (ADD_OP term)*; | ||
term : factor (MUL_OP factor)*; | ||
factor : atom (EXP_OP factor)?; | ||
atom : item (trailer)*; | ||
item : parenthetical | array | obj_literal | identifier | LITERAL; | ||
parenthetical : LPAR test RPAR; | ||
array : LSQR (test (',' test)*)? RSQR; | ||
obj_literal : '{}'; | ||
trailer : function_call | array_lookup | object_lookup; | ||
function_call : LPAR (test (',' test)*)? RPAR; | ||
array_lookup : LSQR test RSQR; | ||
object_lookup : DOT identifier; | ||
identifier : IDENT_START IDENT_PART*; | ||
fragment IDENT_START : [a-zA-Z_]; | ||
fragment IDENT_PART : [a-zA-Z_0-9]; | ||
WS : [ \t\r\n]+ -> skip; |