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