Navigation Menu

Skip to content

Commit

Permalink
WIP: rewrite query parser/compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
aphyr committed Mar 5, 2015
1 parent ee2ba2d commit 6b066f1
Show file tree
Hide file tree
Showing 6 changed files with 502 additions and 159 deletions.
5 changes: 3 additions & 2 deletions project.clj
Expand Up @@ -21,7 +21,7 @@
[net.logstash.log4j/jsonevent-layout "1.7"]
[com.cemerick/pomegranate "0.3.0"]
[org.spootnik/http-kit "2.1.18.1"]
[clj-http "1.0.0" :exclusions [org.clojure/tools.reader]]
[clj-http "1.0.1" :exclusions [org.clojure/tools.reader]]
[cheshire "5.4.0"]
[clj-librato "0.0.5"]
[clj-time "0.9.0"]
Expand All @@ -32,7 +32,8 @@
[interval-metrics "1.0.0"]
[io.netty/netty-all "4.0.24.Final"]
[log4j/apache-log4j-extras "1.2.17"]
[org.antlr/antlr "3.2"]
; [org.antlr/antlr "3.2"]
[clj-antlr "0.2.2"]
[org.slf4j/slf4j-log4j12 "1.7.10"]
[riemann-clojure-client "0.3.2"]
[less-awful-ssl "1.0.0"]
Expand Down
144 changes: 144 additions & 0 deletions resources/query.g4
@@ -0,0 +1,144 @@
grammar Query;

// Kind of a simplified, restructured variant of the Clojure/Java lexers with a
// way different structure.
//
// https://github.com/antlr/grammars-v4/blob/master/clojure/Clojure.g4

predicate
: primary
| 'not' predicate
| predicate 'and' predicate
| predicate 'or' predicate
;

primary
: '(' predicate ')'
| simple
;

simple
: tagged
| equal
| not_equal
| lesser
| greater
| lesser_equal
| greater_equal
| like
| regex_match
| field
| value
;

tagged : 'tagged' string ;
equal : value '=' value ;
not_equal : value '!=' value ;
lesser : value '<' number ;
greater : value '>' number ;
lesser_equal : value '<=' number ;
greater_equal : value '>=' number ;
like : value '=~' string ;
regex_match : value '~=' string ;

// Values -----------------------------------------------------------

value
: true
| false
| nil
| number
| string
| field
;

field : NAME ;
string : STRING ;
true : TRUE ;
false : FALSE ;
nil : NIL ;

number
: float
| bign
| long
;

float : FLOAT;
bign : BIGN;
long : LONG;


// Lexers -------------------------------------------------------------

STRING : '"' ( ~'"' | '\\' '"' )* '"' ;

FLOAT
: '-'? [0-9]+ FLOAT_TAIL
| '-'? 'Infinity'
| '-'? 'NaN'
;

fragment
FLOAT_TAIL
: FLOAT_DECIMAL FLOAT_EXP
| FLOAT_DECIMAL
| FLOAT_EXP
;

fragment
FLOAT_DECIMAL
: '.' [0-9]+
;

fragment
FLOAT_EXP
: [eE] '-'? [0-9]+
;
fragment
HEXD: [0-9a-fA-F] ;
HEX: '0' [xX] HEXD+ ;
LONG: '-'? [0-9]+[lL]?;
BIGN: '-'? [0-9]+[nN];

CHAR_U
: '\\' 'u'[0-9D-Fd-f] HEXD HEXD HEXD ;
CHAR_NAMED
: '\\' ( 'newline'
| 'return'
| 'space'
| 'tab'
| 'formfeed'
| 'backspace' ) ;
CHAR_ANY
: '\\' . ;

NIL : 'nil'
| 'null' ;

TRUE : 'true' ;
FALSE : 'false' ;

// Names

NAME: SYMBOL_HEAD SYMBOL_REST* (':' SYMBOL_REST+)* ;

fragment
SYMBOL_HEAD
: ~('0' .. '9'
| '^' | '`' | '\'' | '"' | '#' | '~' | '@' | ':' | '/' | '%' | '(' | ')' | '[' | ']' | '{' | '}' // FIXME: could be one group
| [ \n\r\t\,] // FIXME: could be WS
)
;

fragment
SYMBOL_REST
: SYMBOL_HEAD
| '0'..'9'
| '.'
;


// Whitespace

WS : [ \n\r\t\,] -> channel(HIDDEN) ;
2 changes: 1 addition & 1 deletion src/riemann/Query.g
Expand Up @@ -118,4 +118,4 @@ fragment UnicodeEscape

fragment HexDigit
: '0'..'9' | 'A'..'F' | 'a'..'f'
;
;

0 comments on commit 6b066f1

Please sign in to comment.