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 @ 46081
History | View | Annotate | Download (8.17 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 |
token.set(Token.OP_MOD, "%");
|
77 |
return token;
|
78 |
case '=': |
79 |
ch = getch(); |
80 |
switch( ch ) {
|
81 |
case EOF:
|
82 |
throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
83 |
case '>': |
84 |
token.set(Token.ARGUMENT_ASSIGNMENT, "=>");
|
85 |
return token;
|
86 |
} |
87 |
ungetch(); |
88 |
token.set(Token.OP_EQ, "=");
|
89 |
return token;
|
90 |
|
91 |
case ':': |
92 |
ch = getch(); |
93 |
switch( ch ) {
|
94 |
case EOF:
|
95 |
throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
96 |
case '=': |
97 |
token.set(Token.ASSIGNMENT, ":=");
|
98 |
return token;
|
99 |
} |
100 |
ungetch(); |
101 |
token.set(Token.COLON, ":");
|
102 |
return token;
|
103 |
|
104 |
case '<': |
105 |
ch = getch(); |
106 |
switch( ch ) {
|
107 |
case EOF:
|
108 |
throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
109 |
case '>': |
110 |
token.set(Token.OP_NE, "<>");
|
111 |
return token;
|
112 |
case '=': |
113 |
token.set(Token.OP_LE, "<=");
|
114 |
return token;
|
115 |
} |
116 |
ungetch(); |
117 |
token.set(Token.OP_LT, "<");
|
118 |
return token;
|
119 |
|
120 |
case '>': |
121 |
ch = getch(); |
122 |
switch( ch ) {
|
123 |
case EOF:
|
124 |
throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
125 |
case '=': |
126 |
token.set(Token.OP_GE, ">=");
|
127 |
return token;
|
128 |
} |
129 |
ungetch(); |
130 |
token.set(Token.OP_GT, ">");
|
131 |
return token;
|
132 |
|
133 |
case '|': |
134 |
ch = getch(); |
135 |
switch( ch ) {
|
136 |
case EOF:
|
137 |
throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
138 |
case '|': |
139 |
token.set(Token.OP_CONCAT, "||");
|
140 |
return token;
|
141 |
} |
142 |
ungetch(); |
143 |
break;
|
144 |
|
145 |
case '.': // SQL Extension to access object methods and attributes |
146 |
token.set(Token.OP_GETATTR, ".");
|
147 |
return token;
|
148 |
case ',': |
149 |
token.set(Token.COMA, ",");
|
150 |
return token;
|
151 |
case '(': |
152 |
token.set(Token.PARENTHESIS_OPEN, "(");
|
153 |
return token;
|
154 |
case ')': |
155 |
token.set(Token.PARENTHESIS_CLOSE, ")");
|
156 |
return token;
|
157 |
case '+': |
158 |
token.set(Token.OP_ADD, "+");
|
159 |
return token;
|
160 |
// ch = getch();
|
161 |
// if( ch == EOF ) {
|
162 |
// throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this);
|
163 |
// }
|
164 |
// if( !Character.isDigit(ch) ) {
|
165 |
// ungetch();
|
166 |
// token.set(Token.OP_ADD, "+");
|
167 |
// return token;
|
168 |
// }
|
169 |
// ungetch();
|
170 |
// parseNumber();
|
171 |
// token.setLiteral("+" + token.getLiteral());
|
172 |
// return token;
|
173 |
case '-': |
174 |
ch = getch(); |
175 |
switch( ch ) {
|
176 |
case EOF:
|
177 |
throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
178 |
case '>': |
179 |
// SQL Extension to access object methods and attributes
|
180 |
token.set(Token.OP_GETATTR, "->");
|
181 |
return token;
|
182 |
} |
183 |
// if( Character.isDigit(ch) ) {
|
184 |
// ungetch();
|
185 |
// ungetch();
|
186 |
// parseNumber();
|
187 |
// return token;
|
188 |
// }
|
189 |
ungetch(); |
190 |
token.set(Token.OP_SUBST, "-");
|
191 |
return token;
|
192 |
|
193 |
case '"': |
194 |
buffer.clear(); |
195 |
ch = getch(); |
196 |
while( ch != '"' ) { |
197 |
if( ch == EOF ) {
|
198 |
throw new ExpressionSyntaxException(I18N.End_of_string_was_expected_and_end_of_source_was_found(), this); |
199 |
} |
200 |
buffer.add(ch); |
201 |
ch = getch(); |
202 |
} |
203 |
if( buffer.length() < 1 ) { |
204 |
throw new ExpressionSyntaxException(I18N.Incorrect_string_length(), this); |
205 |
} |
206 |
token.set(Token.IDENTIFIER, buffer.toString()); |
207 |
return token;
|
208 |
|
209 |
case ']': |
210 |
token.set(Token.CLOSED_BRACKET, "]");
|
211 |
return token;
|
212 |
case '[': |
213 |
if( !this.useBracketsForIdentifiers ) { |
214 |
token.set(Token.OPEN_BRACKET, "[");
|
215 |
return token;
|
216 |
} |
217 |
buffer.clear(); |
218 |
ch = getch(); |
219 |
while( ch != ']' ) { |
220 |
if( ch == EOF ) {
|
221 |
throw new ExpressionSyntaxException(I18N.Closing_square_bracket_was_expected_and_end_of_source_was_found(), this); |
222 |
} |
223 |
buffer.add(ch); |
224 |
ch = getch(); |
225 |
} |
226 |
if( buffer.length() < 1 ) { |
227 |
throw new ExpressionSyntaxException(I18N.Incorrect_identifier_length(), this); |
228 |
} |
229 |
token.set(Token.IDENTIFIER, buffer.toString()); |
230 |
return token;
|
231 |
|
232 |
case '\'': |
233 |
parseString(); |
234 |
return token;
|
235 |
|
236 |
case '@': |
237 |
ungetch(); |
238 |
parseDMSNumber(); |
239 |
return token;
|
240 |
} |
241 |
|
242 |
if( Character.isDigit(ch) ) { |
243 |
ungetch(); |
244 |
parseNumber(); |
245 |
return token;
|
246 |
} |
247 |
if( Character.isAlphabetic(ch) || ch =='$' ) { |
248 |
buffer.clear(); |
249 |
while( Character.isLetterOrDigit(ch) || ch =='$' || ch == '_' ) { |
250 |
buffer.add(ch); |
251 |
ch = getch(); |
252 |
} |
253 |
if( ch != EOF ) {
|
254 |
ungetch(); |
255 |
} |
256 |
String id = buffer.toString();
|
257 |
int type = this.tokens.getOrDefault(id.toLowerCase(), Token.IDENTIFIER); |
258 |
token.set(type, id); |
259 |
return token;
|
260 |
} |
261 |
|
262 |
token.set(Token.CHAR, Character.toString(ch), Character.toString(ch)); |
263 |
return token;
|
264 |
} |
265 |
|
266 |
} |