1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
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 if (str.equals("\\\\"))
169 return "\\";
170
171
172 if (str.length() > 2 && str.startsWith("\\u")) {
173 final String nums = str.substring(2);
174 final int val = Integer.parseInt(nums, 16);
175 final char[] ub = Character.toChars(val);
176 return new String(ub);
177 }
178
179
180 if (str.length() > 1) {
181 final String nums = str.substring(1);
182 final Character val = (char)Integer.parseInt(nums, 8);
183 return val.toString();
184 }
185
186 throw new LexerRecognitionException(null, "Unable to parse: " + str);
187 }
188 }