001: /*
002: [The "BSD licence"]
003: Copyright (c) 2005-2007 Terence Parr
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in the
013: documentation and/or other materials provided with the distribution.
014: 3. The name of the author may not be used to endorse or promote products
015: derived from this software without specific prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028: package org.antlr.runtime.tree;
029:
030: public class TreePatternLexer {
031: public static final int EOF = -1;
032: public static final int BEGIN = 1;
033: public static final int END = 2;
034: public static final int ID = 3;
035: public static final int ARG = 4;
036: public static final int PERCENT = 5;
037: public static final int COLON = 6;
038: public static final int DOT = 7;
039:
040: /** The tree pattern to lex like "(A B C)" */
041: protected String pattern;
042:
043: /** Index into input string */
044: protected int p = -1;
045:
046: /** Current char */
047: protected int c;
048:
049: /** How long is the pattern in char? */
050: protected int n;
051:
052: /** Set when token type is ID or ARG (name mimics Java's StreamTokenizer) */
053: public StringBuffer sval = new StringBuffer();
054:
055: public boolean error = false;
056:
057: public TreePatternLexer(String pattern) {
058: this .pattern = pattern;
059: this .n = pattern.length();
060: consume();
061: }
062:
063: public int nextToken() {
064: sval.setLength(0); // reset, but reuse buffer
065: while (c != EOF) {
066: if (c == ' ' || c == '\n' || c == '\r' || c == '\t') {
067: consume();
068: continue;
069: }
070: if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
071: || c == '_') {
072: sval.append((char) c);
073: consume();
074: while ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
075: || (c >= '0' && c <= '9') || c == '_') {
076: sval.append((char) c);
077: consume();
078: }
079: return ID;
080: }
081: if (c == '(') {
082: consume();
083: return BEGIN;
084: }
085: if (c == ')') {
086: consume();
087: return END;
088: }
089: if (c == '%') {
090: consume();
091: return PERCENT;
092: }
093: if (c == ':') {
094: consume();
095: return COLON;
096: }
097: if (c == '.') {
098: consume();
099: return DOT;
100: }
101: if (c == '[') { // grab [x] as a string, returning x
102: consume();
103: while (c != ']') {
104: if (c == '\\') {
105: consume();
106: if (c != ']') {
107: sval.append('\\');
108: }
109: sval.append((char) c);
110: } else {
111: sval.append((char) c);
112: }
113: consume();
114: }
115: consume();
116: return ARG;
117: }
118: consume();
119: error = true;
120: return EOF;
121: }
122: return EOF;
123: }
124:
125: protected void consume() {
126: p++;
127: if (p >= n) {
128: c = EOF;
129: } else {
130: c = pattern.charAt(p);
131: }
132: }
133: }
|