KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javacc > jjtree > JJTree


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.jjtree;
24
25 import java.util.Hashtable JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import org.javacc.parser.JavaCCGlobals;
29
30 public class JJTree {
31
32   private IO io;
33
34   private void p(String JavaDoc s)
35   {
36     io.getMsg().println(s);
37   }
38
39   private void help_message()
40   {
41     p("Usage:");
42     p(" jjtree option-settings inputfile");
43     p("");
44     p("\"option-settings\" is a sequence of settings separated by spaces.");
45     p("Each option setting must be of one of the following forms:");
46     p("");
47     p(" -optionname=value (e.g., -STATIC=false)");
48     p(" -optionname:value (e.g., -STATIC:false)");
49     p(" -optionname (equivalent to -optionname=true. e.g., -STATIC)");
50     p(" -NOoptionname (equivalent to -optionname=false. e.g., -NOSTATIC)");
51     p("");
52     p("Option settings are not case-sensitive, so one can say \"-nOsTaTiC\" instead");
53     p("of \"-NOSTATIC\". Option values must be appropriate for the corresponding");
54     p("option, and must be either an integer or a string value.");
55     p("");
56
57     p("The boolean valued options are:");
58     p("");
59     p(" STATIC (default true)");
60     p(" MULTI (default false)");
61     p(" NODE_DEFAULT_VOID (default false)");
62     p(" NODE_SCOPE_HOOK (default false)");
63     p(" NODE_FACTORY (default false)");
64     p(" NODE_USES_PARSER (default false)");
65     p(" BUILD_NODE_FILES (default true)");
66     p(" VISITOR (default false)");
67     p("");
68     p("The string valued options are:");
69     p("");
70     p(" JDK_VERSION (default \"1.4\")");
71     p(" NODE_PREFIX (default \"AST\")");
72     p(" NODE_PACKAGE (default \"\")");
73     p(" NODE_EXTENDS (default \"\")");
74     p(" OUTPUT_FILE (default remove input file suffix, add .jj)");
75     p(" OUTPUT_DIRECTORY (default \"\")");
76     p(" VISITOR_EXCEPTION (default \"\")");
77     p("");
78     p("JJTree also accepts JavaCC options, which it inserts into the generated file.");
79     p("");
80
81     p("EXAMPLES:");
82     p(" jjtree -STATIC=false mygrammar.jjt");
83     p("");
84     p("ABOUT JJTree:");
85     p(" JJTree is a preprocessor for JavaCC that inserts actions into a");
86     p(" JavaCC grammar to build parse trees for the input.");
87     p("");
88     p(" For more information, ???");
89     p("");
90   }
91
92   /**
93    * A main program that exercises the parser.
94    */

95   public int main(String JavaDoc args[]) {
96
97     // initialize static state for allowing repeat runs without exiting
98
ASTNodeDescriptor.nodeIds = new Vector JavaDoc();
99     ASTNodeDescriptor.nodeNames = new Vector JavaDoc();
100     ASTNodeDescriptor.nodeSeen = new Hashtable JavaDoc();
101     JJTreeGlobals.jjtreeOptions = new Hashtable JavaDoc();
102     JJTreeGlobals.toolList = new Vector JavaDoc();
103     JJTreeGlobals.parserName = null;
104     JJTreeGlobals.packageName = "";
105     JJTreeGlobals.parserImplements = null;
106     JJTreeGlobals.parserClassBodyStart = null;
107     JJTreeGlobals.productions = new Hashtable JavaDoc();
108     org.javacc.parser.Main.reInitAll();
109
110     JavaCCGlobals.bannerLine("Tree Builder", "");
111
112     io = new IO();
113
114     try {
115
116       initializeOptions();
117       if (args.length == 0) {
118     p("");
119     help_message();
120     return 1;
121       } else {
122     p("(type \"jjtree\" with no arguments for help)");
123       }
124
125       String JavaDoc fn = args[args.length - 1];
126
127       if (JJTreeOptions.isOption(fn)) {
128     p("Last argument \"" + fn + "\" is not a filename");
129     return 1;
130       }
131       for (int arg = 0; arg < args.length - 1; arg++) {
132     if (!JJTreeOptions.isOption(args[arg])) {
133       p("Argument \"" + args[arg] + "\" must be an option setting.");
134       return 1;
135     }
136     JJTreeOptions.setCmdLineOption(args[arg]);
137       }
138
139       try {
140     io.setInput(fn);
141       } catch (JJTreeIOException ioe) {
142     p("Error setting input: " + ioe.getMessage());
143     return 1;
144       }
145       p("Reading from file " + io.getInputFileName() + " . . .");
146
147       JJTreeGlobals.toolList = JavaCCGlobals.getToolNames(fn);
148       JJTreeGlobals.toolList.addElement("JJTree");
149
150       try {
151     JJTreeParser parser = new JJTreeParser(io.getIn());
152     parser.javacc_input();
153
154     ASTGrammar root = (ASTGrammar)parser.jjtree.rootNode();
155     if (Boolean.getBoolean("jjtree-dump")) {
156       root.dump(" ");
157     }
158     root.generate(io);
159     io.getOut().close();
160
161     NodeFiles.generateTreeConstants_java();
162     NodeFiles.generateVisitor_java();
163     JJTreeState.generateTreeState_java();
164
165     p("Annotated grammar generated successfully in " +
166       io.getOutputFileName());
167
168       } catch (ParseException pe) {
169     p("Error parsing input: " + pe.toString());
170     return 1;
171       } catch (Exception JavaDoc e) {
172     p("Error parsing input: " + e.toString());
173     e.printStackTrace(io.getMsg());
174     return 1;
175       }
176
177       return 0;
178
179     } finally {
180       io.closeAll();
181     }
182   }
183
184
185   /**
186    * Initialize for JJTree
187    */

188   private void initializeOptions() {
189
190     JJTreeOptions.init();
191
192     JJTreeGlobals.jjtreeOptions.put("JDK_VERSION", "1.4");
193     JJTreeGlobals.jjtreeOptions.put("MULTI", Boolean.FALSE);
194     JJTreeGlobals.jjtreeOptions.put("NODE_PREFIX", "AST");
195     JJTreeGlobals.jjtreeOptions.put("NODE_PACKAGE", "");
196     JJTreeGlobals.jjtreeOptions.put("NODE_EXTENDS", "");
197     JJTreeGlobals.jjtreeOptions.put("NODE_STACK_SIZE", new Integer JavaDoc(500));
198     JJTreeGlobals.jjtreeOptions.put("NODE_DEFAULT_VOID", Boolean.FALSE);
199     JJTreeGlobals.jjtreeOptions.put("OUTPUT_FILE", "");
200     JJTreeGlobals.jjtreeOptions.put("OUTPUT_DIRECTORY", "");
201     JJTreeGlobals.jjtreeOptions.put("CHECK_DEFINITE_NODE", Boolean.TRUE);
202     JJTreeGlobals.jjtreeOptions.put("NODE_SCOPE_HOOK", Boolean.FALSE);
203     JJTreeGlobals.jjtreeOptions.put("NODE_FACTORY", Boolean.FALSE);
204     JJTreeGlobals.jjtreeOptions.put("NODE_USES_PARSER", Boolean.FALSE);
205     JJTreeGlobals.jjtreeOptions.put("BUILD_NODE_FILES", Boolean.TRUE);
206     JJTreeGlobals.jjtreeOptions.put("VISITOR", Boolean.FALSE);
207     JJTreeGlobals.jjtreeOptions.put("VISITOR_EXCEPTION", "");
208   }
209
210
211 }
212
213 /*end*/
214
Popular Tags