📄 pattern.java
字号:
printObjectTree(((Prolog)node).loop); System.out.println("**** end contents prolog loop"); } else if (node instanceof Loop) { System.out.println(node); printObjectTree(((Loop)node).body); System.out.println("**** end contents Loop body"); } else if (node instanceof Curly) { System.out.println(node); printObjectTree(((Curly)node).atom); System.out.println("**** end contents Curly body"); } else if (node instanceof GroupTail) { System.out.println(node); System.out.println("Tail next is "+node.next); return; } else { System.out.println(node); } node = node.next; if (node != null) System.out.println("->next:"); if (node == Pattern.accept) { System.out.println("Accept Node"); node = null; } } } /** * Used to accumulate information about a subtree of the object graph * so that optimizations can be applied to the subtree. */ static final class TreeInfo { int minLength; int maxLength; boolean maxValid; boolean deterministic; TreeInfo() { reset(); } void reset() { minLength = 0; maxLength = 0; maxValid = true; deterministic = true; } } /** * The following private methods are mainly used to improve the * readability of the code. In order to let the Java compiler easily * inline them, we should not put many assertions or error checks in them. */ /** * Indicates whether a particular flag is set or not. */ private boolean has(int f) { return (flags & f) > 0; } /** * Match next character, signal error if failed. */ private void accept(int ch, String s) { int testChar = temp[cursor++]; if (has(COMMENTS)) testChar = parsePastWhitespace(testChar); if (ch != testChar) { error(s); } } /** * Mark the end of pattern with a specific character. */ private void mark(char c) { temp[patternLength] = c; } /** * Peek the next character, and do not advance the cursor. */ private int peek() { int ch = temp[cursor]; if (has(COMMENTS)) ch = peekPastWhitespace(ch); return ch; } /** * Read the next character, and advance the cursor by one. */ private int read() { int ch = temp[cursor++]; if (has(COMMENTS)) ch = parsePastWhitespace(ch); return ch; } /** * Read the next character, and advance the cursor by one, * ignoring the COMMENTS setting */ private int readEscaped() { int ch = temp[cursor++]; return ch; } /** * Advance the cursor by one, and peek the next character. */ private int next() { int ch = temp[++cursor]; if (has(COMMENTS)) ch = peekPastWhitespace(ch); return ch; } /** * Advance the cursor by one, and peek the next character, * ignoring the COMMENTS setting */ private int nextEscaped() { int ch = temp[++cursor]; return ch; } /** * If in xmode peek past whitespace and comments. */ private int peekPastWhitespace(int ch) { while (ASCII.isSpace(ch) || ch == '#') { while (ASCII.isSpace(ch)) ch = temp[++cursor]; if (ch == '#') { ch = peekPastLine(); } } return ch; } /** * If in xmode parse past whitespace and comments. */ private int parsePastWhitespace(int ch) { while (ASCII.isSpace(ch) || ch == '#') { while (ASCII.isSpace(ch)) ch = temp[cursor++]; if (ch == '#') ch = parsePastLine(); } return ch; } /** * xmode parse past comment to end of line. */ private int parsePastLine() { int ch = temp[cursor++]; while (ch != 0 && !isLineSeparator(ch)) ch = temp[cursor++]; return ch; } /** * xmode peek past comment to end of line. */ private int peekPastLine() { int ch = temp[++cursor]; while (ch != 0 && !isLineSeparator(ch)) ch = temp[++cursor]; return ch; } /** * Determines if character is a line separator in the current mode */ private boolean isLineSeparator(int ch) { if (has(UNIX_LINES)) { return ch == '\n'; } else { return (ch == '\n' || ch == '\r' || (ch|1) == '\u2029' || ch == '\u0085'); } } /** * Read the character after the next one, and advance the cursor by two. */ private int skip() { int i = cursor; int ch = temp[i+1]; cursor = i + 2; return ch; } /** * Unread one next character, and retreat cursor by one. */ private void unread() { cursor--; } /** * Internal method used for handling all syntax errors. The pattern is * displayed with a pointer to aid in locating the syntax error. */ private Node error(String s) { throw new PatternSyntaxException(s, normalizedPattern, cursor - 1); } /** * The following methods handle the main parsing. They are sorted * according to their precedence order, the lowest one first. */ /** * The expression is parsed with branch nodes added for alternations. * This may be called recursively to parse sub expressions that may * contain alternations. */ private Node expr(Node end) { Node prev = null; for (;;) { Node node = sequence(end); if (prev == null) { prev = node; } else { prev = new Branch(prev, node); } if (peek() != '|') { return prev; } next(); } } /** * Parsing of sequences between alternations. */ private Node sequence(Node end) { Node head = null; Node tail = null; Node node = null; int i, j, ch; LOOP: for (;;) { ch = peek(); switch (ch) { case '(': // Because group handles its own closure, // we need to treat it differently node = group0(); // Check for comment or flag group if (node == null) continue; if (head == null) head = node; else tail.next = node; // Double return: Tail was returned in root tail = root; continue; case '[': node = clazz(true); break; case '\\': ch = nextEscaped(); if (ch == 'p' || ch == 'P') { boolean comp = (ch == 'P'); boolean oneLetter = true; ch = next(); // Consume { if present if (ch != '{') { unread(); } else { oneLetter = false; } node = family(comp, oneLetter); } else { unread(); node = atom(); } break; case '^': next(); if (has(MULTILINE)) { if (has(UNIX_LINES)) node = new UnixCaret(); else node = new Caret(); } else { node = new Begin(); } break; case '$': next(); if (has(UNIX_LINES)) node = new UnixDollar(has(MULTILINE)); else node = new Dollar(has(MULTILINE)); break; case '.': next(); if (has(DOTALL)) { node = new All(); } else { if (has(UNIX_LINES)) node = new UnixDot(); else { node = new Dot(); } } break; case '|': case ')': break LOOP; case ']': // Now interpreting dangling ] and } as literals case '}': node = atom(); break; case '?': case '*': case '+': next(); return error("Dangling meta character '" + (
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -