PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/metastore/src/java/org/apache/hadoop/hive/metastore/parser/Filter.g

#
text | 130 lines | 108 code | 22 blank | 0 comment | 0 complexity | 414e0530986153452125525b78ad88d1 MD5 | raw file
  1grammar Filter;
  2
  3options
  4{
  5  k=3;
  6}
  7
  8
  9// Package headers
 10@header {
 11package org.apache.hadoop.hive.metastore.parser;
 12
 13import org.apache.hadoop.hive.metastore.parser.ExpressionTree;
 14import org.apache.hadoop.hive.metastore.parser.ExpressionTree.LeafNode;
 15import org.apache.hadoop.hive.metastore.parser.ExpressionTree.Operator;
 16import org.apache.hadoop.hive.metastore.parser.ExpressionTree.LogicalOperator;
 17}
 18@lexer::header {package org.apache.hadoop.hive.metastore.parser;}
 19
 20@members {
 21  public ExpressionTree tree = new ExpressionTree();
 22
 23  public static String TrimQuotes (String input) {
 24    if (input.length () > 1) {
 25      if ((input.charAt (0) == '"' && input.charAt (input.length () - 1) == '"')
 26        || (input.charAt (0) == '\'' && input.charAt (input.length () - 1) == '\'')) {
 27        return input.substring (1, input.length () - 1);
 28      }
 29    }
 30    return input;
 31  }
 32}
 33
 34@rulecatch{
 35  catch (RecognitionException e){
 36    throw e;
 37  }
 38}
 39
 40//main rule
 41filter
 42    :
 43    orExpression 
 44    ;
 45
 46orExpression
 47    :
 48    andExpression (KW_OR andExpression { tree.addIntermediateNode(LogicalOperator.OR); } )*
 49    ;
 50
 51andExpression
 52    :
 53    expression (KW_AND expression  { tree.addIntermediateNode(LogicalOperator.AND); } )*
 54    ;
 55
 56expression
 57    :
 58    LPAREN orExpression RPAREN
 59    |
 60    operatorExpression
 61    ;
 62
 63operatorExpression 
 64@init { 
 65    boolean isReverseOrder = false;
 66}
 67    :
 68    (
 69       (key = Identifier op = operator  value = StringLiteral)
 70       | 
 71       (value = StringLiteral  op = operator key = Identifier) { isReverseOrder = true; }
 72    )
 73    {
 74        LeafNode node = new LeafNode();
 75        node.keyName = key.getText();
 76        node.value = TrimQuotes(value.getText());
 77        node.operator = op;
 78        node.isReverseOrder = isReverseOrder;
 79
 80        tree.addLeafNode(node);
 81    };
 82
 83operator returns [Operator op]
 84   :
 85   t = (LESSTHAN | LESSTHANOREQUALTO | GREATERTHAN | GREATERTHANOREQUALTO | KW_LIKE | EQUAL | NOTEQUAL)
 86   {
 87      $op = Operator.fromString(t.getText().toUpperCase());
 88   };
 89
 90// Keywords
 91KW_AND : 'AND';
 92KW_OR : 'OR';
 93KW_LIKE : 'LIKE';
 94
 95// Operators
 96LPAREN : '(' ;
 97RPAREN : ')' ;
 98EQUAL : '=';
 99NOTEQUAL : '<>';
100LESSTHANOREQUALTO : '<=';
101LESSTHAN : '<';
102GREATERTHANOREQUALTO : '>=';
103GREATERTHAN : '>';
104
105// LITERALS
106fragment
107Letter
108    : 'a'..'z' | 'A'..'Z'
109    ;
110
111fragment
112Digit
113    :
114    '0'..'9'
115    ;
116
117StringLiteral
118    :
119    ( '\'' ( ~('\''|'\\') | ('\\' .) )* '\''
120    | '\"' ( ~('\"'|'\\') | ('\\' .) )* '\"'
121    )
122    ;
123
124Identifier
125    :
126    (Letter | Digit) (Letter | Digit | '_')*
127    ;
128
129WS  :   (' '|'\r'|'\t'|'\n')+ { skip(); } ;
130