KickJava   Java API By Example, From Geeks To Geeks.

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


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 OtherFilesGen extends JavaCCGlobals implements JavaCCParserConstants {
26
27   public static boolean keepLineCol;
28   static public void start() throws MetaParseException {
29
30     Token t = null;
31     keepLineCol = Options.B("KEEP_LINE_COLUMN");
32
33     if (JavaCCErrors.get_error_count() != 0) throw new MetaParseException();
34
35     JavaFiles.gen_TokenMgrError();
36     JavaFiles.gen_ParseException();
37     JavaFiles.gen_Token();
38     if (Options.B("USER_TOKEN_MANAGER")) {
39       JavaFiles.gen_TokenManager();
40     } else if (Options.B("USER_CHAR_STREAM")) {
41       JavaFiles.gen_CharStream();
42     } else {
43       if (Options.B("JAVA_UNICODE_ESCAPE")) {
44         JavaFiles.gen_JavaCharStream();
45       } else {
46         JavaFiles.gen_SimpleCharStream();
47       }
48     }
49
50     try {
51       ostr = new java.io.PrintWriter(
52                 new java.io.BufferedWriter(
53                    new java.io.FileWriter(
54                      new java.io.File(outputDir, cu_name + "Constants.java")
55                    ),
56                    8192
57                 )
58              );
59     } catch (java.io.IOException e) {
60       JavaCCErrors.semantic_error("Could not open file " + cu_name + "Constants.java for writing.");
61       throw new Error();
62     }
63
64     java.util.Vector tn = (java.util.Vector)(toolNames.clone());
65     tn.addElement(toolName);
66     ostr.println("/* " + getIdString(tn, cu_name + "Constants.java") + " */");
67
68     if (cu_to_insertion_point_1.size() != 0 &&
69         ((Token)cu_to_insertion_point_1.elementAt(0)).kind == PACKAGE
70        ) {
71       for (int i = 1; i < cu_to_insertion_point_1.size(); i++) {
72         if (((Token)cu_to_insertion_point_1.elementAt(i)).kind == SEMICOLON) {
73           printTokenSetup((Token)(cu_to_insertion_point_1.elementAt(0)));
74           for (int j = 0; j <= i; j++) {
75             t = (Token)(cu_to_insertion_point_1.elementAt(j));
76             printToken(t, ostr);
77           }
78           printTrailingComments(t, ostr);
79           ostr.println("");
80           ostr.println("");
81           break;
82         }
83       }
84     }
85     ostr.println("public interface " + cu_name + "Constants {");
86     ostr.println("");
87     RegularExpression re;
88     ostr.println(" int EOF = 0;");
89     for (java.util.Enumeration enum = ordered_named_tokens.elements(); enum.hasMoreElements();) {
90       re = (RegularExpression)enum.nextElement();
91       ostr.println(" int " + re.label + " = " + re.ordinal + ";");
92     }
93     ostr.println("");
94     if (!Options.B("USER_TOKEN_MANAGER") && Options.B("BUILD_TOKEN_MANAGER")) {
95       for (int i = 0; i < LexGen.lexStateName.length; i++) {
96         ostr.println(" int " + LexGen.lexStateName[i] + " = " + i + ";");
97       }
98       ostr.println("");
99     }
100     ostr.println(" String[] tokenImage = {");
101     ostr.println(" \"<EOF>\",");
102
103     for (java.util.Enumeration enum = rexprlist.elements(); enum.hasMoreElements();) {
104       TokenProduction tp = (TokenProduction)(enum.nextElement());
105       java.util.Vector respecs = tp.respecs;
106       for (java.util.Enumeration enum1 = respecs.elements(); enum1.hasMoreElements();) {
107         RegExprSpec res = (RegExprSpec)(enum1.nextElement());
108         re = res.rexp;
109         if (re instanceof RStringLiteral) {
110           ostr.println(" \"\\\"" + add_escapes(add_escapes(((RStringLiteral)re).image)) + "\\\"\",");
111         } else if (!re.label.equals("")) {
112           ostr.println(" \"<" + re.label + ">\",");
113         } else {
114           if (re.tpContext.kind == TokenProduction.TOKEN) {
115             JavaCCErrors.warning(re, "Consider giving this non-string token a label for better error reporting.");
116           }
117           ostr.println(" \"<token of kind " + re.ordinal + ">\",");
118         }
119
120       }
121     }
122     ostr.println(" };");
123     ostr.println("");
124     ostr.println("}");
125
126     ostr.close();
127
128   }
129
130   static private java.io.PrintWriter ostr;
131
132    public static void reInit()
133    {
134       ostr = null;
135    }
136
137 }
138
Popular Tags