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