Revision 45153 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
BasicGrammarFactory.java | ||
---|---|---|
18 | 18 |
import org.gvsig.expressionevaluator.impl.DefaultStatement.StatementBuilderBase; |
19 | 19 |
import org.gvsig.expressionevaluator.impl.DefaultStatement.ArgsBuilderFromNames; |
20 | 20 |
import org.gvsig.expressionevaluator.impl.function.programming.IIFFunction; |
21 |
import org.gvsig.expressionevaluator.impl.function.programming.LetFunction; |
|
22 | 21 |
import org.gvsig.expressionevaluator.impl.function.programming.CaseFunction; |
23 | 22 |
import org.gvsig.expressionevaluator.impl.function.programming.CodeBlockFunction; |
24 | 23 |
import org.gvsig.expressionevaluator.impl.function.programming.CodeBlockWithExceptFunction; |
... | ... | |
29 | 28 |
import org.gvsig.expressionevaluator.spi.AbstractGrammarFactory; |
30 | 29 |
import org.gvsig.expressionevaluator.Code.Callable; |
31 | 30 |
import org.gvsig.expressionevaluator.Codes; |
31 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
32 | 32 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_DICT; |
33 | 33 |
|
34 | 34 |
/** |
... | ... | |
51 | 51 |
|
52 | 52 |
@Override |
53 | 53 |
public String toString() { |
54 |
return "dict_args("+this.keyName+","+this.valueName+")";
|
|
54 |
return "dict_args(" + this.keyName + "," + this.valueName + ")";
|
|
55 | 55 |
} |
56 |
|
|
56 |
|
|
57 | 57 |
@Override |
58 | 58 |
public Codes build(StatementContext context) { |
59 |
context.trace(this.toString()+".build");
|
|
59 |
context.trace(this.toString() + ".build");
|
|
60 | 60 |
BaseCodes args = (BaseCodes) context.getCodeBuilder().args(); |
61 | 61 |
if (keyName.contains("#")) { |
62 | 62 |
int n = 1; |
... | ... | |
80 | 80 |
} |
81 | 81 |
} |
82 | 82 |
|
83 |
|
|
84 | 83 |
private class BeginStatementBuilder extends StatementBuilderBase { |
85 | 84 |
|
86 | 85 |
private final String blockID; |
... | ... | |
96 | 95 |
public Code build(StatementContext context) { |
97 | 96 |
Callable code = (Callable) super.build(context); |
98 | 97 |
Code exceptCode = context.getCode(this.exceptID); |
99 |
if( exceptCode==null ) {
|
|
98 |
if (exceptCode == null) {
|
|
100 | 99 |
return code; |
101 | 100 |
} |
102 | 101 |
BaseCodes args = (BaseCodes) code.parameters(); |
... | ... | |
104 | 103 |
code = context.getCodeBuilder().function(CodeBlockWithExceptFunction.NAME, args); |
105 | 104 |
return code; |
106 | 105 |
} |
107 |
|
|
106 |
|
|
108 | 107 |
} |
108 |
|
|
109 | 109 |
public BasicGrammarFactory() { |
110 | 110 |
super("Basic", true); |
111 | 111 |
} |
112 |
|
|
112 |
|
|
113 | 113 |
@Override |
114 | 114 |
public Grammar create(Object... parameters) { |
115 | 115 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
116 |
if( this.grammar==null ) {
|
|
116 |
if (this.grammar == null) {
|
|
117 | 117 |
Grammar theGrammar = manager.createGrammar(this.getName()); |
118 |
Statement stmt; |
|
119 |
|
|
120 |
theGrammar.addReservedWord("BEGIN"); |
|
121 |
theGrammar.addReservedWord("EXCEPT"); |
|
122 |
theGrammar.addReservedWord("END"); |
|
123 |
theGrammar.addReservedWord("IF"); |
|
124 |
theGrammar.addReservedWord("THEN"); |
|
125 |
theGrammar.addReservedWord("ELSE"); |
|
126 |
theGrammar.addReservedWord("WHILE"); |
|
127 |
theGrammar.addReservedWord("CASE"); |
|
128 |
theGrammar.addReservedWord("WHEN"); |
|
129 |
theGrammar.addReservedWord("LOOP"); |
|
130 |
theGrammar.addReservedWord("FOR"); |
|
131 |
theGrammar.addReservedWord("CREATE"); |
|
132 |
theGrammar.addReservedWord("FUNCTION"); |
|
133 |
theGrammar.addReservedWord("PROCEDURE"); |
|
134 |
theGrammar.addReservedWord("AS"); |
|
135 |
theGrammar.addReservedWord("MATCH"); |
|
136 |
theGrammar.addReservedWord("CAST"); |
|
137 |
theGrammar.addReservedWord("SET"); |
|
138 |
theGrammar.addReservedWord("TO"); |
|
139 |
theGrammar.addReservedWord("FROM"); |
|
140 |
theGrammar.addReservedWord("$$"); // ???? Asi van las funciones |
|
141 |
theGrammar.addReservedWord("PASS"); |
|
142 |
theGrammar.addReservedWord("LAMBDA"); |
|
143 |
|
|
144 |
stmt = theGrammar.createStatement("BLOCK"); |
|
145 |
stmt.addRule(stmt.require_any_token("BEGIN")); |
|
146 |
stmt.addRule(stmt.require_expressions(";").capture_as("VALUES")); |
|
147 |
stmt.addRule(stmt.optional_any_token("EXCEPT") |
|
148 |
.addRuleOnTrue(stmt.require_expressions(";").capture_as("EXCEPT")) |
|
149 |
); |
|
150 |
stmt.addRule(stmt.require_any_token("END")); |
|
151 |
stmt.builder(new BeginStatementBuilder("VALUES","EXCEPT")); |
|
152 |
theGrammar.addStatement(stmt); |
|
153 | 118 |
|
154 |
stmt = theGrammar.createStatement("IF"); |
|
155 |
stmt.addRule(stmt.require_any_token("IF")); |
|
156 |
stmt.addRule(stmt.require_expression().capture_as("CONDITION")); |
|
157 |
stmt.addRule(stmt.require_any_token("THEN", ":")); |
|
158 |
stmt.addRule(stmt.require_expressions(";").capture_as("ONTRUE")); |
|
159 |
stmt.addRule(stmt.optional_any_token("ELSE") |
|
160 |
.addRuleOnTrue(stmt.optional_any_token(":")) |
|
161 |
.addRuleOnTrue(stmt.require_expressions(";").capture_as("ONFALSE")) |
|
162 |
); |
|
163 |
stmt.addRule(stmt.require_any_token("END")); |
|
164 |
stmt.addRule(stmt.optional_any_token("IF")); |
|
165 |
stmt.code( |
|
166 |
IIFFunction.NAME, |
|
167 |
stmt.args_names("CONDITION","ONTRUE","ONFALSE") |
|
168 |
); |
|
169 |
theGrammar.addStatement(stmt); |
|
119 |
theGrammar.addStatement(this.createArrayStatement(theGrammar)); |
|
120 |
theGrammar.addStatement(this.createBlockStatement(theGrammar)); |
|
121 |
theGrammar.addStatement(this.createCallStatement(theGrammar)); |
|
122 |
theGrammar.addStatement(this.createCaseStatement(theGrammar)); |
|
123 |
theGrammar.addStatement(this.createCastStatement(theGrammar)); |
|
124 |
theGrammar.addStatement(this.createDateStatement(theGrammar)); |
|
125 |
theGrammar.addStatement(this.createDictStatement(theGrammar)); |
|
126 |
theGrammar.addStatement(this.createExtractStatement(theGrammar)); |
|
127 |
theGrammar.addStatement(this.createForeachStatement(theGrammar)); |
|
128 |
theGrammar.addStatement(this.createFunctionStatement(theGrammar)); |
|
129 |
theGrammar.addStatement(this.createIfStatement(theGrammar)); |
|
130 |
theGrammar.addStatement(this.createLambdaStatement(theGrammar)); |
|
131 |
theGrammar.addStatement(this.createLet1Statement(theGrammar)); |
|
132 |
theGrammar.addStatement(this.createLet2Statement(theGrammar)); |
|
133 |
theGrammar.addStatement(this.createListStatement(theGrammar)); |
|
134 |
theGrammar.addStatement(this.createMatchStatement(theGrammar)); |
|
135 |
theGrammar.addStatement(this.createPassStatement(theGrammar)); |
|
136 |
theGrammar.addStatement(this.createReturnStatement(theGrammar)); |
|
137 |
theGrammar.addStatement(this.createTimeStatement(theGrammar)); |
|
138 |
theGrammar.addStatement(this.createTimestampStatement(theGrammar)); |
|
139 |
theGrammar.addStatement(this.createWhileStatement(theGrammar)); |
|
170 | 140 |
|
171 |
stmt = theGrammar.createStatement("WHILE"); |
|
172 |
stmt.addRule(stmt.require_any_token("WHILE")); |
|
173 |
stmt.addRule(stmt.require_expression().capture_as("CONDITION")); |
|
174 |
stmt.addRule(stmt.require_any_token("BEGIN", ":","LOOP")); |
|
175 |
stmt.addRule(stmt.require_expressions(";").capture_as("BODY")); |
|
176 |
stmt.addRule(stmt.require_any_token("END")); |
|
177 |
stmt.addRule(stmt.optional_any_token("WHILE","LOOP")); |
|
178 |
stmt.code( |
|
179 |
WhileFunction.NAME, |
|
180 |
stmt.args_names("CONDITION","BODY") |
|
181 |
); |
|
182 |
theGrammar.addStatement(stmt); |
|
141 |
this.grammar = theGrammar; |
|
142 |
} |
|
143 |
return grammar; |
|
144 |
} |
|
183 | 145 |
|
184 |
// stmt = theGrammar.createStatement("FOR"); |
|
185 |
// stmt.addRule(stmt.require_any_token("FOR")); |
|
186 |
// stmt.addRule(stmt.require_expression("INIT")); |
|
187 |
// stmt.addRule(stmt.require_any_token(";")); |
|
188 |
// stmt.addRule(stmt.require_expression("CONDITION")); |
|
189 |
// stmt.addRule(stmt.require_any_token(";")); |
|
190 |
// stmt.addRule(stmt.require_expression("STEP")); |
|
191 |
// stmt.addRule(stmt.require_any_token("BEGIN", ":")); |
|
192 |
// stmt.addRule(stmt.require_expression("BODY")); |
|
193 |
// stmt.addRule(stmt.require_any_token("END")); |
|
194 |
// stmt.addRule(stmt.optional_any_token("FOR")); |
|
195 |
// stmt.code( |
|
196 |
// ForFunction.NAME, |
|
197 |
// stmt.args_names("INIT","CONDITION","STEP","BODY") |
|
198 |
// ); |
|
199 |
// theGrammar.addStatement(stmt); |
|
146 |
private Statement createBlockStatement(Grammar theGrammar) { |
|
147 |
Statement stmt; |
|
200 | 148 |
|
201 |
stmt = theGrammar.createStatement("FOREACH"); |
|
202 |
stmt.addRule(stmt.require_any_token("FOR","FOREACH")); |
|
203 |
stmt.addRule(stmt.require_identifier().capture_as("VAR")); |
|
204 |
stmt.addRule(stmt.require_any_token("IN")); |
|
205 |
stmt.addRule(stmt.require_expression().capture_as("ITERABLE")); |
|
206 |
stmt.addRule(stmt.require_any_token("BEGIN", ":", "LOOP")); |
|
207 |
stmt.addRule(stmt.require_expressions(";").capture_as("BODY")); |
|
208 |
stmt.addRule(stmt.require_any_token("END")); |
|
209 |
stmt.addRule(stmt.optional_any_token("FOR","LOOP")); |
|
210 |
stmt.code( |
|
211 |
ForEachFunction.NAME, |
|
212 |
stmt.args_names("VAR","ITERABLE","BODY") |
|
213 |
); |
|
214 |
theGrammar.addStatement(stmt); |
|
149 |
theGrammar.addReservedWord("BEGIN"); |
|
150 |
theGrammar.addReservedWord("EXCEPT"); |
|
151 |
theGrammar.addReservedWord("END"); |
|
215 | 152 |
|
216 |
stmt = theGrammar.createStatement("CASE"); |
|
217 |
stmt.addRule(stmt.require_any_token("CASE")); |
|
218 |
stmt.addRule( |
|
219 |
stmt.repeat_until_any_tokens("END","ELSE") |
|
220 |
.addRule(stmt.require_any_token("WHEN")) |
|
221 |
.addRule(stmt.require_expression().capture_as("CONDITION#")) |
|
222 |
.addRule(stmt.require_any_token("THEN",":")) |
|
223 |
.addRule(stmt.require_expressions(";").capture_as("ONTRUE#")) |
|
224 |
); |
|
225 |
stmt.addRule(stmt.optional_any_token("ELSE") |
|
226 |
.addRuleOnTrue(stmt.optional_any_token(":")) |
|
227 |
.addRuleOnTrue(stmt.require_expressions(";").capture_as("ELSE")) |
|
228 |
); |
|
229 |
stmt.addRule(stmt.require_any_token("END")); |
|
230 |
stmt.addRule(stmt.optional_any_token("CASE")); |
|
231 |
stmt.code( |
|
232 |
CaseFunction.NAME, |
|
233 |
stmt.args_names("CONDITION#","ONTRUE#","ELSE") |
|
234 |
); |
|
235 |
theGrammar.addStatement(stmt); |
|
236 |
|
|
237 |
stmt = theGrammar.createStatement("USERFUNCTION"); |
|
238 |
stmt.addRule(stmt.require_any_token("CREATE")); |
|
239 |
stmt.addRule(stmt.optional_any_token("OR") |
|
240 |
.addRuleOnTrue(stmt.optional_any_token("REPLACE")) |
|
241 |
); |
|
242 |
stmt.addRule(stmt.optional_any_token("FUNCTION", "PROCEDURE")); |
|
243 |
stmt.addRule(stmt.require_identifier().capture_as("FUNCTION_NAME")); |
|
244 |
stmt.addRule(stmt.optional_any_token("(")); |
|
245 |
stmt.addRule(stmt.optional_identifiers(",").capture_as("PARAMETERS")); |
|
246 |
stmt.addRule(stmt.optional_any_token(")")); |
|
247 |
stmt.addRule(stmt.require_any_token("AS")); |
|
248 |
stmt.addRule(stmt.optional_any_token("$$","BEGIN") |
|
249 |
.addRuleOnTrue(stmt.require_expressions(";").capture_as("BODY")) |
|
250 |
.addRuleOnTrue(stmt.require_any_token("$$", "END")) |
|
251 |
.addRuleOnTrue(stmt.optional_any_token("FUNCTION", "PROCEDURE")) |
|
252 |
.addRuleOnFalse(stmt.optional_literal_string().capture_as("SCRIPT_PATH") |
|
153 |
stmt = theGrammar.createStatement("BLOCK"); |
|
154 |
stmt.addRule(stmt.require_any_token("BEGIN")); |
|
155 |
stmt.addRule(stmt.require_expressions(";").capture_as("VALUES")); |
|
156 |
stmt.addRule(stmt.optional_any_token("EXCEPT") |
|
157 |
.addRuleOnTrue(stmt.require_expressions(";").capture_as("EXCEPT")) |
|
158 |
); |
|
159 |
stmt.addRule(stmt.require_any_token("END")); |
|
160 |
stmt.builder(new BeginStatementBuilder("VALUES", "EXCEPT")); |
|
161 |
return stmt; |
|
162 |
} |
|
163 |
|
|
164 |
private Statement createIfStatement(Grammar theGrammar) { |
|
165 |
Statement stmt; |
|
166 |
|
|
167 |
theGrammar.addReservedWord("IF"); |
|
168 |
theGrammar.addReservedWord("THEN"); |
|
169 |
theGrammar.addReservedWord("ELSE"); |
|
170 |
theGrammar.addReservedWord("END"); |
|
171 |
|
|
172 |
stmt = theGrammar.createStatement("IF"); |
|
173 |
stmt.addRule(stmt.require_any_token("IF")); |
|
174 |
stmt.addRule(stmt.require_expression().capture_as("CONDITION")); |
|
175 |
stmt.addRule(stmt.require_any_token("THEN", ":")); |
|
176 |
stmt.addRule(stmt.require_expressions(";").capture_as("ONTRUE")); |
|
177 |
stmt.addRule(stmt.optional_any_token("ELSE") |
|
178 |
.addRuleOnTrue(stmt.optional_any_token(":")) |
|
179 |
.addRuleOnTrue(stmt.require_expressions(";").capture_as("ONFALSE")) |
|
180 |
); |
|
181 |
stmt.addRule(stmt.require_any_token("END")); |
|
182 |
stmt.addRule(stmt.optional_any_token("IF")); |
|
183 |
stmt.code( |
|
184 |
IIFFunction.NAME, |
|
185 |
stmt.args_names("CONDITION", "ONTRUE", "ONFALSE") |
|
186 |
); |
|
187 |
return stmt; |
|
188 |
} |
|
189 |
|
|
190 |
private Statement createWhileStatement(Grammar theGrammar) { |
|
191 |
Statement stmt; |
|
192 |
|
|
193 |
theGrammar.addReservedWord("WHILE"); |
|
194 |
theGrammar.addReservedWord("BEGIN"); |
|
195 |
theGrammar.addReservedWord("END"); |
|
196 |
theGrammar.addReservedWord("LOOP"); |
|
197 |
|
|
198 |
stmt = theGrammar.createStatement("WHILE"); |
|
199 |
stmt.addRule(stmt.require_any_token("WHILE")); |
|
200 |
stmt.addRule(stmt.require_expression().capture_as("CONDITION")); |
|
201 |
stmt.addRule(stmt.require_any_token("BEGIN", ":", "LOOP")); |
|
202 |
stmt.addRule(stmt.require_expressions(";").capture_as("BODY")); |
|
203 |
stmt.addRule(stmt.require_any_token("END")); |
|
204 |
stmt.addRule(stmt.optional_any_token("WHILE", "LOOP")); |
|
205 |
stmt.code( |
|
206 |
WhileFunction.NAME, |
|
207 |
stmt.args_names("CONDITION", "BODY") |
|
208 |
); |
|
209 |
return stmt; |
|
210 |
} |
|
211 |
|
|
212 |
private Statement createForeachStatement(Grammar theGrammar) { |
|
213 |
Statement stmt; |
|
214 |
|
|
215 |
theGrammar.addReservedWord("FOR"); |
|
216 |
theGrammar.addReservedWord("IN"); |
|
217 |
theGrammar.addReservedWord("BEGIN"); |
|
218 |
theGrammar.addReservedWord("END"); |
|
219 |
theGrammar.addReservedWord("LOOP"); |
|
220 |
|
|
221 |
stmt = theGrammar.createStatement("FOREACH"); |
|
222 |
stmt.addRule(stmt.require_any_token("FOR", "FOREACH")); |
|
223 |
stmt.addRule(stmt.require_identifier().capture_as("VAR")); |
|
224 |
stmt.addRule(stmt.require_any_token("IN")); |
|
225 |
stmt.addRule(stmt.require_expression().capture_as("ITERABLE")); |
|
226 |
stmt.addRule(stmt.require_any_token("BEGIN", ":", "LOOP")); |
|
227 |
stmt.addRule(stmt.require_expressions(";").capture_as("BODY")); |
|
228 |
stmt.addRule(stmt.require_any_token("END")); |
|
229 |
stmt.addRule(stmt.optional_any_token("FOR", "LOOP")); |
|
230 |
stmt.code( |
|
231 |
ForEachFunction.NAME, |
|
232 |
stmt.args_names("VAR", "ITERABLE", "BODY") |
|
233 |
); |
|
234 |
return stmt; |
|
235 |
} |
|
236 |
|
|
237 |
private Statement createCaseStatement(Grammar theGrammar) { |
|
238 |
Statement stmt; |
|
239 |
|
|
240 |
theGrammar.addReservedWord("CASE"); |
|
241 |
theGrammar.addReservedWord("WHEN"); |
|
242 |
theGrammar.addReservedWord("THEN"); |
|
243 |
theGrammar.addReservedWord("ELSE"); |
|
244 |
theGrammar.addReservedWord("END"); |
|
245 |
|
|
246 |
stmt = theGrammar.createStatement("CASE"); |
|
247 |
stmt.addRule(stmt.require_any_token("CASE")); |
|
248 |
stmt.addRule( |
|
249 |
stmt.repeat_until_any_tokens("END", "ELSE") |
|
250 |
.addRule(stmt.require_any_token("WHEN")) |
|
251 |
.addRule(stmt.require_expression().capture_as("CONDITION#")) |
|
252 |
.addRule(stmt.require_any_token("THEN", ":")) |
|
253 |
.addRule(stmt.require_expressions(";").capture_as("ONTRUE#")) |
|
254 |
); |
|
255 |
stmt.addRule(stmt.optional_any_token("ELSE") |
|
256 |
.addRuleOnTrue(stmt.optional_any_token(":")) |
|
257 |
.addRuleOnTrue(stmt.require_expressions(";").capture_as("ELSE")) |
|
258 |
); |
|
259 |
stmt.addRule(stmt.require_any_token("END")); |
|
260 |
stmt.addRule(stmt.optional_any_token("CASE")); |
|
261 |
stmt.code( |
|
262 |
CaseFunction.NAME, |
|
263 |
stmt.args_names("CONDITION#", "ONTRUE#", "ELSE") |
|
264 |
); |
|
265 |
return stmt; |
|
266 |
} |
|
267 |
|
|
268 |
private Statement createFunctionStatement(Grammar theGrammar) { |
|
269 |
Statement stmt; |
|
270 |
|
|
271 |
theGrammar.addReservedWord("CREATE"); |
|
272 |
theGrammar.addReservedWord("FUNCTION"); |
|
273 |
theGrammar.addReservedWord("PROCEDURE"); |
|
274 |
theGrammar.addReservedWord("AS"); |
|
275 |
theGrammar.addReservedWord("BEGIN"); |
|
276 |
theGrammar.addReservedWord("END"); |
|
277 |
theGrammar.addReservedWord("$$"); // ???? Asi van las funciones en PostgreSQL |
|
278 |
|
|
279 |
stmt = theGrammar.createStatement("USERFUNCTION"); |
|
280 |
stmt.addRule(stmt.require_any_token("CREATE")); |
|
281 |
stmt.addRule(stmt.optional_any_token("OR") |
|
282 |
.addRuleOnTrue(stmt.optional_any_token("REPLACE")) |
|
283 |
); |
|
284 |
stmt.addRule(stmt.optional_any_token("FUNCTION", "PROCEDURE")); |
|
285 |
stmt.addRule(stmt.require_identifier().capture_as("FUNCTION_NAME")); |
|
286 |
stmt.addRule(stmt.optional_any_token("(")); |
|
287 |
stmt.addRule(stmt.optional_identifiers(",").capture_as("PARAMETERS")); |
|
288 |
stmt.addRule(stmt.optional_any_token(")")); |
|
289 |
stmt.addRule(stmt.require_any_token("AS")); |
|
290 |
stmt.addRule(stmt.optional_any_token("$$", "BEGIN") |
|
291 |
.addRuleOnTrue(stmt.require_expressions(";").capture_as("BODY")) |
|
292 |
.addRuleOnTrue(stmt.require_any_token("$$", "END")) |
|
293 |
.addRuleOnTrue(stmt.optional_any_token("FUNCTION", "PROCEDURE")) |
|
294 |
.addRuleOnFalse(stmt.optional_literal_string().capture_as("SCRIPT_PATH") |
|
253 | 295 |
.addRuleOnTrue(stmt.require_any_token(",")) |
254 | 296 |
.addRuleOnTrue(stmt.require_literal_string().capture_as("SCRIPT_FUNCTION")) |
255 | 297 |
.addRuleOnTrue(stmt.optional_any_token("LANGUAGE") |
256 | 298 |
.addRuleOnTrue(stmt.require_literal_string().capture_as("LANGUAGE")) |
257 | 299 |
) |
258 |
) |
|
259 |
); |
|
260 |
stmt.code( |
|
261 |
CreateFnFunction.NAME, |
|
262 |
stmt.fixed_args_names("FUNCTION_NAME","PARAMETERS","BODY", "SCRIPT_PATH", "SCRIPT_FUNCTION", "LANGUAGE") |
|
263 |
); |
|
264 |
theGrammar.addStatement(stmt); |
|
265 |
|
|
266 |
stmt = theGrammar.createStatement("LAMBDA"); |
|
267 |
stmt.addRule(stmt.require_any_token("LAMBDA")); |
|
268 |
stmt.addRule(stmt.optional_identifiers(",").capture_as("PARAMETERS")); |
|
269 |
stmt.addRule(stmt.require_any_token(":")); |
|
270 |
stmt.addRule(stmt.require_expression().capture_as("BODY")); |
|
271 |
stmt.code( |
|
272 |
CreateFnFunction.NAME, |
|
273 |
stmt.fixed_args_names("PARAMETERS","BODY") |
|
274 |
); |
|
275 |
theGrammar.addStatement(stmt); |
|
300 |
) |
|
301 |
); |
|
302 |
stmt.code( |
|
303 |
CreateFnFunction.NAME, |
|
304 |
stmt.fixed_args_names("FUNCTION_NAME", "PARAMETERS", "BODY", "SCRIPT_PATH", "SCRIPT_FUNCTION", "LANGUAGE") |
|
305 |
); |
|
306 |
return stmt; |
|
307 |
} |
|
276 | 308 |
|
277 |
stmt = theGrammar.createStatement("RETURN"); |
|
278 |
stmt.addRule(stmt.require_any_token("RETURN")); |
|
279 |
stmt.addRule(stmt.optional_any_token(";") |
|
280 |
.addRuleOnTrue(stmt.set_expression("VALUES", null)) |
|
281 |
.addRuleOnFalse(stmt.require_expression().capture_as("VALUES")) |
|
282 |
.addRuleOnFalse(stmt.require_any_token(";")) |
|
283 |
); |
|
284 |
stmt.code( |
|
285 |
ReturnFunction.NAME, |
|
286 |
stmt.args_expand("VALUES") |
|
287 |
); |
|
288 |
theGrammar.addStatement(stmt); |
|
309 |
private Statement createLambdaStatement(Grammar theGrammar) { |
|
310 |
Statement stmt; |
|
289 | 311 |
|
290 |
stmt = theGrammar.createStatement("LET"); |
|
291 |
stmt.addRule(stmt.require_any_token("LET", "SET")); |
|
292 |
stmt.addRule(stmt.require_identifier().capture_as("NAME")); |
|
293 |
stmt.addRule(stmt.optional_any_token("=","TO")); |
|
294 |
stmt.addRule(stmt.require_expression().capture_as("VALUE")); |
|
295 |
stmt.code( |
|
296 |
LetFunction.NAME, |
|
297 |
stmt.args_names("NAME", "VALUE") |
|
298 |
); |
|
299 |
|
|
300 |
theGrammar.addStatement(stmt); |
|
301 |
stmt = theGrammar.createStatement("LET2"); |
|
302 |
stmt.addRule(stmt.require_identifier().capture_as("NAME")); |
|
303 |
stmt.addRule(stmt.require_any_token(":=")); |
|
304 |
stmt.addRule(stmt.require_expression().capture_as("VALUE")); |
|
305 |
stmt.code( |
|
306 |
LetFunction.NAME, |
|
307 |
stmt.args_names("NAME", "VALUE") |
|
308 |
); |
|
309 |
theGrammar.addStatement(stmt); |
|
310 |
|
|
311 |
stmt = theGrammar.createStatement("ARRAY"); |
|
312 |
stmt.addRule(stmt.require_any_token("ARRAY")); |
|
313 |
stmt.addRule(stmt.require_any_token("[")); |
|
314 |
stmt.addRule(stmt.require_expressions(",").capture_as("VALUES")); |
|
315 |
stmt.addRule(stmt.require_any_token("]")); |
|
316 |
stmt.code( |
|
317 |
FUNCTION_LIST, |
|
318 |
stmt.args_expand("VALUES") |
|
319 |
); |
|
320 |
theGrammar.addStatement(stmt); |
|
312 |
theGrammar.addReservedWord("LAMBDA"); |
|
321 | 313 |
|
322 |
stmt = theGrammar.createStatement("CAST"); |
|
323 |
stmt.addRule(stmt.require_any_token("CAST")); |
|
324 |
stmt.addRule(stmt.optional_any_token("(") |
|
325 |
.addRuleOnTrue(stmt.require_expression().capture_as("VALUE")) |
|
314 |
stmt = theGrammar.createStatement("LAMBDA"); |
|
315 |
stmt.addRule(stmt.require_any_token("LAMBDA")); |
|
316 |
stmt.addRule(stmt.optional_identifiers(",").capture_as("PARAMETERS")); |
|
317 |
stmt.addRule(stmt.require_any_token(":")); |
|
318 |
stmt.addRule(stmt.require_expression().capture_as("BODY")); |
|
319 |
stmt.code( |
|
320 |
CreateFnFunction.NAME, |
|
321 |
stmt.fixed_args_names("PARAMETERS", "BODY") |
|
322 |
); |
|
323 |
return stmt; |
|
324 |
} |
|
325 |
|
|
326 |
private Statement createReturnStatement(Grammar theGrammar) { |
|
327 |
Statement stmt; |
|
328 |
|
|
329 |
theGrammar.addReservedWord("RETURN"); |
|
330 |
|
|
331 |
stmt = theGrammar.createStatement("RETURN"); |
|
332 |
stmt.addRule(stmt.require_any_token("RETURN")); |
|
333 |
stmt.addRule(stmt.optional_any_token(";") |
|
334 |
.addRuleOnTrue(stmt.set_expression("VALUES", null)) |
|
335 |
.addRuleOnFalse(stmt.require_expression().capture_as("VALUES")) |
|
336 |
.addRuleOnFalse(stmt.require_any_token(";")) |
|
337 |
); |
|
338 |
stmt.code( |
|
339 |
ReturnFunction.NAME, |
|
340 |
stmt.args_expand("VALUES") |
|
341 |
); |
|
342 |
return stmt; |
|
343 |
} |
|
344 |
|
|
345 |
private Statement createLet1Statement(Grammar theGrammar) { |
|
346 |
Statement stmt; |
|
347 |
|
|
348 |
theGrammar.addReservedWord("LET"); |
|
349 |
theGrammar.addReservedWord("SET"); |
|
350 |
theGrammar.addReservedWord("TO"); |
|
351 |
|
|
352 |
stmt = theGrammar.createStatement("LET"); |
|
353 |
stmt.addRule(stmt.require_any_token("LET", "SET")); |
|
354 |
stmt.addRule(stmt.require_identifier().capture_as("NAME")); |
|
355 |
stmt.addRule(stmt.optional_any_token("=", "TO")); |
|
356 |
stmt.addRule(stmt.require_expression().capture_as("VALUE")); |
|
357 |
stmt.code( |
|
358 |
ExpressionBuilder.FUNCTION_LET, |
|
359 |
stmt.args_names("NAME", "VALUE") |
|
360 |
); |
|
361 |
return stmt; |
|
362 |
} |
|
363 |
|
|
364 |
private Statement createLet2Statement(Grammar theGrammar) { |
|
365 |
Statement stmt; |
|
366 |
|
|
367 |
stmt = theGrammar.createStatement("LET2"); |
|
368 |
stmt.addRule(stmt.require_identifier().capture_as("NAME")); |
|
369 |
stmt.addRule(stmt.require_any_token(":=")); |
|
370 |
stmt.addRule(stmt.require_expression().capture_as("VALUE")); |
|
371 |
stmt.code( |
|
372 |
ExpressionBuilder.FUNCTION_LET, |
|
373 |
stmt.args_names("NAME", "VALUE") |
|
374 |
); |
|
375 |
return stmt; |
|
376 |
} |
|
377 |
|
|
378 |
private Statement createArrayStatement(Grammar theGrammar) { |
|
379 |
Statement stmt; |
|
380 |
|
|
381 |
theGrammar.addReservedWord("ARRAY"); |
|
382 |
|
|
383 |
stmt = theGrammar.createStatement("ARRAY"); |
|
384 |
stmt.addRule(stmt.require_any_token("ARRAY")); |
|
385 |
stmt.addRule(stmt.require_any_token("[")); |
|
386 |
stmt.addRule(stmt.require_expressions(",").capture_as("VALUES")); |
|
387 |
stmt.addRule(stmt.require_any_token("]")); |
|
388 |
stmt.code( |
|
389 |
FUNCTION_LIST, |
|
390 |
stmt.args_expand("VALUES") |
|
391 |
); |
|
392 |
return stmt; |
|
393 |
} |
|
394 |
|
|
395 |
private Statement createCastStatement(Grammar theGrammar) { |
|
396 |
Statement stmt; |
|
397 |
|
|
398 |
theGrammar.addReservedWord("CAST"); |
|
399 |
theGrammar.addReservedWord("AS"); |
|
400 |
|
|
401 |
stmt = theGrammar.createStatement("CAST"); |
|
402 |
stmt.addRule(stmt.require_any_token("CAST")); |
|
403 |
stmt.addRule(stmt.optional_any_token("(") |
|
404 |
.addRuleOnTrue(stmt.require_expression(false).capture_as("VALUE")) |
|
326 | 405 |
.addRuleOnTrue(stmt.require_any_token("AS")) |
327 | 406 |
.addRuleOnTrue(stmt.require_identifier().capture_as("TYPE")) |
328 |
.addRuleOnTrue(stmt.require_any_token(")"))
|
|
329 |
.addRuleOnFalse(stmt.require_expression().capture_as("VALUE")) |
|
407 |
.addRuleOnTrue(stmt.require_any_token(")")) |
|
408 |
.addRuleOnFalse(stmt.require_expression(false).capture_as("VALUE"))
|
|
330 | 409 |
.addRuleOnFalse(stmt.require_any_token("AS")) |
331 | 410 |
.addRuleOnFalse(stmt.require_identifier().capture_as("TYPE")) |
332 |
); |
|
333 |
stmt.code( |
|
334 |
FUNCTION_CAST, |
|
335 |
stmt.args_names("VALUE","TYPE") |
|
336 |
); |
|
337 |
theGrammar.addStatement(stmt); |
|
338 |
|
|
339 |
stmt = theGrammar.createStatement("MATCH"); |
|
340 |
stmt.addRule(stmt.require_any_token("MATCH")); |
|
341 |
stmt.addRule(stmt.optional_any_token("(") |
|
411 |
); |
|
412 |
stmt.code( |
|
413 |
FUNCTION_CAST, |
|
414 |
stmt.args_names("VALUE", "TYPE") |
|
415 |
); |
|
416 |
return stmt; |
|
417 |
} |
|
418 |
|
|
419 |
private Statement createMatchStatement(Grammar theGrammar) { |
|
420 |
Statement stmt; |
|
421 |
|
|
422 |
theGrammar.addReservedWord("MATCH"); |
|
423 |
|
|
424 |
stmt = theGrammar.createStatement("MATCH"); |
|
425 |
stmt.addRule(stmt.require_any_token("MATCH")); |
|
426 |
stmt.addRule(stmt.optional_any_token("(") |
|
342 | 427 |
.addRuleOnTrue(stmt.require_expression().capture_as("VALUE")) |
343 | 428 |
.addRuleOnTrue(stmt.require_any_token(",")) |
344 | 429 |
.addRuleOnTrue(stmt.require_expression().capture_as("PATTERN")) |
345 |
.addRuleOnTrue(stmt.require_any_token(")"))
|
|
430 |
.addRuleOnTrue(stmt.require_any_token(")")) |
|
346 | 431 |
.addRuleOnFalse(stmt.require_expression().capture_as("VALUE")) |
347 | 432 |
.addRuleOnFalse(stmt.optional_any_token(",")) |
348 | 433 |
.addRuleOnFalse(stmt.require_expression().capture_as("PATTERN")) |
349 |
); |
|
350 |
stmt.code( |
|
351 |
OPERATOR_REGEXP, |
|
352 |
stmt.args_names("VALUE","PATTERN") |
|
353 |
); |
|
354 |
theGrammar.addStatement(stmt); |
|
355 |
|
|
356 |
stmt = theGrammar.createStatement("DATE"); |
|
357 |
stmt.addRule(stmt.require_any_token("DATE")); |
|
358 |
stmt.addRule(stmt.require_expression().capture_as("EXP")); |
|
359 |
stmt.code(FUNCTION_DATE,stmt.args_names("EXP")); |
|
360 |
theGrammar.addStatement(stmt); |
|
434 |
); |
|
435 |
stmt.code( |
|
436 |
OPERATOR_REGEXP, |
|
437 |
stmt.args_names("VALUE", "PATTERN") |
|
438 |
); |
|
439 |
return stmt; |
|
440 |
} |
|
361 | 441 |
|
362 |
stmt = theGrammar.createStatement("TIME"); |
|
363 |
stmt.addRule(stmt.require_any_token("TIME")); |
|
364 |
stmt.addRule(stmt.require_expression().capture_as("EXP")); |
|
365 |
stmt.code(FUNCTION_TIME,stmt.args_names("EXP")); |
|
366 |
theGrammar.addStatement(stmt); |
|
442 |
private Statement createDateStatement(Grammar theGrammar) { |
|
443 |
Statement stmt; |
|
367 | 444 |
|
368 |
stmt = theGrammar.createStatement("TIMESTAMP"); |
|
369 |
stmt.addRule(stmt.require_any_token("TIMESTAMP")); |
|
370 |
stmt.addRule(stmt.require_expression().capture_as("EXP")); |
|
371 |
stmt.code(FUNCTION_TIMESTAMP,stmt.args_names("EXP")); |
|
372 |
theGrammar.addStatement(stmt); |
|
445 |
stmt = theGrammar.createStatement("DATE"); |
|
446 |
stmt.addRule(stmt.require_any_token("DATE")); |
|
447 |
stmt.addRule(stmt.require_expression().capture_as("EXP")); |
|
448 |
stmt.code(FUNCTION_DATE, stmt.args_names("EXP")); |
|
449 |
return stmt; |
|
450 |
} |
|
373 | 451 |
|
374 |
stmt = theGrammar.createStatement("EXTRACT"); |
|
375 |
stmt.addRule(stmt.require_any_token("EXTRACT")); |
|
376 |
stmt.addRule(stmt.require_any_token("(")); |
|
377 |
stmt.addRule(stmt.require_any_token("CENTURY","DAY","DECADE", |
|
378 |
"DOW","DOY","HOUR","ISODOW","ISOYEAR","MICROSECONDS", |
|
379 |
"MILLENNIUM","MILLUSECONDS","MINUTE","MONTH","QUARTER", |
|
380 |
"SECOND","WEEK","YEAR").capture_as("FIELD")); |
|
381 |
stmt.addRule(stmt.require_any_token("FROM")); |
|
382 |
stmt.addRule(stmt.require_expression().capture_as("EXP")); |
|
383 |
stmt.addRule(stmt.require_any_token(")")); |
|
384 |
stmt.code(FUNCTION_EXTRACT,stmt.args_names("FIELD", "EXP")); |
|
385 |
theGrammar.addStatement(stmt); |
|
452 |
private Statement createTimeStatement(Grammar theGrammar) { |
|
453 |
Statement stmt; |
|
386 | 454 |
|
387 |
stmt = theGrammar.createStatement("DICT"); |
|
388 |
stmt.addRule(stmt.require_any_token("{")); |
|
389 |
stmt.addRule(stmt.repeat() |
|
390 |
.addRule(stmt.optional_literal_string().capture_as("NAME#") |
|
391 |
.addRuleOnFalse(stmt.require_identifier().capture_as("NAME#")) |
|
392 |
) |
|
393 |
.addRule(stmt.require_any_token(":","=>")) |
|
394 |
.addRule(stmt.require_expression().capture_as("VALUE#")) |
|
395 |
.addRule(stmt.optional_any_token(",") |
|
396 |
.addRuleOnFalse(stmt.break_loop()) |
|
397 |
) |
|
398 |
); |
|
399 |
stmt.addRule(stmt.require_any_token("}")); |
|
400 |
stmt.code(FUNCTION_DICT,new DictArgsBuilder("NAME#", "VALUE#")); |
|
401 |
theGrammar.addStatement(stmt); |
|
455 |
stmt = theGrammar.createStatement("TIME"); |
|
456 |
stmt.addRule(stmt.require_any_token("TIME")); |
|
457 |
stmt.addRule(stmt.require_expression().capture_as("EXP")); |
|
458 |
stmt.code(FUNCTION_TIME, stmt.args_names("EXP")); |
|
459 |
return stmt; |
|
460 |
} |
|
402 | 461 |
|
403 |
stmt = theGrammar.createStatement("LIST"); |
|
404 |
stmt.addRule(stmt.require_any_token("[")); |
|
405 |
stmt.addRule(stmt.repeat() |
|
406 |
.addRule(stmt.require_expression().capture_as("VALUE#")) |
|
407 |
.addRule(stmt.optional_any_token(",") |
|
408 |
.addRuleOnFalse(stmt.break_loop()) |
|
409 |
) |
|
410 |
); |
|
411 |
stmt.addRule(stmt.require_any_token("]")); |
|
412 |
stmt.code(FUNCTION_LIST,stmt.args_names("VALUE#")); |
|
413 |
theGrammar.addStatement(stmt); |
|
414 |
|
|
415 |
this.grammar = theGrammar; |
|
462 |
private Statement createTimestampStatement(Grammar theGrammar) { |
|
463 |
Statement stmt; |
|
416 | 464 |
|
417 |
stmt = theGrammar.createStatement("PASS"); |
|
418 |
stmt.addRule(stmt.require_any_token("PASS")); |
|
419 |
stmt.addRule(stmt.optional_any_token("(") |
|
465 |
stmt = theGrammar.createStatement("TIMESTAMP"); |
|
466 |
stmt.addRule(stmt.require_any_token("TIMESTAMP")); |
|
467 |
stmt.addRule(stmt.require_expression().capture_as("EXP")); |
|
468 |
stmt.code(FUNCTION_TIMESTAMP, stmt.args_names("EXP")); |
|
469 |
return stmt; |
|
470 |
} |
|
471 |
|
|
472 |
private Statement createExtractStatement(Grammar theGrammar) { |
|
473 |
Statement stmt; |
|
474 |
|
|
475 |
stmt = theGrammar.createStatement("EXTRACT"); |
|
476 |
stmt.addRule(stmt.require_any_token("EXTRACT")); |
|
477 |
stmt.addRule(stmt.require_any_token("(")); |
|
478 |
stmt.addRule(stmt.require_any_token("CENTURY", "DAY", "DECADE", |
|
479 |
"DOW", "DOY", "HOUR", "ISODOW", "ISOYEAR", "MICROSECONDS", |
|
480 |
"MILLENNIUM", "MILLUSECONDS", "MINUTE", "MONTH", "QUARTER", |
|
481 |
"SECOND", "WEEK", "YEAR").capture_as("FIELD")); |
|
482 |
stmt.addRule(stmt.require_any_token("FROM")); |
|
483 |
stmt.addRule(stmt.require_expression().capture_as("EXP")); |
|
484 |
stmt.addRule(stmt.require_any_token(")")); |
|
485 |
stmt.code(FUNCTION_EXTRACT, stmt.args_names("FIELD", "EXP")); |
|
486 |
return stmt; |
|
487 |
} |
|
488 |
|
|
489 |
private Statement createDictStatement(Grammar theGrammar) { |
|
490 |
Statement stmt; |
|
491 |
|
|
492 |
stmt = theGrammar.createStatement("DICT"); |
|
493 |
stmt.addRule(stmt.require_any_token("{")); |
|
494 |
stmt.addRule(stmt.repeat() |
|
495 |
.addRule(stmt.optional_literal_string().capture_as("NAME#") |
|
496 |
.addRuleOnFalse(stmt.require_identifier().capture_as("NAME#")) |
|
497 |
) |
|
498 |
.addRule(stmt.require_any_token(":", "=>")) |
|
499 |
.addRule(stmt.require_expression().capture_as("VALUE#")) |
|
500 |
.addRule(stmt.optional_any_token(",") |
|
501 |
.addRuleOnFalse(stmt.break_loop()) |
|
502 |
) |
|
503 |
); |
|
504 |
stmt.addRule(stmt.require_any_token("}")); |
|
505 |
stmt.code(FUNCTION_DICT, new DictArgsBuilder("NAME#", "VALUE#")); |
|
506 |
return stmt; |
|
507 |
} |
|
508 |
|
|
509 |
private Statement createListStatement(Grammar theGrammar) { |
|
510 |
Statement stmt; |
|
511 |
|
|
512 |
stmt = theGrammar.createStatement("LIST"); |
|
513 |
stmt.addRule(stmt.require_any_token("[")); |
|
514 |
stmt.addRule(stmt.repeat() |
|
515 |
.addRule(stmt.require_expression().capture_as("VALUE#")) |
|
516 |
.addRule(stmt.optional_any_token(",") |
|
517 |
.addRuleOnFalse(stmt.break_loop()) |
|
518 |
) |
|
519 |
); |
|
520 |
stmt.addRule(stmt.require_any_token("]")); |
|
521 |
stmt.code(FUNCTION_LIST, stmt.args_names("VALUE#")); |
|
522 |
return stmt; |
|
523 |
} |
|
524 |
|
|
525 |
private Statement createPassStatement(Grammar theGrammar) { |
|
526 |
Statement stmt; |
|
527 |
|
|
528 |
theGrammar.addReservedWord("PASS"); |
|
529 |
|
|
530 |
stmt = theGrammar.createStatement("PASS"); |
|
531 |
stmt.addRule(stmt.require_any_token("PASS")); |
|
532 |
stmt.addRule(stmt.optional_any_token("(") |
|
420 | 533 |
.addRuleOnTrue(stmt.require_expressions(",").capture_as("ARGS#")) |
421 |
.addRuleOnTrue(stmt.require_any_token(")")) |
|
422 |
); |
|
423 |
stmt.code( |
|
424 |
"PASS", |
|
425 |
stmt.args_names("ARGS#") |
|
426 |
); |
|
427 |
theGrammar.addStatement(stmt); |
|
534 |
.addRuleOnTrue(stmt.require_any_token(")")) |
|
535 |
); |
|
536 |
stmt.code( |
|
537 |
"PASS", |
|
538 |
stmt.args_names("ARGS#") |
|
539 |
); |
|
540 |
return stmt; |
|
541 |
} |
|
428 | 542 |
|
429 |
stmt = theGrammar.createStatement("CALL"); |
|
430 |
stmt.addRule(stmt.require_any_token("CALL")); |
|
431 |
stmt.addRule(stmt.require_identifier().capture_as("CALLABLE")); |
|
432 |
stmt.addRule(stmt.optional_any_token("(") |
|
543 |
private Statement createCallStatement(Grammar theGrammar) { |
|
544 |
Statement stmt; |
|
545 |
|
|
546 |
theGrammar.addReservedWord("CALL"); |
|
547 |
|
|
548 |
stmt = theGrammar.createStatement("CALL"); |
|
549 |
stmt.addRule(stmt.require_any_token("CALL")); |
|
550 |
stmt.addRule(stmt.require_identifier().capture_as("CALLABLE")); |
|
551 |
stmt.addRule(stmt.optional_any_token("(") |
|
433 | 552 |
.addRuleOnTrue(stmt.require_expressions(",").capture_as("ARGS#")) |
434 |
.addRuleOnTrue(stmt.require_any_token(")")) |
|
435 |
); |
|
436 |
stmt.code( |
|
437 |
"CALL", |
|
438 |
stmt.args_names("CALLABLE","ARGS#") |
|
439 |
); |
|
440 |
theGrammar.addStatement(stmt); |
|
441 |
} |
|
442 |
return grammar; |
|
553 |
.addRuleOnTrue(stmt.require_any_token(")")) |
|
554 |
); |
|
555 |
stmt.code( |
|
556 |
"CALL", |
|
557 |
stmt.args_names("CALLABLE", "ARGS#") |
|
558 |
); |
|
559 |
return stmt; |
|
443 | 560 |
} |
444 |
|
|
561 |
|
|
445 | 562 |
} |
Also available in: Unified diff