Skip to content

Commit

Permalink
ENH: Add ANTLR grammar for BIDS Schema expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Oct 5, 2023
1 parent 2d0c111 commit b0bd577
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions tools/BSExpressions/BSExpressions.g4
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;

0 comments on commit b0bd577

Please sign in to comment.