diff options
Diffstat (limited to 'src/monkey/seaspider/org/gnunet/seaspider/parser/visitors/TreeFormatter.java')
-rw-r--r-- | src/monkey/seaspider/org/gnunet/seaspider/parser/visitors/TreeFormatter.java | 980 |
1 files changed, 980 insertions, 0 deletions
diff --git a/src/monkey/seaspider/org/gnunet/seaspider/parser/visitors/TreeFormatter.java b/src/monkey/seaspider/org/gnunet/seaspider/parser/visitors/TreeFormatter.java new file mode 100644 index 000000000..7cb5ae051 --- /dev/null +++ b/src/monkey/seaspider/org/gnunet/seaspider/parser/visitors/TreeFormatter.java | |||
@@ -0,0 +1,980 @@ | |||
1 | // | ||
2 | // Generated by JTB 1.3.2 | ||
3 | // | ||
4 | package org.gnunet.seaspider.parser.visitors; | ||
5 | |||
6 | import org.gnunet.seaspider.parser.nodes.*; | ||
7 | |||
8 | import java.util.*; | ||
9 | |||
10 | /** | ||
11 | * A skeleton output formatter for your language grammar. Using the | ||
12 | * add() method along with force(), indent(), and outdent(), you can | ||
13 | * easily specify how this visitor will format the given syntax tree. | ||
14 | * See the JTB documentation for more details. | ||
15 | * | ||
16 | * Pass your syntax tree to this visitor, and then to the TreeDumper | ||
17 | * visitor in order to "pretty print" your tree. | ||
18 | */ | ||
19 | public class TreeFormatter extends DepthFirstVisitor { | ||
20 | private Vector<FormatCommand> cmdQueue = new Vector<FormatCommand>(); | ||
21 | private boolean lineWrap; | ||
22 | private int wrapWidth; | ||
23 | private int indentAmt; | ||
24 | private int curLine = 1; | ||
25 | private int curColumn = 1; | ||
26 | private int curIndent = 0; | ||
27 | |||
28 | /** | ||
29 | * The default constructor assumes an indentation amount of 3 spaces | ||
30 | * and no line-wrap. You may alternately use the other constructor to | ||
31 | * specify your own indentation amount and line width. | ||
32 | */ | ||
33 | public TreeFormatter() { this(3, 0); } | ||
34 | |||
35 | /** | ||
36 | * This constructor accepts an indent amount and a line width which is | ||
37 | * used to wrap long lines. If a token's beginColumn value is greater | ||
38 | * than the specified wrapWidth, it will be moved to the next line and | ||
39 | * indented one extra level. To turn off line-wrapping, specify a | ||
40 | * wrapWidth of 0. | ||
41 | * | ||
42 | * @param indentAmt Amount of spaces per indentation level. | ||
43 | * @param wrapWidth Wrap lines longer than wrapWidth. 0 for no wrap. | ||
44 | */ | ||
45 | public TreeFormatter(int indentAmt, int wrapWidth) { | ||
46 | this.indentAmt = indentAmt; | ||
47 | this.wrapWidth = wrapWidth; | ||
48 | |||
49 | if ( wrapWidth > 0 ) | ||
50 | lineWrap = true; | ||
51 | else | ||
52 | lineWrap = false; | ||
53 | } | ||
54 | |||
55 | /** | ||
56 | * Accepts a NodeListInterface object and performs an optional format | ||
57 | * command between each node in the list (but not after the last node). | ||
58 | */ | ||
59 | protected void processList(NodeListInterface n) { | ||
60 | processList(n, null); | ||
61 | } | ||
62 | |||
63 | protected void processList(NodeListInterface n, FormatCommand cmd) { | ||
64 | for ( Enumeration<Node> e = n.elements(); e.hasMoreElements(); ) { | ||
65 | e.nextElement().accept(this); | ||
66 | if ( cmd != null && e.hasMoreElements() ) | ||
67 | cmdQueue.addElement(cmd); | ||
68 | } | ||
69 | } | ||
70 | |||
71 | /** | ||
72 | * A Force command inserts a line break and indents the next line to | ||
73 | * the current indentation level. Use "add(force());". | ||
74 | */ | ||
75 | protected FormatCommand force() { return force(1); } | ||
76 | protected FormatCommand force(int i) { | ||
77 | return new FormatCommand(FormatCommand.FORCE, i); | ||
78 | } | ||
79 | |||
80 | /** | ||
81 | * An Indent command increases the indentation level by one (or a | ||
82 | * user-specified amount). Use "add(indent());". | ||
83 | */ | ||
84 | protected FormatCommand indent() { return indent(1); } | ||
85 | protected FormatCommand indent(int i) { | ||
86 | return new FormatCommand(FormatCommand.INDENT, i); | ||
87 | } | ||
88 | |||
89 | /** | ||
90 | * An Outdent command is the reverse of the Indent command: it reduces | ||
91 | * the indentation level. Use "add(outdent());". | ||
92 | */ | ||
93 | protected FormatCommand outdent() { return outdent(1); } | ||
94 | protected FormatCommand outdent(int i) { | ||
95 | return new FormatCommand(FormatCommand.OUTDENT, i); | ||
96 | } | ||
97 | |||
98 | /** | ||
99 | * A Space command simply adds one or a user-specified number of | ||
100 | * spaces between tokens. Use "add(space());". | ||
101 | */ | ||
102 | protected FormatCommand space() { return space(1); } | ||
103 | protected FormatCommand space(int i) { | ||
104 | return new FormatCommand(FormatCommand.SPACE, i); | ||
105 | } | ||
106 | |||
107 | /** | ||
108 | * Use this method to add FormatCommands to the command queue to be | ||
109 | * executed when the next token in the tree is visited. | ||
110 | */ | ||
111 | protected void add(FormatCommand cmd) { | ||
112 | cmdQueue.addElement(cmd); | ||
113 | } | ||
114 | |||
115 | /** | ||
116 | * Executes the commands waiting in the command queue, then inserts the | ||
117 | * proper location information into the current NodeToken. | ||
118 | * | ||
119 | * If there are any special tokens preceding this token, they will be | ||
120 | * given the current location information. The token will follow on | ||
121 | * the next line, at the proper indentation level. If this is not the | ||
122 | * behavior you want from special tokens, feel free to modify this | ||
123 | * method. | ||
124 | */ | ||
125 | public void visit(NodeToken n) { | ||
126 | for ( Enumeration<FormatCommand> e = cmdQueue.elements(); e.hasMoreElements(); ) { | ||
127 | FormatCommand cmd = e.nextElement(); | ||
128 | switch ( cmd.getCommand() ) { | ||
129 | case FormatCommand.FORCE : | ||
130 | curLine += cmd.getNumCommands(); | ||
131 | curColumn = curIndent + 1; | ||
132 | break; | ||
133 | case FormatCommand.INDENT : | ||
134 | curIndent += indentAmt * cmd.getNumCommands(); | ||
135 | break; | ||
136 | case FormatCommand.OUTDENT : | ||
137 | if ( curIndent >= indentAmt ) | ||
138 | curIndent -= indentAmt * cmd.getNumCommands(); | ||
139 | break; | ||
140 | case FormatCommand.SPACE : | ||
141 | curColumn += cmd.getNumCommands(); | ||
142 | break; | ||
143 | default : | ||
144 | throw new TreeFormatterException( | ||
145 | "Invalid value in command queue."); | ||
146 | } | ||
147 | } | ||
148 | |||
149 | cmdQueue.removeAllElements(); | ||
150 | |||
151 | // | ||
152 | // Handle all special tokens preceding this NodeToken | ||
153 | // | ||
154 | if ( n.numSpecials() > 0 ) | ||
155 | for ( Enumeration<NodeToken> e = n.specialTokens.elements(); | ||
156 | e.hasMoreElements(); ) { | ||
157 | NodeToken special = e.nextElement(); | ||
158 | |||
159 | // | ||
160 | // -Place the token. | ||
161 | // -Move cursor to next line after the special token. | ||
162 | // -Don't update curColumn--want to keep current indent level. | ||
163 | // | ||
164 | placeToken(special, curLine, curColumn); | ||
165 | curLine = special.endLine + 1; | ||
166 | } | ||
167 | |||
168 | placeToken(n, curLine, curColumn); | ||
169 | curLine = n.endLine; | ||
170 | curColumn = n.endColumn; | ||
171 | } | ||
172 | |||
173 | /** | ||
174 | * Inserts token location (beginLine, beginColumn, endLine, endColumn) | ||
175 | * information into the NodeToken. Takes into account line-wrap. | ||
176 | * Does not update curLine and curColumn. | ||
177 | */ | ||
178 | private void placeToken(NodeToken n, int line, int column) { | ||
179 | int length = n.tokenImage.length(); | ||
180 | |||
181 | // | ||
182 | // Find beginning of token. Only line-wrap for single-line tokens | ||
183 | // | ||
184 | if ( !lineWrap || n.tokenImage.indexOf('\n') != -1 || | ||
185 | column + length <= wrapWidth ) | ||
186 | n.beginColumn = column; | ||
187 | else { | ||
188 | ++line; | ||
189 | column = curIndent + indentAmt + 1; | ||
190 | n.beginColumn = column; | ||
191 | } | ||
192 | |||
193 | n.beginLine = line; | ||
194 | |||
195 | // | ||
196 | // Find end of token; don't count \n if it's the last character | ||
197 | // | ||
198 | for ( int i = 0; i < length; ++i ) { | ||
199 | if ( n.tokenImage.charAt(i) == '\n' && i < length - 1 ) { | ||
200 | ++line; | ||
201 | column = 1; | ||
202 | } | ||
203 | else | ||
204 | ++column; | ||
205 | } | ||
206 | |||
207 | n.endLine = line; | ||
208 | n.endColumn = column; | ||
209 | } | ||
210 | |||
211 | // | ||
212 | // User-generated visitor methods below | ||
213 | // | ||
214 | |||
215 | /** | ||
216 | * <PRE> | ||
217 | * f0 -> ( ExternalDeclaration() )+ | ||
218 | * </PRE> | ||
219 | */ | ||
220 | public void visit(TranslationUnit n) { | ||
221 | processList(n.f0); | ||
222 | } | ||
223 | |||
224 | /** | ||
225 | * <PRE> | ||
226 | * f0 -> ( StorageClassSpecifier() )* | ||
227 | * f1 -> ( FunctionDeclaration() | StructOrUnionSpecifier() | VariableDeclaration() | TypeDeclaration() ) | ||
228 | * </PRE> | ||
229 | */ | ||
230 | public void visit(ExternalDeclaration n) { | ||
231 | if ( n.f0.present() ) { | ||
232 | processList(n.f0); | ||
233 | } | ||
234 | n.f1.accept(this); | ||
235 | } | ||
236 | |||
237 | /** | ||
238 | * <PRE> | ||
239 | * f0 -> TypeSpecifier() | ||
240 | * f1 -> <IDENTIFIER> | ||
241 | * f2 -> "(" | ||
242 | * f3 -> [ ParameterList() ] | ||
243 | * f4 -> ")" | ||
244 | * f5 -> ( ";" | CompoundStatement() ) | ||
245 | * </PRE> | ||
246 | */ | ||
247 | public void visit(FunctionDeclaration n) { | ||
248 | n.f0.accept(this); | ||
249 | n.f1.accept(this); | ||
250 | n.f2.accept(this); | ||
251 | if ( n.f3.present() ) { | ||
252 | n.f3.accept(this); | ||
253 | } | ||
254 | n.f4.accept(this); | ||
255 | n.f5.accept(this); | ||
256 | } | ||
257 | |||
258 | /** | ||
259 | * <PRE> | ||
260 | * f0 -> ( <STATIC> | <EXTERN> ) | ||
261 | * </PRE> | ||
262 | */ | ||
263 | public void visit(StorageClassSpecifier n) { | ||
264 | n.f0.accept(this); | ||
265 | } | ||
266 | |||
267 | /** | ||
268 | * <PRE> | ||
269 | * f0 -> <TYPEDEF> | ||
270 | * f1 -> ( DataType() | FunctionType() ) | ||
271 | * f2 -> ";" | ||
272 | * </PRE> | ||
273 | */ | ||
274 | public void visit(TypeDeclaration n) { | ||
275 | n.f0.accept(this); | ||
276 | n.f1.accept(this); | ||
277 | n.f2.accept(this); | ||
278 | } | ||
279 | |||
280 | /** | ||
281 | * <PRE> | ||
282 | * f0 -> StructOrUnionSpecifier() | ||
283 | * f1 -> <IDENTIFIER> | ||
284 | * </PRE> | ||
285 | */ | ||
286 | public void visit(DataType n) { | ||
287 | n.f0.accept(this); | ||
288 | n.f1.accept(this); | ||
289 | } | ||
290 | |||
291 | /** | ||
292 | * <PRE> | ||
293 | * f0 -> TypeSpecifier() | ||
294 | * f1 -> "(" | ||
295 | * f2 -> "*" | ||
296 | * f3 -> <IDENTIFIER> | ||
297 | * f4 -> ")" | ||
298 | * f5 -> "(" | ||
299 | * f6 -> [ ParameterList() ] | ||
300 | * f7 -> ")" | ||
301 | * </PRE> | ||
302 | */ | ||
303 | public void visit(FunctionType n) { | ||
304 | n.f0.accept(this); | ||
305 | n.f1.accept(this); | ||
306 | n.f2.accept(this); | ||
307 | n.f3.accept(this); | ||
308 | n.f4.accept(this); | ||
309 | n.f5.accept(this); | ||
310 | if ( n.f6.present() ) { | ||
311 | n.f6.accept(this); | ||
312 | } | ||
313 | n.f7.accept(this); | ||
314 | } | ||
315 | |||
316 | /** | ||
317 | * <PRE> | ||
318 | * f0 -> ParameterDeclaration() | ||
319 | * f1 -> ( "," ParameterDeclaration() )* | ||
320 | * f2 -> [ "," "..." ] | ||
321 | * </PRE> | ||
322 | */ | ||
323 | public void visit(ParameterList n) { | ||
324 | n.f0.accept(this); | ||
325 | if ( n.f1.present() ) { | ||
326 | processList(n.f1); | ||
327 | } | ||
328 | if ( n.f2.present() ) { | ||
329 | n.f2.accept(this); | ||
330 | } | ||
331 | } | ||
332 | |||
333 | /** | ||
334 | * <PRE> | ||
335 | * f0 -> TypeSpecifier() | ||
336 | * f1 -> <IDENTIFIER> | ||
337 | * f2 -> [ Array() ] | ||
338 | * </PRE> | ||
339 | */ | ||
340 | public void visit(ParameterDeclaration n) { | ||
341 | n.f0.accept(this); | ||
342 | n.f1.accept(this); | ||
343 | if ( n.f2.present() ) { | ||
344 | n.f2.accept(this); | ||
345 | } | ||
346 | } | ||
347 | |||
348 | /** | ||
349 | * <PRE> | ||
350 | * f0 -> VariableClassSpecifier() | ||
351 | * f1 -> TypeSpecifier() | ||
352 | * f2 -> InitDeclaratorList() | ||
353 | * f3 -> ";" | ||
354 | * </PRE> | ||
355 | */ | ||
356 | public void visit(VariableDeclaration n) { | ||
357 | n.f0.accept(this); | ||
358 | n.f1.accept(this); | ||
359 | n.f2.accept(this); | ||
360 | n.f3.accept(this); | ||
361 | } | ||
362 | |||
363 | /** | ||
364 | * <PRE> | ||
365 | * f0 -> [ <STATIC> ] | ||
366 | * f1 -> VariableDeclaration() | ||
367 | * </PRE> | ||
368 | */ | ||
369 | public void visit(LocalVariableDeclaration n) { | ||
370 | if ( n.f0.present() ) { | ||
371 | n.f0.accept(this); | ||
372 | } | ||
373 | n.f1.accept(this); | ||
374 | } | ||
375 | |||
376 | /** | ||
377 | * <PRE> | ||
378 | * f0 -> ( <AUTO> | <REGISTER> )* | ||
379 | * </PRE> | ||
380 | */ | ||
381 | public void visit(VariableClassSpecifier n) { | ||
382 | if ( n.f0.present() ) { | ||
383 | processList(n.f0); | ||
384 | } | ||
385 | } | ||
386 | |||
387 | /** | ||
388 | * <PRE> | ||
389 | * f0 -> [ <CONST> ] | ||
390 | * f1 -> ( <VOID> | <CHAR> | <SHORT> [ <INT> ] | <INT> | <LONG> [ <LONG> ] | <FLOAT> | <DOUBLE> | ( <SIGNED> | <UNSIGNED> ) [ <CHAR> | <SHORT> [ <INT> ] | <INT> | <LONG> [ <LONG> ] ] | StructOrUnionSpecifier() | EnumSpecifier() | <IDENTIFIER> ) | ||
391 | * f2 -> [ Pointer() ] | ||
392 | * f3 -> [ Array() ] | ||
393 | * </PRE> | ||
394 | */ | ||
395 | public void visit(TypeSpecifier n) { | ||
396 | if ( n.f0.present() ) { | ||
397 | n.f0.accept(this); | ||
398 | } | ||
399 | n.f1.accept(this); | ||
400 | if ( n.f2.present() ) { | ||
401 | n.f2.accept(this); | ||
402 | } | ||
403 | if ( n.f3.present() ) { | ||
404 | n.f3.accept(this); | ||
405 | } | ||
406 | } | ||
407 | |||
408 | /** | ||
409 | * <PRE> | ||
410 | * f0 -> [ <CONST> ] | ||
411 | * f1 -> ( <VOID> | <CHAR> | <SHORT> [ <INT> ] | <INT> | <LONG> [ <LONG> ] | <FLOAT> | <DOUBLE> | ( <SIGNED> | <UNSIGNED> ) [ <CHAR> | <SHORT> [ <INT> ] | <INT> | <LONG> [ <LONG> ] ] | StructOrUnionSpecifier() | EnumSpecifier() ) | ||
412 | * f2 -> [ Pointer() ] | ||
413 | * f3 -> [ Array() ] | ||
414 | * </PRE> | ||
415 | */ | ||
416 | public void visit(NoIdentifierTypeSpecifier n) { | ||
417 | if ( n.f0.present() ) { | ||
418 | n.f0.accept(this); | ||
419 | } | ||
420 | n.f1.accept(this); | ||
421 | if ( n.f2.present() ) { | ||
422 | n.f2.accept(this); | ||
423 | } | ||
424 | if ( n.f3.present() ) { | ||
425 | n.f3.accept(this); | ||
426 | } | ||
427 | } | ||
428 | |||
429 | /** | ||
430 | * <PRE> | ||
431 | * f0 -> StructOrUnion() [ <IDENTIFIER> ] "{" StructDeclarationList() "}" | ||
432 | * | StructOrUnion() <IDENTIFIER> | ||
433 | * </PRE> | ||
434 | */ | ||
435 | public void visit(StructOrUnionSpecifier n) { | ||
436 | n.f0.accept(this); | ||
437 | } | ||
438 | |||
439 | /** | ||
440 | * <PRE> | ||
441 | * f0 -> ( <STRUCT> | <UNION> ) | ||
442 | * </PRE> | ||
443 | */ | ||
444 | public void visit(StructOrUnion n) { | ||
445 | n.f0.accept(this); | ||
446 | } | ||
447 | |||
448 | /** | ||
449 | * <PRE> | ||
450 | * f0 -> ( StructDeclaration() )+ | ||
451 | * </PRE> | ||
452 | */ | ||
453 | public void visit(StructDeclarationList n) { | ||
454 | processList(n.f0); | ||
455 | } | ||
456 | |||
457 | /** | ||
458 | * <PRE> | ||
459 | * f0 -> InitDeclarator() | ||
460 | * f1 -> ( "," InitDeclarator() )* | ||
461 | * </PRE> | ||
462 | */ | ||
463 | public void visit(InitDeclaratorList n) { | ||
464 | n.f0.accept(this); | ||
465 | if ( n.f1.present() ) { | ||
466 | processList(n.f1); | ||
467 | } | ||
468 | } | ||
469 | |||
470 | /** | ||
471 | * <PRE> | ||
472 | * f0 -> <IDENTIFIER> | ||
473 | * f1 -> [ Array() ] | ||
474 | * f2 -> [ "=" Initializer() ] | ||
475 | * </PRE> | ||
476 | */ | ||
477 | public void visit(InitDeclarator n) { | ||
478 | n.f0.accept(this); | ||
479 | if ( n.f1.present() ) { | ||
480 | n.f1.accept(this); | ||
481 | } | ||
482 | if ( n.f2.present() ) { | ||
483 | n.f2.accept(this); | ||
484 | } | ||
485 | } | ||
486 | |||
487 | /** | ||
488 | * <PRE> | ||
489 | * f0 -> TypeSpecifier() | ||
490 | * f1 -> <IDENTIFIER> | ||
491 | * f2 -> [ Array() | ":" ConstantExpression() ] | ||
492 | * f3 -> [ <IDENTIFIER> ] | ||
493 | * f4 -> ";" | ||
494 | * </PRE> | ||
495 | */ | ||
496 | public void visit(StructDeclaration n) { | ||
497 | n.f0.accept(this); | ||
498 | n.f1.accept(this); | ||
499 | if ( n.f2.present() ) { | ||
500 | n.f2.accept(this); | ||
501 | } | ||
502 | if ( n.f3.present() ) { | ||
503 | n.f3.accept(this); | ||
504 | } | ||
505 | n.f4.accept(this); | ||
506 | } | ||
507 | |||
508 | /** | ||
509 | * <PRE> | ||
510 | * f0 -> <ENUM> | ||
511 | * f1 -> ( [ <IDENTIFIER> ] "{" EnumeratorList() "}" | <IDENTIFIER> ) | ||
512 | * </PRE> | ||
513 | */ | ||
514 | public void visit(EnumSpecifier n) { | ||
515 | n.f0.accept(this); | ||
516 | n.f1.accept(this); | ||
517 | } | ||
518 | |||
519 | /** | ||
520 | * <PRE> | ||
521 | * f0 -> Enumerator() | ||
522 | * f1 -> ( "," Enumerator() )* | ||
523 | * </PRE> | ||
524 | */ | ||
525 | public void visit(EnumeratorList n) { | ||
526 | n.f0.accept(this); | ||
527 | if ( n.f1.present() ) { | ||
528 | processList(n.f1); | ||
529 | } | ||
530 | } | ||
531 | |||
532 | /** | ||
533 | * <PRE> | ||
534 | * f0 -> <IDENTIFIER> | ||
535 | * f1 -> [ "=" ConstantExpression() ] | ||
536 | * </PRE> | ||
537 | */ | ||
538 | public void visit(Enumerator n) { | ||
539 | n.f0.accept(this); | ||
540 | if ( n.f1.present() ) { | ||
541 | n.f1.accept(this); | ||
542 | } | ||
543 | } | ||
544 | |||
545 | /** | ||
546 | * <PRE> | ||
547 | * f0 -> "*" | ||
548 | * f1 -> [ <CONST> ] | ||
549 | * f2 -> [ Pointer() ] | ||
550 | * </PRE> | ||
551 | */ | ||
552 | public void visit(Pointer n) { | ||
553 | n.f0.accept(this); | ||
554 | if ( n.f1.present() ) { | ||
555 | n.f1.accept(this); | ||
556 | } | ||
557 | if ( n.f2.present() ) { | ||
558 | n.f2.accept(this); | ||
559 | } | ||
560 | } | ||
561 | |||
562 | /** | ||
563 | * <PRE> | ||
564 | * f0 -> <IDENTIFIER> | ||
565 | * f1 -> ( "," <IDENTIFIER> )* | ||
566 | * </PRE> | ||
567 | */ | ||
568 | public void visit(IdentifierList n) { | ||
569 | n.f0.accept(this); | ||
570 | if ( n.f1.present() ) { | ||
571 | processList(n.f1); | ||
572 | } | ||
573 | } | ||
574 | |||
575 | /** | ||
576 | * <PRE> | ||
577 | * f0 -> ( AssignmentExpression() | "{" InitializerList() [ "," ] "}" ) | ||
578 | * </PRE> | ||
579 | */ | ||
580 | public void visit(Initializer n) { | ||
581 | n.f0.accept(this); | ||
582 | } | ||
583 | |||
584 | /** | ||
585 | * <PRE> | ||
586 | * f0 -> Initializer() | ||
587 | * f1 -> ( "," Initializer() )* | ||
588 | * </PRE> | ||
589 | */ | ||
590 | public void visit(InitializerList n) { | ||
591 | n.f0.accept(this); | ||
592 | if ( n.f1.present() ) { | ||
593 | processList(n.f1); | ||
594 | } | ||
595 | } | ||
596 | |||
597 | /** | ||
598 | * <PRE> | ||
599 | * f0 -> "[" | ||
600 | * f1 -> [ ConstantExpression() ] | ||
601 | * f2 -> "]" | ||
602 | * </PRE> | ||
603 | */ | ||
604 | public void visit(Array n) { | ||
605 | n.f0.accept(this); | ||
606 | if ( n.f1.present() ) { | ||
607 | n.f1.accept(this); | ||
608 | } | ||
609 | n.f2.accept(this); | ||
610 | } | ||
611 | |||
612 | /** | ||
613 | * <PRE> | ||
614 | * f0 -> ( LabeledStatement() | ExpressionStatement() | CompoundStatement() | SelectionStatement() | IterationStatement() | JumpStatement() ) | ||
615 | * </PRE> | ||
616 | */ | ||
617 | public void visit(Statement n) { | ||
618 | n.f0.accept(this); | ||
619 | } | ||
620 | |||
621 | /** | ||
622 | * <PRE> | ||
623 | * f0 -> ( <IDENTIFIER> ":" Statement() | <CASE> ConstantExpression() ":" Statement() | <DFLT> ":" Statement() ) | ||
624 | * </PRE> | ||
625 | */ | ||
626 | public void visit(LabeledStatement n) { | ||
627 | n.f0.accept(this); | ||
628 | } | ||
629 | |||
630 | /** | ||
631 | * <PRE> | ||
632 | * f0 -> [ Expression() ] | ||
633 | * f1 -> ";" | ||
634 | * </PRE> | ||
635 | */ | ||
636 | public void visit(ExpressionStatement n) { | ||
637 | if ( n.f0.present() ) { | ||
638 | n.f0.accept(this); | ||
639 | } | ||
640 | n.f1.accept(this); | ||
641 | } | ||
642 | |||
643 | /** | ||
644 | * <PRE> | ||
645 | * f0 -> "{" | ||
646 | * f1 -> ( LocalVariableDeclaration() | Statement() )* | ||
647 | * f2 -> "}" | ||
648 | * </PRE> | ||
649 | */ | ||
650 | public void visit(CompoundStatement n) { | ||
651 | n.f0.accept(this); | ||
652 | if ( n.f1.present() ) { | ||
653 | processList(n.f1); | ||
654 | } | ||
655 | n.f2.accept(this); | ||
656 | } | ||
657 | |||
658 | /** | ||
659 | * <PRE> | ||
660 | * f0 -> ( <IF> "(" Expression() ")" Statement() [ <ELSE> Statement() ] | <SWITCH> "(" Expression() ")" Statement() ) | ||
661 | * </PRE> | ||
662 | */ | ||
663 | public void visit(SelectionStatement n) { | ||
664 | n.f0.accept(this); | ||
665 | } | ||
666 | |||
667 | /** | ||
668 | * <PRE> | ||
669 | * f0 -> ( <WHILE> "(" Expression() ")" Statement() | <DO> Statement() <WHILE> "(" Expression() ")" ";" | <FOR> "(" [ Expression() ] ";" [ Expression() ] ";" [ Expression() ] ")" Statement() ) | ||
670 | * </PRE> | ||
671 | */ | ||
672 | public void visit(IterationStatement n) { | ||
673 | n.f0.accept(this); | ||
674 | } | ||
675 | |||
676 | /** | ||
677 | * <PRE> | ||
678 | * f0 -> ( <GOTO> <IDENTIFIER> ";" | <CONTINUE> ";" | <BREAK> ";" | <RETURN> [ Expression() ] ";" ) | ||
679 | * </PRE> | ||
680 | */ | ||
681 | public void visit(JumpStatement n) { | ||
682 | n.f0.accept(this); | ||
683 | } | ||
684 | |||
685 | /** | ||
686 | * <PRE> | ||
687 | * f0 -> AssignmentExpression() | ||
688 | * f1 -> ( "," AssignmentExpression() )* | ||
689 | * </PRE> | ||
690 | */ | ||
691 | public void visit(Expression n) { | ||
692 | n.f0.accept(this); | ||
693 | if ( n.f1.present() ) { | ||
694 | processList(n.f1); | ||
695 | } | ||
696 | } | ||
697 | |||
698 | /** | ||
699 | * <PRE> | ||
700 | * f0 -> UnaryExpression() AssignmentOperator() AssignmentExpression() | ||
701 | * | ConditionalExpression() | ||
702 | * </PRE> | ||
703 | */ | ||
704 | public void visit(AssignmentExpression n) { | ||
705 | n.f0.accept(this); | ||
706 | } | ||
707 | |||
708 | /** | ||
709 | * <PRE> | ||
710 | * f0 -> ( "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | "&=" | "^=" | "|=" ) | ||
711 | * </PRE> | ||
712 | */ | ||
713 | public void visit(AssignmentOperator n) { | ||
714 | n.f0.accept(this); | ||
715 | } | ||
716 | |||
717 | /** | ||
718 | * <PRE> | ||
719 | * f0 -> LogicalORExpression() | ||
720 | * f1 -> [ "?" Expression() ":" ConditionalExpression() ] | ||
721 | * </PRE> | ||
722 | */ | ||
723 | public void visit(ConditionalExpression n) { | ||
724 | n.f0.accept(this); | ||
725 | if ( n.f1.present() ) { | ||
726 | n.f1.accept(this); | ||
727 | } | ||
728 | } | ||
729 | |||
730 | /** | ||
731 | * <PRE> | ||
732 | * f0 -> ConditionalExpression() | ||
733 | * </PRE> | ||
734 | */ | ||
735 | public void visit(ConstantExpression n) { | ||
736 | n.f0.accept(this); | ||
737 | } | ||
738 | |||
739 | /** | ||
740 | * <PRE> | ||
741 | * f0 -> LogicalANDExpression() | ||
742 | * f1 -> [ "||" LogicalORExpression() ] | ||
743 | * </PRE> | ||
744 | */ | ||
745 | public void visit(LogicalORExpression n) { | ||
746 | n.f0.accept(this); | ||
747 | if ( n.f1.present() ) { | ||
748 | n.f1.accept(this); | ||
749 | } | ||
750 | } | ||
751 | |||
752 | /** | ||
753 | * <PRE> | ||
754 | * f0 -> InclusiveORExpression() | ||
755 | * f1 -> [ "&&" LogicalANDExpression() ] | ||
756 | * </PRE> | ||
757 | */ | ||
758 | public void visit(LogicalANDExpression n) { | ||
759 | n.f0.accept(this); | ||
760 | if ( n.f1.present() ) { | ||
761 | n.f1.accept(this); | ||
762 | } | ||
763 | } | ||
764 | |||
765 | /** | ||
766 | * <PRE> | ||
767 | * f0 -> ExclusiveORExpression() | ||
768 | * f1 -> [ "|" InclusiveORExpression() ] | ||
769 | * </PRE> | ||
770 | */ | ||
771 | public void visit(InclusiveORExpression n) { | ||
772 | n.f0.accept(this); | ||
773 | if ( n.f1.present() ) { | ||
774 | n.f1.accept(this); | ||
775 | } | ||
776 | } | ||
777 | |||
778 | /** | ||
779 | * <PRE> | ||
780 | * f0 -> ANDExpression() | ||
781 | * f1 -> [ "^" ExclusiveORExpression() ] | ||
782 | * </PRE> | ||
783 | */ | ||
784 | public void visit(ExclusiveORExpression n) { | ||
785 | n.f0.accept(this); | ||
786 | if ( n.f1.present() ) { | ||
787 | n.f1.accept(this); | ||
788 | } | ||
789 | } | ||
790 | |||
791 | /** | ||
792 | * <PRE> | ||
793 | * f0 -> EqualityExpression() | ||
794 | * f1 -> [ "&" ANDExpression() ] | ||
795 | * </PRE> | ||
796 | */ | ||
797 | public void visit(ANDExpression n) { | ||
798 | n.f0.accept(this); | ||
799 | if ( n.f1.present() ) { | ||
800 | n.f1.accept(this); | ||
801 | } | ||
802 | } | ||
803 | |||
804 | /** | ||
805 | * <PRE> | ||
806 | * f0 -> RelationalExpression() | ||
807 | * f1 -> [ ( "==" | "!=" ) EqualityExpression() ] | ||
808 | * </PRE> | ||
809 | */ | ||
810 | public void visit(EqualityExpression n) { | ||
811 | n.f0.accept(this); | ||
812 | if ( n.f1.present() ) { | ||
813 | n.f1.accept(this); | ||
814 | } | ||
815 | } | ||
816 | |||
817 | /** | ||
818 | * <PRE> | ||
819 | * f0 -> ShiftExpression() | ||
820 | * f1 -> [ ( "<" | ">" | "<=" | ">=" ) RelationalExpression() ] | ||
821 | * </PRE> | ||
822 | */ | ||
823 | public void visit(RelationalExpression n) { | ||
824 | n.f0.accept(this); | ||
825 | if ( n.f1.present() ) { | ||
826 | n.f1.accept(this); | ||
827 | } | ||
828 | } | ||
829 | |||
830 | /** | ||
831 | * <PRE> | ||
832 | * f0 -> AdditiveExpression() | ||
833 | * f1 -> [ ( "<<" | ">>" ) ShiftExpression() ] | ||
834 | * </PRE> | ||
835 | */ | ||
836 | public void visit(ShiftExpression n) { | ||
837 | n.f0.accept(this); | ||
838 | if ( n.f1.present() ) { | ||
839 | n.f1.accept(this); | ||
840 | } | ||
841 | } | ||
842 | |||
843 | /** | ||
844 | * <PRE> | ||
845 | * f0 -> MultiplicativeExpression() | ||
846 | * f1 -> [ ( "+" | "-" ) AdditiveExpression() ] | ||
847 | * </PRE> | ||
848 | */ | ||
849 | public void visit(AdditiveExpression n) { | ||
850 | n.f0.accept(this); | ||
851 | if ( n.f1.present() ) { | ||
852 | n.f1.accept(this); | ||
853 | } | ||
854 | } | ||
855 | |||
856 | /** | ||
857 | * <PRE> | ||
858 | * f0 -> CastExpression() | ||
859 | * f1 -> [ ( "*" | "/" | "%" ) MultiplicativeExpression() ] | ||
860 | * </PRE> | ||
861 | */ | ||
862 | public void visit(MultiplicativeExpression n) { | ||
863 | n.f0.accept(this); | ||
864 | if ( n.f1.present() ) { | ||
865 | n.f1.accept(this); | ||
866 | } | ||
867 | } | ||
868 | |||
869 | /** | ||
870 | * <PRE> | ||
871 | * f0 -> ( "(" TypeSpecifier() ")" CastExpression() | UnaryExpression() ) | ||
872 | * </PRE> | ||
873 | */ | ||
874 | public void visit(CastExpression n) { | ||
875 | n.f0.accept(this); | ||
876 | } | ||
877 | |||
878 | /** | ||
879 | * <PRE> | ||
880 | * f0 -> ( PostfixExpression() | "++" UnaryExpression() | "--" UnaryExpression() | UnaryOperator() CastExpression() | <SIZEOF> ( UnaryExpression() | "(" TypeSpecifier() ")" ) ) | ||
881 | * </PRE> | ||
882 | */ | ||
883 | public void visit(UnaryExpression n) { | ||
884 | n.f0.accept(this); | ||
885 | } | ||
886 | |||
887 | /** | ||
888 | * <PRE> | ||
889 | * f0 -> ( "&" | "*" | "+" | "-" | "~" | "!" ) | ||
890 | * </PRE> | ||
891 | */ | ||
892 | public void visit(UnaryOperator n) { | ||
893 | n.f0.accept(this); | ||
894 | } | ||
895 | |||
896 | /** | ||
897 | * <PRE> | ||
898 | * f0 -> PrimaryExpression() | ||
899 | * f1 -> ( "[" Expression() "]" | "(" [ ArgumentExpressionList() ] ")" | "." <IDENTIFIER> | "->" <IDENTIFIER> | "++" | "--" )* | ||
900 | * </PRE> | ||
901 | */ | ||
902 | public void visit(PostfixExpression n) { | ||
903 | n.f0.accept(this); | ||
904 | if ( n.f1.present() ) { | ||
905 | processList(n.f1); | ||
906 | } | ||
907 | } | ||
908 | |||
909 | /** | ||
910 | * <PRE> | ||
911 | * f0 -> <IDENTIFIER> | ||
912 | * | Constant() | ||
913 | * | "(" Expression() ")" | ||
914 | * </PRE> | ||
915 | */ | ||
916 | public void visit(PrimaryExpression n) { | ||
917 | n.f0.accept(this); | ||
918 | } | ||
919 | |||
920 | /** | ||
921 | * <PRE> | ||
922 | * f0 -> AssignmentOrTypeExpression() | ||
923 | * f1 -> ( "," AssignmentOrTypeExpression() )* | ||
924 | * </PRE> | ||
925 | */ | ||
926 | public void visit(ArgumentExpressionList n) { | ||
927 | n.f0.accept(this); | ||
928 | if ( n.f1.present() ) { | ||
929 | processList(n.f1); | ||
930 | } | ||
931 | } | ||
932 | |||
933 | /** | ||
934 | * <PRE> | ||
935 | * f0 -> NoIdentifierTypeSpecifier() | ||
936 | * | AssignmentExpression() | ||
937 | * </PRE> | ||
938 | */ | ||
939 | public void visit(AssignmentOrTypeExpression n) { | ||
940 | n.f0.accept(this); | ||
941 | } | ||
942 | |||
943 | /** | ||
944 | * <PRE> | ||
945 | * f0 -> <INTEGER_LITERAL> | ||
946 | * | <FLOATING_POINT_LITERAL> | ||
947 | * | <CHARACTER_LITERAL> | ||
948 | * | <STRING_LITERAL> | ||
949 | * </PRE> | ||
950 | */ | ||
951 | public void visit(Constant n) { | ||
952 | n.f0.accept(this); | ||
953 | } | ||
954 | |||
955 | } | ||
956 | |||
957 | class FormatCommand { | ||
958 | public static final int FORCE = 0; | ||
959 | public static final int INDENT = 1; | ||
960 | public static final int OUTDENT = 2; | ||
961 | public static final int SPACE = 3; | ||
962 | |||
963 | private int command; | ||
964 | private int numCommands; | ||
965 | |||
966 | FormatCommand(int command, int numCommands) { | ||
967 | this.command = command; | ||
968 | this.numCommands = numCommands; | ||
969 | } | ||
970 | |||
971 | public int getCommand() { return command; } | ||
972 | public int getNumCommands() { return numCommands; } | ||
973 | public void setCommand(int i) { command = i; } | ||
974 | public void setNumCommands(int i) { numCommands = i; } | ||
975 | } | ||
976 | |||
977 | class TreeFormatterException extends RuntimeException { | ||
978 | TreeFormatterException() { super(); } | ||
979 | TreeFormatterException(String s) { super(s); } | ||
980 | } | ||