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