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 @ 44262
History | View | Annotate | Download (11.4 KB)
1 | 44139 | jjdelcerro | package org.gvsig.expressionevaluator.impl.grammars; |
---|---|---|---|
2 | |||
3 | 44198 | jjdelcerro | import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_CAST; |
4 | 44207 | jjdelcerro | import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_REGEXP; |
5 | 44139 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
6 | import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
||
7 | import org.gvsig.expressionevaluator.Grammar; |
||
8 | import org.gvsig.expressionevaluator.Statement; |
||
9 | import org.gvsig.expressionevaluator.impl.function.operator.RegExpOperator; |
||
10 | import org.gvsig.expressionevaluator.impl.function.programming.IIFFunction; |
||
11 | import org.gvsig.expressionevaluator.impl.function.programming.LetFunction; |
||
12 | import org.gvsig.expressionevaluator.impl.function.programming.CaseFunction; |
||
13 | import org.gvsig.expressionevaluator.impl.function.programming.CodeBlockFunction; |
||
14 | import org.gvsig.expressionevaluator.impl.function.programming.CreateFnFunction; |
||
15 | import org.gvsig.expressionevaluator.impl.function.programming.ForEachFunction; |
||
16 | import org.gvsig.expressionevaluator.impl.function.programming.ReturnFunction; |
||
17 | 44144 | jjdelcerro | import org.gvsig.expressionevaluator.impl.function.programming.TryFunction; |
18 | 44139 | jjdelcerro | import org.gvsig.expressionevaluator.impl.function.programming.WhileFunction; |
19 | import org.gvsig.expressionevaluator.spi.AbstractGrammarFactory; |
||
20 | |||
21 | /**
|
||
22 | *
|
||
23 | * @author jjdelcerro
|
||
24 | */
|
||
25 | public class BasicGrammarFactory extends AbstractGrammarFactory { |
||
26 | |||
27 | private Grammar grammar;
|
||
28 | |||
29 | public BasicGrammarFactory() {
|
||
30 | super("Basic", true); |
||
31 | } |
||
32 | |||
33 | @Override
|
||
34 | public Grammar createGrammar() {
|
||
35 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
36 | if( this.grammar==null ) { |
||
37 | Grammar theGrammar = manager.createGrammar(this.getName());
|
||
38 | Statement stmt;
|
||
39 | |||
40 | theGrammar.addReservedWord("BEGIN");
|
||
41 | theGrammar.addReservedWord("END");
|
||
42 | theGrammar.addReservedWord("IF");
|
||
43 | theGrammar.addReservedWord("THEN");
|
||
44 | theGrammar.addReservedWord("ELSE");
|
||
45 | theGrammar.addReservedWord("WHILE");
|
||
46 | theGrammar.addReservedWord("CASE");
|
||
47 | theGrammar.addReservedWord("WHEN");
|
||
48 | theGrammar.addReservedWord("DEF");
|
||
49 | theGrammar.addReservedWord("MATCH");
|
||
50 | theGrammar.addReservedWord("CAST");
|
||
51 | 44144 | jjdelcerro | theGrammar.addReservedWord("TRY");
|
52 | theGrammar.addReservedWord("EXCEPT");
|
||
53 | 44139 | jjdelcerro | |
54 | stmt = theGrammar.createStatement("LET");
|
||
55 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("LET", "SET")); |
56 | 44139 | jjdelcerro | stmt.addRule(stmt.require_identifier("NAME"));
|
57 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("="));
|
58 | 44139 | jjdelcerro | stmt.addRule(stmt.require_expression("VALUE"));
|
59 | stmt.code( |
||
60 | LetFunction.NAME, |
||
61 | stmt.args_names("NAME", "VALUE") |
||
62 | ); |
||
63 | theGrammar.addStatement(stmt); |
||
64 | |||
65 | 44262 | jjdelcerro | /*
|
66 | |||
67 | BEGIN
|
||
68 | SET x = 0;
|
||
69 | FOR n in FETCH GEOMETRY FROM STORE 'view','points':
|
||
70 | IF ST_Intersects(n,GEOMETRY) THEN
|
||
71 | SET x = x+1
|
||
72 | EXCEPT
|
||
73 | SET x = -1
|
||
74 | END WITH VALUE x
|
||
75 | |||
76 | */
|
||
77 | |||
78 | |||
79 | 44139 | jjdelcerro | stmt = theGrammar.createStatement("BLOCK");
|
80 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("BEGIN"));
|
81 | 44139 | jjdelcerro | stmt.addRule(stmt.require_expressions("VALUES", ";")); |
82 | 44262 | jjdelcerro | // stmt.addRule(stmt.optional_any_token("EXCEPT")
|
83 | // .addRuleOnTrue(stmt.require_expression("EXCEPT"))
|
||
84 | // );
|
||
85 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("END"));
|
86 | 44262 | jjdelcerro | // stmt.addRule(stmt.optional_any_token("WITH")
|
87 | // .addRuleOnTrue(stmt.require_any_token("VALUE"))
|
||
88 | // .addRuleOnTrue(stmt.require_expression("VALUE"))
|
||
89 | // );
|
||
90 | 44139 | jjdelcerro | stmt.code( |
91 | CodeBlockFunction.NAME, |
||
92 | 44262 | jjdelcerro | // stmt.args_names("EXCEPT","VALUE"),
|
93 | 44139 | jjdelcerro | stmt.args_expand("VALUES")
|
94 | ); |
||
95 | theGrammar.addStatement(stmt); |
||
96 | |||
97 | stmt = theGrammar.createStatement("IF");
|
||
98 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("IF"));
|
99 | 44139 | jjdelcerro | stmt.addRule(stmt.require_expression("CONDITION"));
|
100 | 44144 | jjdelcerro | stmt.addRule(stmt.optional_any_token("THEN"));
|
101 | stmt.addRule(stmt.optional_any_token(":"));
|
||
102 | 44139 | jjdelcerro | stmt.addRule(stmt.require_expression("ONTRUE"));
|
103 | 44144 | jjdelcerro | stmt.addRule(stmt.optional_any_token("ELSE")
|
104 | .addRuleOnTrue(stmt.optional_any_token(":"))
|
||
105 | 44139 | jjdelcerro | .addRuleOnTrue(stmt.require_expression("ONFALSE"))
|
106 | ); |
||
107 | stmt.code( |
||
108 | IIFFunction.NAME, |
||
109 | stmt.args_names("CONDITION","ONTRUE","ONFALSE") |
||
110 | ); |
||
111 | theGrammar.addStatement(stmt); |
||
112 | |||
113 | stmt = theGrammar.createStatement("WHILE");
|
||
114 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("WHILE"));
|
115 | 44139 | jjdelcerro | stmt.addRule(stmt.require_expression("CONDITION"));
|
116 | 44144 | jjdelcerro | stmt.addRule(stmt.optional_any_token("DO"));
|
117 | stmt.addRule(stmt.optional_any_token(":"));
|
||
118 | 44139 | jjdelcerro | stmt.addRule(stmt.require_expression("BODY"));
|
119 | 44144 | jjdelcerro | stmt.addRule(stmt.optional_any_token("DONE"));
|
120 | 44139 | jjdelcerro | stmt.code( |
121 | WhileFunction.NAME, |
||
122 | stmt.args_names("CONDITION","BODY") |
||
123 | ); |
||
124 | theGrammar.addStatement(stmt); |
||
125 | |||
126 | stmt = theGrammar.createStatement("CAST");
|
||
127 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("CAST"));
|
128 | stmt.addRule(stmt.optional_any_token("(")
|
||
129 | 44139 | jjdelcerro | .addRuleOnTrue(stmt.require_expression("VALUE"))
|
130 | 44144 | jjdelcerro | .addRuleOnTrue(stmt.require_any_token("AS"))
|
131 | 44139 | jjdelcerro | .addRuleOnTrue(stmt.require_identifier("TYPE"))
|
132 | 44144 | jjdelcerro | .addRuleOnTrue(stmt.require_any_token(")"))
|
133 | 44139 | jjdelcerro | .addRuleOnFalse(stmt.require_expression("VALUE"))
|
134 | 44144 | jjdelcerro | .addRuleOnFalse(stmt.require_any_token("AS"))
|
135 | 44139 | jjdelcerro | .addRuleOnFalse(stmt.require_identifier("TYPE"))
|
136 | ); |
||
137 | stmt.code( |
||
138 | 44198 | jjdelcerro | FUNCTION_CAST, |
139 | 44139 | jjdelcerro | stmt.args_names("VALUE","TYPE") |
140 | ); |
||
141 | theGrammar.addStatement(stmt); |
||
142 | |||
143 | stmt = theGrammar.createStatement("MATCH");
|
||
144 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("MATCH"));
|
145 | stmt.addRule(stmt.optional_any_token("(")
|
||
146 | 44139 | jjdelcerro | .addRuleOnTrue(stmt.require_expression("VALUE"))
|
147 | 44144 | jjdelcerro | .addRuleOnTrue(stmt.require_any_token(","))
|
148 | 44139 | jjdelcerro | .addRuleOnTrue(stmt.require_expression("PATTERN"))
|
149 | 44144 | jjdelcerro | .addRuleOnTrue(stmt.require_any_token(")"))
|
150 | 44139 | jjdelcerro | .addRuleOnFalse(stmt.require_expression("VALUE"))
|
151 | 44144 | jjdelcerro | .addRuleOnFalse(stmt.optional_any_token(","))
|
152 | 44139 | jjdelcerro | .addRuleOnFalse(stmt.require_expression("PATTERN"))
|
153 | ); |
||
154 | stmt.code( |
||
155 | 44207 | jjdelcerro | OPERATOR_REGEXP, |
156 | 44139 | jjdelcerro | stmt.args_names("VALUE","PATTERN") |
157 | ); |
||
158 | theGrammar.addStatement(stmt); |
||
159 | |||
160 | // stmt = theGrammar.createStatement("FOR");
|
||
161 | // stmt.addRule(stmt.require_token("FOR"));
|
||
162 | // stmt.addRule(stmt.require_expression("INIT"));
|
||
163 | // stmt.addRule(stmt.require_token(";"));
|
||
164 | // stmt.addRule(stmt.require_expression("CONDITION"));
|
||
165 | // stmt.addRule(stmt.require_token(";"));
|
||
166 | // stmt.addRule(stmt.require_expression("STEP"));
|
||
167 | // stmt.addRule(stmt.optional_token(":"));
|
||
168 | // stmt.addRule(stmt.require_expression("BODY"));
|
||
169 | // stmt.addRule(stmt.optional_token("END"));
|
||
170 | // stmt.code(ForFunction.NAME,"INIT","CONDITION","STEP","BODY");
|
||
171 | // theGrammar.addStatement(stmt);
|
||
172 | |||
173 | stmt = theGrammar.createStatement("FOREACH");
|
||
174 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("FOR"));
|
175 | 44139 | jjdelcerro | stmt.addRule(stmt.require_identifier("VAR"));
|
176 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("IN"));
|
177 | 44139 | jjdelcerro | stmt.addRule(stmt.require_expression("ITERABLE"));
|
178 | 44144 | jjdelcerro | stmt.addRule(stmt.optional_any_token(":"));
|
179 | 44139 | jjdelcerro | stmt.addRule(stmt.require_expression("BODY"));
|
180 | stmt.code( |
||
181 | ForEachFunction.NAME, |
||
182 | stmt.args_names("VAR","ITERABLE","BODY") |
||
183 | ); |
||
184 | theGrammar.addStatement(stmt); |
||
185 | |||
186 | stmt = theGrammar.createStatement("CASE");
|
||
187 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("CASE"));
|
188 | 44139 | jjdelcerro | stmt.addRule( |
189 | 44144 | jjdelcerro | stmt.repeat_until_any_tokens("END","ELSE") |
190 | .addRule(stmt.require_any_token("WHEN"))
|
||
191 | 44139 | jjdelcerro | .addRule(stmt.require_expression("CONDITION#"))
|
192 | 44144 | jjdelcerro | .addRule(stmt.optional_any_token("THEN"))
|
193 | .addRule(stmt.optional_any_token(":"))
|
||
194 | 44139 | jjdelcerro | .addRule(stmt.require_expression("ONTRUE#"))
|
195 | ); |
||
196 | 44144 | jjdelcerro | stmt.addRule(stmt.optional_any_token("ELSE")
|
197 | .addRuleOnTrue(stmt.optional_any_token(":"))
|
||
198 | 44139 | jjdelcerro | .addRuleOnTrue(stmt.require_expression("ELSE"))
|
199 | ); |
||
200 | 44144 | jjdelcerro | stmt.addRule(stmt.optional_any_token("END"));
|
201 | 44139 | jjdelcerro | stmt.code( |
202 | CaseFunction.NAME, |
||
203 | stmt.args_names("CONDITION#","ONTRUE#","ELSE") |
||
204 | ); |
||
205 | theGrammar.addStatement(stmt); |
||
206 | |||
207 | stmt = theGrammar.createStatement("USERFUNCTION");
|
||
208 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("DEF"));
|
209 | 44139 | jjdelcerro | stmt.addRule(stmt.require_identifier("FUNCTION_NAME"));
|
210 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("("));
|
211 | 44139 | jjdelcerro | stmt.addRule(stmt.optional_identifiers("PARAM_NAMES", ",")); |
212 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token(")"));
|
213 | stmt.addRule(stmt.optional_any_token(":"));
|
||
214 | 44139 | jjdelcerro | stmt.addRule(stmt.require_expression("BODY"));
|
215 | stmt.code( |
||
216 | CreateFnFunction.NAME, |
||
217 | stmt.args_names("FUNCTION_NAME","PARAM_NAMES","BODY") |
||
218 | ); |
||
219 | theGrammar.addStatement(stmt); |
||
220 | |||
221 | stmt = theGrammar.createStatement("RETURN");
|
||
222 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("RETURN"));
|
223 | 44139 | jjdelcerro | stmt.addRule(stmt.require_expressions("VALUES", ",")); |
224 | stmt.code( |
||
225 | ReturnFunction.NAME, |
||
226 | stmt.args_expand("VALUES")
|
||
227 | ); |
||
228 | theGrammar.addStatement(stmt); |
||
229 | |||
230 | 44144 | jjdelcerro | stmt = theGrammar.createStatement("TRY");
|
231 | stmt.addRule(stmt.require_any_token("TRY"));
|
||
232 | stmt.addRule(stmt.optional_any_token(":"));
|
||
233 | stmt.addRule(stmt.require_expression("VALUE"));
|
||
234 | stmt.addRule(stmt.require_any_token("EXCEPT","CATH")); |
||
235 | stmt.addRule(stmt.optional_any_token(":"));
|
||
236 | stmt.addRule(stmt.require_expression("ONERROR"));
|
||
237 | stmt.code( |
||
238 | TryFunction.NAME, |
||
239 | stmt.args_names("VALUE","ONERROR") |
||
240 | ); |
||
241 | theGrammar.addStatement(stmt); |
||
242 | |||
243 | 44139 | jjdelcerro | // stmt = theGrammar.createStatement("FETCHFIRST");
|
244 | // stmt.addRule(stmt.require_token("FETCH"));
|
||
245 | // stmt.addRule(stmt.require_token("FIRST"));
|
||
246 | // stmt.addRule(stmt.require_expression("VALUE"));
|
||
247 | // stmt.addRule(stmt.optional_token("FROM"));
|
||
248 | // stmt.addRule(stmt.require_expression("FROM"));
|
||
249 | // stmt.addRule(stmt.optional_token("WHERE").addRuleOnTrue(stmt.require_expression("WHERE")));
|
||
250 | // stmt.addRule(stmt.optional_token("ORDER","BY").addRuleOnTrue(stmt.require_expression("ORDER")));
|
||
251 | // stmt.code("FETCH_FIRST","VALUE","FROM","WHERE");
|
||
252 | // theGrammar.addStatement(stmt);
|
||
253 | |||
254 | this.grammar = theGrammar;
|
||
255 | } |
||
256 | return grammar;
|
||
257 | } |
||
258 | |||
259 | } |