Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SCHEMA] ENH: Add ANTLR grammar for BIDS Schema expressions #1626

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading