View Javadoc

1   /*
2    * Copyright (c) 2010.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.hbql.parser;
22  
23  import antlr.collections.impl.BitSet;
24  import org.antlr.runtime.IntStream;
25  import org.antlr.runtime.MismatchedTokenException;
26  import org.antlr.runtime.Parser;
27  import org.antlr.runtime.RecognitionException;
28  import org.antlr.runtime.RecognizerSharedState;
29  import org.antlr.runtime.TokenStream;
30  import org.apache.expreval.client.LexerRecognitionException;
31  import org.apache.expreval.expr.Operator;
32  import org.apache.expreval.expr.calculation.DelegateCalculation;
33  import org.apache.expreval.expr.compare.BooleanCompare;
34  import org.apache.expreval.expr.node.GenericValue;
35  import org.apache.hadoop.hbase.hbql.client.HBqlException;
36  import org.apache.hadoop.hbase.hbql.mapping.ColumnDefinition;
37  import org.apache.hadoop.hbase.hbql.mapping.FamilyMapping;
38  import org.apache.hadoop.hbase.hbql.mapping.TableMapping;
39  import org.apache.hadoop.hbase.hbql.util.Lists;
40  
41  import java.util.List;
42  
43  public class ParserSupport extends Parser {
44  
45      public ParserSupport(final TokenStream input) {
46          super(input);
47      }
48  
49      public ParserSupport(final TokenStream input, final RecognizerSharedState state) {
50          super(input, state);
51      }
52  
53      protected boolean isKeyword(final TokenStream input, final String str) {
54          final String s = input.LT(1).getText();
55          return s != null && s.equalsIgnoreCase(str);
56      }
57  
58      /*
59      public String getErrorMessage(RecognitionException e, String[] tokenNames) {
60          List stack = getRuleInvocationStack(e, this.getClass().getName());
61          String msg = null;
62          if (e instanceof NoViableAltException) {
63              NoViableAltException nvae = (NoViableAltException)e;
64              msg = " no viable alt; token=" + e.token +
65                    " (decision=" + nvae.decisionNumber +
66                    " state " + nvae.stateNumber + ")" +
67                    " decision=<<" + nvae.grammarDecisionDescription + ">>";
68          }
69          else {
70              msg = super.getErrorMessage(e, tokenNames);
71          }
72          return stack + " " + msg;
73      }
74  
75      public String getTokenErrorDisplay(Token t) {
76          return t.toString();
77      }
78      */
79  
80      public static final boolean attemptRecovery = false;
81  
82      protected void handleRecognitionException(final RecognitionException re) throws RecognitionException {
83  
84          if (attemptRecovery) {
85              reportError(re);
86              recover(input, re);
87          }
88          else {
89              throw re;
90          }
91      }
92  
93      protected void mismatch(IntStream input, int ttype, BitSet follow) throws RecognitionException {
94          throw new MismatchedTokenException(ttype, input);
95      }
96  
97      public Object recoverFromMismatchedSet(final IntStream input,
98                                             final RecognitionException e,
99                                             final BitSet follow) throws RecognitionException {
100         throw e;
101     }
102 
103     public GenericValue getLeftAssociativeCalculation(final List<GenericValue> exprList,
104                                                       final List<Operator> opList) {
105         if (exprList.size() == 1)
106             return exprList.get(0);
107 
108         GenericValue root = new DelegateCalculation(exprList.get(0), opList.get(0), exprList.get(1));
109 
110         for (int i = 1; i < opList.size(); i++)
111             root = new DelegateCalculation(root, opList.get(i), exprList.get(i + 1));
112 
113         return root;
114     }
115 
116     public GenericValue getLeftAssociativeBooleanCompare(final List<GenericValue> exprList,
117                                                          final List<Operator> opList) {
118         if (exprList.size() == 1)
119             return exprList.get(0);
120 
121         GenericValue root = new BooleanCompare(exprList.get(0), opList.get(0), exprList.get(1));
122 
123         for (int i = 1; i < opList.size(); i++)
124             root = new BooleanCompare(root, opList.get(i), exprList.get(i + 1));
125 
126         return root;
127     }
128 
129     public static final String EMBEDDED = "embedded";
130 
131     // This keeps antlr code out of TableMapping, which is accessed server-side in HBase
132     public static TableMapping newTableMapping(final TokenStream input,
133                                                final List<ColumnDefinition> columnList) throws RecognitionException {
134 
135         final FamilyMapping mapping = new FamilyMapping(EMBEDDED, false, columnList);
136         final List<FamilyMapping> mappingList = Lists.newArrayList(mapping);
137 
138         try {
139             return new TableMapping(null, true, false, EMBEDDED, null, null, mappingList);
140         }
141         catch (HBqlException e) {
142             e.printStackTrace();
143             throw new RecognitionException(input);
144         }
145     }
146 
147     public static String decodeEscapedChar(final String str) {
148 
149         if (!str.startsWith("\\"))
150             return str;
151 
152         if (str.equals("\\b"))
153             return "\b";
154         if (str.equals("\\t"))
155             return "\t";
156         if (str.equals("\\n"))
157             return "\n";
158         if (str.equals("\\f"))
159             return "\f";
160         if (str.equals("\\r"))
161             return "\r";
162         if (str.equals("\\"))
163             return "\"";
164         if (str.equals("\\'"))
165             return "\'";
166         if (str.equals("\\\\"))
167             return "\\";
168 
169         // Escaped Unicode
170         if (str.length() > 2 && str.startsWith("\\u")) {
171             final String nums = str.substring(2);
172             final int val = Integer.parseInt(nums, 16);
173             final char[] ub = Character.toChars(val);
174             return new String(ub);
175         }
176 
177         // Escaped Octal
178         if (str.length() > 1) {
179             final String nums = str.substring(1);
180             final Character val = (char)Integer.parseInt(nums, 8);
181             return val.toString();
182         }
183 
184         throw new LexerRecognitionException(null, "Unable to parse: " + str);
185     }
186 }