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 | 43512 | jjdelcerro | package org.gvsig.expressionevaluator.impl; |
---|---|---|---|
2 | |||
3 | 44098 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionSyntaxException; |
4 | 43983 | jjdelcerro | import org.gvsig.expressionevaluator.spi.AbstractLexicalAnalyzer; |
5 | 43809 | jjdelcerro | import org.gvsig.expressionevaluator.LexicalAnalyzer; |
6 | |||
7 | 43512 | jjdelcerro | 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 | 43809 | jjdelcerro | 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 | 44379 | jjdelcerro | 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 | 43809 | jjdelcerro | |
57 | @Override
|
||
58 | 43512 | jjdelcerro | protected Token getToken() {
|
59 | skipblanks(); |
||
60 | 44379 | jjdelcerro | skipcomments(); |
61 | 43512 | jjdelcerro | char ch = getch();
|
62 | switch( ch ) {
|
||
63 | case EOF:
|
||
64 | token.set(Token.EOF, null, null); |
||
65 | return token;
|
||
66 | 43532 | jjdelcerro | case '~': |
67 | token.set(Token.OP_REGEXP, "~");
|
||
68 | return token;
|
||
69 | 43512 | jjdelcerro | case '*': |
70 | token.set(Token.OP_MULT, "*");
|
||
71 | return token;
|
||
72 | case '/': |
||
73 | token.set(Token.OP_DIV, "/");
|
||
74 | return token;
|
||
75 | case '%': |
||
76 | 46091 | omartinez | 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 | 43512 | jjdelcerro | token.set(Token.OP_MOD, "%");
|
86 | return token;
|
||
87 | case '=': |
||
88 | 45132 | jjdelcerro | 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 | 43512 | jjdelcerro | token.set(Token.OP_EQ, "=");
|
98 | return token;
|
||
99 | |||
100 | 45125 | jjdelcerro | 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 | 46081 | jjdelcerro | token.set(Token.COLON, ":");
|
111 | 45125 | jjdelcerro | return token;
|
112 | |||
113 | 43512 | jjdelcerro | case '<': |
114 | ch = getch(); |
||
115 | switch( ch ) {
|
||
116 | 44098 | jjdelcerro | case EOF:
|
117 | throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
||
118 | 43512 | jjdelcerro | case '>': |
119 | token.set(Token.OP_NE, "<>");
|
||
120 | return token;
|
||
121 | case '=': |
||
122 | token.set(Token.OP_LE, "<=");
|
||
123 | return token;
|
||
124 | 46091 | omartinez | case '%': |
125 | token.set(Token.BEGIN_$CONSTANT, "<%"); |
||
126 | return token;
|
||
127 | 43512 | jjdelcerro | } |
128 | ungetch(); |
||
129 | token.set(Token.OP_LT, "<");
|
||
130 | return token;
|
||
131 | |||
132 | case '>': |
||
133 | ch = getch(); |
||
134 | 44098 | jjdelcerro | switch( ch ) {
|
135 | case EOF:
|
||
136 | throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
||
137 | case '=': |
||
138 | 43512 | jjdelcerro | token.set(Token.OP_GE, ">=");
|
139 | return token;
|
||
140 | } |
||
141 | ungetch(); |
||
142 | token.set(Token.OP_GT, ">");
|
||
143 | return token;
|
||
144 | |||
145 | 44139 | jjdelcerro | 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 | 44862 | jjdelcerro | break;
|
156 | 44139 | jjdelcerro | |
157 | 43939 | jjdelcerro | case '.': // SQL Extension to access object methods and attributes |
158 | token.set(Token.OP_GETATTR, ".");
|
||
159 | return token;
|
||
160 | 43512 | jjdelcerro | 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 | 44098 | jjdelcerro | token.set(Token.OP_ADD, "+");
|
171 | 43512 | jjdelcerro | return token;
|
172 | 44098 | jjdelcerro | // 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 | 43512 | jjdelcerro | case '-': |
186 | ch = getch(); |
||
187 | 44098 | jjdelcerro | switch( ch ) {
|
188 | case EOF:
|
||
189 | throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
||
190 | case '>': |
||
191 | 43939 | jjdelcerro | // SQL Extension to access object methods and attributes
|
192 | token.set(Token.OP_GETATTR, "->");
|
||
193 | return token;
|
||
194 | } |
||
195 | 44098 | jjdelcerro | // if( Character.isDigit(ch) ) {
|
196 | // ungetch();
|
||
197 | // ungetch();
|
||
198 | // parseNumber();
|
||
199 | // return token;
|
||
200 | // }
|
||
201 | ungetch(); |
||
202 | 43512 | jjdelcerro | 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 | 44098 | jjdelcerro | throw new ExpressionSyntaxException(I18N.End_of_string_was_expected_and_end_of_source_was_found(), this); |
211 | 43512 | jjdelcerro | } |
212 | buffer.add(ch); |
||
213 | ch = getch(); |
||
214 | } |
||
215 | if( buffer.length() < 1 ) { |
||
216 | 44098 | jjdelcerro | throw new ExpressionSyntaxException(I18N.Incorrect_string_length(), this); |
217 | 43512 | jjdelcerro | } |
218 | token.set(Token.IDENTIFIER, buffer.toString()); |
||
219 | return token;
|
||
220 | |||
221 | 44139 | jjdelcerro | case ']': |
222 | token.set(Token.CLOSED_BRACKET, "]");
|
||
223 | return token;
|
||
224 | 43512 | jjdelcerro | case '[': |
225 | 44139 | jjdelcerro | if( !this.useBracketsForIdentifiers ) { |
226 | token.set(Token.OPEN_BRACKET, "[");
|
||
227 | return token;
|
||
228 | } |
||
229 | 43512 | jjdelcerro | buffer.clear(); |
230 | ch = getch(); |
||
231 | while( ch != ']' ) { |
||
232 | if( ch == EOF ) {
|
||
233 | 44098 | jjdelcerro | throw new ExpressionSyntaxException(I18N.Closing_square_bracket_was_expected_and_end_of_source_was_found(), this); |
234 | 43512 | jjdelcerro | } |
235 | buffer.add(ch); |
||
236 | ch = getch(); |
||
237 | } |
||
238 | if( buffer.length() < 1 ) { |
||
239 | 44098 | jjdelcerro | throw new ExpressionSyntaxException(I18N.Incorrect_identifier_length(), this); |
240 | 43512 | jjdelcerro | } |
241 | token.set(Token.IDENTIFIER, buffer.toString()); |
||
242 | return token;
|
||
243 | |||
244 | case '\'': |
||
245 | parseString(); |
||
246 | return token;
|
||
247 | 46091 | omartinez | |
248 | 44421 | jjdelcerro | case '@': |
249 | ungetch(); |
||
250 | 44430 | jjdelcerro | parseDMSNumber(); |
251 | 44421 | jjdelcerro | return token;
|
252 | 46091 | omartinez | //
|
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 | 43512 | jjdelcerro | } |
266 | 44421 | jjdelcerro | |
267 | 43512 | jjdelcerro | if( Character.isDigit(ch) ) { |
268 | ungetch(); |
||
269 | parseNumber(); |
||
270 | return token;
|
||
271 | 46091 | omartinez | } |
272 | 44384 | jjdelcerro | if( Character.isAlphabetic(ch) || ch =='$' ) { |
273 | 43512 | jjdelcerro | buffer.clear(); |
274 | 44384 | jjdelcerro | while( Character.isLetterOrDigit(ch) || ch =='$' || ch == '_' ) { |
275 | 43512 | jjdelcerro | buffer.add(ch); |
276 | ch = getch(); |
||
277 | } |
||
278 | 43989 | jjdelcerro | if( ch != EOF ) {
|
279 | 43983 | jjdelcerro | ungetch(); |
280 | } |
||
281 | 43512 | jjdelcerro | 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 | 44139 | jjdelcerro | token.set(Token.CHAR, Character.toString(ch), Character.toString(ch)); |
288 | 43512 | jjdelcerro | return token;
|
289 | } |
||
290 | |||
291 | } |