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 | 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 | token.set(Token.OP_MOD, "%");
|
||
77 | return token;
|
||
78 | case '=': |
||
79 | 45132 | jjdelcerro | 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 | 43512 | jjdelcerro | token.set(Token.OP_EQ, "=");
|
89 | return token;
|
||
90 | |||
91 | 45125 | jjdelcerro | 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 | 46081 | jjdelcerro | token.set(Token.COLON, ":");
|
102 | 45125 | jjdelcerro | return token;
|
103 | |||
104 | 43512 | jjdelcerro | case '<': |
105 | ch = getch(); |
||
106 | switch( ch ) {
|
||
107 | 44098 | jjdelcerro | case EOF:
|
108 | throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
||
109 | 43512 | jjdelcerro | 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 | 44098 | jjdelcerro | switch( ch ) {
|
123 | case EOF:
|
||
124 | throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
||
125 | case '=': |
||
126 | 43512 | jjdelcerro | token.set(Token.OP_GE, ">=");
|
127 | return token;
|
||
128 | } |
||
129 | ungetch(); |
||
130 | token.set(Token.OP_GT, ">");
|
||
131 | return token;
|
||
132 | |||
133 | 44139 | jjdelcerro | 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 | 44862 | jjdelcerro | break;
|
144 | 44139 | jjdelcerro | |
145 | 43939 | jjdelcerro | case '.': // SQL Extension to access object methods and attributes |
146 | token.set(Token.OP_GETATTR, ".");
|
||
147 | return token;
|
||
148 | 43512 | jjdelcerro | 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 | 44098 | jjdelcerro | token.set(Token.OP_ADD, "+");
|
159 | 43512 | jjdelcerro | return token;
|
160 | 44098 | jjdelcerro | // 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 | 43512 | jjdelcerro | case '-': |
174 | ch = getch(); |
||
175 | 44098 | jjdelcerro | switch( ch ) {
|
176 | case EOF:
|
||
177 | throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
||
178 | case '>': |
||
179 | 43939 | jjdelcerro | // SQL Extension to access object methods and attributes
|
180 | token.set(Token.OP_GETATTR, "->");
|
||
181 | return token;
|
||
182 | } |
||
183 | 44098 | jjdelcerro | // if( Character.isDigit(ch) ) {
|
184 | // ungetch();
|
||
185 | // ungetch();
|
||
186 | // parseNumber();
|
||
187 | // return token;
|
||
188 | // }
|
||
189 | ungetch(); |
||
190 | 43512 | jjdelcerro | 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 | 44098 | jjdelcerro | throw new ExpressionSyntaxException(I18N.End_of_string_was_expected_and_end_of_source_was_found(), this); |
199 | 43512 | jjdelcerro | } |
200 | buffer.add(ch); |
||
201 | ch = getch(); |
||
202 | } |
||
203 | if( buffer.length() < 1 ) { |
||
204 | 44098 | jjdelcerro | throw new ExpressionSyntaxException(I18N.Incorrect_string_length(), this); |
205 | 43512 | jjdelcerro | } |
206 | token.set(Token.IDENTIFIER, buffer.toString()); |
||
207 | return token;
|
||
208 | |||
209 | 44139 | jjdelcerro | case ']': |
210 | token.set(Token.CLOSED_BRACKET, "]");
|
||
211 | return token;
|
||
212 | 43512 | jjdelcerro | case '[': |
213 | 44139 | jjdelcerro | if( !this.useBracketsForIdentifiers ) { |
214 | token.set(Token.OPEN_BRACKET, "[");
|
||
215 | return token;
|
||
216 | } |
||
217 | 43512 | jjdelcerro | buffer.clear(); |
218 | ch = getch(); |
||
219 | while( ch != ']' ) { |
||
220 | if( ch == EOF ) {
|
||
221 | 44098 | jjdelcerro | throw new ExpressionSyntaxException(I18N.Closing_square_bracket_was_expected_and_end_of_source_was_found(), this); |
222 | 43512 | jjdelcerro | } |
223 | buffer.add(ch); |
||
224 | ch = getch(); |
||
225 | } |
||
226 | if( buffer.length() < 1 ) { |
||
227 | 44098 | jjdelcerro | throw new ExpressionSyntaxException(I18N.Incorrect_identifier_length(), this); |
228 | 43512 | jjdelcerro | } |
229 | token.set(Token.IDENTIFIER, buffer.toString()); |
||
230 | return token;
|
||
231 | |||
232 | case '\'': |
||
233 | parseString(); |
||
234 | return token;
|
||
235 | 44421 | jjdelcerro | |
236 | case '@': |
||
237 | ungetch(); |
||
238 | 44430 | jjdelcerro | parseDMSNumber(); |
239 | 44421 | jjdelcerro | return token;
|
240 | 43512 | jjdelcerro | } |
241 | 44421 | jjdelcerro | |
242 | 43512 | jjdelcerro | if( Character.isDigit(ch) ) { |
243 | ungetch(); |
||
244 | parseNumber(); |
||
245 | return token;
|
||
246 | } |
||
247 | 44384 | jjdelcerro | if( Character.isAlphabetic(ch) || ch =='$' ) { |
248 | 43512 | jjdelcerro | buffer.clear(); |
249 | 44384 | jjdelcerro | while( Character.isLetterOrDigit(ch) || ch =='$' || ch == '_' ) { |
250 | 43512 | jjdelcerro | buffer.add(ch); |
251 | ch = getch(); |
||
252 | } |
||
253 | 43989 | jjdelcerro | if( ch != EOF ) {
|
254 | 43983 | jjdelcerro | ungetch(); |
255 | } |
||
256 | 43512 | jjdelcerro | 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 | 44139 | jjdelcerro | token.set(Token.CHAR, Character.toString(ch), Character.toString(ch)); |
263 | 43512 | jjdelcerro | return token;
|
264 | } |
||
265 | |||
266 | } |