📄 lexgen.java
字号:
/* * Copyright © 2002 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has * intellectual property rights relating to technology embodied in the product * that is described in this document. In particular, and without limitation, * these intellectual property rights may include one or more of the U.S. * patents listed at http://www.sun.com/patents and one or more additional * patents or pending patent applications in the U.S. and in other countries. * U.S. Government Rights - Commercial software. Government users are subject * to the Sun Microsystems, Inc. standard license agreement and applicable * provisions of the FAR and its supplements. Use is subject to license terms. * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered * trademarks of Sun Microsystems, Inc. in the U.S. and other countries. This * product is covered and controlled by U.S. Export Control laws and may be * subject to the export or import laws in other countries. Nuclear, missile, * chemical biological weapons or nuclear maritime end uses or end users, * whether direct or indirect, are strictly prohibited. Export or reexport * to countries subject to U.S. embargo or to entities identified on U.S. * export exclusion lists, including, but not limited to, the denied persons * and specially designated nationals lists is strictly prohibited. */package org.javacc.parser;import java.util.*;import java.io.*;public class LexGen extends JavaCCGlobals implements JavaCCParserConstants{ static private java.io.PrintWriter ostr; static private String staticString; static private String tokMgrClassName; // Hashtable of vectors static Hashtable allTpsForState = new Hashtable(); public static int lexStateIndex = 0; static int[] kinds; public static int maxOrdinal = 1; public static String lexStateSuffix; static String[] newLexState; public static int[] lexStates; public static boolean[] ignoreCase; public static Action[] actions; public static Hashtable initStates = new Hashtable(); public static int stateSetSize; public static int maxLexStates; public static String[] lexStateName; static NfaState[] singlesToSkip; public static long[] toSkip; public static long[] toSpecial; public static long[] toMore; public static long[] toToken; public static int defaultLexState; public static RegularExpression[] rexprs; public static int[] maxLongsReqd; public static int[] initMatch; public static int[] canMatchAnyChar; public static boolean hasEmptyMatch; public static boolean[] canLoop; public static boolean[] stateHasActions; public static boolean hasLoop = false; public static boolean[] canReachOnMore; public static boolean[] hasNfa; public static boolean[] mixed; public static NfaState initialState; public static int curKind; static boolean hasSkipActions = false; static boolean hasMoreActions = false; static boolean hasTokenActions = false; static boolean hasSpecial = false; static boolean hasSkip = false; static boolean hasMore = false; static boolean hasToken = false; public static RegularExpression curRE; public static boolean keepLineCol; static void PrintClassHead() { int i, j; try { File tmp = new File(outputDir, tokMgrClassName + ".java"); ostr = new java.io.PrintWriter( new java.io.BufferedWriter( new java.io.FileWriter(tmp), 8092 ) ); Vector tn = (Vector)(toolNames.clone()); tn.addElement(toolName); ostr.println("/* " + getIdString(tn, tokMgrClassName + ".java") + " */"); int l = 0, kind; i = 1; for (;;) { if (cu_to_insertion_point_1.size() <= l) break; kind = ((Token)cu_to_insertion_point_1.elementAt(l)).kind; if(kind == PACKAGE || kind == IMPORT) { for (; i < cu_to_insertion_point_1.size(); i++) { kind = ((Token)cu_to_insertion_point_1.elementAt(i)).kind; if (kind == SEMICOLON || kind == ABSTRACT || kind == FINAL || kind == PUBLIC || kind == CLASS || kind == INTERFACE) { cline = ((Token)(cu_to_insertion_point_1.elementAt(l))).beginLine; ccol = ((Token)(cu_to_insertion_point_1.elementAt(l))).beginColumn; for (j = l; j < i; j++) { printToken((Token)(cu_to_insertion_point_1.elementAt(j)), ostr); } if (kind == SEMICOLON) printToken((Token)(cu_to_insertion_point_1.elementAt(j)), ostr); ostr.println(""); break; } } l = ++i; } else break; } ostr.println(""); ostr.println("public class " + tokMgrClassName + " implements " + cu_name + "Constants"); ostr.println("{"); // } } catch (java.io.IOException err) { JavaCCErrors.semantic_error("Could not create file : " + tokMgrClassName + ".java\n"); throw new Error(); } if (token_mgr_decls != null && token_mgr_decls.size() > 0) { Token t = (Token)token_mgr_decls.elementAt(0); boolean commonTokenActionSeen = false; boolean commonTokenActionNeeded = Options.B("COMMON_TOKEN_ACTION"); printTokenSetup((Token)token_mgr_decls.elementAt(0)); ccol = 1; for (j = 0; j < token_mgr_decls.size(); j++) { t = (Token)token_mgr_decls.elementAt(j); if (t.kind == IDENTIFIER && commonTokenActionNeeded && !commonTokenActionSeen) commonTokenActionSeen = t.image.equals("CommonTokenAction"); printToken(t, ostr); } ostr.println(""); if (commonTokenActionNeeded && !commonTokenActionSeen) JavaCCErrors.warning("You have the COMMON_TOKEN_ACTION option set. But it appears you have not defined the method :\n"+ " " + staticString + "void CommonTokenAction(Token t)\n" + "in your TOKEN_MGR_DECLS. The generated token manager will not compile."); } else if (Options.B("COMMON_TOKEN_ACTION")) { JavaCCErrors.warning("You have the COMMON_TOKEN_ACTION option set. But you have not defined the method :\n"+ " " + staticString + "void CommonTokenAction(Token t)\n" + "in your TOKEN_MGR_DECLS. The generated token manager will not compile."); } ostr.println(" public " + staticString + " java.io.PrintStream debugStream = System.out;"); ostr.println(" public " + staticString + " void setDebugStream(java.io.PrintStream ds) { debugStream = ds; }"); } static void DumpDebugMethods() { ostr.println(" " + staticString + " int kindCnt = 0;"); ostr.println(" protected " + staticString + " final String jjKindsForBitVector(int i, long vec)"); ostr.println(" {"); ostr.println(" String retVal = \"\";"); ostr.println(" if (i == 0)"); ostr.println(" kindCnt = 0;"); ostr.println(" for (int j = 0; j < 64; j++)"); ostr.println(" {"); ostr.println(" if ((vec & (1L << j)) != 0L)"); ostr.println(" {"); ostr.println(" if (kindCnt++ > 0)"); ostr.println(" retVal += \", \";"); ostr.println(" if (kindCnt % 5 == 0)"); ostr.println(" retVal += \"\\n \";"); ostr.println(" retVal += tokenImage[i * 64 + j];"); ostr.println(" }"); ostr.println(" }"); ostr.println(" return retVal;"); ostr.println(" }"); ostr.println(""); ostr.println(" protected " + staticString + " final String jjKindsForStateVector(int lexState, int[] vec, int start, int end)"); ostr.println(" {"); ostr.println(" boolean[] kindDone = new boolean[" + maxOrdinal + "];"); ostr.println(" String retVal = \"\";"); ostr.println(" int cnt = 0;"); ostr.println(" for (int i = start; i < end; i++)"); ostr.println(" {"); ostr.println(" if (vec[i] == -1)"); ostr.println(" continue;"); ostr.println(" int[] stateSet = statesForState[curLexState][vec[i]];"); ostr.println(" for (int j = 0; j < stateSet.length; j++)"); ostr.println(" {"); ostr.println(" int state = stateSet[j];"); ostr.println(" if (!kindDone[kindForState[lexState][state]])"); ostr.println(" {"); ostr.println(" kindDone[kindForState[lexState][state]] = true;"); ostr.println(" if (cnt++ > 0)"); ostr.println(" retVal += \", \";"); ostr.println(" if (cnt % 5 == 0)"); ostr.println(" retVal += \"\\n \";"); ostr.println(" retVal += tokenImage[kindForState[lexState][state]];"); ostr.println(" }"); ostr.println(" }"); ostr.println(" }"); ostr.println(" if (cnt == 0)"); ostr.println(" return \"{ }\";"); ostr.println(" else"); ostr.println(" return \"{ \" + retVal + \" }\";"); ostr.println(" }"); ostr.println(""); } static void BuildLexStatesTable() { Enumeration e = rexprlist.elements(); TokenProduction tp; int i; String[] tmpLexStateName = new String[lexstate_I2S.size()]; while (e.hasMoreElements()) { tp = (TokenProduction)e.nextElement(); Vector respecs = tp.respecs; Vector tps; for (i = 0; i < tp.lexStates.length; i++) { if ((tps = (Vector)allTpsForState.get(tp.lexStates[i])) == null) { tmpLexStateName[maxLexStates++] = tp.lexStates[i]; allTpsForState.put(tp.lexStates[i], tps = new Vector()); } tps.addElement(tp); } if (respecs == null || respecs.size() == 0) continue; RegularExpression re; for (i = 0; i < respecs.size(); i++) if (maxOrdinal <= (re = ((RegExprSpec)respecs.elementAt(i)).rexp).ordinal) maxOrdinal = re.ordinal + 1; } kinds = new int[maxOrdinal]; toSkip = new long[maxOrdinal / 64 + 1]; toSpecial = new long[maxOrdinal / 64 + 1]; toMore = new long[maxOrdinal / 64 + 1]; toToken = new long[maxOrdinal / 64 + 1]; toToken[0] = 1L; actions = new Action[maxOrdinal]; actions[0] = actForEof; hasTokenActions = actForEof != null; initStates = new Hashtable(); canMatchAnyChar = new int[maxLexStates]; canLoop = new boolean[maxLexStates]; stateHasActions = new boolean[maxLexStates]; lexStateName = new String[maxLexStates]; singlesToSkip = new NfaState[maxLexStates]; System.arraycopy(tmpLexStateName, 0, lexStateName, 0, maxLexStates); for (i = 0; i < maxLexStates; i++) canMatchAnyChar[i] = -1; hasNfa = new boolean[maxLexStates]; mixed = new boolean[maxLexStates]; maxLongsReqd = new int[maxLexStates]; initMatch = new int[maxLexStates]; newLexState = new String[maxOrdinal]; newLexState[0] = nextStateForEof; hasEmptyMatch = false; lexStates = new int[maxOrdinal]; ignoreCase = new boolean[maxOrdinal]; rexprs = new RegularExpression[maxOrdinal]; RStringLiteral.allImages = new String[maxOrdinal]; canReachOnMore = new boolean[maxLexStates]; } static int GetIndex(String name) { for (int i = 0; i < lexStateName.length; i++) if (lexStateName[i] != null && lexStateName[i].equals(name)) return i; throw new Error(); // Should never come here } public static void AddCharToSkip(char c, int kind) { singlesToSkip[lexStateIndex].AddChar(c); singlesToSkip[lexStateIndex].kind = kind; } public static void start() { if (!Options.B("BUILD_TOKEN_MANAGER") || Options.B("USER_TOKEN_MANAGER") || JavaCCErrors.get_error_count() > 0) return; keepLineCol = Options.B("KEEP_LINE_COLUMN"); Vector choices = new Vector(); Enumeration e; TokenProduction tp; int i, j; staticString = (Options.B("STATIC") ? "static " : ""); tokMgrClassName = cu_name + "TokenManager"; PrintClassHead(); BuildLexStatesTable(); e = allTpsForState.keys(); boolean ignoring = false; while (e.hasMoreElements()) { NfaState.ReInit(); RStringLiteral.ReInit(); String key = (String)e.nextElement(); lexStateIndex = GetIndex(key); lexStateSuffix = "_" + lexStateIndex; Vector allTps = (Vector)allTpsForState.get(key); initStates.put(key, initialState = new NfaState()); ignoring = false; singlesToSkip[lexStateIndex] = new NfaState(); singlesToSkip[lexStateIndex].dummy = true; if (key.equals("DEFAULT")) defaultLexState = lexStateIndex; for (i = 0; i < allTps.size(); i++) { tp = (TokenProduction)allTps.elementAt(i); int kind = tp.kind; boolean ignore = tp.ignoreCase; Vector rexps = tp.respecs; if (i == 0) ignoring = ignore; for (j = 0; j < rexps.size(); j++) { RegExprSpec respec = (RegExprSpec)rexps.elementAt(j); curRE = respec.rexp; rexprs[curKind = curRE.ordinal] = curRE; lexStates[curRE.ordinal] = lexStateIndex; ignoreCase[curRE.ordinal] = ignore; if (curRE.private_rexp) { kinds[curRE.ordinal] = -1; continue; } if (curRE instanceof RStringLiteral && !((RStringLiteral)curRE).image.equals(""))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -