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 / DefaultCodeBuilder.java @ 44198
History | View | Annotate | Download (21.3 KB)
1 | 43512 | jjdelcerro | package org.gvsig.expressionevaluator.impl; |
---|---|---|---|
2 | |||
3 | import java.util.ArrayList; |
||
4 | 44139 | jjdelcerro | import java.util.Collections; |
5 | 43512 | jjdelcerro | import java.util.Iterator; |
6 | import java.util.List; |
||
7 | 43519 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
8 | 43512 | jjdelcerro | import org.gvsig.expressionevaluator.Code; |
9 | import static org.gvsig.expressionevaluator.Code.CALLER; |
||
10 | import static org.gvsig.expressionevaluator.Code.CONSTANT; |
||
11 | 44006 | jjdelcerro | import static org.gvsig.expressionevaluator.Code.IDENTIFIER; |
12 | import static org.gvsig.expressionevaluator.Code.UNDEFINED; |
||
13 | 44198 | jjdelcerro | import static org.gvsig.expressionevaluator.Code.CODES; |
14 | 43512 | jjdelcerro | import org.gvsig.expressionevaluator.Code.Caller; |
15 | import org.gvsig.expressionevaluator.Code.Constant; |
||
16 | 44198 | jjdelcerro | import static org.gvsig.expressionevaluator.Code.EMPTY_FORMATTER; |
17 | 43512 | jjdelcerro | import org.gvsig.expressionevaluator.Code.Identifier; |
18 | 43939 | jjdelcerro | import org.gvsig.expressionevaluator.Code.Method; |
19 | 43512 | jjdelcerro | import org.gvsig.expressionevaluator.CodeBuilder; |
20 | 44139 | jjdelcerro | import org.gvsig.expressionevaluator.Codes; |
21 | 44198 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionBuilder; |
22 | import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
||
23 | import org.gvsig.expressionevaluator.ExpressionUtils; |
||
24 | import org.gvsig.expressionevaluator.Formatter; |
||
25 | 43512 | jjdelcerro | import org.gvsig.expressionevaluator.Function; |
26 | 43521 | jjdelcerro | import org.gvsig.expressionevaluator.Interpreter; |
27 | 44198 | jjdelcerro | import org.gvsig.expressionevaluator.SymbolTable; |
28 | 44139 | jjdelcerro | import org.gvsig.expressionevaluator.impl.function.operator.AddOperator; |
29 | import org.gvsig.expressionevaluator.impl.function.operator.AndOperator; |
||
30 | import org.gvsig.expressionevaluator.impl.function.operator.ConcatOperator; |
||
31 | import org.gvsig.expressionevaluator.impl.function.operator.DivOperator; |
||
32 | import org.gvsig.expressionevaluator.impl.function.operator.EqOperator; |
||
33 | import org.gvsig.expressionevaluator.impl.function.operator.GeOperator; |
||
34 | import org.gvsig.expressionevaluator.impl.function.operator.GtOperator; |
||
35 | import org.gvsig.expressionevaluator.impl.function.operator.IsOperator; |
||
36 | import org.gvsig.expressionevaluator.impl.function.operator.LeOperator; |
||
37 | import org.gvsig.expressionevaluator.impl.function.operator.LtOperator; |
||
38 | import org.gvsig.expressionevaluator.impl.function.operator.ModOperator; |
||
39 | import org.gvsig.expressionevaluator.impl.function.operator.MulOperator; |
||
40 | import org.gvsig.expressionevaluator.impl.function.operator.NeOperator; |
||
41 | import org.gvsig.expressionevaluator.impl.function.operator.NegOperator; |
||
42 | import org.gvsig.expressionevaluator.impl.function.operator.NotOperator; |
||
43 | import org.gvsig.expressionevaluator.impl.function.operator.OrOperator; |
||
44 | import org.gvsig.expressionevaluator.impl.function.operator.RegExpOperator; |
||
45 | import org.gvsig.expressionevaluator.impl.function.operator.SubstOperator; |
||
46 | import org.gvsig.expressionevaluator.impl.function.predicate.IlikeOperator; |
||
47 | import org.gvsig.expressionevaluator.impl.function.predicate.LikeOperator; |
||
48 | import org.gvsig.expressionevaluator.impl.function.programming.GetattrFunction; |
||
49 | import org.gvsig.expressionevaluator.impl.function.programming.GetitemFunction; |
||
50 | 44009 | jjdelcerro | import org.gvsig.fmap.geom.Geometry; |
51 | 43512 | jjdelcerro | import org.gvsig.tools.exception.BaseException; |
52 | import org.gvsig.tools.visitor.Visitor; |
||
53 | |||
54 | 44009 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
55 | 44198 | jjdelcerro | public class DefaultCodeBuilder implements CodeBuilder { |
56 | 43512 | jjdelcerro | |
57 | 44198 | jjdelcerro | public abstract class BaseCode implements Code { |
58 | 43512 | jjdelcerro | |
59 | @Override
|
||
60 | public int code() { |
||
61 | return UNDEFINED;
|
||
62 | } |
||
63 | |||
64 | @Override
|
||
65 | public void accept(Visitor visitor) throws BaseException { |
||
66 | visitor.visit(this);
|
||
67 | } |
||
68 | |||
69 | 44198 | jjdelcerro | @Override
|
70 | public Value toValue(ExpressionBuilder builder) {
|
||
71 | return null; |
||
72 | } |
||
73 | |||
74 | @Override
|
||
75 | public Value toValue() {
|
||
76 | ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
||
77 | return this.toValue(builder); |
||
78 | } |
||
79 | |||
80 | @Override
|
||
81 | public void link(SymbolTable symbolTable) { |
||
82 | if( this.code() == Code.CALLER ) { |
||
83 | Caller caller = (Caller) this;
|
||
84 | if( caller.function() == null ) { |
||
85 | caller.function(symbolTable.function(caller.name())); |
||
86 | } |
||
87 | if( caller.parameters() != null ) { |
||
88 | for( Code arg : caller.parameters() ) {
|
||
89 | arg.link(symbolTable); |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | 43512 | jjdelcerro | } |
95 | |||
96 | class BaseConstant extends BaseCode implements Constant { |
||
97 | |||
98 | private final Object value; |
||
99 | |||
100 | public BaseConstant(Object value) { |
||
101 | this.value = value;
|
||
102 | } |
||
103 | |||
104 | @Override
|
||
105 | public int code() { |
||
106 | return CONSTANT;
|
||
107 | } |
||
108 | |||
109 | @Override
|
||
110 | public Object value() { |
||
111 | return this.value; |
||
112 | } |
||
113 | |||
114 | 43521 | jjdelcerro | @Override
|
115 | 44198 | jjdelcerro | public Value toValue(ExpressionBuilder builder) {
|
116 | return builder.constant(this.value); |
||
117 | } |
||
118 | |||
119 | @Override
|
||
120 | 43521 | jjdelcerro | public String toString() { |
121 | 44198 | jjdelcerro | return this.toString(EMPTY_FORMATTER); |
122 | } |
||
123 | |||
124 | @Override
|
||
125 | public String toString(Formatter<Code> formatter) { |
||
126 | if( formatter.canApply(this) ) { |
||
127 | return formatter.format(this); |
||
128 | } |
||
129 | 43521 | jjdelcerro | StringBuilder builder = new StringBuilder(); |
130 | Object v = this.value(); |
||
131 | 44139 | jjdelcerro | if( v == null ) { |
132 | builder.append("NULL");
|
||
133 | } else if( v instanceof CharSequence ) { |
||
134 | if( StringUtils.isEmpty((CharSequence)v) ) { |
||
135 | builder.append("''");
|
||
136 | } else {
|
||
137 | 44198 | jjdelcerro | v = "'" + StringUtils.replace(v.toString(), "'", "''") + "'"; |
138 | 44139 | jjdelcerro | builder.append(v); |
139 | } |
||
140 | 44009 | jjdelcerro | |
141 | 44139 | jjdelcerro | } else if( v instanceof Boolean ) { |
142 | builder.append(((Boolean)v).toString().toUpperCase());
|
||
143 | |||
144 | 44009 | jjdelcerro | } else if( v instanceof Geometry ) { |
145 | try {
|
||
146 | builder.append("'");
|
||
147 | builder.append(((Geometry) v).convertToWKT()); |
||
148 | builder.append("'::geometry");
|
||
149 | } catch (Exception ex) { |
||
150 | builder.append("'UNKNOW'::geometry");
|
||
151 | } |
||
152 | |||
153 | } else {
|
||
154 | builder.append(v); |
||
155 | 43521 | jjdelcerro | } |
156 | return builder.toString();
|
||
157 | } |
||
158 | |||
159 | 43512 | jjdelcerro | } |
160 | |||
161 | 44154 | jjdelcerro | public interface RecursionControlSupport { |
162 | |||
163 | public boolean enterCode(int max); |
||
164 | |||
165 | public void exitCode(); |
||
166 | |||
167 | public void resetRecursionState(); |
||
168 | } |
||
169 | |||
170 | private class RecursionSupport implements RecursionControlSupport { |
||
171 | |||
172 | private int counter; |
||
173 | |||
174 | public RecursionSupport() {
|
||
175 | this.counter = 0; |
||
176 | } |
||
177 | 43512 | jjdelcerro | |
178 | 44154 | jjdelcerro | @Override
|
179 | public boolean enterCode(int max) { |
||
180 | this.counter += 1; |
||
181 | return this.counter < max; |
||
182 | } |
||
183 | 43512 | jjdelcerro | |
184 | 44154 | jjdelcerro | @Override
|
185 | public void exitCode() { |
||
186 | this.counter -= 1; |
||
187 | } |
||
188 | |||
189 | @Override
|
||
190 | public void resetRecursionState() { |
||
191 | this.counter = 0; |
||
192 | } |
||
193 | |||
194 | } |
||
195 | |||
196 | public class BaseIdentifier extends BaseCode implements Identifier, RecursionControlSupport { |
||
197 | |||
198 | 44198 | jjdelcerro | private final String name; |
199 | private final RecursionSupport recursionSupport; |
||
200 | 44154 | jjdelcerro | |
201 | 43512 | jjdelcerro | public BaseIdentifier(String name) { |
202 | this.name = name;
|
||
203 | 44154 | jjdelcerro | this.recursionSupport = new RecursionSupport(); |
204 | 43512 | jjdelcerro | } |
205 | |||
206 | @Override
|
||
207 | public int code() { |
||
208 | return IDENTIFIER;
|
||
209 | } |
||
210 | |||
211 | @Override
|
||
212 | public String name() { |
||
213 | return this.name; |
||
214 | } |
||
215 | |||
216 | 43521 | jjdelcerro | @Override
|
217 | 44198 | jjdelcerro | public Value toValue(ExpressionBuilder builder) {
|
218 | return builder.variable(this.name); |
||
219 | } |
||
220 | |||
221 | @Override
|
||
222 | 43521 | jjdelcerro | public String toString() { |
223 | 44198 | jjdelcerro | return this.toString(EMPTY_FORMATTER); |
224 | } |
||
225 | |||
226 | @Override
|
||
227 | public String toString(Formatter<Code> formatter) { |
||
228 | if( formatter.canApply(this) ) { |
||
229 | return formatter.format(this); |
||
230 | } |
||
231 | 43521 | jjdelcerro | StringBuilder builder = new StringBuilder(); |
232 | 44006 | jjdelcerro | builder.append("\"");
|
233 | 43521 | jjdelcerro | builder.append(this.name());
|
234 | 44006 | jjdelcerro | builder.append("\"");
|
235 | 43521 | jjdelcerro | return builder.toString();
|
236 | } |
||
237 | |||
238 | 44154 | jjdelcerro | @Override
|
239 | public boolean enterCode(int max) { |
||
240 | return this.recursionSupport.enterCode(max); |
||
241 | } |
||
242 | |||
243 | @Override
|
||
244 | public void exitCode() { |
||
245 | this.recursionSupport.exitCode();
|
||
246 | } |
||
247 | |||
248 | @Override
|
||
249 | public void resetRecursionState() { |
||
250 | this.recursionSupport.resetRecursionState();
|
||
251 | } |
||
252 | |||
253 | 43512 | jjdelcerro | } |
254 | |||
255 | 44139 | jjdelcerro | public class BaseCodes implements Codes { |
256 | 43512 | jjdelcerro | |
257 | 44139 | jjdelcerro | private final List<Code> codes; |
258 | 43512 | jjdelcerro | |
259 | 44139 | jjdelcerro | public BaseCodes() {
|
260 | this.codes = new ArrayList<>(); |
||
261 | 43512 | jjdelcerro | } |
262 | |||
263 | @Override
|
||
264 | 44198 | jjdelcerro | public int code() { |
265 | return CODES;
|
||
266 | } |
||
267 | |||
268 | @Override
|
||
269 | 44139 | jjdelcerro | public int size() { |
270 | if( codes == null ) { |
||
271 | 43512 | jjdelcerro | return 0; |
272 | } |
||
273 | 44139 | jjdelcerro | return this.codes.size(); |
274 | 43512 | jjdelcerro | } |
275 | |||
276 | public void add(Code arg) { |
||
277 | 44139 | jjdelcerro | this.codes.add(arg);
|
278 | 43512 | jjdelcerro | } |
279 | |||
280 | @Override
|
||
281 | public Iterator<Code> iterator() { |
||
282 | 44139 | jjdelcerro | return this.codes.iterator(); |
283 | 43512 | jjdelcerro | } |
284 | |||
285 | @Override
|
||
286 | public Code get(int n) { |
||
287 | 44139 | jjdelcerro | return this.codes.get(n); |
288 | 43512 | jjdelcerro | } |
289 | |||
290 | @Override
|
||
291 | 44139 | jjdelcerro | public boolean isEmpty() { |
292 | return this.codes.isEmpty(); |
||
293 | } |
||
294 | |||
295 | @Override
|
||
296 | public List<Code> toList() { |
||
297 | return Collections.unmodifiableList(this.codes); |
||
298 | } |
||
299 | |||
300 | @Override
|
||
301 | 43512 | jjdelcerro | public void accept(Visitor visitor) throws BaseException { |
302 | 44139 | jjdelcerro | for( Code arg : this.codes ) { |
303 | 44190 | jjdelcerro | arg.accept(visitor); |
304 | 43512 | jjdelcerro | } |
305 | } |
||
306 | |||
307 | 43521 | jjdelcerro | @Override
|
308 | public String toString() { |
||
309 | 44198 | jjdelcerro | return this.toString(EMPTY_FORMATTER); |
310 | } |
||
311 | |||
312 | @Override
|
||
313 | public Value toValue(ExpressionBuilder builder) {
|
||
314 | throw new UnsupportedOperationException(); |
||
315 | } |
||
316 | |||
317 | @Override
|
||
318 | public Value toValue() {
|
||
319 | throw new UnsupportedOperationException(); |
||
320 | } |
||
321 | |||
322 | @Override
|
||
323 | public String toString(Formatter<Code> formatter) { |
||
324 | if( formatter.canApply(this) ) { |
||
325 | return formatter.format(this); |
||
326 | } |
||
327 | 44139 | jjdelcerro | if( codes != null ) { |
328 | 43521 | jjdelcerro | StringBuilder builder = new StringBuilder(); |
329 | boolean skipcoma = true; |
||
330 | 44139 | jjdelcerro | for( Code code : codes ) {
|
331 | if( code == null ) { |
||
332 | continue;
|
||
333 | } |
||
334 | 43521 | jjdelcerro | if( skipcoma ) {
|
335 | skipcoma = false;
|
||
336 | } else {
|
||
337 | builder.append(", ");
|
||
338 | } |
||
339 | 44198 | jjdelcerro | builder.append(code.toString(formatter)); |
340 | 43521 | jjdelcerro | } |
341 | return builder.toString();
|
||
342 | } |
||
343 | return ""; |
||
344 | } |
||
345 | |||
346 | 44198 | jjdelcerro | @Override
|
347 | public void link(SymbolTable symbolTable) { |
||
348 | for (Code code : this.codes) { |
||
349 | code.link(symbolTable); |
||
350 | } |
||
351 | } |
||
352 | |||
353 | 43512 | jjdelcerro | } |
354 | |||
355 | 44154 | jjdelcerro | public class BaseCaller extends BaseCode implements Caller, RecursionControlSupport { |
356 | 43512 | jjdelcerro | |
357 | private final String name; |
||
358 | 44139 | jjdelcerro | private final Codes args; |
359 | 43512 | jjdelcerro | private Function function;
|
360 | private final int type; |
||
361 | 44198 | jjdelcerro | private final RecursionSupport recursionSupport; |
362 | 43512 | jjdelcerro | |
363 | 44139 | jjdelcerro | public BaseCaller(String name, int type, Codes args) { |
364 | 43512 | jjdelcerro | this.name = name;
|
365 | this.args = args;
|
||
366 | this.type = type;
|
||
367 | this.function = null; |
||
368 | 44154 | jjdelcerro | this.recursionSupport = new RecursionSupport(); |
369 | 43512 | jjdelcerro | } |
370 | |||
371 | @Override
|
||
372 | public int code() { |
||
373 | return CALLER;
|
||
374 | } |
||
375 | |||
376 | @Override
|
||
377 | 43521 | jjdelcerro | public Object call(Interpreter interpreter, Object[] args) throws Exception { |
378 | return this.function.call(interpreter, args); |
||
379 | 43512 | jjdelcerro | } |
380 | |||
381 | @Override
|
||
382 | public String name() { |
||
383 | return this.name; |
||
384 | } |
||
385 | |||
386 | @Override
|
||
387 | public Function function() {
|
||
388 | return this.function; |
||
389 | } |
||
390 | |||
391 | @Override
|
||
392 | public Function function(Function function) {
|
||
393 | this.function = function;
|
||
394 | return this.function; |
||
395 | } |
||
396 | |||
397 | @Override
|
||
398 | 44198 | jjdelcerro | public Codes parameters() {
|
399 | 43512 | jjdelcerro | return this.args; |
400 | } |
||
401 | |||
402 | @Override
|
||
403 | public int type() { |
||
404 | return this.type; |
||
405 | } |
||
406 | |||
407 | @Override
|
||
408 | public void accept(Visitor visitor) throws BaseException { |
||
409 | visitor.visit(this);
|
||
410 | 44052 | omartinez | if(this.args!=null) { |
411 | this.args.accept(visitor);
|
||
412 | } |
||
413 | 43512 | jjdelcerro | } |
414 | |||
415 | 43521 | jjdelcerro | @Override
|
416 | 44198 | jjdelcerro | public Value toValue(ExpressionBuilder builder) {
|
417 | switch(this.type) { |
||
418 | case UNARY_OPERATOR:
|
||
419 | return builder.function(
|
||
420 | NegOperator.NAME.equalsIgnoreCase(this.name())?
|
||
421 | "-" :
|
||
422 | this.name(),
|
||
423 | this.parameters().get(0).toValue(builder) |
||
424 | ); |
||
425 | case BINARY_OPERATOR:
|
||
426 | return builder.binaryOperator(
|
||
427 | this.name(),
|
||
428 | this.parameters().get(0).toValue(builder), |
||
429 | this.parameters().get(1).toValue(builder) |
||
430 | ); |
||
431 | case FUNCTION:
|
||
432 | default:
|
||
433 | ExpressionBuilder.Function f = builder.function(this.name());
|
||
434 | if( this.parameters()!=null ) { |
||
435 | for (Code parameter : this.parameters()) { |
||
436 | f.parameter(parameter.toValue(builder)); |
||
437 | } |
||
438 | |||
439 | } |
||
440 | return f;
|
||
441 | |||
442 | } |
||
443 | } |
||
444 | |||
445 | @Override
|
||
446 | 43521 | jjdelcerro | public String toString() { |
447 | 44198 | jjdelcerro | return this.toString(EMPTY_FORMATTER); |
448 | } |
||
449 | |||
450 | @Override
|
||
451 | public String toString(Formatter<Code> formatter) { |
||
452 | if( formatter.canApply(this) ) { |
||
453 | return formatter.format(this); |
||
454 | } |
||
455 | 43521 | jjdelcerro | StringBuilder builder = new StringBuilder(); |
456 | 44198 | jjdelcerro | switch(this.type) { |
457 | case UNARY_OPERATOR:
|
||
458 | if( NegOperator.NAME.equalsIgnoreCase(this.name()) ) { |
||
459 | builder.append("-");
|
||
460 | } else {
|
||
461 | builder.append(this.name());
|
||
462 | } |
||
463 | builder.append("(");
|
||
464 | builder.append(this.parameters().get(0).toString(formatter)); |
||
465 | builder.append(")");
|
||
466 | break;
|
||
467 | case BINARY_OPERATOR:
|
||
468 | builder.append("(");
|
||
469 | builder.append(this.parameters().get(0).toString(formatter)); |
||
470 | builder.append(" ");
|
||
471 | builder.append(this.name());
|
||
472 | builder.append(" ");
|
||
473 | builder.append(this.parameters().get(1).toString(formatter)); |
||
474 | builder.append(")");
|
||
475 | break;
|
||
476 | case FUNCTION:
|
||
477 | default:
|
||
478 | builder.append(this.name());
|
||
479 | builder.append("(");
|
||
480 | if( this.parameters()!=null ) { |
||
481 | builder.append(this.parameters().toString(formatter));
|
||
482 | } |
||
483 | builder.append(")");
|
||
484 | 43521 | jjdelcerro | } |
485 | return builder.toString();
|
||
486 | } |
||
487 | |||
488 | 44154 | jjdelcerro | @Override
|
489 | public boolean enterCode(int max) { |
||
490 | return this.recursionSupport.enterCode(max); |
||
491 | } |
||
492 | |||
493 | @Override
|
||
494 | public void exitCode() { |
||
495 | this.recursionSupport.exitCode();
|
||
496 | } |
||
497 | |||
498 | @Override
|
||
499 | public void resetRecursionState() { |
||
500 | this.recursionSupport.resetRecursionState();
|
||
501 | } |
||
502 | 43512 | jjdelcerro | } |
503 | |||
504 | 43939 | jjdelcerro | public class BaseMethod extends BaseCaller implements Method { |
505 | |||
506 | private final Code obj; |
||
507 | private final String methodname; |
||
508 | |||
509 | 44139 | jjdelcerro | public BaseMethod(Code obj, String methodname, Codes args) { |
510 | 43939 | jjdelcerro | super(methodname, FUNCTION, args);
|
511 | this.obj = obj;
|
||
512 | this.methodname = methodname;
|
||
513 | } |
||
514 | |||
515 | @Override
|
||
516 | public int code() { |
||
517 | return METHOD;
|
||
518 | } |
||
519 | |||
520 | @Override
|
||
521 | public String methodname() { |
||
522 | return this.methodname; |
||
523 | } |
||
524 | |||
525 | @Override
|
||
526 | public Code obj() {
|
||
527 | return this.obj; |
||
528 | } |
||
529 | |||
530 | @Override
|
||
531 | public String toString() { |
||
532 | 44198 | jjdelcerro | return this.toString(EMPTY_FORMATTER); |
533 | } |
||
534 | |||
535 | @Override
|
||
536 | public Value toValue(ExpressionBuilder builder) {
|
||
537 | ExpressionBuilder.Method m = builder.method(this.obj.toValue(builder), this.methodname); |
||
538 | if( this.parameters()!=null ) { |
||
539 | for (Code parameter : this.parameters()) { |
||
540 | m.parameter(parameter.toValue(builder)); |
||
541 | } |
||
542 | } |
||
543 | return m;
|
||
544 | } |
||
545 | |||
546 | @Override
|
||
547 | public String toString(Formatter<Code> formatter) { |
||
548 | if( formatter.canApply(this) ) { |
||
549 | return formatter.format(this); |
||
550 | } |
||
551 | 43939 | jjdelcerro | StringBuilder builder = new StringBuilder(); |
552 | 44198 | jjdelcerro | builder.append(this.obj.toString(formatter));
|
553 | 43939 | jjdelcerro | builder.append("->");
|
554 | builder.append(this.methodname());
|
||
555 | builder.append("(");
|
||
556 | 44198 | jjdelcerro | if( this.parameters()!=null ) { |
557 | builder.append(this.parameters().toString(formatter));
|
||
558 | 43939 | jjdelcerro | } |
559 | builder.append(")");
|
||
560 | return builder.toString();
|
||
561 | } |
||
562 | } |
||
563 | |||
564 | 43512 | jjdelcerro | @Override
|
565 | 43809 | jjdelcerro | public CodeBuilder clone() throws CloneNotSupportedException { |
566 | // This implementation of CodeBuilder does not maintain state, so
|
||
567 | // we only call the super class.
|
||
568 | DefaultCodeBuilder other = (DefaultCodeBuilder) super.clone();
|
||
569 | return other;
|
||
570 | } |
||
571 | |||
572 | @Override
|
||
573 | 43512 | jjdelcerro | public Constant constant(Object value) { |
574 | return new BaseConstant(value); |
||
575 | } |
||
576 | |||
577 | @Override
|
||
578 | public Identifier identifier(String name) { |
||
579 | return new BaseIdentifier(name); |
||
580 | } |
||
581 | |||
582 | @Override
|
||
583 | 44139 | jjdelcerro | public BaseCodes args() {
|
584 | return new BaseCodes(); |
||
585 | 43512 | jjdelcerro | } |
586 | |||
587 | @Override
|
||
588 | 44139 | jjdelcerro | public Caller function(String name, int type, Codes args) { |
589 | 43512 | jjdelcerro | return new BaseCaller(name, type, args); |
590 | } |
||
591 | |||
592 | @Override
|
||
593 | 44139 | jjdelcerro | public Caller function(String name, Codes args) { |
594 | 43512 | jjdelcerro | return function(name, Caller.FUNCTION, args);
|
595 | } |
||
596 | 43939 | jjdelcerro | |
597 | 43512 | jjdelcerro | @Override
|
598 | 44139 | jjdelcerro | public Code method(Code obj, String methodname, Codes methodargs) { |
599 | 43939 | jjdelcerro | Method m = new BaseMethod(obj, methodname, methodargs); |
600 | return m;
|
||
601 | } |
||
602 | |||
603 | @Override
|
||
604 | 43512 | jjdelcerro | public Caller operator(String name, Code arg1) { |
605 | 44139 | jjdelcerro | BaseCodes args = args(); |
606 | args.add(arg1); |
||
607 | 43512 | jjdelcerro | return function(name, Caller.UNARY_OPERATOR, args);
|
608 | } |
||
609 | |||
610 | @Override
|
||
611 | public Caller operator(String name, Code arg1, Code arg2) { |
||
612 | 44139 | jjdelcerro | BaseCodes args = args(); |
613 | args.add(arg1); |
||
614 | args.add(arg2); |
||
615 | 43512 | jjdelcerro | return function(name, Caller.BINARY_OPERATOR, args);
|
616 | } |
||
617 | 44198 | jjdelcerro | |
618 | 43512 | jjdelcerro | @Override
|
619 | public Code not(Code op1) {
|
||
620 | 44139 | jjdelcerro | return operator(NotOperator.NAME, op1);
|
621 | 43512 | jjdelcerro | } |
622 | |||
623 | @Override
|
||
624 | 44098 | jjdelcerro | public Code negate(Code op1) {
|
625 | 44139 | jjdelcerro | return operator(NegOperator.NAME, op1);
|
626 | 44098 | jjdelcerro | } |
627 | |||
628 | @Override
|
||
629 | 44139 | jjdelcerro | public Code concat(Code op1, Code op2) {
|
630 | return operator(ConcatOperator.NAME, op1, op2);
|
||
631 | } |
||
632 | |||
633 | @Override
|
||
634 | 43512 | jjdelcerro | public Code add(Code op1, Code op2) {
|
635 | 44139 | jjdelcerro | return operator(AddOperator.NAME, op1, op2);
|
636 | 43512 | jjdelcerro | } |
637 | |||
638 | @Override
|
||
639 | public Code subst(Code op1, Code op2) {
|
||
640 | 44139 | jjdelcerro | return operator(SubstOperator.NAME, op1, op2);
|
641 | 43512 | jjdelcerro | } |
642 | |||
643 | @Override
|
||
644 | public Code mult(Code op1, Code op2) {
|
||
645 | 44139 | jjdelcerro | return operator(MulOperator.NAME, op1, op2);
|
646 | 43512 | jjdelcerro | } |
647 | |||
648 | @Override
|
||
649 | public Code div(Code op1, Code op2) {
|
||
650 | 44139 | jjdelcerro | return operator(DivOperator.NAME, op1, op2);
|
651 | 43512 | jjdelcerro | } |
652 | |||
653 | @Override
|
||
654 | public Code mod(Code op1, Code op2) {
|
||
655 | 44139 | jjdelcerro | return operator(ModOperator.NAME, op1, op2);
|
656 | 43512 | jjdelcerro | } |
657 | |||
658 | @Override
|
||
659 | public Code or(Code op1, Code op2) {
|
||
660 | 44139 | jjdelcerro | return operator(OrOperator.NAME, op1, op2);
|
661 | 43512 | jjdelcerro | } |
662 | |||
663 | @Override
|
||
664 | public Code and(Code op1, Code op2) {
|
||
665 | 44139 | jjdelcerro | return operator(AndOperator.NAME, op1, op2);
|
666 | 43512 | jjdelcerro | } |
667 | |||
668 | @Override
|
||
669 | public Code like(Code op1, Code op2) {
|
||
670 | 44139 | jjdelcerro | return operator(LikeOperator.NAME, op1, op2);
|
671 | 43512 | jjdelcerro | } |
672 | |||
673 | @Override
|
||
674 | public Code ilike(Code op1, Code op2) {
|
||
675 | 44139 | jjdelcerro | return operator(IlikeOperator.NAME, op1, op2);
|
676 | 43512 | jjdelcerro | } |
677 | |||
678 | @Override
|
||
679 | 43532 | jjdelcerro | public Code regexp(Code op1, Code op2) {
|
680 | 44139 | jjdelcerro | return operator(RegExpOperator.NAME, op1, op2);
|
681 | 43532 | jjdelcerro | } |
682 | |||
683 | @Override
|
||
684 | 43512 | jjdelcerro | public Code lt(Code op1, Code op2) {
|
685 | 44139 | jjdelcerro | return operator(LtOperator.NAME, op1, op2);
|
686 | 43512 | jjdelcerro | } |
687 | |||
688 | @Override
|
||
689 | public Code gt(Code op1, Code op2) {
|
||
690 | 44139 | jjdelcerro | return operator(GtOperator.NAME, op1, op2);
|
691 | 43512 | jjdelcerro | } |
692 | |||
693 | @Override
|
||
694 | public Code le(Code op1, Code op2) {
|
||
695 | 44139 | jjdelcerro | return operator(LeOperator.NAME, op1, op2);
|
696 | 43512 | jjdelcerro | } |
697 | |||
698 | @Override
|
||
699 | public Code ge(Code op1, Code op2) {
|
||
700 | 44139 | jjdelcerro | return operator(GeOperator.NAME, op1, op2);
|
701 | 43512 | jjdelcerro | } |
702 | |||
703 | @Override
|
||
704 | public Code eq(Code op1, Code op2) {
|
||
705 | 44139 | jjdelcerro | return operator(EqOperator.NAME, op1, op2);
|
706 | 43512 | jjdelcerro | } |
707 | |||
708 | @Override
|
||
709 | public Code ne(Code op1, Code op2) {
|
||
710 | 44139 | jjdelcerro | return operator(NeOperator.NAME, op1, op2);
|
711 | 43512 | jjdelcerro | } |
712 | |||
713 | @Override
|
||
714 | public Code is(Code op1, Code op2) {
|
||
715 | 44139 | jjdelcerro | return operator(IsOperator.NAME, op1, op2);
|
716 | 43512 | jjdelcerro | } |
717 | |||
718 | 43939 | jjdelcerro | @Override
|
719 | public Code getattr(Code obj, String attrname) { |
||
720 | 44139 | jjdelcerro | BaseCodes args = args(); |
721 | args.add(obj); |
||
722 | args.add(constant(attrname)); |
||
723 | return function(GetattrFunction.NAME, args);
|
||
724 | 43939 | jjdelcerro | } |
725 | |||
726 | 44139 | jjdelcerro | @Override
|
727 | public Code getitem(Code obj, Code index) {
|
||
728 | BaseCodes args = args(); |
||
729 | args.add(obj); |
||
730 | args.add(index); |
||
731 | return function(GetitemFunction.NAME, args);
|
||
732 | } |
||
733 | |||
734 | |||
735 | 44198 | jjdelcerro | |
736 | 43512 | jjdelcerro | } |