Skip to content

Instantly share code, notes, and snippets.

@virtix
Created January 19, 2011 21:23
Show Gist options
  • Save virtix/786889 to your computer and use it in GitHub Desktop.
Save virtix/786889 to your computer and use it in GitHub Desktop.
description here?
grammar E;
options {
backtrack = true;
memoize = true;
output = AST;
ASTLabelType = CommonTree;
}
tokens {
SELECT;
}
@header {
package org.foo.bar;
import java.util.HashMap;
}
@lexer::header {
package org.foo.bar;
import java.util.HashMap;
}
sql_stmt: expression* ';'! ;
expression:
conditional_expression
;
conditional_expression:
eq_expression
| neq_expression
| gt_expression
| lt_expression
| lte_expression
| gte_expression
| or_expression
| and_expression
;
or_expression:
atom OR atom -> ^(OR atom atom);
and_expression:
atom AND atom -> ^(AND atom atom);
gte_expression:
atom '>=' atom -> ^('>=' atom atom);
lte_expression:
atom '<=' atom -> ^('<=' atom atom);
lt_expression:
atom '<' atom -> ^('<' atom atom);
gt_expression:
atom '>' atom -> ^('>' atom atom);
eq_expression:
atom '=' atom -> ^('=' atom atom);
neq_expression:
atom '<>' atom -> ^('<>' atom atom);
atom : ID
| CHAR+
| INT+
| '(' expression ')' -> expression
;
OR : O R;
AND : A N D;
//To Do: Should enforce balanced brackets
CHAR : ('a'..'z'|'A'..'Z');
INT : '0'..'9'+ ;
ID : '['? CHAR+ INT* ']'? ('.' ID)* ;
NEWLINE:'\r'? '\n' ;
WS : (' '|'\t')+ {$channel=HIDDEN;} ;
COMMENT
: '/*' ( options {greedy=false;} : . )* '*/'
{
$channel = HIDDEN;
}
;
LINE_COMMENT
: '--' ~('\n'|'\r')* '\r'? '\n'
{
$channel = HIDDEN;
}
;
fragment A:('a'|'A');
fragment B:('b'|'B');
fragment C:('c'|'C');
fragment D:('d'|'D');
fragment E:('e'|'E');
fragment F:('f'|'F');
fragment G:('g'|'G');
fragment H:('h'|'H');
fragment I:('i'|'I');
fragment J:('j'|'J');
fragment K:('k'|'K');
fragment L:('l'|'L');
fragment M:('m'|'M');
fragment N:('n'|'N');
fragment O:('o'|'O');
fragment P:('p'|'P');
fragment Q:('q'|'Q');
fragment R:('r'|'R');
fragment S:('s'|'S');
fragment T:('t'|'T');
fragment U:('u'|'U');
fragment V:('v'|'V');
fragment W:('w'|'W');
fragment X:('x'|'X');
fragment Y:('y'|'Y');
fragment Z:('z'|'Z');
gunit E;
@header {
// package org.foo.bar;
}
sql_stmt:
<<
select * from foo where 1=1;
>> FAIL
expression:
"a=z" OK
"asd <> bar" OK
"asd = 1" OK
"asd <> 1" OK
"1 =1" OK
"asd > 123" OK
"asd < 123" OK
//To Do: Match strings in tics
// "aasd123.asd123.zxcsd = '123' " OK
"asd <= 123" OK
"asd >= 123" OK
"asd = (xyz<=123)" OK
"true OR true" OK
"true AND true" OK
"asd = 4 AND (xyz<=123)" OK
<<
asd = 123 oR (1=1 anD 2 < 3) AND ( foo=bar oR (bar=foo aNd asd.asd.asd <= -9999) )
>> OK
ID:
"asd" OK
"asd123" OK
"asd123.asd123" OK
"asd123.asd123.zxc" OK
"owner.schema.table.column" OK
"[owner].schema.table.column" OK
"[owner].[schema].[table].[column]" OK
//To Do: balanced brackets
//"[owner.[schema].[table.[column]" FAIL
@virtix
Copy link
Author

virtix commented Jan 19, 2011

To Do: booleans for expression statements ... OR/AND .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment