KickJava   Java API By Example, From Geeks To Geeks.

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


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 public class Main {
26
27   static void help_message() {
28     System.out.println("Usage:");
29     System.out.println(" javacc option-settings inputfile");
30     System.out.println("");
31     System.out.println("\"option-settings\" is a sequence of settings separated by spaces.");
32     System.out.println("Each option setting must be of one of the following forms:");
33     System.out.println("");
34     System.out.println(" -optionname=value (e.g., -STATIC=false)");
35     System.out.println(" -optionname:value (e.g., -STATIC:false)");
36     System.out.println(" -optionname (equivalent to -optionname=true. e.g., -STATIC)");
37     System.out.println(" -NOoptionname (equivalent to -optionname=false. e.g., -NOSTATIC)");
38     System.out.println("");
39     System.out.println("Option settings are not case-sensitive, so one can say \"-nOsTaTiC\" instead");
40     System.out.println("of \"-NOSTATIC\". Option values must be appropriate for the corresponding");
41     System.out.println("option, and must be either an integer, a boolean, or a string value.");
42     System.out.println("");
43     System.out.println("The integer valued options are:");
44     System.out.println("");
45     System.out.println(" LOOKAHEAD (default 1)");
46     System.out.println(" CHOICE_AMBIGUITY_CHECK (default 2)");
47     System.out.println(" OTHER_AMBIGUITY_CHECK (default 1)");
48     System.out.println("");
49     System.out.println("The boolean valued options are:");
50     System.out.println("");
51     System.out.println(" STATIC (default true)");
52     System.out.println(" DEBUG_PARSER (default false)");
53     System.out.println(" DEBUG_LOOKAHEAD (default false)");
54     System.out.println(" DEBUG_TOKEN_MANAGER (default false)");
55     System.out.println(" OPTIMIZE_TOKEN_MANAGER (default true)");
56     System.out.println(" ERROR_REPORTING (default true)");
57     System.out.println(" JAVA_UNICODE_ESCAPE (default false)");
58     System.out.println(" UNICODE_INPUT (default false)");
59     System.out.println(" IGNORE_CASE (default false)");
60     System.out.println(" COMMON_TOKEN_ACTION (default false)");
61     System.out.println(" USER_TOKEN_MANAGER (default false)");
62     System.out.println(" USER_CHAR_STREAM (default false)");
63     System.out.println(" BUILD_PARSER (default true)");
64     System.out.println(" BUILD_TOKEN_MANAGER (default true)");
65     System.out.println(" TOKEN_MANAGER_USES_PARSER (default false)");
66     System.out.println(" SANITY_CHECK (default true)");
67     System.out.println(" FORCE_LA_CHECK (default false)");
68     System.out.println(" CACHE_TOKENS (default false)");
69     System.out.println(" KEEP_LINE_COLUMN (default true)");
70     System.out.println("");
71     System.out.println("The string valued options are:");
72     System.out.println("");
73     System.out.println(" OUTPUT_DIRECTORY (default Current Directory)");
74     System.out.println(" JDK_VERSION (default 1.4)");
75     System.out.println("");
76     System.out.println("EXAMPLE:");
77     System.out.println(" javacc -STATIC=false -LOOKAHEAD:2 -debug_parser mygrammar.jj");
78     System.out.println("");
79   }
80
81   /**
82    * A main program that exercises the parser.
83    */

84   public static void main(String JavaDoc args[]) throws Exception JavaDoc {
85     int errorcode = mainProgram(args);
86     System.exit(errorcode);
87   }
88
89   /**
90    * The method to call to exercise the parser from other Java programs.
91    * It returns an error code. See how the main program above uses
92    * this method.
93    */

94   public static int mainProgram(String JavaDoc args[]) throws Exception JavaDoc {
95
96     // Initialize all static state
97
reInitAll();
98
99     JavaCCGlobals.bannerLine("Parser Generator", "");
100
101     JavaCCParser parser = null;
102     if (args.length == 0) {
103       System.out.println("");
104       help_message();
105       return 1;
106     } else {
107       System.out.println("(type \"javacc\" with no arguments for help)");
108     }
109
110     if (Options.isOption(args[args.length-1])) {
111       System.out.println("Last argument \"" + args[args.length-1] + "\" is not a filename.");
112       return 1;
113     }
114     for (int arg = 0; arg < args.length-1; arg++) {
115       if (!Options.isOption(args[arg])) {
116         System.out.println("Argument \"" + args[arg] + "\" must be an option setting.");
117         return 1;
118       }
119       Options.setCmdLineOption(args[arg]);
120     }
121
122     try {
123       java.io.File JavaDoc fp = new java.io.File JavaDoc(args[args.length-1]);
124       if (!fp.exists()) {
125          System.out.println("File " + args[args.length-1] + " not found.");
126          return 1;
127       }
128       if (fp.isDirectory()) {
129          System.out.println(args[args.length-1] + " is a directory. Please use a valid file name.");
130          return 1;
131       }
132       parser = new JavaCCParser(new java.io.FileReader JavaDoc(args[args.length-1]));
133     } catch (NullPointerException JavaDoc ne) { // Should never happen
134
} catch (SecurityException JavaDoc se) {
135       System.out.println("Security voilation while trying to open " + args[args.length-1]);
136       return 1;
137     } catch (java.io.FileNotFoundException JavaDoc e) {
138       System.out.println("File " + args[args.length-1] + " not found.");
139       return 1;
140     }
141
142     try {
143       System.out.println("Reading from file " + args[args.length-1] + " . . .");
144       JavaCCGlobals.fileName = JavaCCGlobals.origFileName = args[args.length-1];
145       JavaCCGlobals.jjtreeGenerated = JavaCCGlobals.isGeneratedBy("JJTree", args[args.length-1]);
146       JavaCCGlobals.jjcovGenerated = JavaCCGlobals.isGeneratedBy("JJCov", args[args.length-1]);
147       JavaCCGlobals.toolNames = JavaCCGlobals.getToolNames(args[args.length-1]);
148       parser.javacc_input();
149       JavaCCGlobals.createOutputDir(Options.getOutputDirectory());
150
151       if (Options.getUnicodeInput())
152       {
153          NfaState.unicodeWarningGiven = true;
154          System.out.println("Note: UNICODE_INPUT option is specified. " +
155               "Please make sure you create the parser/lexer using a Reader with the correct character encoding.");
156       }
157
158       Semanticize.start();
159       ParseGen.start();
160       LexGen.start();
161       OtherFilesGen.start();
162
163       if ((JavaCCErrors.get_error_count() == 0) && (Options.getBuildParser() || Options.getBuildTokenManager())) {
164         if (JavaCCErrors.get_warning_count() == 0) {
165           System.out.println("Parser generated successfully.");
166         } else {
167           System.out.println("Parser generated with 0 errors and "
168                              + JavaCCErrors.get_warning_count() + " warnings.");
169         }
170         return 0;
171       } else {
172         System.out.println("Detected " + JavaCCErrors.get_error_count() + " errors and "
173                            + JavaCCErrors.get_warning_count() + " warnings.");
174         return (JavaCCErrors.get_error_count()==0)?0:1;
175       }
176     } catch (MetaParseException e) {
177       System.out.println("Detected " + JavaCCErrors.get_error_count() + " errors and "
178                          + JavaCCErrors.get_warning_count() + " warnings.");
179       return 1;
180     } catch (ParseException e) {
181       System.out.println(e.toString());
182       System.out.println("Detected " + (JavaCCErrors.get_error_count()+1) + " errors and "
183                          + JavaCCErrors.get_warning_count() + " warnings.");
184       return 1;
185     }
186   }
187
188    public static void reInitAll()
189    {
190       org.javacc.parser.Expansion.reInit();
191       org.javacc.parser.JavaCCErrors.reInit();
192       org.javacc.parser.JavaCCGlobals.reInit();
193       Options.init();
194       org.javacc.parser.JavaCCParserInternals.reInit();
195       org.javacc.parser.RStringLiteral.reInit();
196       org.javacc.parser.JavaFiles.reInit();
197       org.javacc.parser.LexGen.reInit();
198       org.javacc.parser.NfaState.reInit();
199       org.javacc.parser.MatchInfo.reInit();
200       org.javacc.parser.LookaheadWalk.reInit();
201       org.javacc.parser.Semanticize.reInit();
202       org.javacc.parser.ParseGen.reInit();
203       org.javacc.parser.OtherFilesGen.reInit();
204       org.javacc.parser.ParseEngine.reInit();
205    }
206
207 }
208
Popular Tags