svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / SQLLexicalAnalyzer.java @ 46091
History | View | Annotate | Download (8.97 KB)
1 |
package org.gvsig.expressionevaluator.impl; |
---|---|
2 |
|
3 |
import org.gvsig.expressionevaluator.ExpressionSyntaxException; |
4 |
import org.gvsig.expressionevaluator.spi.AbstractLexicalAnalyzer; |
5 |
import org.gvsig.expressionevaluator.LexicalAnalyzer; |
6 |
|
7 |
public class SQLLexicalAnalyzer extends AbstractLexicalAnalyzer { |
8 |
|
9 |
public SQLLexicalAnalyzer(String source) { |
10 |
super(source);
|
11 |
this.tokens.put("null", Token.NULL); |
12 |
this.tokens.put("true", Token.TRUE); |
13 |
this.tokens.put("false", Token.FALSE); |
14 |
this.tokens.put("not", Token.OP_NOT); |
15 |
this.tokens.put("like", Token.PRED_LIKE); |
16 |
this.tokens.put("ilike", Token.PRED_ILIKE); |
17 |
this.tokens.put("is", Token.PRED_IS); |
18 |
this.tokens.put("between", Token.PRED_BETWEEN); |
19 |
this.tokens.put("and", Token.OP_AND); |
20 |
this.tokens.put("or", Token.OP_OR); |
21 |
this.tokens.put("isnull", Token.ISNULL); |
22 |
this.tokens.put("notnull", Token.NOTNULL); |
23 |
} |
24 |
|
25 |
public SQLLexicalAnalyzer() {
|
26 |
this(null); |
27 |
} |
28 |
|
29 |
@Override
|
30 |
public LexicalAnalyzer clone() throws CloneNotSupportedException { |
31 |
// As this implementation does not add state to the abstract class, we
|
32 |
// just call the super class.
|
33 |
SQLLexicalAnalyzer other = (SQLLexicalAnalyzer) super.clone();
|
34 |
|
35 |
return other;
|
36 |
} |
37 |
|
38 |
private void skipcomments() { |
39 |
if (isEOF()) {
|
40 |
return;
|
41 |
} |
42 |
char ch = getch();
|
43 |
while( ch == '-' ) { |
44 |
ch = lookch(); |
45 |
if( ch == '-' ) { |
46 |
while( ch != EOF && ch != '\n' ) { |
47 |
ch = getch(); |
48 |
} |
49 |
ungetch(); |
50 |
skipblanks(); |
51 |
ch = getch(); |
52 |
} |
53 |
} |
54 |
ungetch(); |
55 |
} |
56 |
|
57 |
@Override
|
58 |
protected Token getToken() {
|
59 |
skipblanks(); |
60 |
skipcomments(); |
61 |
char ch = getch();
|
62 |
switch( ch ) {
|
63 |
case EOF:
|
64 |
token.set(Token.EOF, null, null); |
65 |
return token;
|
66 |
case '~': |
67 |
token.set(Token.OP_REGEXP, "~");
|
68 |
return token;
|
69 |
case '*': |
70 |
token.set(Token.OP_MULT, "*");
|
71 |
return token;
|
72 |
case '/': |
73 |
token.set(Token.OP_DIV, "/");
|
74 |
return token;
|
75 |
case '%': |
76 |
ch = getch(); |
77 |
switch( ch ) {
|
78 |
case EOF:
|
79 |
throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
80 |
case '>': |
81 |
token.set(Token.END_$CONSTANT, "%>"); |
82 |
return token;
|
83 |
} |
84 |
ungetch(); |
85 |
token.set(Token.OP_MOD, "%");
|
86 |
return token;
|
87 |
case '=': |
88 |
ch = getch(); |
89 |
switch( ch ) {
|
90 |
case EOF:
|
91 |
throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
92 |
case '>': |
93 |
token.set(Token.ARGUMENT_ASSIGNMENT, "=>");
|
94 |
return token;
|
95 |
} |
96 |
ungetch(); |
97 |
token.set(Token.OP_EQ, "=");
|
98 |
return token;
|
99 |
|
100 |
case ':': |
101 |
ch = getch(); |
102 |
switch( ch ) {
|
103 |
case EOF:
|
104 |
throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
105 |
case '=': |
106 |
token.set(Token.ASSIGNMENT, ":=");
|
107 |
return token;
|
108 |
} |
109 |
ungetch(); |
110 |
token.set(Token.COLON, ":");
|
111 |
return token;
|
112 |
|
113 |
case '<': |
114 |
ch = getch(); |
115 |
switch( ch ) {
|
116 |
case EOF:
|
117 |
throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
118 |
case '>': |
119 |
token.set(Token.OP_NE, "<>");
|
120 |
return token;
|
121 |
case '=': |
122 |
token.set(Token.OP_LE, "<=");
|
123 |
return token;
|
124 |
case '%': |
125 |
token.set(Token.BEGIN_$CONSTANT, "<%"); |
126 |
return token;
|
127 |
} |
128 |
ungetch(); |
129 |
token.set(Token.OP_LT, "<");
|
130 |
return token;
|
131 |
|
132 |
case '>': |
133 |
ch = getch(); |
134 |
switch( ch ) {
|
135 |
case EOF:
|
136 |
throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
137 |
case '=': |
138 |
token.set(Token.OP_GE, ">=");
|
139 |
return token;
|
140 |
} |
141 |
ungetch(); |
142 |
token.set(Token.OP_GT, ">");
|
143 |
return token;
|
144 |
|
145 |
case '|': |
146 |
ch = getch(); |
147 |
switch( ch ) {
|
148 |
case EOF:
|
149 |
throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
150 |
case '|': |
151 |
token.set(Token.OP_CONCAT, "||");
|
152 |
return token;
|
153 |
} |
154 |
ungetch(); |
155 |
break;
|
156 |
|
157 |
case '.': // SQL Extension to access object methods and attributes |
158 |
token.set(Token.OP_GETATTR, ".");
|
159 |
return token;
|
160 |
case ',': |
161 |
token.set(Token.COMA, ",");
|
162 |
return token;
|
163 |
case '(': |
164 |
token.set(Token.PARENTHESIS_OPEN, "(");
|
165 |
return token;
|
166 |
case ')': |
167 |
token.set(Token.PARENTHESIS_CLOSE, ")");
|
168 |
return token;
|
169 |
case '+': |
170 |
token.set(Token.OP_ADD, "+");
|
171 |
return token;
|
172 |
// ch = getch();
|
173 |
// if( ch == EOF ) {
|
174 |
// throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this);
|
175 |
// }
|
176 |
// if( !Character.isDigit(ch) ) {
|
177 |
// ungetch();
|
178 |
// token.set(Token.OP_ADD, "+");
|
179 |
// return token;
|
180 |
// }
|
181 |
// ungetch();
|
182 |
// parseNumber();
|
183 |
// token.setLiteral("+" + token.getLiteral());
|
184 |
// return token;
|
185 |
case '-': |
186 |
ch = getch(); |
187 |
switch( ch ) {
|
188 |
case EOF:
|
189 |
throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
190 |
case '>': |
191 |
// SQL Extension to access object methods and attributes
|
192 |
token.set(Token.OP_GETATTR, "->");
|
193 |
return token;
|
194 |
} |
195 |
// if( Character.isDigit(ch) ) {
|
196 |
// ungetch();
|
197 |
// ungetch();
|
198 |
// parseNumber();
|
199 |
// return token;
|
200 |
// }
|
201 |
ungetch(); |
202 |
token.set(Token.OP_SUBST, "-");
|
203 |
return token;
|
204 |
|
205 |
case '"': |
206 |
buffer.clear(); |
207 |
ch = getch(); |
208 |
while( ch != '"' ) { |
209 |
if( ch == EOF ) {
|
210 |
throw new ExpressionSyntaxException(I18N.End_of_string_was_expected_and_end_of_source_was_found(), this); |
211 |
} |
212 |
buffer.add(ch); |
213 |
ch = getch(); |
214 |
} |
215 |
if( buffer.length() < 1 ) { |
216 |
throw new ExpressionSyntaxException(I18N.Incorrect_string_length(), this); |
217 |
} |
218 |
token.set(Token.IDENTIFIER, buffer.toString()); |
219 |
return token;
|
220 |
|
221 |
case ']': |
222 |
token.set(Token.CLOSED_BRACKET, "]");
|
223 |
return token;
|
224 |
case '[': |
225 |
if( !this.useBracketsForIdentifiers ) { |
226 |
token.set(Token.OPEN_BRACKET, "[");
|
227 |
return token;
|
228 |
} |
229 |
buffer.clear(); |
230 |
ch = getch(); |
231 |
while( ch != ']' ) { |
232 |
if( ch == EOF ) {
|
233 |
throw new ExpressionSyntaxException(I18N.Closing_square_bracket_was_expected_and_end_of_source_was_found(), this); |
234 |
} |
235 |
buffer.add(ch); |
236 |
ch = getch(); |
237 |
} |
238 |
if( buffer.length() < 1 ) { |
239 |
throw new ExpressionSyntaxException(I18N.Incorrect_identifier_length(), this); |
240 |
} |
241 |
token.set(Token.IDENTIFIER, buffer.toString()); |
242 |
return token;
|
243 |
|
244 |
case '\'': |
245 |
parseString(); |
246 |
return token;
|
247 |
|
248 |
case '@': |
249 |
ungetch(); |
250 |
parseDMSNumber(); |
251 |
return token;
|
252 |
//
|
253 |
// case '$':
|
254 |
// ch = getch();
|
255 |
// switch( ch ) {
|
256 |
// case EOF:
|
257 |
// throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this);
|
258 |
// case '$':
|
259 |
// parseStringBlock();
|
260 |
// return token;
|
261 |
// }
|
262 |
// ungetch();
|
263 |
// ungetch();
|
264 |
// break;
|
265 |
} |
266 |
|
267 |
if( Character.isDigit(ch) ) { |
268 |
ungetch(); |
269 |
parseNumber(); |
270 |
return token;
|
271 |
} |
272 |
if( Character.isAlphabetic(ch) || ch =='$' ) { |
273 |
buffer.clear(); |
274 |
while( Character.isLetterOrDigit(ch) || ch =='$' || ch == '_' ) { |
275 |
buffer.add(ch); |
276 |
ch = getch(); |
277 |
} |
278 |
if( ch != EOF ) {
|
279 |
ungetch(); |
280 |
} |
281 |
String id = buffer.toString();
|
282 |
int type = this.tokens.getOrDefault(id.toLowerCase(), Token.IDENTIFIER); |
283 |
token.set(type, id); |
284 |
return token;
|
285 |
} |
286 |
|
287 |
token.set(Token.CHAR, Character.toString(ch), Character.toString(ch)); |
288 |
return token;
|
289 |
} |
290 |
|
291 |
} |