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 @ 44198
History | View | Annotate | Download (10.7 KB)
1 | 44139 | jjdelcerro | package org.gvsig.expressionevaluator.impl.grammars; |
---|---|---|---|
2 | |||
3 | 44198 | jjdelcerro | import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_CAST; |
4 | 44139 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
5 | import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
||
6 | import org.gvsig.expressionevaluator.Grammar; |
||
7 | import org.gvsig.expressionevaluator.Statement; |
||
8 | import org.gvsig.expressionevaluator.impl.function.operator.RegExpOperator; |
||
9 | import org.gvsig.expressionevaluator.impl.function.programming.IIFFunction; |
||
10 | import org.gvsig.expressionevaluator.impl.function.programming.LetFunction; |
||
11 | import org.gvsig.expressionevaluator.impl.function.programming.CaseFunction; |
||
12 | import org.gvsig.expressionevaluator.impl.function.programming.CodeBlockFunction; |
||
13 | import org.gvsig.expressionevaluator.impl.function.programming.CreateFnFunction; |
||
14 | import org.gvsig.expressionevaluator.impl.function.programming.ForEachFunction; |
||
15 | import org.gvsig.expressionevaluator.impl.function.programming.ReturnFunction; |
||
16 | 44144 | jjdelcerro | import org.gvsig.expressionevaluator.impl.function.programming.TryFunction; |
17 | 44139 | jjdelcerro | import org.gvsig.expressionevaluator.impl.function.programming.WhileFunction; |
18 | import org.gvsig.expressionevaluator.impl.function.typeconversion.CastFunction; |
||
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 | stmt = theGrammar.createStatement("BLOCK");
|
||
66 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("BEGIN"));
|
67 | 44139 | jjdelcerro | stmt.addRule(stmt.require_expressions("VALUES", ";")); |
68 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("END"));
|
69 | 44139 | jjdelcerro | stmt.code( |
70 | CodeBlockFunction.NAME, |
||
71 | stmt.args_expand("VALUES")
|
||
72 | ); |
||
73 | theGrammar.addStatement(stmt); |
||
74 | |||
75 | stmt = theGrammar.createStatement("IF");
|
||
76 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("IF"));
|
77 | 44139 | jjdelcerro | stmt.addRule(stmt.require_expression("CONDITION"));
|
78 | 44144 | jjdelcerro | stmt.addRule(stmt.optional_any_token("THEN"));
|
79 | stmt.addRule(stmt.optional_any_token(":"));
|
||
80 | 44139 | jjdelcerro | stmt.addRule(stmt.require_expression("ONTRUE"));
|
81 | 44144 | jjdelcerro | stmt.addRule(stmt.optional_any_token("ELSE")
|
82 | .addRuleOnTrue(stmt.optional_any_token(":"))
|
||
83 | 44139 | jjdelcerro | .addRuleOnTrue(stmt.require_expression("ONFALSE"))
|
84 | ); |
||
85 | stmt.code( |
||
86 | IIFFunction.NAME, |
||
87 | stmt.args_names("CONDITION","ONTRUE","ONFALSE") |
||
88 | ); |
||
89 | theGrammar.addStatement(stmt); |
||
90 | |||
91 | stmt = theGrammar.createStatement("WHILE");
|
||
92 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("WHILE"));
|
93 | 44139 | jjdelcerro | stmt.addRule(stmt.require_expression("CONDITION"));
|
94 | 44144 | jjdelcerro | stmt.addRule(stmt.optional_any_token("DO"));
|
95 | stmt.addRule(stmt.optional_any_token(":"));
|
||
96 | 44139 | jjdelcerro | stmt.addRule(stmt.require_expression("BODY"));
|
97 | 44144 | jjdelcerro | stmt.addRule(stmt.optional_any_token("DONE"));
|
98 | 44139 | jjdelcerro | stmt.code( |
99 | WhileFunction.NAME, |
||
100 | stmt.args_names("CONDITION","BODY") |
||
101 | ); |
||
102 | theGrammar.addStatement(stmt); |
||
103 | |||
104 | stmt = theGrammar.createStatement("CAST");
|
||
105 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("CAST"));
|
106 | stmt.addRule(stmt.optional_any_token("(")
|
||
107 | 44139 | jjdelcerro | .addRuleOnTrue(stmt.require_expression("VALUE"))
|
108 | 44144 | jjdelcerro | .addRuleOnTrue(stmt.require_any_token("AS"))
|
109 | 44139 | jjdelcerro | .addRuleOnTrue(stmt.require_identifier("TYPE"))
|
110 | 44144 | jjdelcerro | .addRuleOnTrue(stmt.require_any_token(")"))
|
111 | 44139 | jjdelcerro | .addRuleOnFalse(stmt.require_expression("VALUE"))
|
112 | 44144 | jjdelcerro | .addRuleOnFalse(stmt.require_any_token("AS"))
|
113 | 44139 | jjdelcerro | .addRuleOnFalse(stmt.require_identifier("TYPE"))
|
114 | ); |
||
115 | stmt.code( |
||
116 | 44198 | jjdelcerro | FUNCTION_CAST, |
117 | 44139 | jjdelcerro | stmt.args_names("VALUE","TYPE") |
118 | ); |
||
119 | theGrammar.addStatement(stmt); |
||
120 | |||
121 | stmt = theGrammar.createStatement("MATCH");
|
||
122 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("MATCH"));
|
123 | stmt.addRule(stmt.optional_any_token("(")
|
||
124 | 44139 | jjdelcerro | .addRuleOnTrue(stmt.require_expression("VALUE"))
|
125 | 44144 | jjdelcerro | .addRuleOnTrue(stmt.require_any_token(","))
|
126 | 44139 | jjdelcerro | .addRuleOnTrue(stmt.require_expression("PATTERN"))
|
127 | 44144 | jjdelcerro | .addRuleOnTrue(stmt.require_any_token(")"))
|
128 | 44139 | jjdelcerro | .addRuleOnFalse(stmt.require_expression("VALUE"))
|
129 | 44144 | jjdelcerro | .addRuleOnFalse(stmt.optional_any_token(","))
|
130 | 44139 | jjdelcerro | .addRuleOnFalse(stmt.require_expression("PATTERN"))
|
131 | ); |
||
132 | stmt.code( |
||
133 | RegExpOperator.NAME, |
||
134 | stmt.args_names("VALUE","PATTERN") |
||
135 | ); |
||
136 | theGrammar.addStatement(stmt); |
||
137 | |||
138 | // stmt = theGrammar.createStatement("FOR");
|
||
139 | // stmt.addRule(stmt.require_token("FOR"));
|
||
140 | // stmt.addRule(stmt.require_expression("INIT"));
|
||
141 | // stmt.addRule(stmt.require_token(";"));
|
||
142 | // stmt.addRule(stmt.require_expression("CONDITION"));
|
||
143 | // stmt.addRule(stmt.require_token(";"));
|
||
144 | // stmt.addRule(stmt.require_expression("STEP"));
|
||
145 | // stmt.addRule(stmt.optional_token(":"));
|
||
146 | // stmt.addRule(stmt.require_expression("BODY"));
|
||
147 | // stmt.addRule(stmt.optional_token("END"));
|
||
148 | // stmt.code(ForFunction.NAME,"INIT","CONDITION","STEP","BODY");
|
||
149 | // theGrammar.addStatement(stmt);
|
||
150 | |||
151 | stmt = theGrammar.createStatement("FOREACH");
|
||
152 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("FOR"));
|
153 | 44139 | jjdelcerro | stmt.addRule(stmt.require_identifier("VAR"));
|
154 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("IN"));
|
155 | 44139 | jjdelcerro | stmt.addRule(stmt.require_expression("ITERABLE"));
|
156 | 44144 | jjdelcerro | stmt.addRule(stmt.optional_any_token(":"));
|
157 | 44139 | jjdelcerro | stmt.addRule(stmt.require_expression("BODY"));
|
158 | stmt.code( |
||
159 | ForEachFunction.NAME, |
||
160 | stmt.args_names("VAR","ITERABLE","BODY") |
||
161 | ); |
||
162 | theGrammar.addStatement(stmt); |
||
163 | |||
164 | stmt = theGrammar.createStatement("CASE");
|
||
165 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("CASE"));
|
166 | 44139 | jjdelcerro | stmt.addRule( |
167 | 44144 | jjdelcerro | stmt.repeat_until_any_tokens("END","ELSE") |
168 | .addRule(stmt.require_any_token("WHEN"))
|
||
169 | 44139 | jjdelcerro | .addRule(stmt.require_expression("CONDITION#"))
|
170 | 44144 | jjdelcerro | .addRule(stmt.optional_any_token("THEN"))
|
171 | .addRule(stmt.optional_any_token(":"))
|
||
172 | 44139 | jjdelcerro | .addRule(stmt.require_expression("ONTRUE#"))
|
173 | ); |
||
174 | 44144 | jjdelcerro | stmt.addRule(stmt.optional_any_token("ELSE")
|
175 | .addRuleOnTrue(stmt.optional_any_token(":"))
|
||
176 | 44139 | jjdelcerro | .addRuleOnTrue(stmt.require_expression("ELSE"))
|
177 | ); |
||
178 | 44144 | jjdelcerro | stmt.addRule(stmt.optional_any_token("END"));
|
179 | 44139 | jjdelcerro | stmt.code( |
180 | CaseFunction.NAME, |
||
181 | stmt.args_names("CONDITION#","ONTRUE#","ELSE") |
||
182 | ); |
||
183 | theGrammar.addStatement(stmt); |
||
184 | |||
185 | stmt = theGrammar.createStatement("USERFUNCTION");
|
||
186 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("DEF"));
|
187 | 44139 | jjdelcerro | stmt.addRule(stmt.require_identifier("FUNCTION_NAME"));
|
188 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("("));
|
189 | 44139 | jjdelcerro | stmt.addRule(stmt.optional_identifiers("PARAM_NAMES", ",")); |
190 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token(")"));
|
191 | stmt.addRule(stmt.optional_any_token(":"));
|
||
192 | 44139 | jjdelcerro | stmt.addRule(stmt.require_expression("BODY"));
|
193 | stmt.code( |
||
194 | CreateFnFunction.NAME, |
||
195 | stmt.args_names("FUNCTION_NAME","PARAM_NAMES","BODY") |
||
196 | ); |
||
197 | theGrammar.addStatement(stmt); |
||
198 | |||
199 | stmt = theGrammar.createStatement("RETURN");
|
||
200 | 44144 | jjdelcerro | stmt.addRule(stmt.require_any_token("RETURN"));
|
201 | 44139 | jjdelcerro | stmt.addRule(stmt.require_expressions("VALUES", ",")); |
202 | stmt.code( |
||
203 | ReturnFunction.NAME, |
||
204 | stmt.args_expand("VALUES")
|
||
205 | ); |
||
206 | theGrammar.addStatement(stmt); |
||
207 | |||
208 | 44144 | jjdelcerro | stmt = theGrammar.createStatement("TRY");
|
209 | stmt.addRule(stmt.require_any_token("TRY"));
|
||
210 | stmt.addRule(stmt.optional_any_token(":"));
|
||
211 | stmt.addRule(stmt.require_expression("VALUE"));
|
||
212 | stmt.addRule(stmt.require_any_token("EXCEPT","CATH")); |
||
213 | stmt.addRule(stmt.optional_any_token(":"));
|
||
214 | stmt.addRule(stmt.require_expression("ONERROR"));
|
||
215 | stmt.code( |
||
216 | TryFunction.NAME, |
||
217 | stmt.args_names("VALUE","ONERROR") |
||
218 | ); |
||
219 | theGrammar.addStatement(stmt); |
||
220 | |||
221 | 44139 | jjdelcerro | // stmt = theGrammar.createStatement("FETCHFIRST");
|
222 | // stmt.addRule(stmt.require_token("FETCH"));
|
||
223 | // stmt.addRule(stmt.require_token("FIRST"));
|
||
224 | // stmt.addRule(stmt.require_expression("VALUE"));
|
||
225 | // stmt.addRule(stmt.optional_token("FROM"));
|
||
226 | // stmt.addRule(stmt.require_expression("FROM"));
|
||
227 | // stmt.addRule(stmt.optional_token("WHERE").addRuleOnTrue(stmt.require_expression("WHERE")));
|
||
228 | // stmt.addRule(stmt.optional_token("ORDER","BY").addRuleOnTrue(stmt.require_expression("ORDER")));
|
||
229 | // stmt.code("FETCH_FIRST","VALUE","FROM","WHERE");
|
||
230 | // theGrammar.addStatement(stmt);
|
||
231 | |||
232 | this.grammar = theGrammar;
|
||
233 | } |
||
234 | return grammar;
|
||
235 | } |
||
236 | |||
237 | } |