📄 rstringliteral.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.*;final class KindInfo{ long[] validKinds; long[] finalKinds; int validKindCnt = 0; int finalKindCnt = 0; KindInfo(int maxKind) { validKinds = new long[maxKind / 64 + 1]; finalKinds = new long[maxKind / 64 + 1]; } public void InsertValidKind(int kind) { validKinds[kind / 64] |= (1L << (kind % 64)); validKindCnt++; } public void InsertFinalKind(int kind) { finalKinds[kind / 64] |= (1L << (kind % 64)); finalKindCnt++; }};/** * Describes string literals. */public class RStringLiteral extends RegularExpression { /** * The string image of the literal. */ public String image; static int maxStrKind = 0; static int maxLen = 0; static int charCnt = 0; static Vector charPosKind = new Vector(); // Elements are hashtables // with single char keys; static int[] maxLenForActive = new int[100]; // 6400 tokens public static String[] allImages; static int[][] intermediateKinds; static int[][] intermediateMatchedPos; static int startStateCnt = 0; static boolean subString[]; static boolean subStringAtPos[]; static Hashtable[] statesForPos; // Need to call this method after gnerating code for each lexical state. It // initializes all the static variables, so that there is no interference // between the various states of the lexer. public static void ReInit() { maxStrKind = 0; maxLen = 0; charPosKind = new Vector(); maxLenForActive = new int[100]; // 6400 tokens intermediateKinds = null; intermediateMatchedPos = null; startStateCnt = 0; subString = null; subStringAtPos = null; statesForPos = null; } public static void DumpStrLiteralImages(java.io.PrintWriter ostr) { String image; int charCnt = 0, i; ostr.println("public static final String[] jjstrLiteralImages = {"); if (allImages == null || allImages.length == 0) { ostr.println("};"); return; } allImages[0] = ""; for (i = 0; i < allImages.length; i++) { if ((image = allImages[i]) == null || ((LexGen.toSkip[i / 64] & (1L << (i % 64))) == 0L && (LexGen.toMore[i / 64] & (1L << (i % 64))) == 0L && (LexGen.toToken[i / 64] & (1L << (i % 64))) == 0L) || (LexGen.toSkip[i / 64] & (1L << (i % 64))) != 0L || (LexGen.toMore[i / 64] & (1L << (i % 64))) != 0L || LexGen.canReachOnMore[LexGen.lexStates[i]] || ((Options.B("IGNORE_CASE") || LexGen.ignoreCase[i]) && (!image.equals(image.toLowerCase()) || !image.equals(image.toUpperCase())))) { allImages[i] = null; if ((charCnt += 6) > 80) { ostr.println(""); charCnt = 0; } ostr.print("null, "); continue; } String toPrint = "\""; for (int j = 0; j < image.length(); j++) { if (image.charAt(j) <= 0xff) toPrint += ("\\" + Integer.toOctalString((int)image.charAt(j))); else { String hexVal = Integer.toHexString((int)image.charAt(j)); if (hexVal.length() == 3) hexVal = "0" + hexVal; toPrint += ("\\u" + hexVal); } } toPrint += ( "\", "); if ((charCnt += toPrint.length()) >= 80) { ostr.println(""); charCnt = 0; } ostr.print(toPrint); } while (++i < LexGen.maxOrdinal) { if ((charCnt += 6) > 80) { ostr.println(""); charCnt = 0; } ostr.print("null, "); continue; } ostr.println("};"); } // Used for top level string literals public void GenerateDfa(java.io.PrintWriter ostr, int kind) { String s; Hashtable temp; KindInfo info; int len; if (maxStrKind <= ordinal) maxStrKind = ordinal + 1; if ((len = image.length()) > maxLen) maxLen = len; char c; for (int i = 0; i < len; i++) { if (Options.B("IGNORE_CASE")) s = ("" + (c = image.charAt(i))).toLowerCase(); else s = "" + (c = image.charAt(i)); if (!NfaState.unicodeWarningGiven && c > 0xff && !Options.B("JAVA_UNICODE_ESCAPE") && !Options.B("USER_CHAR_STREAM")) { NfaState.unicodeWarningGiven = true; JavaCCErrors.warning(LexGen.curRE, "Non-ASCII characters used in regular expression." + "Please make sure you use the correct Reader when you create the parser that can handle your character set."); } if (i >= charPosKind.size()) // Kludge, but OK charPosKind.addElement(temp = new Hashtable()); else temp = (Hashtable)charPosKind.elementAt(i); if ((info = (KindInfo)temp.get(s)) == null) temp.put(s, info = new KindInfo(LexGen.maxOrdinal)); if (i + 1 == len) info.InsertFinalKind(ordinal); else info.InsertValidKind(ordinal); if (!Options.B("IGNORE_CASE") && LexGen.ignoreCase[ordinal] && c != Character.toLowerCase(c)) { s = ("" + image.charAt(i)).toLowerCase(); if (i >= charPosKind.size()) // Kludge, but OK charPosKind.addElement(temp = new Hashtable()); else temp = (Hashtable)charPosKind.elementAt(i); if ((info = (KindInfo)temp.get(s)) == null) temp.put(s, info = new KindInfo(LexGen.maxOrdinal)); if (i + 1 == len) info.InsertFinalKind(ordinal); else info.InsertValidKind(ordinal); } if (!Options.B("IGNORE_CASE") && LexGen.ignoreCase[ordinal] && c != Character.toUpperCase(c)) { s = ("" + image.charAt(i)).toUpperCase(); if (i >= charPosKind.size()) // Kludge, but OK charPosKind.addElement(temp = new Hashtable()); else temp = (Hashtable)charPosKind.elementAt(i); if ((info = (KindInfo)temp.get(s)) == null) temp.put(s, info = new KindInfo(LexGen.maxOrdinal)); if (i + 1 == len) info.InsertFinalKind(ordinal); else info.InsertValidKind(ordinal); } } maxLenForActive[ordinal / 64] = Math.max(maxLenForActive[ordinal / 64], len -1); allImages[ordinal] = image; } public Nfa GenerateNfa(boolean ignoreCase) { if (image.length() == 1) { RCharacterList temp = new RCharacterList(image.charAt(0)); return temp.GenerateNfa(ignoreCase); } NfaState startState = new NfaState(); NfaState theStartState = startState; NfaState finalState = null; if (image.length() == 0) return new Nfa(theStartState, theStartState); int i; for (i = 0; i < image.length(); i++) { finalState = new NfaState(); startState.charMoves = new char[1]; startState.AddChar(image.charAt(i)); if (Options.B("IGNORE_CASE") || ignoreCase) { startState.AddChar(Character.toLowerCase(image.charAt(i))); startState.AddChar(Character.toUpperCase(image.charAt(i))); } startState.next = finalState; startState = finalState; } return new Nfa(theStartState, finalState); } static void DumpNullStrLiterals(java.io.PrintWriter ostr) { ostr.println("{"); if (NfaState.generatedStates != 0) ostr.println(" return jjMoveNfa" + LexGen.lexStateSuffix + "(" + NfaState.InitStateName() + ", 0);"); else ostr.println(" return 1;"); ostr.println("}"); } private static int GetStateSetForKind(int pos, int kind) { if (LexGen.mixed[LexGen.lexStateIndex] || NfaState.generatedStates == 0) return -1; Hashtable allStateSets = statesForPos[pos]; if (allStateSets == null) return -1; Enumeration e = allStateSets.keys(); while (e.hasMoreElements()) { String s = (String)e.nextElement(); long[] actives = (long[])allStateSets.get(s); s = s.substring(s.indexOf(", ") + 2); s = s.substring(s.indexOf(", ") + 2); if (s.equals("null;")) continue; if (actives != null && (actives[kind / 64] & (1L << (kind % 64))) != 0L) { return NfaState.AddStartStateSet(s); } } return -1; } static String GetLabel(int kind) { RegularExpression re = LexGen.rexprs[kind]; if (re instanceof RStringLiteral) return " \"" + JavaCCGlobals.add_escapes(((RStringLiteral)re).image) + "\""; else if (!re.label.equals("")) return " <" + re.label + ">"; else return " <token of kind " + kind + ">"; } static int GetLine(int kind) { return LexGen.rexprs[kind].line; } static int GetColumn(int kind) { return LexGen.rexprs[kind].column; } /** * Returns true if s1 starts with s2 (ignoring case for each character). */ static private boolean StartsWithIgnoreCase(String s1, String s2) { if (s1.length() < s2.length()) return false; for (int i = 0; i < s2.length(); i++) { char c1 = s1.charAt(i), c2 = s2.charAt(i); if (c1 != c2 && Character.toLowerCase(c2) != c1 && Character.toUpperCase(c2) != c1) return false; } return true; } static void FillSubString() { String image; subString = new boolean[maxStrKind + 1]; subStringAtPos = new boolean[maxLen]; for (int i = 0; i < maxStrKind; i++) { subString[i] = false; if ((image = allImages[i]) == null || LexGen.lexStates[i] != LexGen.lexStateIndex) continue; if (LexGen.mixed[LexGen.lexStateIndex]) { // We will not optimize for mixed case subString[i] = true; subStringAtPos[image.length() - 1] = true; continue; } for (int j = 0; j < maxStrKind; j++) { if (j != i && LexGen.lexStates[j] == LexGen.lexStateIndex && ((String)allImages[j]) != null) { if (((String)allImages[j]).indexOf(image) == 0) { subString[i] = true; subStringAtPos[image.length() - 1] = true; break; } else if (Options.B("IGNORE_CASE") && StartsWithIgnoreCase((String)allImages[j], image)) { subString[i] = true; subStringAtPos[image.length() - 1] = true; break; } } } } } static void DumpStartWithStates(java.io.PrintWriter ostr) { ostr.println((Options.B("STATIC") ? "static " : "") + "private final int " + "jjStartNfaWithStates" + LexGen.lexStateSuffix + "(int pos, int kind, int state)"); ostr.println("{");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -