KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javacc > parser > ExpansionTreeWalker


1 /*
2  * Copyright © 2002 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
3  * California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has
4  * intellectual property rights relating to technology embodied in the product
5  * that is described in this document. In particular, and without limitation,
6  * these intellectual property rights may include one or more of the U.S.
7  * patents listed at http://www.sun.com/patents and one or more additional
8  * patents or pending patent applications in the U.S. and in other countries.
9  * U.S. Government Rights - Commercial software. Government users are subject
10  * to the Sun Microsystems, Inc. standard license agreement and applicable
11  * provisions of the FAR and its supplements. Use is subject to license terms.
12  * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
13  * trademarks of Sun Microsystems, Inc. in the U.S. and other countries. This
14  * product is covered and controlled by U.S. Export Control laws and may be
15  * subject to the export or import laws in other countries. Nuclear, missile,
16  * chemical biological weapons or nuclear maritime end uses or end users,
17  * whether direct or indirect, are strictly prohibited. Export or reexport
18  * to countries subject to U.S. embargo or to entities identified on U.S.
19  * export exclusion lists, including, but not limited to, the denied persons
20  * and specially designated nationals lists is strictly prohibited.
21  */

22
23 package org.javacc.parser;
24
25 /**
26  * A set of routines that walk down the Expansion tree in
27  * various ways.
28  */

29 public class ExpansionTreeWalker {
30
31   /**
32    * Visits the nodes of the tree rooted at "node" in pre-order.
33    * i.e., it executes opObj.action first and then visits the
34    * children.
35    */

36   static void preOrderWalk(Expansion node, TreeWalkerOp opObj) {
37     opObj.action(node);
38     if (opObj.goDeeper(node)) {
39       if (node instanceof Choice) {
40         for (java.util.Enumeration enum = ((Choice)node).choices.elements(); enum.hasMoreElements();) {
41           preOrderWalk((Expansion)enum.nextElement(), opObj);
42         }
43       } else if (node instanceof Sequence) {
44         for (java.util.Enumeration enum = ((Sequence)node).units.elements(); enum.hasMoreElements();) {
45           preOrderWalk((Expansion)enum.nextElement(), opObj);
46         }
47       } else if (node instanceof OneOrMore) {
48         preOrderWalk(((OneOrMore)node).expansion, opObj);
49       } else if (node instanceof ZeroOrMore) {
50         preOrderWalk(((ZeroOrMore)node).expansion, opObj);
51       } else if (node instanceof ZeroOrOne) {
52         preOrderWalk(((ZeroOrOne)node).expansion, opObj);
53       } else if (node instanceof Lookahead) {
54         Expansion nested_e = ((Lookahead)node).la_expansion;
55         if (!(nested_e instanceof Sequence && (Expansion)(((Sequence)nested_e).units.elementAt(0)) == node)) {
56           preOrderWalk(nested_e, opObj);
57         }
58       } else if (node instanceof TryBlock) {
59         preOrderWalk(((TryBlock)node).exp, opObj);
60       } else if (node instanceof RChoice) {
61         for (java.util.Enumeration enum = ((RChoice)node).choices.elements(); enum.hasMoreElements();) {
62           preOrderWalk((Expansion)enum.nextElement(), opObj);
63         }
64       } else if (node instanceof RSequence) {
65         for (java.util.Enumeration enum = ((RSequence)node).units.elements(); enum.hasMoreElements();) {
66           preOrderWalk((Expansion)enum.nextElement(), opObj);
67         }
68       } else if (node instanceof ROneOrMore) {
69         preOrderWalk(((ROneOrMore)node).regexpr, opObj);
70       } else if (node instanceof RZeroOrMore) {
71         preOrderWalk(((RZeroOrMore)node).regexpr, opObj);
72       } else if (node instanceof RZeroOrOne) {
73         preOrderWalk(((RZeroOrOne)node).regexpr, opObj);
74       } else if (node instanceof RRepetitionRange) {
75         preOrderWalk(((RRepetitionRange)node).regexpr, opObj);
76       }
77     }
78   }
79
80   /**
81    * Visits the nodes of the tree rooted at "node" in post-order.
82    * i.e., it visits the children first and then executes
83    * opObj.action.
84    */

85   static void postOrderWalk(Expansion node, TreeWalkerOp opObj) {
86     if (opObj.goDeeper(node)) {
87       if (node instanceof Choice) {
88         for (java.util.Enumeration enum = ((Choice)node).choices.elements(); enum.hasMoreElements();) {
89           postOrderWalk((Expansion)enum.nextElement(), opObj);
90         }
91       } else if (node instanceof Sequence) {
92         for (java.util.Enumeration enum = ((Sequence)node).units.elements(); enum.hasMoreElements();) {
93           postOrderWalk((Expansion)enum.nextElement(), opObj);
94         }
95       } else if (node instanceof OneOrMore) {
96         postOrderWalk(((OneOrMore)node).expansion, opObj);
97       } else if (node instanceof ZeroOrMore) {
98         postOrderWalk(((ZeroOrMore)node).expansion, opObj);
99       } else if (node instanceof ZeroOrOne) {
100         postOrderWalk(((ZeroOrOne)node).expansion, opObj);
101       } else if (node instanceof Lookahead) {
102         Expansion nested_e = ((Lookahead)node).la_expansion;
103         if (!(nested_e instanceof Sequence && (Expansion)(((Sequence)nested_e).units.elementAt(0)) == node)) {
104           postOrderWalk(nested_e, opObj);
105         }
106       } else if (node instanceof TryBlock) {
107         postOrderWalk(((TryBlock)node).exp, opObj);
108       } else if (node instanceof RChoice) {
109         for (java.util.Enumeration enum = ((RChoice)node).choices.elements(); enum.hasMoreElements();) {
110           postOrderWalk((Expansion)enum.nextElement(), opObj);
111         }
112       } else if (node instanceof RSequence) {
113         for (java.util.Enumeration enum = ((RSequence)node).units.elements(); enum.hasMoreElements();) {
114           postOrderWalk((Expansion)enum.nextElement(), opObj);
115         }
116       } else if (node instanceof ROneOrMore) {
117         postOrderWalk(((ROneOrMore)node).regexpr, opObj);
118       } else if (node instanceof RZeroOrMore) {
119         postOrderWalk(((RZeroOrMore)node).regexpr, opObj);
120       } else if (node instanceof RZeroOrOne) {
121         postOrderWalk(((RZeroOrOne)node).regexpr, opObj);
122       } else if (node instanceof RRepetitionRange) {
123         postOrderWalk(((RRepetitionRange)node).regexpr, opObj);
124       }
125     }
126     opObj.action(node);
127   }
128
129 }
130
Popular Tags