Statistics
| Revision:

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 @ 47017

History | View | Annotate | Download (26.6 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

    
34
/**
35
 *
36
 * @author jjdelcerro
37
 */
38
public class BasicGrammarFactory extends AbstractGrammarFactory {
39

    
40
    private Grammar grammar;
41

    
42
    public static class DictArgsBuilder implements Statement.ArgsBuilder {
43

    
44
        protected final String keyName;
45
        protected final String valueName;
46

    
47
        public DictArgsBuilder(String keyName, String valueName) {
48
            this.keyName = keyName;
49
            this.valueName = valueName;
50
        }
51

    
52
        @Override
53
        public String toString() {
54
            return "dict_args(" + this.keyName + "," + this.valueName + ")";
55
        }
56

    
57
        @Override
58
        public Codes build(StatementContext context) {
59
            context.trace(this.toString() + ".build");
60
            BaseCodes args = (BaseCodes) context.getCodeBuilder().args();
61
            if (keyName.contains("#")) {
62
                int n = 1;
63
                while (true) {
64
                    String keyNameX = StringUtils.replace(keyName, "#", String.valueOf(n).trim(), 1);
65
                    String valueNameX = StringUtils.replace(valueName, "#", String.valueOf(n).trim(), 1);
66
                    Code keyCodeX = context.getCode(keyNameX);
67
                    if (keyCodeX == null) {
68
                        break;
69
                    }
70
                    Code valueCodeX = context.getCode(valueNameX);
71
                    args.add(keyCodeX);
72
                    args.add(valueCodeX);
73
                    n++;
74
                }
75
            } else {
76
                args.add(context.getCodeBuilder().constant(keyName));
77
                args.add(context.getCode(valueName));
78
            }
79
            return args;
80
        }
81
    }
82

    
83
    private class BeginStatementBuilder extends StatementBuilderBase {
84

    
85
        private final String blockID;
86
        private final String exceptID;
87

    
88
        public BeginStatementBuilder(String blockID, String exceptID) {
89
            super(CodeBlockFunction.NAME, new ArgsBuilderFromNames(blockID));
90
            this.blockID = blockID;
91
            this.exceptID = exceptID;
92
        }
93

    
94
        @Override
95
        public Code build(StatementContext context) {
96
            Callable code = (Callable) super.build(context);
97
            Code exceptCode = context.getCode(this.exceptID);
98
            if (exceptCode == null) {
99
                return code;
100
            }
101
            BaseCodes args = (BaseCodes) code.parameters();
102
            args.add(exceptCode);
103
            code = context.getCodeBuilder().function(CodeBlockWithExceptFunction.NAME, args);
104
            return code;
105
        }
106

    
107
    }
108

    
109
    public static class VarArgsBuilder implements Statement.ArgsBuilder {
110

    
111
        protected final String varName;
112
        protected final String valueName;
113

    
114
        public VarArgsBuilder(String varName, String valueName) {
115
            this.varName = varName;
116
            this.valueName = valueName;
117
        }
118

    
119
        @Override
120
        public String toString() {
121
          return "var_args("+varName+","+valueName+")";
122
        }
123
        
124
        @Override
125
        public Codes build(StatementContext context) {
126
            context.trace(this.toString()+".build");
127
            BaseCodes args = (BaseCodes) context.getCodeBuilder().args();
128
            args.add(context.getCodeBuilder().identifier((String) ((Code.Constant)context.getCode(varName)).value()));
129
            args.add(context.getCode(valueName));
130
            return args;
131
        }
132
    }
133

    
134
    
135
    
136
    public BasicGrammarFactory() {
137
        super("Basic", true);
138
    }
139

    
140
    @Override
141
    public Grammar create(Object... parameters) {
142
        ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
143
        if (this.grammar == null) {
144
            Grammar theGrammar = manager.createGrammar(this.getName());
145

    
146
            theGrammar.addStatement(this.createArrayStatement(theGrammar));
147
            theGrammar.addStatement(this.createBlockStatement(theGrammar));
148
            theGrammar.addStatement(this.createCallStatement(theGrammar));
149
            theGrammar.addStatement(this.createCaseStatement(theGrammar));
150
            theGrammar.addStatement(this.createCastStatement(theGrammar));
151
            theGrammar.addStatement(this.createDateStatement(theGrammar));
152
            theGrammar.addStatement(this.createDictStatement(theGrammar));
153
            theGrammar.addStatement(this.createExtractStatement(theGrammar));
154
            theGrammar.addStatement(this.createForeachStatement(theGrammar));
155
            theGrammar.addStatement(this.createFunctionStatement(theGrammar));
156
            theGrammar.addStatement(this.createIfStatement(theGrammar));
157
            theGrammar.addStatement(this.createLambdaStatement(theGrammar));
158
            theGrammar.addStatement(this.createLet1Statement(theGrammar));
159
            theGrammar.addStatement(this.createLet2Statement(theGrammar));
160
            theGrammar.addStatement(this.createSetattr1Statement(theGrammar));
161
            theGrammar.addStatement(this.createSetattr2Statement(theGrammar));
162
            theGrammar.addStatement(this.createVarStatement(theGrammar));
163
            theGrammar.addStatement(this.createListStatement(theGrammar));
164
            theGrammar.addStatement(this.createMatchStatement(theGrammar));
165
            theGrammar.addStatement(this.createPassStatement(theGrammar));
166
            theGrammar.addStatement(this.createReturnStatement(theGrammar));
167
            theGrammar.addStatement(this.createTimeStatement(theGrammar));
168
            theGrammar.addStatement(this.createTimestampStatement(theGrammar));
169
            theGrammar.addStatement(this.createWhileStatement(theGrammar));
170
            theGrammar.addStatement(this.create$ConstantStatement(theGrammar));
171
            theGrammar.addStatement(this.createBreakStatement(theGrammar));
172
            this.grammar = theGrammar;
173
        }
174
        return grammar;
175
    }
176

    
177
    private Statement createBlockStatement(Grammar theGrammar) {
178
        Statement stmt;
179

    
180
        theGrammar.addReservedWord("BEGIN");
181
        theGrammar.addReservedWord("EXCEPT");
182
        theGrammar.addReservedWord("END");
183

    
184
        stmt = theGrammar.createStatement("BLOCK");
185
        stmt.addRule(stmt.require_any_token("BEGIN"));
186
        stmt.addRule(stmt.require_expressions(";").capture_as("VALUES"));
187
        stmt.addRule(stmt.optional_any_token("EXCEPT")
188
                .addRuleOnTrue(stmt.require_expressions(";").capture_as("EXCEPT"))
189
        );
190
        stmt.addRule(stmt.require_any_token("END"));
191
        stmt.builder(new BeginStatementBuilder("VALUES", "EXCEPT"));
192
        return stmt;
193
    }
194
        
195
    private Statement create$ConstantStatement(Grammar theGrammar) {
196
        Statement stmt;
197

    
198
        stmt = theGrammar.createStatement("$Constant");
199
        stmt.addRule(stmt.require_any_token("<%"));
200
        stmt.addRule(stmt.require_any_token("="));
201
        stmt.addRule(stmt.require_expression().capture_as("VALUE"));
202
        stmt.addRule(stmt.require_any_token("%>"));
203
                stmt.code(
204
                                  ExpressionBuilder.FUNCTION_$HOSTEXPRESSION,
205
                                  stmt.args_names("VALUE")
206
                          );
207
        return stmt;
208
    }
209

    
210
    private Statement createIfStatement(Grammar theGrammar) {
211
        Statement stmt;
212

    
213
        theGrammar.addReservedWord("IF");
214
        theGrammar.addReservedWord("THEN");
215
        theGrammar.addReservedWord("ELSE");
216
        theGrammar.addReservedWord("END");
217

    
218
        stmt = theGrammar.createStatement("IF");
219
        stmt.addRule(stmt.require_any_token("IF"));
220
        stmt.addRule(stmt.require_expression().capture_as("CONDITION"));
221
        stmt.addRule(stmt.require_any_token("THEN", ":"));
222
        stmt.addRule(stmt.require_expressions(";").capture_as("ONTRUE"));
223
        stmt.addRule(stmt.optional_any_token("ELSE")
224
                .addRuleOnTrue(stmt.optional_any_token(":"))
225
                .addRuleOnTrue(stmt.require_expressions(";").capture_as("ONFALSE"))
226
        );
227
        stmt.addRule(stmt.require_any_token("END"));
228
        stmt.addRule(stmt.optional_any_token("IF"));
229
        stmt.code(
230
                ExpressionBuilder.FUNCTION_IF,
231
                stmt.args_names("CONDITION", "ONTRUE", "ONFALSE")
232
        );
233
        return stmt;
234
    }
235

    
236
    private Statement createWhileStatement(Grammar theGrammar) {
237
        Statement stmt;
238

    
239
        theGrammar.addReservedWord("WHILE");
240
        theGrammar.addReservedWord("BEGIN");
241
        theGrammar.addReservedWord("END");
242
        theGrammar.addReservedWord("LOOP");
243

    
244
        stmt = theGrammar.createStatement("WHILE");
245
        stmt.addRule(stmt.require_any_token("WHILE"));
246
        stmt.addRule(stmt.require_expression().capture_as("CONDITION"));
247
        stmt.addRule(stmt.require_any_token("BEGIN", ":", "LOOP"));
248
        stmt.addRule(stmt.require_expressions(";").capture_as("BODY"));
249
        stmt.addRule(stmt.require_any_token("END"));
250
        stmt.addRule(stmt.optional_any_token("WHILE", "LOOP"));
251
        stmt.code(
252
                WhileFunction.NAME,
253
                stmt.args_names("CONDITION", "BODY")
254
        );
255
        return stmt;
256
    }
257

    
258
    private Statement createForeachStatement(Grammar theGrammar) {
259
        Statement stmt;
260

    
261
        theGrammar.addReservedWord("FOR");
262
        theGrammar.addReservedWord("IN");
263
//        theGrammar.addReservedWord("BEGIN");
264
        theGrammar.addReservedWord("END");
265
        theGrammar.addReservedWord("LOOP");
266

    
267
        stmt = theGrammar.createStatement("FOREACH");
268
        stmt.addRule(stmt.require_any_token("FOR", "FOREACH"));
269
        stmt.addRule(stmt.require_identifier().capture_as("VAR"));
270
        stmt.addRule(stmt.require_any_token("IN"));
271
        stmt.addRule(stmt.require_expression().capture_as("ITERABLE"));
272
        stmt.addRule(stmt.require_any_token("LOOP"));
273
        stmt.addRule(stmt.require_expressions(";").capture_as("BODY"));
274
        stmt.addRule(stmt.require_any_token("END"));
275
        stmt.addRule(stmt.optional_any_token("LOOP"));
276
        stmt.code(
277
                ForEachFunction.NAME,
278
                stmt.args_names("VAR", "ITERABLE", "BODY")
279
        );
280
        return stmt;
281
    }
282

    
283
    private Statement createCaseStatement(Grammar theGrammar) {
284
        Statement stmt;
285

    
286
        theGrammar.addReservedWord("CASE");
287
        theGrammar.addReservedWord("WHEN");
288
        theGrammar.addReservedWord("THEN");
289
        theGrammar.addReservedWord("ELSE");
290
        theGrammar.addReservedWord("END");
291

    
292
        stmt = theGrammar.createStatement("CASE");
293
        stmt.addRule(stmt.require_any_token("CASE"));
294
        stmt.addRule(
295
                stmt.repeat_until_any_tokens("END", "ELSE")
296
                        .addRule(stmt.require_any_token("WHEN"))
297
                        .addRule(stmt.require_expression().capture_as("CONDITION#"))
298
                        .addRule(stmt.require_any_token("THEN", ":"))
299
                        .addRule(stmt.require_expressions(";").capture_as("ONTRUE#"))
300
        );
301
        stmt.addRule(stmt.optional_any_token("ELSE")
302
                .addRuleOnTrue(stmt.optional_any_token(":"))
303
                .addRuleOnTrue(stmt.require_expressions(";").capture_as("ELSE"))
304
        );
305
        stmt.addRule(stmt.require_any_token("END"));
306
        stmt.addRule(stmt.optional_any_token("CASE"));
307
        stmt.code(
308
                CaseFunction.NAME,
309
                stmt.args_names("CONDITION#", "ONTRUE#", "ELSE")
310
        );
311
        return stmt;
312
    }
313

    
314
    private Statement createFunctionStatement(Grammar theGrammar) {
315
        Statement stmt;
316

    
317
        theGrammar.addReservedWord("CREATE");
318
        theGrammar.addReservedWord("FUNCTION");
319
        theGrammar.addReservedWord("PROCEDURE");
320
        theGrammar.addReservedWord("AS");
321
        theGrammar.addReservedWord("BEGIN");
322
        theGrammar.addReservedWord("END");
323
        theGrammar.addReservedWord("$$"); // ???? Asi van las funciones en PostgreSQL
324

    
325
        stmt = theGrammar.createStatement("USERFUNCTION");
326
        stmt.addRule(stmt.require_any_token("CREATE"));
327
        stmt.addRule(stmt.optional_any_token("OR")
328
                .addRuleOnTrue(stmt.optional_any_token("REPLACE"))
329
        );
330
        stmt.addRule(stmt.optional_any_token("FUNCTION", "PROCEDURE"));
331
        stmt.addRule(stmt.require_identifier().capture_as("FUNCTION_NAME"));
332
        stmt.addRule(stmt.optional_any_token("("));
333
        stmt.addRule(stmt.optional_identifiers(",").capture_as("PARAMETERS"));
334
        stmt.addRule(stmt.optional_any_token(")"));
335
        stmt.addRule(stmt.require_any_token("AS"));
336
        stmt.addRule(stmt.optional_any_token("$$", "BEGIN")
337
                .addRuleOnTrue(stmt.require_expressions(";").capture_as("BODY"))
338
                .addRuleOnTrue(stmt.require_any_token("$$", "END"))
339
                .addRuleOnTrue(stmt.optional_any_token("FUNCTION", "PROCEDURE"))
340
                .addRuleOnFalse(stmt.optional_literal_string().capture_as("SCRIPT_PATH")
341
                        .addRuleOnTrue(stmt.require_any_token(","))
342
                        .addRuleOnTrue(stmt.require_literal_string().capture_as("SCRIPT_FUNCTION"))
343
                        .addRuleOnTrue(stmt.optional_any_token("LANGUAGE")
344
                                .addRuleOnTrue(stmt.require_literal_string().capture_as("LANGUAGE"))
345
                        )
346
                )
347
        );
348
        stmt.code(
349
                CreateFnFunction.NAME,
350
                stmt.fixed_args_names("FUNCTION_NAME", "PARAMETERS", "BODY", "SCRIPT_PATH", "SCRIPT_FUNCTION", "LANGUAGE")
351
        );
352
        return stmt;
353
    }
354

    
355
    private Statement createLambdaStatement(Grammar theGrammar) {
356
        Statement stmt;
357

    
358
        theGrammar.addReservedWord("LAMBDA");
359

    
360
        stmt = theGrammar.createStatement("LAMBDA");
361
        stmt.addRule(stmt.require_any_token("LAMBDA"));
362
        stmt.addRule(stmt.optional_identifiers(",").capture_as("PARAMETERS"));
363
        stmt.addRule(stmt.require_any_token(":"));
364
        stmt.addRule(stmt.require_expression().capture_as("BODY"));
365
        stmt.code(
366
                CreateFnFunction.NAME,
367
                stmt.fixed_args_names("PARAMETERS", "BODY")
368
        );
369
        return stmt;
370
    }
371

    
372
    private Statement createReturnStatement(Grammar theGrammar) {
373
        Statement stmt;
374

    
375
        theGrammar.addReservedWord("RETURN");
376

    
377
        stmt = theGrammar.createStatement("RETURN");
378
        stmt.addRule(stmt.require_any_token("RETURN"));
379
        stmt.addRule(stmt.optional_expression().capture_as("VALUES"));
380
        stmt.code(
381
                ReturnFunction.NAME,
382
                stmt.args_expand("VALUES")
383
        );
384
        return stmt;
385
    }
386

    
387
    private Statement createBreakStatement(Grammar theGrammar) {
388
        Statement stmt;
389

    
390
        theGrammar.addReservedWord("BREAK");
391

    
392
        stmt = theGrammar.createStatement("BREAK");
393
        stmt.addRule(stmt.require_any_token("BREAK"));
394
        stmt.code(
395
                BreakFunction.NAME,
396
                stmt.args_expand()
397
        );
398
        return stmt;
399
    }
400

    
401
    private Statement createVarStatement(Grammar theGrammar) {
402
        Statement stmt;
403

    
404
        theGrammar.addReservedWord("VAR");
405

    
406
        stmt = theGrammar.createStatement("VAR");
407
        stmt.addRule(stmt.require_any_token("VAR"));
408
        stmt.addRule(stmt.require_identifier().capture_as("NAME"));
409
        stmt.addRule(stmt.optional_any_token("DEFAULT")
410
            .addRuleOnTrue(stmt.require_expression().capture_as("VALUE"))
411
            .addRuleOnFalse(stmt.set_expression("VALUE", null))
412
        );
413
        stmt.code(
414
                "VAR",
415
                new VarArgsBuilder("NAME", "VALUE")
416
        );
417
        return stmt;
418
    }
419

    
420
    private Statement createLet1Statement(Grammar theGrammar) {
421
        Statement stmt;
422

    
423
        theGrammar.addReservedWord("LET");
424
        theGrammar.addReservedWord("SET");
425
        theGrammar.addReservedWord("TO");
426

    
427
        stmt = theGrammar.createStatement("LET");
428
        stmt.addRule(stmt.require_any_token("LET", "SET"));
429
        stmt.addRule(stmt.require_identifier().capture_as("NAME"));
430
        stmt.addRule(stmt.optional_any_token("=", "TO"));
431
        stmt.addRule(stmt.require_expression().capture_as("VALUE"));
432
        stmt.code(
433
                ExpressionBuilder.FUNCTION_LET,
434
                stmt.args_names("NAME", "VALUE")
435
        );
436
        return stmt;
437
    }
438

    
439
    private Statement createLet2Statement(Grammar theGrammar) {
440
        Statement stmt;
441

    
442
        stmt = theGrammar.createStatement("LET2");
443
        stmt.addRule(stmt.require_identifier().capture_as("NAME"));
444
        stmt.addRule(stmt.require_any_token(":="));
445
        stmt.addRule(stmt.require_expression().capture_as("VALUE"));
446
        stmt.code(
447
                ExpressionBuilder.FUNCTION_LET,
448
                stmt.args_names("NAME", "VALUE")
449
        );
450
        return stmt;
451
    }
452

    
453
    private Statement createSetattr1Statement(Grammar theGrammar) {
454
        Statement stmt;
455

    
456
        stmt = theGrammar.createStatement("SETATTR");
457
        stmt.addRule(stmt.require_identifier().capture_as("NAME"));
458
        stmt.addRule(stmt.require_any_token("["));
459
        stmt.addRule(stmt.require_expression().capture_as("ATTRNAME"));
460
        stmt.addRule(stmt.require_any_token("]"));
461
        stmt.addRule(stmt.require_any_token(":="));
462
        stmt.addRule(stmt.require_expression().capture_as("VALUE"));
463
        stmt.code(
464
                ExpressionBuilder.FUNCTION_SETATTR,
465
                stmt.args_names("NAME", "ATTRNAME", "VALUE")
466
        );
467
        return stmt;
468
    }
469

    
470
    private Statement createSetattr2Statement(Grammar theGrammar) {
471
        Statement stmt;
472

    
473
        stmt = theGrammar.createStatement("SETATTR");
474
        stmt.addRule(stmt.require_identifier().capture_as("NAME"));
475
        stmt.addRule(stmt.require_any_token("."));
476
        stmt.addRule(stmt.require_identifier().capture_as("ATTRNAME"));
477
        stmt.addRule(stmt.require_any_token(":="));
478
        stmt.addRule(stmt.require_expression().capture_as("VALUE"));
479
        stmt.code(
480
                ExpressionBuilder.FUNCTION_SETATTR,
481
                stmt.args_names("NAME", "ATTRNAME", "VALUE")
482
        );
483
        return stmt;
484
    }
485

    
486
    private Statement createArrayStatement(Grammar theGrammar) {
487
        Statement stmt;
488

    
489
        theGrammar.addReservedWord("ARRAY");
490

    
491
        stmt = theGrammar.createStatement("ARRAY");
492
        stmt.addRule(stmt.require_any_token("ARRAY"));
493
        stmt.addRule(stmt.require_any_token("["));
494
        stmt.addRule(stmt.require_expressions(",").capture_as("VALUES"));
495
        stmt.addRule(stmt.require_any_token("]"));
496
        stmt.code(
497
                FUNCTION_LIST,
498
                stmt.args_expand("VALUES")
499
        );
500
        return stmt;
501
    }
502

    
503
    private Statement createCastStatement(Grammar theGrammar) {
504
        Statement stmt;
505

    
506
        theGrammar.addReservedWord("CAST");
507
        theGrammar.addReservedWord("AS");
508

    
509
        stmt = theGrammar.createStatement("CAST");
510
        stmt.addRule(stmt.require_any_token("CAST"));
511
        stmt.addRule(stmt.optional_any_token("(")
512
                .addRuleOnTrue(stmt.require_expression(false).capture_as("VALUE"))
513
                .addRuleOnTrue(stmt.require_any_token("AS"))
514
                .addRuleOnTrue(stmt.require_identifier().capture_as("TYPE"))
515
                .addRuleOnTrue(stmt.require_any_token(")"))
516
                .addRuleOnFalse(stmt.require_expression(false).capture_as("VALUE"))
517
                .addRuleOnFalse(stmt.require_any_token("AS"))
518
                .addRuleOnFalse(stmt.require_identifier().capture_as("TYPE"))
519
        );
520
        stmt.code(
521
                FUNCTION_CAST,
522
                stmt.args_names("VALUE", "TYPE")
523
        );
524
        return stmt;
525
    }
526

    
527
    private Statement createMatchStatement(Grammar theGrammar) {
528
        Statement stmt;
529

    
530
        theGrammar.addReservedWord("MATCH");
531

    
532
        stmt = theGrammar.createStatement("MATCH");
533
        stmt.addRule(stmt.require_any_token("MATCH"));
534
        stmt.addRule(stmt.optional_any_token("(")
535
                .addRuleOnTrue(stmt.require_expression().capture_as("VALUE"))
536
                .addRuleOnTrue(stmt.require_any_token(","))
537
                .addRuleOnTrue(stmt.require_expression().capture_as("PATTERN"))
538
                .addRuleOnTrue(stmt.require_any_token(")"))
539
                .addRuleOnFalse(stmt.require_expression().capture_as("VALUE"))
540
                .addRuleOnFalse(stmt.optional_any_token(","))
541
                .addRuleOnFalse(stmt.require_expression().capture_as("PATTERN"))
542
        );
543
        stmt.code(
544
                OPERATOR_REGEXP,
545
                stmt.args_names("VALUE", "PATTERN")
546
        );
547
        return stmt;
548
    }
549

    
550
    private Statement createDateStatement(Grammar theGrammar) {
551
        Statement stmt;
552

    
553
        stmt = theGrammar.createStatement("DATE");
554
        stmt.addRule(stmt.require_any_token("DATE"));
555
        stmt.addRule(stmt.require_expression().capture_as("EXP"));
556
        stmt.code(FUNCTION_DATE, stmt.args_names("EXP"));
557
        return stmt;
558
    }
559

    
560
    private Statement createTimeStatement(Grammar theGrammar) {
561
        Statement stmt;
562

    
563
        stmt = theGrammar.createStatement("TIME");
564
        stmt.addRule(stmt.require_any_token("TIME"));
565
        stmt.addRule(stmt.require_expression().capture_as("EXP"));
566
        stmt.code(FUNCTION_TIME, stmt.args_names("EXP"));
567
        return stmt;
568
    }
569

    
570
    private Statement createTimestampStatement(Grammar theGrammar) {
571
        Statement stmt;
572

    
573
        stmt = theGrammar.createStatement("TIMESTAMP");
574
        stmt.addRule(stmt.require_any_token("TIMESTAMP"));
575
        stmt.addRule(stmt.require_expression().capture_as("EXP"));
576
        stmt.code(FUNCTION_TIMESTAMP, stmt.args_names("EXP"));
577
        return stmt;
578
    }
579

    
580
    private Statement createExtractStatement(Grammar theGrammar) {
581
        Statement stmt;
582

    
583
        stmt = theGrammar.createStatement("EXTRACT");
584
        stmt.addRule(stmt.require_any_token("EXTRACT"));
585
        stmt.addRule(stmt.require_any_token("("));
586
        stmt.addRule(stmt.require_any_token("CENTURY", "DAY", "DECADE",
587
                "DOW", "DOY", "HOUR", "ISODOW", "ISOYEAR", "MICROSECONDS",
588
                "MILLENNIUM", "MILLUSECONDS", "MINUTE", "MONTH", "QUARTER",
589
                "SECOND", "WEEK", "YEAR").capture_as("FIELD"));
590
        stmt.addRule(stmt.require_any_token("FROM"));
591
        stmt.addRule(stmt.require_expression().capture_as("EXP"));
592
        stmt.addRule(stmt.require_any_token(")"));
593
        stmt.code(FUNCTION_EXTRACT, stmt.args_names("FIELD", "EXP"));
594
        return stmt;
595
    }
596

    
597
    private Statement createDictStatement(Grammar theGrammar) {
598
        Statement stmt;
599

    
600
        stmt = theGrammar.createStatement("DICT");
601
        stmt.addRule(stmt.require_any_token("{"));
602
        stmt.addRule(stmt.optional_any_token("}") // Diccionario vacio.
603
            .addRuleOnFalse(stmt.repeat()
604
                    .addRule(stmt.optional_literal_string().capture_as("NAME#")
605
                            .addRuleOnFalse(stmt.require_identifier().capture_as("NAME#"))
606
                    )
607
                    .addRule(stmt.require_any_token(":", "=>"))
608
                    .addRule(stmt.require_expression().capture_as("VALUE#"))
609
                    .addRule(stmt.optional_any_token(",")
610
                            .addRuleOnFalse(stmt.break_loop())
611
                    )
612
                )
613
            .addRuleOnFalse(stmt.require_any_token("}"))
614
        );
615
        stmt.code(FUNCTION_DICT, new DictArgsBuilder("NAME#", "VALUE#"));
616
        return stmt;
617
    }
618

    
619
    private Statement createListStatement(Grammar theGrammar) {
620
        Statement stmt;
621

    
622
        stmt = theGrammar.createStatement("LIST");
623
        stmt.addRule(stmt.require_any_token("["));
624
        stmt.addRule(stmt.optional_any_token("]")  // lista vacia.
625
            .addRuleOnFalse(stmt.repeat()
626
                    .addRule(stmt.require_expression().capture_as("VALUE#"))
627
                    .addRule(stmt.optional_any_token(",")
628
                            .addRuleOnFalse(stmt.break_loop())
629
                    )
630
                )
631
            .addRuleOnFalse(stmt.require_any_token("]"))
632
        );
633
        stmt.code(FUNCTION_LIST, stmt.args_names("VALUE#"));
634
        return stmt;
635
    }
636

    
637
    private Statement createPassStatement(Grammar theGrammar) {
638
        Statement stmt;
639

    
640
        theGrammar.addReservedWord("PASS");
641

    
642
        stmt = theGrammar.createStatement("PASS");
643
        stmt.addRule(stmt.require_any_token("PASS"));
644
        stmt.addRule(stmt.optional_any_token("(")
645
                .addRuleOnTrue(stmt.require_expressions(",").capture_as("ARGS#"))
646
                .addRuleOnTrue(stmt.require_any_token(")"))
647
        );
648
        stmt.code(
649
                "PASS",
650
                stmt.args_names("ARGS#")
651
        );
652
        return stmt;
653
    }
654

    
655
    private Statement createCallStatement(Grammar theGrammar) {
656
        Statement stmt;
657

    
658
        theGrammar.addReservedWord("CALL");
659

    
660
        stmt = theGrammar.createStatement("CALL");
661
        stmt.addRule(stmt.require_any_token("CALL"));
662
        stmt.addRule(stmt.require_identifier().capture_as("CALLABLE"));
663
        stmt.addRule(stmt.optional_any_token("(")
664
                .addRuleOnTrue(stmt.require_expressions(",").capture_as("ARGS#"))
665
                .addRuleOnTrue(stmt.require_any_token(")"))
666
        );
667
        stmt.code(
668
                "CALL",
669
                stmt.args_names("CALLABLE", "ARGS#")
670
        );
671
        return stmt;
672
    }
673

    
674
}