comparison src/parser/Action.d @ 206:d3c148ca429b

Major moving of files. all src now goes into src, all docs in docs.
author Anders Johnsen <skabet@gmail.com>
date Tue, 12 Aug 2008 18:14:56 +0200
parents
children e0551773a005
comparison
equal deleted inserted replaced
205:8387cbaa85ab 206:d3c148ca429b
1 module parser.Action;
2
3 import lexer.Token;
4
5 /**
6 Used to indicate what type of operator is used in a given binary expression
7 (and unary expressions?)
8 */
9 public enum Operator
10 {
11 Assign,
12
13 Eq, Ne,
14
15 Lt, Le,
16 Gt, Ge,
17
18 Add, Sub,
19 Mul, Div, Mod,
20 }
21
22
23 class Id
24 {
25 public static Id opCall(Token tok)
26 {
27 auto id = new Id();
28 id.tok = tok;
29 return id;
30 }
31 Token tok;
32 }
33
34 class PointerId : Id
35 {
36 public static PointerId opCall(Id id)
37 {
38 auto p = new PointerId();
39 p.id = id;
40 return p;
41 }
42
43 Id id;
44 }
45
46 class ArrayId : Id
47 {
48 public static ArrayId opCall(Id id, Object number)
49 {
50 auto a = new ArrayId();
51 a.id = id;
52 a.number = number;
53 return a;
54 }
55
56 Id id;
57 Object number;
58 }
59
60 /**
61 Represents a fully qualified name, with some packages and a final identifier.
62 The identifier should always be set, but packages may have length 0.
63 **/
64 struct ModuleName
65 {
66 Id id;
67 Id[] packages;
68
69 /// Get the full ranged spanned by packages and identifier
70 SourceRange asRange()
71 {
72 SourceRange r = id.tok.asRange();
73 foreach (identifier; packages)
74 r = r + identifier.tok.asRange();
75 return r;
76 }
77 }
78
79 /**
80 All methods are optional.
81
82 Warning: Interface is not stable yet. Use the `override` keyword in all classes
83 inheriting from this to get warning if the interface changes.
84 */
85 abstract class Action
86 {
87 /**
88 A few aliases to indicate what methods should be dealing with the same
89 types.
90
91 Not typesafe, and not using typedef because users would need a lot of
92 casts (and base type would be void*, so no possibility to synchronize,
93 print etc.)
94 */
95 alias Object ExprT;
96 alias Object StmtT; /// ditto
97 alias Object DeclT; /// ditto
98 alias Object ModuleT; /// ditto
99
100 // -- Modules --
101
102 ModuleT actOnModule(ref Token _module, char[] name)
103 {
104 return null;
105 }
106
107 /**
108 This action is called when a file does not start with a module
109 declaration - in which case there is no Token available.
110
111 Instead a SLoc to the start of the file is given.
112 */
113 ModuleT actOnImplicitModule(SourceLocation fileStart, char[] name)
114 {
115 return null;
116 }
117
118 void actOnModuleDecl(ModuleT m, DeclT d)
119 {
120 }
121
122 // -- Declarations --
123
124 /**
125 Called for an import statement, that may be renamed. Id name is null,
126 there is no rename.
127
128 If there are selective imports, its handled in add
129 */
130 DeclT actOnImport(ref Token _import, ref ModuleName target, Id* name)
131 {
132 return null;
133 }
134
135 /**
136 */
137 void addSelectiveImport(DeclT _import, ref Id target, Id* name)
138 {
139 }
140
141 /**
142 Either we should have one case that handles a lot of things, or we should
143 have a lot of separate cases.
144 As an example, this method could handle the params in `int f(int, int)`
145 as well as handling `int x` at both top-level, in classes and in methods.
146
147 The other solution is an addParamToFunc or similar.
148 */
149 DeclT actOnDeclarator(ref Id type, ref Id name, ExprT init)
150 {
151 return null;
152 }
153
154 /**
155 Add a struct member to a struct.
156 */
157 void actOnStructMember(DeclT st_decl, DeclT m_decl) //ref Id type, ref Id name, ExprT init)
158 {
159 return null;
160 }
161
162 /**
163 Add an initialization expression to a previously created decl.
164
165 Used for default values on function params and for values to local
166 variables.
167 */
168 void addInitToDeclarator(DeclT decl, ExprT exp)
169 {
170 }
171
172 /**
173 Called at the start of a function, doesn't get a lot of info - that is
174 added later on, through addFuncArg and actOnEndOfFunction.
175 */
176 DeclT actOnStartOfFunctionDef(ref Id type, ref Id name)
177 {
178 return null;
179 }
180
181 /**
182 Add a new parameter to the function func.
183 */
184 void addFuncArg(DeclT func, Id type, Id name)
185 {
186 }
187
188 /**
189 Finish off the function, by giving it the body (a single statement, so
190 you probably want some sort of compound statement)
191 */
192 DeclT actOnEndOfFunction(DeclT func, StmtT stmts)
193 {
194 return func;
195 }
196
197 // -- Statements --
198
199 /**
200 Called after parsing a function/while/for/whatever body.
201
202 Note that stmts is to be considered temporary, it might point into the
203 stack and needs to be copied before saving.
204 */
205 StmtT actOnCompoundStmt(ref Token left, ref Token right, StmtT[] stmts)
206 {
207 return null;
208 }
209
210 /**
211 An expression was used as a statement - this includes assignments,
212 function calls.
213
214 Additionally the D spec dictates that expressions with no effect are not
215 legal as statements, but the parser can't test for this so it has to be
216 done in the later stages.
217 */
218 StmtT actOnExprStmt(ExprT exp)
219 {
220 return null;
221 }
222
223 /**
224 Called after parsing return statements.
225
226 loc is the return token.
227 */
228 StmtT actOnReturnStmt(ref Token loc, ExprT exp)
229 {
230 return null;
231 }
232
233 /**
234 */
235 StmtT actOnIfStmt(ref Token ifTok, ExprT cond, StmtT thenBody,
236 ref Token elseTok, StmtT elseBody)
237 {
238 return null;
239 }
240
241 /**
242 */
243 StmtT actOnWhileStmt(ref Token whileTok, ExprT cond, StmtT whileBody)
244 {
245 return null;
246 }
247
248 /**
249 */
250 StmtT actOnDeclStmt(DeclT decl)
251 {
252 return null;
253 }
254
255 StmtT actOnStartOfSwitchStmt()
256 {
257 return null;
258 }
259
260 void actOnCaseStmt()
261 {
262 }
263
264 void actOnDefaultStmt()
265 {
266 }
267
268 StmtT actOnFinishSwitchStmt(StmtT sw)
269 {
270 return sw;
271 }
272
273 // -- Expressions --
274
275 /**
276 A single numerical constant -- this can be absolutely any kind of number.
277 Integers, floats, hex, octal, binary, imaginary and so on.
278 */
279 ExprT actOnNumericConstant(Token op)
280 {
281 return null;
282 }
283
284 /**
285 This is called when identifiers are used in expressions.
286 */
287 ExprT actOnIdentifierExp(Id id)
288 {
289 return null;
290 }
291
292 /**
293 This is called when strings are used in expression
294 */
295 ExprT actOnStringExp(Token t)
296 {
297 return null;
298 }
299
300 /**
301 Unary operator.
302 */
303 ExprT actOnUnaryOp(Token op, ExprT operand)
304 {
305 return null;
306 }
307
308 /**
309 Binary operator.
310 */
311 ExprT actOnBinaryOp(SLoc op_loc, Operator op, ExprT l, ExprT r)
312 {
313 return null;
314 }
315
316 /**
317 Called when using the 'dot' operator.
318 The left hand side can be any expression, but its only possible to look
319 up an identifier.
320 */
321 ExprT actOnMemberReference(ExprT lhs, SourceLocation op, Id member)
322 {
323 return null;
324 }
325
326 /**
327 Called when function calls are encountered.
328
329 Note that args is temporary and might point into the stack. Remember to
330 copy before saving a reference to it.
331 */
332 ExprT actOnCallExpr(ExprT func, ref Token left_paren, ExprT[] args,
333 ref Token right_paren)
334 {
335 return null;
336 }
337
338 /**
339 Called when function calls are encountered.
340 */
341 ExprT actOnIndexEpr(ExprT array, ref Token left_bracket, ExprT index,
342 ref Token right_bracket)
343 {
344 return null;
345 }
346
347 /**
348 Cast expression.
349 */
350 ExprT actOnCastExpr(ref Token _cast, Id type, ExprT exp)
351 {
352 return null;
353 }
354 }
355
356 /**
357 Doesn't do anything at all - can be used for benchmarking the parser.
358 */
359 class NullAction : Action
360 {
361 }
362