Skip to content

Instantly share code, notes, and snippets.

@tnine
Created March 16, 2012 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tnine/ee5ef357b9261ff1bfa9 to your computer and use it in GitHub Desktop.
Save tnine/ee5ef357b9261ff1bfa9 to your computer and use it in GitHub Desktop.
/*******************************************************************************
* Copyright 2012 Apigee Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.usergrid.persistence.query.tree;
/**
* A base class for any equality expression. Expressions must have a property and a value.
* Examples are >=, >, =, <, <=,
*
* @author tnine
*
*/
public abstract class EqualityOperand extends Operand {
protected Property property;
protected Literal literal;
/**
* @param property
* @param literal
*/
public EqualityOperand(Property property, Literal literal) {
super();
this.property = property;
this.literal = literal;
}
}
/*******************************************************************************
* Copyright 2012 Apigee Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.usergrid.persistence.query.tree;
import org.antlr.runtime.Token;
/**
* @author tnine
*
*/
public class Float extends Literal {
/**
* @param t
*/
protected Float(Token t) {
super(t);
}
}
/*******************************************************************************
* Copyright 2012 Apigee Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.usergrid.persistence.query.tree;
/**
* @author tnine
*
*/
public class LessThan extends EqualityOperand{
/**
* @param property
* @param literal
*/
public LessThan(Property property, Literal literal) {
super(property, literal);
}
}
/*******************************************************************************
* Copyright 2012 Apigee Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.usergrid.persistence.query.tree;
import org.antlr.runtime.Token;
/**
* A property
* @author tnine
*
*/
public class Property extends Literal {
protected Property(Token t) {
super(t);
}
}
grammar QueryFilter;
//NOTES: '^' denotes operator, all others in the string become operands
options {
output=AST;
// ASTLabelType=CommonTree;
}
@header {
package org.usergrid.persistence.query.tree;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.usergrid.persistence.Query;
import org.usergrid.persistence.Query.FilterPredicate;
import org.usergrid.persistence.Query.SortPredicate;
}
@lexer::header {
package org.usergrid.persistence.query.tree;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
}
@members {
Query query = new Query();
private static final Logger logger = LoggerFactory
.getLogger(QueryFilterLexer.class);
@Override
public void emitErrorMessage(String msg) {
logger.info(msg);
}
}
@lexer::members {
private static final Logger logger = LoggerFactory
.getLogger(QueryFilterLexer.class);
@Override
public void emitErrorMessage(String msg) {
logger.info(msg);
}
}
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_'|'.')*
;
INT : ('-')? '0'..'9'+
;
FLOAT
: ('-')? ( ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
| '.' ('0'..'9')+ EXPONENT?
| ('0'..'9')+ EXPONENT)
;
STRING
: '\'' ( ESC_SEQ | ~('\\'|'\'') )* '\''
;
fragment
BOOLEAN : ('true' |'false');
UUID : HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT '-'
HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT '-'
HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT '-'
HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT '-'
HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
;
fragment
EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
fragment
HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;
fragment
ESC_SEQ
: '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
| UNICODE_ESC
| OCTAL_ESC
;
fragment
OCTAL_ESC
: '\\' ('0'..'3') ('0'..'7') ('0'..'7')
| '\\' ('0'..'7') ('0'..'7')
| '\\' ('0'..'7')
;
fragment
UNICODE_ESC
: '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
;
WS : (' ' | '\t' | '\n' | '\r' | '\f')+ {$channel=HIDDEN;};
LT : '<' | 'lt';
LTE : '<=' | 'lte';
EQ : '=' | 'eq';
GT : '>' | 'gt';
GTE : '>=' | 'gte';
//NE : '!=';
property
: ID<Property>;
value : BOOLEAN<Boolean> | STRING<StringQuery> | INT<Integer> | FLOAT<Float> | UUID<UUID>;
//begin equality expressions
//mathmatical equality operations
equalityop :
property ( LT<LessThan>
| LTE <LessThanEqual>^
| EQ <Equal>^
| GT <GreaterThan>^
| GTE <GreaterThanEqual>^) value {
};
// | NE
//geo location search
locationop:
property 'within'<Within>^ FLOAT<Float> 'of' FLOAT<Float> ',' FLOAT<Float> ;
//string search
containsop :
property 'contains'<Contains>^ STRING<StringQuery> ;
//
operation :
'('! expression ')'! | equalityop | locationop | containsop;
//negations of expressions
notexp :
'not'^ operation|operation;
//and expressions contain operands. These should always be closer to the leaves of a tree, it allows
//for faster result intersection sooner in the query execution
andexp :
notexp ('and'^ notexp)*;
//or expression should always be after AND expressions. This will give us a smaller result set to union when evaluating trees
orexp :
andexp ('or'^ andexp)*;
//root level boolean expression
expression:
orexp;
//end expressions
//begin order clauses
//direction for ordering
direction : ('asc' | 'desc');
//order clause
order
: (property direction?);
//end order clauses
//Begin select clauses
select_subject
: ID {
query.addSelect($select_subject.text);
};
select_assign_target
: ID;
select_assign_source
: ID;
select_assign
: select_assign_target ':' select_assign_source {
query.addSelect($select_assign_source.text, $select_assign_target.text);
};
select_expr
: ('*' | select_subject (',' select_subject) * | '{' select_assign (',' select_assign) * '}');
//end select clauses
ql returns [Query q]
: 'select' select_expr ('where' expression )? ('order by' order (',' order)*)? {
//q = query;
//TODO other stuff
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment