1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
//-----------------------------------------------------------------------------
//
// Filter parsing grammar
// Pablo Cingolani
//
//-----------------------------------------------------------------------------
grammar SnpSift;
//-----------------------------------------------------------------------------
// Lexer
//-----------------------------------------------------------------------------
//---
// Fragments
//---
// A digit
fragment DIGIT : '0'..'9' ;
// A number is a set of digits
fragment NUMBER : (DIGIT)+;
// A letter
fragment LETTER : LOWER | UPPER;
fragment LOWER : 'a'..'z';
fragment UPPER : 'A'..'Z';
fragment NEWLINE : ( '\n' | '\r' )+;
// Letter or digit
fragment ALPHANUM : LETTER | DIGIT;
//---
// Lexer entries
//---
// Discard spaces, tabs and newlines
WS : (' ' | '\t' | '\r' | '\n')+ { skip(); };
// 'C' style single line comments
COMMENT_SL : '//' ~('\r' | '\n')* NEWLINE { skip(); };
COMMENT_HASH : '#' ~('\r' | '\n')* NEWLINE { skip(); };
BOOL_LITERAL : 'true' | 'false' ;
// FLOAT number (float/double) without any signNUMBER
INT_LITERAL : NUMBER ;
FLOAT_LITERAL : NUMBER ( '.' NUMBER )? (('e'|'E') ('+'|'-')? NUMBER)?
| 'NaN' | 'NAN'
;
// A string literal
STRING_LITERAL : '\'' ~( '\n' | '\r' | '\'' )* '\'' { setText(getText().substring( 1, getText().length()-1 ) ); } ;
// An identifier.
ID : (ALPHANUM | '_' | '.' )+;
//-----------------------------------------------------------------------------
// Parser
//-----------------------------------------------------------------------------
// Compilation Unit
compilationUnit : expression EOF;
// In SNpSift, everything is an expression
expression : BOOL_LITERAL # literalBool
| INT_LITERAL # literalInt
| FLOAT_LITERAL # literalFloat
| STRING_LITERAL # literalString
| idx=('ANY' | '*' | 'ALL' | '?') # literalIndex
| ID '('(expression (',' expression )*)? ')' # functionCall
| ID # varReference
| expression '[' expression ']' # varReferenceList
| expression '[' expression '].' expression # varReferenceListSub
| expression '[' ('ANY' | '*' | 'ALL' | '?') '].' expression # varReferenceListSub
| expression op=('*' | '/' | '%') expression # expressionTimes
| expression op=('+' | '-') expression # expressionPlus
| expression op=('=' | '==' | '!=' | '<' | '<=' | '>' | '>=' | '=~' | '!~' | 'has' ) expression # expressionComp
| op=('!' | '-' | '+') expression # expressionUnary
| expression op=('&' | '&&' | '|' | '||' | '^') expression # expressionLogic
| op=('exists' | 'na') expression # expressionExists
| expression 'in' 'SET' '[' expression ']' # expressionSet
| '(' expression ')' # expressionParen
| expression '?' expression ':' expression # expressionCond
;
|