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 / grammars / BasicGrammarFactory.java @ 47077
History | View | Annotate | Download (28 KB)
1 |
package org.gvsig.expressionevaluator.impl.grammars; |
---|---|
2 |
|
3 |
import org.apache.commons.lang3.StringUtils; |
4 |
import org.gvsig.expressionevaluator.Code; |
5 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_CAST; |
6 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_DATE; |
7 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_EXTRACT; |
8 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_LIST; |
9 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_TIME; |
10 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_TIMESTAMP; |
11 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_REGEXP; |
12 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
13 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
14 |
import org.gvsig.expressionevaluator.Grammar; |
15 |
import org.gvsig.expressionevaluator.Statement; |
16 |
import org.gvsig.expressionevaluator.Statement.StatementContext; |
17 |
import org.gvsig.expressionevaluator.impl.DefaultCodeBuilder.BaseCodes; |
18 |
import org.gvsig.expressionevaluator.impl.DefaultStatement.StatementBuilderBase; |
19 |
import org.gvsig.expressionevaluator.impl.DefaultStatement.ArgsBuilderFromNames; |
20 |
import org.gvsig.expressionevaluator.impl.function.programming.CaseFunction; |
21 |
import org.gvsig.expressionevaluator.impl.function.programming.CodeBlockFunction; |
22 |
import org.gvsig.expressionevaluator.impl.function.programming.CodeBlockWithExceptFunction; |
23 |
import org.gvsig.expressionevaluator.impl.function.programming.CreateFnFunction; |
24 |
import org.gvsig.expressionevaluator.impl.function.programming.ForEachFunction; |
25 |
import org.gvsig.expressionevaluator.impl.function.programming.ReturnFunction; |
26 |
import org.gvsig.expressionevaluator.impl.function.programming.WhileFunction; |
27 |
import org.gvsig.expressionevaluator.spi.AbstractGrammarFactory; |
28 |
import org.gvsig.expressionevaluator.Code.Callable; |
29 |
import org.gvsig.expressionevaluator.Codes; |
30 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
31 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_DICT; |
32 |
import org.gvsig.expressionevaluator.impl.function.programming.BreakFunction; |
33 |
import org.gvsig.expressionevaluator.impl.function.programming.ImportFunction; |
34 |
|
35 |
/**
|
36 |
*
|
37 |
* @author jjdelcerro
|
38 |
*/
|
39 |
public class BasicGrammarFactory extends AbstractGrammarFactory { |
40 |
|
41 |
private Grammar grammar;
|
42 |
|
43 |
public static class DictArgsBuilder implements Statement.ArgsBuilder { |
44 |
|
45 |
protected final String keyName; |
46 |
protected final String valueName; |
47 |
|
48 |
public DictArgsBuilder(String keyName, String valueName) { |
49 |
this.keyName = keyName;
|
50 |
this.valueName = valueName;
|
51 |
} |
52 |
|
53 |
@Override
|
54 |
public String toString() { |
55 |
return "dict_args(" + this.keyName + "," + this.valueName + ")"; |
56 |
} |
57 |
|
58 |
@Override
|
59 |
public Codes build(StatementContext context) {
|
60 |
context.trace(this.toString() + ".build"); |
61 |
BaseCodes args = (BaseCodes) context.getCodeBuilder().args(); |
62 |
if (keyName.contains("#")) { |
63 |
int n = 1; |
64 |
while (true) { |
65 |
String keyNameX = StringUtils.replace(keyName, "#", String.valueOf(n).trim(), 1); |
66 |
String valueNameX = StringUtils.replace(valueName, "#", String.valueOf(n).trim(), 1); |
67 |
Code keyCodeX = context.getCode(keyNameX); |
68 |
if (keyCodeX == null) { |
69 |
break;
|
70 |
} |
71 |
Code valueCodeX = context.getCode(valueNameX); |
72 |
args.add(keyCodeX); |
73 |
args.add(valueCodeX); |
74 |
n++; |
75 |
} |
76 |
} else {
|
77 |
args.add(context.getCodeBuilder().constant(keyName)); |
78 |
args.add(context.getCode(valueName)); |
79 |
} |
80 |
return args;
|
81 |
} |
82 |
} |
83 |
|
84 |
private class BeginStatementBuilder extends StatementBuilderBase { |
85 |
|
86 |
private final String blockID; |
87 |
private final String exceptID; |
88 |
private final String declareID; |
89 |
|
90 |
public BeginStatementBuilder(String blockID, String exceptID, String declareID) { |
91 |
super(CodeBlockFunction.NAME, new ArgsBuilderFromNames(blockID)); |
92 |
this.blockID = blockID;
|
93 |
this.exceptID = exceptID;
|
94 |
this.declareID = declareID;
|
95 |
} |
96 |
|
97 |
@Override
|
98 |
public Code build(StatementContext context) {
|
99 |
Callable code = (Callable) super.build(context); |
100 |
Code exceptCode = context.getCode(this.exceptID);
|
101 |
Code declareCode = context.getCode(this.declareID);
|
102 |
if (exceptCode == null && declareCode == null ) { |
103 |
return code;
|
104 |
} |
105 |
BaseCodes args = (BaseCodes) code.parameters(); |
106 |
args.add(exceptCode); |
107 |
args.add(declareCode); |
108 |
code = context.getCodeBuilder().function(CodeBlockWithExceptFunction.NAME, args); |
109 |
return code;
|
110 |
} |
111 |
|
112 |
} |
113 |
|
114 |
public static class VarArgsBuilder implements Statement.ArgsBuilder { |
115 |
|
116 |
protected final String varName; |
117 |
protected final String valueName; |
118 |
|
119 |
public VarArgsBuilder(String varName, String valueName) { |
120 |
this.varName = varName;
|
121 |
this.valueName = valueName;
|
122 |
} |
123 |
|
124 |
@Override
|
125 |
public String toString() { |
126 |
return "var_args("+varName+","+valueName+")"; |
127 |
} |
128 |
|
129 |
@Override
|
130 |
public Codes build(StatementContext context) {
|
131 |
context.trace(this.toString()+".build"); |
132 |
BaseCodes args = (BaseCodes) context.getCodeBuilder().args(); |
133 |
args.add(context.getCodeBuilder().identifier((String) ((Code.Constant)context.getCode(varName)).value()));
|
134 |
args.add(context.getCode(valueName)); |
135 |
return args;
|
136 |
} |
137 |
} |
138 |
|
139 |
|
140 |
|
141 |
public BasicGrammarFactory() {
|
142 |
super("Basic", true); |
143 |
} |
144 |
|
145 |
@Override
|
146 |
public Grammar create(Object... parameters) { |
147 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
148 |
if (this.grammar == null) { |
149 |
Grammar theGrammar = manager.createGrammar(this.getName());
|
150 |
|
151 |
theGrammar.addStatement(this.createArrayStatement(theGrammar));
|
152 |
theGrammar.addStatement(this.createBlockStatement(theGrammar));
|
153 |
theGrammar.addStatement(this.createCallStatement(theGrammar));
|
154 |
theGrammar.addStatement(this.createCaseStatement(theGrammar));
|
155 |
theGrammar.addStatement(this.createCastStatement(theGrammar));
|
156 |
theGrammar.addStatement(this.createDateStatement(theGrammar));
|
157 |
theGrammar.addStatement(this.createDictStatement(theGrammar));
|
158 |
theGrammar.addStatement(this.createExtractStatement(theGrammar));
|
159 |
theGrammar.addStatement(this.createForeachStatement(theGrammar));
|
160 |
theGrammar.addStatement(this.createFunctionStatement(theGrammar));
|
161 |
theGrammar.addStatement(this.createIfStatement(theGrammar));
|
162 |
theGrammar.addStatement(this.createLambdaStatement(theGrammar));
|
163 |
theGrammar.addStatement(this.createLet1Statement(theGrammar));
|
164 |
theGrammar.addStatement(this.createLet2Statement(theGrammar));
|
165 |
theGrammar.addStatement(this.createSetattr1Statement(theGrammar));
|
166 |
theGrammar.addStatement(this.createSetattr2Statement(theGrammar));
|
167 |
theGrammar.addStatement(this.createVarStatement(theGrammar));
|
168 |
theGrammar.addStatement(this.createListStatement(theGrammar));
|
169 |
theGrammar.addStatement(this.createMatchStatement(theGrammar));
|
170 |
theGrammar.addStatement(this.createPassStatement(theGrammar));
|
171 |
theGrammar.addStatement(this.createReturnStatement(theGrammar));
|
172 |
theGrammar.addStatement(this.createTimeStatement(theGrammar));
|
173 |
theGrammar.addStatement(this.createTimestampStatement(theGrammar));
|
174 |
theGrammar.addStatement(this.createWhileStatement(theGrammar));
|
175 |
theGrammar.addStatement(this.create$ConstantStatement(theGrammar)); |
176 |
theGrammar.addStatement(this.createBreakStatement(theGrammar));
|
177 |
theGrammar.addStatement(this.createImportStatement(theGrammar));
|
178 |
this.grammar = theGrammar;
|
179 |
} |
180 |
return grammar;
|
181 |
} |
182 |
|
183 |
private Statement createBlockStatement(Grammar theGrammar) { |
184 |
Statement stmt;
|
185 |
|
186 |
theGrammar.addReservedWord("BEGIN");
|
187 |
theGrammar.addReservedWord("EXCEPT");
|
188 |
theGrammar.addReservedWord("END");
|
189 |
|
190 |
stmt = theGrammar.createStatement("BLOCK");
|
191 |
stmt.addRule(stmt.optional_any_token("DECLARE")
|
192 |
.addRuleOnTrue(stmt.require_expressions(";","BEGIN").capture_as("DECLARE")) |
193 |
); |
194 |
stmt.addRule(stmt.require_any_token("BEGIN"));
|
195 |
stmt.addRule(stmt.require_expressions(";").capture_as("VALUES")); |
196 |
stmt.addRule(stmt.optional_any_token("EXCEPT")
|
197 |
.addRuleOnTrue(stmt.require_expressions(";").capture_as("EXCEPT")) |
198 |
); |
199 |
stmt.addRule(stmt.require_any_token("END"));
|
200 |
stmt.builder(new BeginStatementBuilder("VALUES", "EXCEPT","DECLARE")); |
201 |
return stmt;
|
202 |
} |
203 |
|
204 |
private Statement create$ConstantStatement(Grammar theGrammar) { |
205 |
Statement stmt;
|
206 |
|
207 |
stmt = theGrammar.createStatement("$Constant");
|
208 |
stmt.addRule(stmt.require_any_token("<%"));
|
209 |
stmt.addRule(stmt.require_any_token("="));
|
210 |
stmt.addRule(stmt.require_expression().capture_as("VALUE"));
|
211 |
stmt.addRule(stmt.require_any_token("%>"));
|
212 |
stmt.code( |
213 |
ExpressionBuilder.FUNCTION_$HOSTEXPRESSION,
|
214 |
stmt.args_names("VALUE")
|
215 |
); |
216 |
return stmt;
|
217 |
} |
218 |
|
219 |
private Statement createIfStatement(Grammar theGrammar) { |
220 |
Statement stmt;
|
221 |
|
222 |
theGrammar.addReservedWord("IF");
|
223 |
theGrammar.addReservedWord("THEN");
|
224 |
theGrammar.addReservedWord("ELSE");
|
225 |
theGrammar.addReservedWord("END");
|
226 |
|
227 |
stmt = theGrammar.createStatement("IF");
|
228 |
stmt.addRule(stmt.require_any_token("IF"));
|
229 |
stmt.addRule(stmt.require_expression().capture_as("CONDITION"));
|
230 |
stmt.addRule(stmt.require_any_token("THEN", ":")); |
231 |
stmt.addRule(stmt.require_expressions(";").capture_as("ONTRUE")); |
232 |
stmt.addRule(stmt.optional_any_token("ELSE")
|
233 |
.addRuleOnTrue(stmt.optional_any_token(":"))
|
234 |
.addRuleOnTrue(stmt.require_expressions(";").capture_as("ONFALSE")) |
235 |
); |
236 |
stmt.addRule(stmt.require_any_token("END"));
|
237 |
stmt.addRule(stmt.optional_any_token("IF"));
|
238 |
stmt.code( |
239 |
ExpressionBuilder.FUNCTION_IF, |
240 |
stmt.args_names("CONDITION", "ONTRUE", "ONFALSE") |
241 |
); |
242 |
return stmt;
|
243 |
} |
244 |
|
245 |
private Statement createWhileStatement(Grammar theGrammar) { |
246 |
Statement stmt;
|
247 |
|
248 |
theGrammar.addReservedWord("WHILE");
|
249 |
theGrammar.addReservedWord("BEGIN");
|
250 |
theGrammar.addReservedWord("END");
|
251 |
theGrammar.addReservedWord("LOOP");
|
252 |
|
253 |
stmt = theGrammar.createStatement("WHILE");
|
254 |
stmt.addRule(stmt.require_any_token("WHILE"));
|
255 |
stmt.addRule(stmt.require_expression().capture_as("CONDITION"));
|
256 |
stmt.addRule(stmt.require_any_token("BEGIN", ":", "LOOP")); |
257 |
stmt.addRule(stmt.require_expressions(";").capture_as("BODY")); |
258 |
stmt.addRule(stmt.require_any_token("END"));
|
259 |
stmt.addRule(stmt.optional_any_token("WHILE", "LOOP")); |
260 |
stmt.code( |
261 |
WhileFunction.NAME, |
262 |
stmt.args_names("CONDITION", "BODY") |
263 |
); |
264 |
return stmt;
|
265 |
} |
266 |
|
267 |
private Statement createForeachStatement(Grammar theGrammar) { |
268 |
Statement stmt;
|
269 |
|
270 |
theGrammar.addReservedWord("FOR");
|
271 |
theGrammar.addReservedWord("IN");
|
272 |
// theGrammar.addReservedWord("BEGIN");
|
273 |
theGrammar.addReservedWord("END");
|
274 |
theGrammar.addReservedWord("LOOP");
|
275 |
|
276 |
stmt = theGrammar.createStatement("FOREACH");
|
277 |
stmt.addRule(stmt.require_any_token("FOR", "FOREACH")); |
278 |
stmt.addRule(stmt.require_identifier().capture_as("VAR"));
|
279 |
stmt.addRule(stmt.require_any_token("IN"));
|
280 |
stmt.addRule(stmt.require_expression().capture_as("ITERABLE"));
|
281 |
stmt.addRule(stmt.require_any_token("LOOP"));
|
282 |
stmt.addRule(stmt.require_expressions(";").capture_as("BODY")); |
283 |
stmt.addRule(stmt.require_any_token("END"));
|
284 |
stmt.addRule(stmt.optional_any_token("LOOP"));
|
285 |
stmt.code( |
286 |
ForEachFunction.NAME, |
287 |
stmt.args_names("VAR", "ITERABLE", "BODY") |
288 |
); |
289 |
return stmt;
|
290 |
} |
291 |
|
292 |
private Statement createCaseStatement(Grammar theGrammar) { |
293 |
Statement stmt;
|
294 |
|
295 |
theGrammar.addReservedWord("CASE");
|
296 |
theGrammar.addReservedWord("WHEN");
|
297 |
theGrammar.addReservedWord("THEN");
|
298 |
theGrammar.addReservedWord("ELSE");
|
299 |
theGrammar.addReservedWord("END");
|
300 |
|
301 |
stmt = theGrammar.createStatement("CASE");
|
302 |
stmt.addRule(stmt.require_any_token("CASE"));
|
303 |
stmt.addRule( |
304 |
stmt.repeat_until_any_tokens("END", "ELSE") |
305 |
.addRule(stmt.require_any_token("WHEN"))
|
306 |
.addRule(stmt.require_expression().capture_as("CONDITION#"))
|
307 |
.addRule(stmt.require_any_token("THEN", ":")) |
308 |
.addRule(stmt.require_expressions(";").capture_as("ONTRUE#")) |
309 |
); |
310 |
stmt.addRule(stmt.optional_any_token("ELSE")
|
311 |
.addRuleOnTrue(stmt.optional_any_token(":"))
|
312 |
.addRuleOnTrue(stmt.require_expressions(";").capture_as("ELSE")) |
313 |
); |
314 |
stmt.addRule(stmt.require_any_token("END"));
|
315 |
stmt.addRule(stmt.optional_any_token("CASE"));
|
316 |
stmt.code( |
317 |
CaseFunction.NAME, |
318 |
stmt.args_names("CONDITION#", "ONTRUE#", "ELSE") |
319 |
); |
320 |
return stmt;
|
321 |
} |
322 |
|
323 |
private Statement createFunctionStatement(Grammar theGrammar) { |
324 |
Statement stmt;
|
325 |
|
326 |
theGrammar.addReservedWord("CREATE");
|
327 |
theGrammar.addReservedWord("FUNCTION");
|
328 |
theGrammar.addReservedWord("PROCEDURE");
|
329 |
theGrammar.addReservedWord("AS");
|
330 |
theGrammar.addReservedWord("IS");
|
331 |
theGrammar.addReservedWord("BEGIN");
|
332 |
theGrammar.addReservedWord("END");
|
333 |
theGrammar.addReservedWord("$$"); // ???? Asi van las funciones en PostgreSQL |
334 |
|
335 |
stmt = theGrammar.createStatement("USERFUNCTION");
|
336 |
stmt.addRule(stmt.require_any_token("CREATE"));
|
337 |
stmt.addRule(stmt.optional_any_token("OR")
|
338 |
.addRuleOnTrue(stmt.optional_any_token("REPLACE"))
|
339 |
); |
340 |
stmt.addRule(stmt.optional_any_token("FUNCTION", "PROCEDURE")); |
341 |
stmt.addRule(stmt.require_identifier().capture_as("FUNCTION_NAME"));
|
342 |
stmt.addRule(stmt.optional_any_token("("));
|
343 |
stmt.addRule(stmt.optional_identifiers(",").capture_as("PARAMETERS")); |
344 |
stmt.addRule(stmt.optional_any_token(")"));
|
345 |
stmt.addRule(stmt.require_any_token("AS","IS")); |
346 |
stmt.addRule(stmt.optional_any_token("$$"));
|
347 |
stmt.addRule(stmt.optional_any_token("DECLARE")
|
348 |
.addRuleOnTrue(stmt.require_expressions(";","BEGIN").capture_as("DECLARE")) |
349 |
); |
350 |
stmt.addRule(stmt.optional_any_token("BEGIN")
|
351 |
.addRuleOnTrue(stmt.require_expressions(";").capture_as("BODY")) |
352 |
.addRuleOnTrue(stmt.optional_any_token("EXCEPT")
|
353 |
.addRuleOnTrue(stmt.require_expressions(";").capture_as("EXCEPT")) |
354 |
) |
355 |
.addRuleOnTrue(stmt.require_any_token("END"))
|
356 |
.addRuleOnTrue(stmt.optional_any_token("$$"))
|
357 |
.addRuleOnTrue(stmt.optional_any_token("FUNCTION", "PROCEDURE")) |
358 |
.addRuleOnFalse(stmt.optional_literal_string().capture_as("SCRIPT_PATH")
|
359 |
.addRuleOnTrue(stmt.require_any_token(","))
|
360 |
.addRuleOnTrue(stmt.require_literal_string().capture_as("SCRIPT_FUNCTION"))
|
361 |
.addRuleOnTrue(stmt.optional_any_token("LANGUAGE")
|
362 |
.addRuleOnTrue(stmt.require_literal_string().capture_as("LANGUAGE"))
|
363 |
) |
364 |
) |
365 |
); |
366 |
stmt.code( |
367 |
CreateFnFunction.NAME, |
368 |
stmt.fixed_args_names("FUNCTION_NAME", "PARAMETERS", "BODY", "SCRIPT_PATH", "SCRIPT_FUNCTION", "LANGUAGE","EXCEPT","DECLARE") |
369 |
); |
370 |
return stmt;
|
371 |
} |
372 |
|
373 |
private Statement createLambdaStatement(Grammar theGrammar) { |
374 |
Statement stmt;
|
375 |
|
376 |
theGrammar.addReservedWord("LAMBDA");
|
377 |
|
378 |
stmt = theGrammar.createStatement("LAMBDA");
|
379 |
stmt.addRule(stmt.require_any_token("LAMBDA"));
|
380 |
stmt.addRule(stmt.optional_identifiers(",").capture_as("PARAMETERS")); |
381 |
stmt.addRule(stmt.require_any_token(":"));
|
382 |
stmt.addRule(stmt.require_expression().capture_as("BODY"));
|
383 |
stmt.code( |
384 |
CreateFnFunction.NAME, |
385 |
stmt.fixed_args_names("PARAMETERS", "BODY") |
386 |
); |
387 |
return stmt;
|
388 |
} |
389 |
|
390 |
private Statement createReturnStatement(Grammar theGrammar) { |
391 |
Statement stmt;
|
392 |
|
393 |
theGrammar.addReservedWord("RETURN");
|
394 |
|
395 |
stmt = theGrammar.createStatement("RETURN");
|
396 |
stmt.addRule(stmt.require_any_token("RETURN"));
|
397 |
stmt.addRule(stmt.optional_expression().capture_as("VALUES"));
|
398 |
stmt.code( |
399 |
ReturnFunction.NAME, |
400 |
stmt.args_expand("VALUES")
|
401 |
); |
402 |
return stmt;
|
403 |
} |
404 |
|
405 |
private Statement createBreakStatement(Grammar theGrammar) { |
406 |
Statement stmt;
|
407 |
|
408 |
theGrammar.addReservedWord("BREAK");
|
409 |
|
410 |
stmt = theGrammar.createStatement("BREAK");
|
411 |
stmt.addRule(stmt.require_any_token("BREAK"));
|
412 |
stmt.code( |
413 |
BreakFunction.NAME, |
414 |
stmt.args_expand() |
415 |
); |
416 |
return stmt;
|
417 |
} |
418 |
|
419 |
private Statement createVarStatement(Grammar theGrammar) { |
420 |
Statement stmt;
|
421 |
|
422 |
theGrammar.addReservedWord("VAR");
|
423 |
|
424 |
stmt = theGrammar.createStatement("VAR");
|
425 |
stmt.addRule(stmt.require_any_token("VAR"));
|
426 |
stmt.addRule(stmt.require_identifier().capture_as("NAME"));
|
427 |
stmt.addRule(stmt.optional_any_token("DEFAULT")
|
428 |
.addRuleOnTrue(stmt.require_expression().capture_as("VALUE"))
|
429 |
.addRuleOnFalse(stmt.set_expression("VALUE", null)) |
430 |
); |
431 |
stmt.code( |
432 |
"VAR",
|
433 |
new VarArgsBuilder("NAME", "VALUE") |
434 |
); |
435 |
return stmt;
|
436 |
} |
437 |
|
438 |
private Statement createLet1Statement(Grammar theGrammar) { |
439 |
Statement stmt;
|
440 |
|
441 |
theGrammar.addReservedWord("LET");
|
442 |
theGrammar.addReservedWord("SET");
|
443 |
theGrammar.addReservedWord("TO");
|
444 |
|
445 |
stmt = theGrammar.createStatement("LET");
|
446 |
stmt.addRule(stmt.require_any_token("LET", "SET")); |
447 |
stmt.addRule(stmt.require_identifier().capture_as("NAME"));
|
448 |
stmt.addRule(stmt.optional_any_token("=", "TO")); |
449 |
stmt.addRule(stmt.require_expression().capture_as("VALUE"));
|
450 |
stmt.code( |
451 |
ExpressionBuilder.FUNCTION_LET, |
452 |
stmt.args_names("NAME", "VALUE") |
453 |
); |
454 |
return stmt;
|
455 |
} |
456 |
|
457 |
private Statement createLet2Statement(Grammar theGrammar) { |
458 |
Statement stmt;
|
459 |
|
460 |
stmt = theGrammar.createStatement("LET2");
|
461 |
stmt.addRule(stmt.require_identifier().capture_as("NAME"));
|
462 |
stmt.addRule(stmt.require_any_token(":="));
|
463 |
stmt.addRule(stmt.require_expression().capture_as("VALUE"));
|
464 |
stmt.code( |
465 |
ExpressionBuilder.FUNCTION_LET, |
466 |
stmt.args_names("NAME", "VALUE") |
467 |
); |
468 |
return stmt;
|
469 |
} |
470 |
|
471 |
private Statement createSetattr1Statement(Grammar theGrammar) { |
472 |
Statement stmt;
|
473 |
|
474 |
stmt = theGrammar.createStatement("SETATTR");
|
475 |
stmt.addRule(stmt.require_identifier().capture_as("NAME"));
|
476 |
stmt.addRule(stmt.require_any_token("["));
|
477 |
stmt.addRule(stmt.require_expression().capture_as("ATTRNAME"));
|
478 |
stmt.addRule(stmt.require_any_token("]"));
|
479 |
stmt.addRule(stmt.require_any_token(":="));
|
480 |
stmt.addRule(stmt.require_expression().capture_as("VALUE"));
|
481 |
stmt.code( |
482 |
ExpressionBuilder.FUNCTION_SETATTR, |
483 |
stmt.args_names("NAME", "ATTRNAME", "VALUE") |
484 |
); |
485 |
return stmt;
|
486 |
} |
487 |
|
488 |
private Statement createSetattr2Statement(Grammar theGrammar) { |
489 |
Statement stmt;
|
490 |
|
491 |
stmt = theGrammar.createStatement("SETATTR");
|
492 |
stmt.addRule(stmt.require_identifier().capture_as("NAME"));
|
493 |
stmt.addRule(stmt.require_any_token("."));
|
494 |
stmt.addRule(stmt.require_identifier().capture_as("ATTRNAME"));
|
495 |
stmt.addRule(stmt.require_any_token(":="));
|
496 |
stmt.addRule(stmt.require_expression().capture_as("VALUE"));
|
497 |
stmt.code( |
498 |
ExpressionBuilder.FUNCTION_SETATTR, |
499 |
stmt.args_names("NAME", "ATTRNAME", "VALUE") |
500 |
); |
501 |
return stmt;
|
502 |
} |
503 |
|
504 |
private Statement createArrayStatement(Grammar theGrammar) { |
505 |
Statement stmt;
|
506 |
|
507 |
theGrammar.addReservedWord("ARRAY");
|
508 |
|
509 |
stmt = theGrammar.createStatement("ARRAY");
|
510 |
stmt.addRule(stmt.require_any_token("ARRAY"));
|
511 |
stmt.addRule(stmt.require_any_token("["));
|
512 |
stmt.addRule(stmt.require_expressions(",").capture_as("VALUES")); |
513 |
stmt.addRule(stmt.require_any_token("]"));
|
514 |
stmt.code( |
515 |
FUNCTION_LIST, |
516 |
stmt.args_expand("VALUES")
|
517 |
); |
518 |
return stmt;
|
519 |
} |
520 |
|
521 |
private Statement createCastStatement(Grammar theGrammar) { |
522 |
Statement stmt;
|
523 |
|
524 |
theGrammar.addReservedWord("CAST");
|
525 |
theGrammar.addReservedWord("AS");
|
526 |
|
527 |
stmt = theGrammar.createStatement("CAST");
|
528 |
stmt.addRule(stmt.require_any_token("CAST"));
|
529 |
stmt.addRule(stmt.optional_any_token("(")
|
530 |
.addRuleOnTrue(stmt.require_expression(false).capture_as("VALUE")) |
531 |
.addRuleOnTrue(stmt.require_any_token("AS"))
|
532 |
.addRuleOnTrue(stmt.require_identifier().capture_as("TYPE"))
|
533 |
.addRuleOnTrue(stmt.require_any_token(")"))
|
534 |
.addRuleOnFalse(stmt.require_expression(false).capture_as("VALUE")) |
535 |
.addRuleOnFalse(stmt.require_any_token("AS"))
|
536 |
.addRuleOnFalse(stmt.require_identifier().capture_as("TYPE"))
|
537 |
); |
538 |
stmt.code( |
539 |
FUNCTION_CAST, |
540 |
stmt.args_names("VALUE", "TYPE") |
541 |
); |
542 |
return stmt;
|
543 |
} |
544 |
|
545 |
private Statement createMatchStatement(Grammar theGrammar) { |
546 |
Statement stmt;
|
547 |
|
548 |
theGrammar.addReservedWord("MATCH");
|
549 |
|
550 |
stmt = theGrammar.createStatement("MATCH");
|
551 |
stmt.addRule(stmt.require_any_token("MATCH"));
|
552 |
stmt.addRule(stmt.optional_any_token("(")
|
553 |
.addRuleOnTrue(stmt.require_expression().capture_as("VALUE"))
|
554 |
.addRuleOnTrue(stmt.require_any_token(","))
|
555 |
.addRuleOnTrue(stmt.require_expression().capture_as("PATTERN"))
|
556 |
.addRuleOnTrue(stmt.require_any_token(")"))
|
557 |
.addRuleOnFalse(stmt.require_expression().capture_as("VALUE"))
|
558 |
.addRuleOnFalse(stmt.optional_any_token(","))
|
559 |
.addRuleOnFalse(stmt.require_expression().capture_as("PATTERN"))
|
560 |
); |
561 |
stmt.code( |
562 |
OPERATOR_REGEXP, |
563 |
stmt.args_names("VALUE", "PATTERN") |
564 |
); |
565 |
return stmt;
|
566 |
} |
567 |
|
568 |
private Statement createDateStatement(Grammar theGrammar) { |
569 |
Statement stmt;
|
570 |
|
571 |
stmt = theGrammar.createStatement("DATE");
|
572 |
stmt.addRule(stmt.require_any_token("DATE"));
|
573 |
stmt.addRule(stmt.require_expression().capture_as("EXP"));
|
574 |
stmt.code(FUNCTION_DATE, stmt.args_names("EXP"));
|
575 |
return stmt;
|
576 |
} |
577 |
|
578 |
private Statement createTimeStatement(Grammar theGrammar) { |
579 |
Statement stmt;
|
580 |
|
581 |
stmt = theGrammar.createStatement("TIME");
|
582 |
stmt.addRule(stmt.require_any_token("TIME"));
|
583 |
stmt.addRule(stmt.require_expression().capture_as("EXP"));
|
584 |
stmt.code(FUNCTION_TIME, stmt.args_names("EXP"));
|
585 |
return stmt;
|
586 |
} |
587 |
|
588 |
private Statement createImportStatement(Grammar theGrammar) { |
589 |
Statement stmt;
|
590 |
|
591 |
stmt = theGrammar.createStatement("IMPORT");
|
592 |
stmt.addRule(stmt.require_any_token("IMPORT"));
|
593 |
stmt.addRule(stmt.require_expression().capture_as("NAME"));
|
594 |
stmt.code(ImportFunction.FUNCTION_IMPORT, stmt.args_names("NAME"));
|
595 |
return stmt;
|
596 |
} |
597 |
|
598 |
private Statement createTimestampStatement(Grammar theGrammar) { |
599 |
Statement stmt;
|
600 |
|
601 |
stmt = theGrammar.createStatement("TIMESTAMP");
|
602 |
stmt.addRule(stmt.require_any_token("TIMESTAMP"));
|
603 |
stmt.addRule(stmt.require_expression().capture_as("EXP"));
|
604 |
stmt.code(FUNCTION_TIMESTAMP, stmt.args_names("EXP"));
|
605 |
return stmt;
|
606 |
} |
607 |
|
608 |
private Statement createExtractStatement(Grammar theGrammar) { |
609 |
Statement stmt;
|
610 |
|
611 |
stmt = theGrammar.createStatement("EXTRACT");
|
612 |
stmt.addRule(stmt.require_any_token("EXTRACT"));
|
613 |
stmt.addRule(stmt.require_any_token("("));
|
614 |
stmt.addRule(stmt.require_any_token("CENTURY", "DAY", "DECADE", |
615 |
"DOW", "DOY", "HOUR", "ISODOW", "ISOYEAR", "MICROSECONDS", |
616 |
"MILLENNIUM", "MILLUSECONDS", "MINUTE", "MONTH", "QUARTER", |
617 |
"SECOND", "WEEK", "YEAR").capture_as("FIELD")); |
618 |
stmt.addRule(stmt.require_any_token("FROM"));
|
619 |
stmt.addRule(stmt.require_expression().capture_as("EXP"));
|
620 |
stmt.addRule(stmt.require_any_token(")"));
|
621 |
stmt.code(FUNCTION_EXTRACT, stmt.args_names("FIELD", "EXP")); |
622 |
return stmt;
|
623 |
} |
624 |
|
625 |
private Statement createDictStatement(Grammar theGrammar) { |
626 |
Statement stmt;
|
627 |
|
628 |
stmt = theGrammar.createStatement("DICT");
|
629 |
stmt.addRule(stmt.require_any_token("{"));
|
630 |
stmt.addRule(stmt.optional_any_token("}") // Diccionario vacio. |
631 |
.addRuleOnFalse(stmt.repeat() |
632 |
.addRule(stmt.optional_literal_string().capture_as("NAME#")
|
633 |
.addRuleOnFalse(stmt.require_identifier().capture_as("NAME#"))
|
634 |
) |
635 |
.addRule(stmt.require_any_token(":", "=>")) |
636 |
.addRule(stmt.require_expression().capture_as("VALUE#"))
|
637 |
.addRule(stmt.optional_any_token(",")
|
638 |
.addRuleOnFalse(stmt.break_loop()) |
639 |
) |
640 |
) |
641 |
.addRuleOnFalse(stmt.require_any_token("}"))
|
642 |
); |
643 |
stmt.code(FUNCTION_DICT, new DictArgsBuilder("NAME#", "VALUE#")); |
644 |
return stmt;
|
645 |
} |
646 |
|
647 |
private Statement createListStatement(Grammar theGrammar) { |
648 |
Statement stmt;
|
649 |
|
650 |
stmt = theGrammar.createStatement("LIST");
|
651 |
stmt.addRule(stmt.require_any_token("["));
|
652 |
stmt.addRule(stmt.optional_any_token("]") // lista vacia. |
653 |
.addRuleOnFalse(stmt.repeat() |
654 |
.addRule(stmt.require_expression().capture_as("VALUE#"))
|
655 |
.addRule(stmt.optional_any_token(",")
|
656 |
.addRuleOnFalse(stmt.break_loop()) |
657 |
) |
658 |
) |
659 |
.addRuleOnFalse(stmt.require_any_token("]"))
|
660 |
); |
661 |
stmt.code(FUNCTION_LIST, stmt.args_names("VALUE#"));
|
662 |
return stmt;
|
663 |
} |
664 |
|
665 |
private Statement createPassStatement(Grammar theGrammar) { |
666 |
Statement stmt;
|
667 |
|
668 |
theGrammar.addReservedWord("PASS");
|
669 |
|
670 |
stmt = theGrammar.createStatement("PASS");
|
671 |
stmt.addRule(stmt.require_any_token("PASS"));
|
672 |
stmt.addRule(stmt.optional_any_token("(")
|
673 |
.addRuleOnTrue(stmt.require_expressions(",").capture_as("ARGS#")) |
674 |
.addRuleOnTrue(stmt.require_any_token(")"))
|
675 |
); |
676 |
stmt.code( |
677 |
"PASS",
|
678 |
stmt.args_names("ARGS#")
|
679 |
); |
680 |
return stmt;
|
681 |
} |
682 |
|
683 |
private Statement createCallStatement(Grammar theGrammar) { |
684 |
Statement stmt;
|
685 |
|
686 |
theGrammar.addReservedWord("CALL");
|
687 |
|
688 |
stmt = theGrammar.createStatement("CALL");
|
689 |
stmt.addRule(stmt.require_any_token("CALL"));
|
690 |
stmt.addRule(stmt.require_identifier().capture_as("CALLABLE"));
|
691 |
stmt.addRule(stmt.optional_any_token("(")
|
692 |
.addRuleOnTrue(stmt.require_expressions(",").capture_as("ARGS#")) |
693 |
.addRuleOnTrue(stmt.require_any_token(")"))
|
694 |
); |
695 |
stmt.code( |
696 |
"CALL",
|
697 |
stmt.args_names("CALLABLE", "ARGS#") |
698 |
); |
699 |
return stmt;
|
700 |
} |
701 |
|
702 |
} |