TreePatternLexer.java revision 324c4644fee44b9898524c09511bd33c3f12e2df
1/*
2 [The "BSD license"]
3 Copyright (c) 2005-2009 Terence Parr
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 1. Redistributions of source code must retain the above copyright
10     notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12     notice, this list of conditions and the following disclaimer in the
13     documentation and/or other materials provided with the distribution.
14 3. The name of the author may not be used to endorse or promote products
15     derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28package org.antlr.runtime.tree;
29
30public class TreePatternLexer {
31	public static final int EOF = -1;
32	public static final int BEGIN = 1;
33	public static final int END = 2;
34	public static final int ID = 3;
35	public static final int ARG = 4;
36	public static final int PERCENT = 5;
37	public static final int COLON = 6;
38	public static final int DOT = 7;
39
40	/** The tree pattern to lex like "(A B C)" */
41	protected String pattern;
42
43	/** Index into input string */
44	protected int p = -1;
45
46	/** Current char */
47	protected int c;
48
49	/** How long is the pattern in char? */
50	protected int n;
51
52	/** Set when token type is ID or ARG (name mimics Java's StreamTokenizer) */
53	public StringBuffer sval = new StringBuffer();
54
55	public boolean error = false;
56
57	public TreePatternLexer(String pattern) {
58		this.pattern = pattern;
59		this.n = pattern.length();
60		consume();
61	}
62
63	public int nextToken() {
64		sval.setLength(0); // reset, but reuse buffer
65		while ( c != EOF ) {
66			if ( c==' ' || c=='\n' || c=='\r' || c=='\t' ) {
67				consume();
68				continue;
69			}
70			if ( (c>='a' && c<='z') || (c>='A' && c<='Z') || c=='_' ) {
71				sval.append((char)c);
72				consume();
73				while ( (c>='a' && c<='z') || (c>='A' && c<='Z') ||
74						(c>='0' && c<='9') || c=='_' )
75				{
76					sval.append((char)c);
77					consume();
78				}
79				return ID;
80			}
81			if ( c=='(' ) {
82				consume();
83				return BEGIN;
84			}
85			if ( c==')' ) {
86				consume();
87				return END;
88			}
89			if ( c=='%' ) {
90				consume();
91				return PERCENT;
92			}
93			if ( c==':' ) {
94				consume();
95				return COLON;
96			}
97			if ( c=='.' ) {
98				consume();
99				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					}
111					else {
112						sval.append((char)c);
113					}
114					consume();
115				}
116				consume();
117				return ARG;
118			}
119			consume();
120			error = true;
121			return EOF;
122		}
123		return EOF;
124	}
125
126	protected void consume() {
127		p++;
128		if ( p>=n ) {
129			c = EOF;
130		}
131		else {
132			c = pattern.charAt(p);
133		}
134	}
135}