KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > TokenStreamHiddenTokenFilter


1 package persistence.antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/license.html
6  *
7  */

8
9 import persistence.antlr.collections.impl.BitSet;
10
11 /**This object filters a token stream coming from a lexer
12  * or another TokenStream so that only certain token channels
13  * get transmitted to the parser.
14  *
15  * Any of the channels can be filtered off as "hidden" channels whose
16  * tokens can be accessed from the parser.
17  */

18 public class TokenStreamHiddenTokenFilter extends TokenStreamBasicFilter implements TokenStream {
19     // protected BitSet discardMask;
20
protected BitSet hideMask;
21
22     protected CommonHiddenStreamToken nextMonitoredToken;
23
24     /** track tail of hidden list emanating from previous
25      * monitored token
26      */

27     protected CommonHiddenStreamToken lastHiddenToken;
28
29     protected CommonHiddenStreamToken firstHidden = null;
30
31     public TokenStreamHiddenTokenFilter(TokenStream input) {
32         super(input);
33         hideMask = new BitSet();
34     }
35
36     protected void consume() throws TokenStreamException {
37         nextMonitoredToken = (CommonHiddenStreamToken)input.nextToken();
38     }
39
40     private void consumeFirst() throws TokenStreamException {
41         consume(); // get first token of input stream
42

43         // Handle situation where hidden or discarded tokens
44
// appear first in input stream
45
CommonHiddenStreamToken p = null;
46         // while hidden or discarded scarf tokens
47
while (hideMask.member(LA(1).getType()) || discardMask.member(LA(1).getType())) {
48             if (hideMask.member(LA(1).getType())) {
49                 if (p == null) {
50                     p = LA(1);
51                 }
52                 else {
53                     p.setHiddenAfter(LA(1));
54                     LA(1).setHiddenBefore(p); // double-link
55
p = LA(1);
56                 }
57                 lastHiddenToken = p;
58                 if (firstHidden == null) {
59                     firstHidden = p; // record hidden token if first
60
}
61             }
62             consume();
63         }
64     }
65
66     public BitSet getDiscardMask() {
67         return discardMask;
68     }
69
70     /** Return a ptr to the hidden token appearing immediately after
71      * token t in the input stream.
72      */

73     public CommonHiddenStreamToken getHiddenAfter(CommonHiddenStreamToken t) {
74         return t.getHiddenAfter();
75     }
76
77     /** Return a ptr to the hidden token appearing immediately before
78      * token t in the input stream.
79      */

80     public CommonHiddenStreamToken getHiddenBefore(CommonHiddenStreamToken t) {
81         return t.getHiddenBefore();
82     }
83
84     public BitSet getHideMask() {
85         return hideMask;
86     }
87
88     /** Return the first hidden token if one appears
89      * before any monitored token.
90      */

91     public CommonHiddenStreamToken getInitialHiddenToken() {
92         return firstHidden;
93     }
94
95     public void hide(int m) {
96         hideMask.add(m);
97     }
98
99     public void hide(BitSet mask) {
100         hideMask = mask;
101     }
102
103     protected CommonHiddenStreamToken LA(int i) {
104         return nextMonitoredToken;
105     }
106
107     /** Return the next monitored token.
108      * Test the token following the monitored token.
109      * If following is another monitored token, save it
110      * for the next invocation of nextToken (like a single
111      * lookahead token) and return it then.
112      * If following is unmonitored, nondiscarded (hidden)
113      * channel token, add it to the monitored token.
114      *
115      * Note: EOF must be a monitored Token.
116      */

117     public Token nextToken() throws TokenStreamException {
118         // handle an initial condition; don't want to get lookahead
119
// token of this splitter until first call to nextToken
120
if (LA(1) == null) {
121             consumeFirst();
122         }
123
124         // we always consume hidden tokens after monitored, thus,
125
// upon entry LA(1) is a monitored token.
126
CommonHiddenStreamToken monitored = LA(1);
127         // point to hidden tokens found during last invocation
128
monitored.setHiddenBefore(lastHiddenToken);
129         lastHiddenToken = null;
130
131         // Look for hidden tokens, hook them into list emanating
132
// from the monitored tokens.
133
consume();
134         CommonHiddenStreamToken p = monitored;
135         // while hidden or discarded scarf tokens
136
while (hideMask.member(LA(1).getType()) || discardMask.member(LA(1).getType())) {
137             if (hideMask.member(LA(1).getType())) {
138                 // attach the hidden token to the monitored in a chain
139
// link forwards
140
p.setHiddenAfter(LA(1));
141                 // link backwards
142
if (p != monitored) { //hidden cannot point to monitored tokens
143
LA(1).setHiddenBefore(p);
144                 }
145                 p = lastHiddenToken = LA(1);
146             }
147             consume();
148         }
149         return monitored;
150     }
151 }
152
Popular Tags