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 @ 43809
History | View | Annotate | Download (4.99 KB)
1 |
package org.gvsig.expressionevaluator.impl; |
---|---|
2 |
|
3 |
import org.gvsig.expressionevaluator.LexicalAnalyzer; |
4 |
|
5 |
public class SQLLexicalAnalyzer extends AbstractLexicalAnalyzer { |
6 |
|
7 |
public SQLLexicalAnalyzer(String source) { |
8 |
super(source);
|
9 |
this.tokens.put("null", Token.NULL); |
10 |
this.tokens.put("true", Token.TRUE); |
11 |
this.tokens.put("false", Token.FALSE); |
12 |
this.tokens.put("not", Token.OP_NOT); |
13 |
this.tokens.put("like", Token.PRED_LIKE); |
14 |
this.tokens.put("ilike", Token.PRED_ILIKE); |
15 |
this.tokens.put("is", Token.PRED_IS); |
16 |
this.tokens.put("between", Token.PRED_BETWEEN); |
17 |
this.tokens.put("and", Token.OP_AND); |
18 |
this.tokens.put("or", Token.OP_OR); |
19 |
this.tokens.put("isnull", Token.ISNULL); |
20 |
this.tokens.put("notnull", Token.NOTNULL); |
21 |
} |
22 |
|
23 |
public SQLLexicalAnalyzer() {
|
24 |
this(null); |
25 |
} |
26 |
|
27 |
@Override
|
28 |
public LexicalAnalyzer clone() throws CloneNotSupportedException { |
29 |
// As this implementation does not add state to the abstract class, we
|
30 |
// just call the super class.
|
31 |
SQLLexicalAnalyzer other = (SQLLexicalAnalyzer) super.clone();
|
32 |
|
33 |
return other;
|
34 |
} |
35 |
|
36 |
|
37 |
@Override
|
38 |
protected Token getToken() {
|
39 |
skipblanks(); |
40 |
char ch = getch();
|
41 |
switch( ch ) {
|
42 |
case EOF:
|
43 |
token.set(Token.EOF, null, null); |
44 |
return token;
|
45 |
case '~': |
46 |
token.set(Token.OP_REGEXP, "~");
|
47 |
return token;
|
48 |
case '*': |
49 |
token.set(Token.OP_MULT, "*");
|
50 |
return token;
|
51 |
case '/': |
52 |
token.set(Token.OP_DIV, "/");
|
53 |
return token;
|
54 |
case '%': |
55 |
token.set(Token.OP_MOD, "%");
|
56 |
return token;
|
57 |
case '=': |
58 |
token.set(Token.OP_EQ, "=");
|
59 |
return token;
|
60 |
|
61 |
case '<': |
62 |
ch = getch(); |
63 |
switch( ch ) {
|
64 |
case '>': |
65 |
token.set(Token.OP_NE, "<>");
|
66 |
return token;
|
67 |
case '=': |
68 |
token.set(Token.OP_LE, "<=");
|
69 |
return token;
|
70 |
} |
71 |
ungetch(); |
72 |
token.set(Token.OP_LT, "<");
|
73 |
return token;
|
74 |
|
75 |
case '>': |
76 |
ch = getch(); |
77 |
if( ch == '=' ) { |
78 |
token.set(Token.OP_GE, ">=");
|
79 |
return token;
|
80 |
} |
81 |
ungetch(); |
82 |
token.set(Token.OP_GT, ">");
|
83 |
return token;
|
84 |
|
85 |
case ',': |
86 |
token.set(Token.COMA, ",");
|
87 |
return token;
|
88 |
case '(': |
89 |
token.set(Token.PARENTHESIS_OPEN, "(");
|
90 |
return token;
|
91 |
case ')': |
92 |
token.set(Token.PARENTHESIS_CLOSE, ")");
|
93 |
return token;
|
94 |
case '+': |
95 |
ch = getch(); |
96 |
if( !Character.isDigit(ch) ) { |
97 |
ungetch(); |
98 |
token.set(Token.OP_ADD, "+");
|
99 |
return token;
|
100 |
} |
101 |
ungetch(); |
102 |
parseNumber(); |
103 |
token.setLiteral("+" + token.getLiteral());
|
104 |
return token;
|
105 |
case '-': |
106 |
ch = getch(); |
107 |
if( Character.isDigit(ch) ) { |
108 |
ungetch(); |
109 |
ungetch(); |
110 |
parseNumber(); |
111 |
return token;
|
112 |
} |
113 |
token.set(Token.OP_SUBST, "-");
|
114 |
return token;
|
115 |
|
116 |
case '"': |
117 |
buffer.clear(); |
118 |
ch = getch(); |
119 |
while( ch != '"' ) { |
120 |
if( ch == EOF ) {
|
121 |
throw new RuntimeException("Found end of source and expected end of string"); |
122 |
} |
123 |
buffer.add(ch); |
124 |
ch = getch(); |
125 |
} |
126 |
if( buffer.length() < 1 ) { |
127 |
throw new RuntimeException(); |
128 |
} |
129 |
token.set(Token.IDENTIFIER, buffer.toString()); |
130 |
return token;
|
131 |
|
132 |
case '[': |
133 |
buffer.clear(); |
134 |
ch = getch(); |
135 |
while( ch != ']' ) { |
136 |
if( ch == EOF ) {
|
137 |
throw new RuntimeException("Found end of source and expected end of string"); |
138 |
} |
139 |
buffer.add(ch); |
140 |
ch = getch(); |
141 |
} |
142 |
if( buffer.length() < 1 ) { |
143 |
throw new RuntimeException(); |
144 |
} |
145 |
token.set(Token.IDENTIFIER, buffer.toString()); |
146 |
return token;
|
147 |
|
148 |
case '\'': |
149 |
parseString(); |
150 |
return token;
|
151 |
} |
152 |
if( Character.isDigit(ch) ) { |
153 |
ungetch(); |
154 |
parseNumber(); |
155 |
return token;
|
156 |
} |
157 |
if( Character.isAlphabetic(ch) ) { |
158 |
buffer.clear(); |
159 |
while( Character.isLetterOrDigit(ch) || ch == '_' ) { |
160 |
buffer.add(ch); |
161 |
ch = getch(); |
162 |
} |
163 |
ungetch(); |
164 |
String id = buffer.toString();
|
165 |
int type = this.tokens.getOrDefault(id.toLowerCase(), Token.IDENTIFIER); |
166 |
token.set(type, id); |
167 |
return token;
|
168 |
} |
169 |
|
170 |
token.set(Token.EOF, null, null); |
171 |
return token;
|
172 |
} |
173 |
|
174 |
} |