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 @ 44098
History | View | Annotate | Download (6.29 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 | |||
39 | @Override
|
||
40 | 43512 | jjdelcerro | protected Token getToken() {
|
41 | skipblanks(); |
||
42 | char ch = getch();
|
||
43 | switch( ch ) {
|
||
44 | case EOF:
|
||
45 | token.set(Token.EOF, null, null); |
||
46 | return token;
|
||
47 | 43532 | jjdelcerro | case '~': |
48 | token.set(Token.OP_REGEXP, "~");
|
||
49 | return token;
|
||
50 | 43512 | jjdelcerro | case '*': |
51 | token.set(Token.OP_MULT, "*");
|
||
52 | return token;
|
||
53 | case '/': |
||
54 | token.set(Token.OP_DIV, "/");
|
||
55 | return token;
|
||
56 | case '%': |
||
57 | token.set(Token.OP_MOD, "%");
|
||
58 | return token;
|
||
59 | case '=': |
||
60 | token.set(Token.OP_EQ, "=");
|
||
61 | return token;
|
||
62 | |||
63 | case '<': |
||
64 | ch = getch(); |
||
65 | switch( ch ) {
|
||
66 | 44098 | jjdelcerro | case EOF:
|
67 | throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
||
68 | 43512 | jjdelcerro | case '>': |
69 | token.set(Token.OP_NE, "<>");
|
||
70 | return token;
|
||
71 | case '=': |
||
72 | token.set(Token.OP_LE, "<=");
|
||
73 | return token;
|
||
74 | } |
||
75 | ungetch(); |
||
76 | token.set(Token.OP_LT, "<");
|
||
77 | return token;
|
||
78 | |||
79 | case '>': |
||
80 | ch = getch(); |
||
81 | 44098 | jjdelcerro | switch( ch ) {
|
82 | case EOF:
|
||
83 | throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this); |
||
84 | case '=': |
||
85 | 43512 | jjdelcerro | token.set(Token.OP_GE, ">=");
|
86 | return token;
|
||
87 | } |
||
88 | ungetch(); |
||
89 | token.set(Token.OP_GT, ">");
|
||
90 | return token;
|
||
91 | |||
92 | 43939 | jjdelcerro | case '.': // SQL Extension to access object methods and attributes |
93 | token.set(Token.OP_GETATTR, ".");
|
||
94 | return token;
|
||
95 | 43512 | jjdelcerro | case ',': |
96 | token.set(Token.COMA, ",");
|
||
97 | return token;
|
||
98 | case '(': |
||
99 | token.set(Token.PARENTHESIS_OPEN, "(");
|
||
100 | return token;
|
||
101 | case ')': |
||
102 | token.set(Token.PARENTHESIS_CLOSE, ")");
|
||
103 | return token;
|
||
104 | case '+': |
||
105 | 44098 | jjdelcerro | token.set(Token.OP_ADD, "+");
|
106 | 43512 | jjdelcerro | return token;
|
107 | 44098 | jjdelcerro | // ch = getch();
|
108 | // if( ch == EOF ) {
|
||
109 | // throw new ExpressionSyntaxException(I18N.unexpected_end_of_source(), this);
|
||
110 | // }
|
||
111 | // if( !Character.isDigit(ch) ) {
|
||
112 | // ungetch();
|
||
113 | // token.set(Token.OP_ADD, "+");
|
||
114 | // return token;
|
||
115 | // }
|
||
116 | // ungetch();
|
||
117 | // parseNumber();
|
||
118 | // token.setLiteral("+" + token.getLiteral());
|
||
119 | // return token;
|
||
120 | 43512 | jjdelcerro | 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 | 43939 | jjdelcerro | // SQL Extension to access object methods and attributes
|
127 | token.set(Token.OP_GETATTR, "->");
|
||
128 | return token;
|
||
129 | } |
||
130 | 44098 | jjdelcerro | // if( Character.isDigit(ch) ) {
|
131 | // ungetch();
|
||
132 | // ungetch();
|
||
133 | // parseNumber();
|
||
134 | // return token;
|
||
135 | // }
|
||
136 | ungetch(); |
||
137 | 43512 | jjdelcerro | token.set(Token.OP_SUBST, "-");
|
138 | return token;
|
||
139 | |||
140 | case '"': |
||
141 | buffer.clear(); |
||
142 | ch = getch(); |
||
143 | while( ch != '"' ) { |
||
144 | if( ch == EOF ) {
|
||
145 | 44098 | jjdelcerro | throw new ExpressionSyntaxException(I18N.End_of_string_was_expected_and_end_of_source_was_found(), this); |
146 | 43512 | jjdelcerro | } |
147 | buffer.add(ch); |
||
148 | ch = getch(); |
||
149 | } |
||
150 | if( buffer.length() < 1 ) { |
||
151 | 44098 | jjdelcerro | throw new ExpressionSyntaxException(I18N.Incorrect_string_length(), this); |
152 | 43512 | jjdelcerro | } |
153 | token.set(Token.IDENTIFIER, buffer.toString()); |
||
154 | return token;
|
||
155 | |||
156 | case '[': |
||
157 | buffer.clear(); |
||
158 | ch = getch(); |
||
159 | while( ch != ']' ) { |
||
160 | if( ch == EOF ) {
|
||
161 | 44098 | jjdelcerro | throw new ExpressionSyntaxException(I18N.Closing_square_bracket_was_expected_and_end_of_source_was_found(), this); |
162 | 43512 | jjdelcerro | } |
163 | buffer.add(ch); |
||
164 | ch = getch(); |
||
165 | } |
||
166 | if( buffer.length() < 1 ) { |
||
167 | 44098 | jjdelcerro | throw new ExpressionSyntaxException(I18N.Incorrect_identifier_length(), this); |
168 | 43512 | jjdelcerro | } |
169 | token.set(Token.IDENTIFIER, buffer.toString()); |
||
170 | return token;
|
||
171 | |||
172 | case '\'': |
||
173 | parseString(); |
||
174 | return token;
|
||
175 | } |
||
176 | if( Character.isDigit(ch) ) { |
||
177 | ungetch(); |
||
178 | parseNumber(); |
||
179 | return token;
|
||
180 | } |
||
181 | if( Character.isAlphabetic(ch) ) { |
||
182 | buffer.clear(); |
||
183 | while( Character.isLetterOrDigit(ch) || ch == '_' ) { |
||
184 | buffer.add(ch); |
||
185 | ch = getch(); |
||
186 | } |
||
187 | 43989 | jjdelcerro | if( ch != EOF ) {
|
188 | 43983 | jjdelcerro | ungetch(); |
189 | } |
||
190 | 43512 | jjdelcerro | String id = buffer.toString();
|
191 | int type = this.tokens.getOrDefault(id.toLowerCase(), Token.IDENTIFIER); |
||
192 | token.set(type, id); |
||
193 | return token;
|
||
194 | } |
||
195 | |||
196 | token.set(Token.EOF, null, null); |
||
197 | return token;
|
||
198 | } |
||
199 | |||
200 | } |