LookaheadSet.java revision 324c4644fee44b9898524c09511bd33c3f12e2df
1/*
2 * [The "BSD license"]
3 *  Copyright (c) 2010 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.analysis;
29
30import org.antlr.misc.IntSet;
31import org.antlr.misc.IntervalSet;
32import org.antlr.tool.Grammar;
33
34/** An LL(1) lookahead set; contains a set of token types and a "hasEOF"
35 *  condition when the set contains EOF.  Since EOF is -1 everywhere and -1
36 *  cannot be stored in my BitSet, I set a condition here.  There may be other
37 *  reasons in the future to abstract a LookaheadSet over a raw BitSet.
38 */
39public class LookaheadSet {
40	public IntervalSet tokenTypeSet;
41
42	public LookaheadSet() {
43		tokenTypeSet = new IntervalSet();
44	}
45
46	public LookaheadSet(IntSet s) {
47		this();
48		tokenTypeSet.addAll(s);
49	}
50
51	public LookaheadSet(int atom) {
52		tokenTypeSet = IntervalSet.of(atom);
53	}
54
55    public LookaheadSet(LookaheadSet other) {
56        this();
57        this.tokenTypeSet.addAll(other.tokenTypeSet);
58    }
59
60    public void orInPlace(LookaheadSet other) {
61		this.tokenTypeSet.addAll(other.tokenTypeSet);
62	}
63
64	public LookaheadSet or(LookaheadSet other) {
65		return new LookaheadSet(tokenTypeSet.or(other.tokenTypeSet));
66	}
67
68	public LookaheadSet subtract(LookaheadSet other) {
69		return new LookaheadSet(this.tokenTypeSet.subtract(other.tokenTypeSet));
70	}
71
72	public boolean member(int a) {
73		return tokenTypeSet.member(a);
74	}
75
76	public LookaheadSet intersection(LookaheadSet s) {
77		IntSet i = this.tokenTypeSet.and(s.tokenTypeSet);
78		LookaheadSet intersection = new LookaheadSet(i);
79		return intersection;
80	}
81
82	public boolean isNil() {
83		return tokenTypeSet.isNil();
84	}
85
86	public void remove(int a) {
87		tokenTypeSet = (IntervalSet)tokenTypeSet.subtract(IntervalSet.of(a));
88	}
89
90	public int hashCode() {
91		return tokenTypeSet.hashCode();
92	}
93
94	public boolean equals(Object other) {
95		return tokenTypeSet.equals(((LookaheadSet)other).tokenTypeSet);
96	}
97
98	public String toString(Grammar g) {
99		if ( tokenTypeSet==null ) {
100			return "";
101		}
102		String r = tokenTypeSet.toString(g);
103		return r;
104	}
105
106	public String toString() {
107		return toString(null);
108	}
109}
110