source: trunk/yao/share/antlr-2.7.7/lib/csharp/antlr.runtime/antlr/TokenStreamHiddenTokenFilter.cs @ 1

Last change on this file since 1 was 1, checked in by lnalod, 15 years ago

Initial import of YAO sources

File size: 4.7 KB
Line 
1using System;
2using BitSet = antlr.collections.impl.BitSet;
3       
4namespace antlr
5{
6        /*ANTLR Translator Generator
7        * Project led by Terence Parr at http://www.jGuru.com
8        * Software rights: http://www.antlr.org/license.html
9        *
10        * $Id:$
11        */
12
13        //
14        // ANTLR C# Code Generator by Micheal Jordan
15        //                            Kunle Odutola       : kunle UNDERSCORE odutola AT hotmail DOT com
16        //                            Anthony Oguntimehin
17        //
18        // With many thanks to Eric V. Smith from the ANTLR list.
19        //
20
21        /*This object filters a token stream coming from a lexer
22        * or another TokenStream so that only certain token channels
23        * get transmitted to the parser.
24        *
25        * Any of the channels can be filtered off as "hidden" channels whose
26        * tokens can be accessed from the parser.
27        */
28        public class TokenStreamHiddenTokenFilter : TokenStreamBasicFilter, TokenStream
29        {
30                // protected BitSet discardMask;
31                protected internal BitSet hideMask;
32               
33                private IHiddenStreamToken nextMonitoredToken;
34               
35                /*track tail of hidden list emanating from previous
36                *  monitored token
37                */
38                protected internal IHiddenStreamToken lastHiddenToken;
39               
40                protected internal IHiddenStreamToken firstHidden = null;
41               
42                public TokenStreamHiddenTokenFilter(TokenStream input) : base(input)
43                {
44                        hideMask = new BitSet();
45                }
46                protected internal virtual void  consume()
47                {
48                        nextMonitoredToken = (IHiddenStreamToken) input.nextToken();
49                }
50                private void  consumeFirst()
51                {
52                        consume(); // get first token of input stream
53                       
54                        // Handle situation where hidden or discarded tokens
55                        // appear first in input stream
56                        IHiddenStreamToken p = null;
57                        // while hidden or discarded scarf tokens
58                        while (hideMask.member(LA(1).Type) || discardMask.member(LA(1).Type))
59                        {
60                                if (hideMask.member(LA(1).Type))
61                                {
62                                        if (p == null)
63                                        {
64                                                p = LA(1);
65                                        }
66                                        else
67                                        {
68                                                p.setHiddenAfter(LA(1));
69                                                LA(1).setHiddenBefore(p); // double-link
70                                                p = LA(1);
71                                        }
72                                        lastHiddenToken = p;
73                                        if (firstHidden == null)
74                                        {
75                                                firstHidden = p; // record hidden token if first
76                                        }
77                                }
78                                consume();
79                        }
80                }
81                public virtual BitSet getDiscardMask()
82                {
83                        return discardMask;
84                }
85                /*Return a ptr to the hidden token appearing immediately after
86                *  token t in the input stream.
87                */
88                public virtual IHiddenStreamToken getHiddenAfter(IHiddenStreamToken t)
89                {
90                        return t.getHiddenAfter();
91                }
92                /*Return a ptr to the hidden token appearing immediately before
93                *  token t in the input stream.
94                */
95                public virtual IHiddenStreamToken getHiddenBefore(IHiddenStreamToken t)
96                {
97                        return t.getHiddenBefore();
98                }
99                public virtual BitSet getHideMask()
100                {
101                        return hideMask;
102                }
103                /*Return the first hidden token if one appears
104                *  before any monitored token.
105                */
106                public virtual IHiddenStreamToken getInitialHiddenToken()
107                {
108                        return firstHidden;
109                }
110                public virtual void  hide(int m)
111                {
112                        hideMask.add(m);
113                }
114                public virtual void  hide(BitSet mask)
115                {
116                        hideMask = mask;
117                }
118                protected internal virtual IHiddenStreamToken LA(int i)
119                {
120                        return nextMonitoredToken;
121                }
122                /*Return the next monitored token.
123                *  Test the token following the monitored token.
124                *  If following is another monitored token, save it
125                *  for the next invocation of nextToken (like a single
126                *  lookahead token) and return it then.
127                *  If following is unmonitored, nondiscarded (hidden)
128                *  channel token, add it to the monitored token.
129                *
130                *  Note: EOF must be a monitored Token.
131                */
132                override public IToken nextToken()
133                {
134                        // handle an initial condition; don't want to get lookahead
135                        // token of this splitter until first call to nextToken
136                        if (LA(1) == null)
137                        {
138                                consumeFirst();
139                        }
140                       
141                        // we always consume hidden tokens after monitored, thus,
142                        // upon entry LA(1) is a monitored token.
143                        IHiddenStreamToken monitored = LA(1);
144                        // point to hidden tokens found during last invocation
145                        monitored.setHiddenBefore(lastHiddenToken);
146                        lastHiddenToken = null;
147                       
148                        // Look for hidden tokens, hook them into list emanating
149                        // from the monitored tokens.
150                        consume();
151                        IHiddenStreamToken p = monitored;
152                        // while hidden or discarded scarf tokens
153                        while (hideMask.member(LA(1).Type) || discardMask.member(LA(1).Type))
154                        {
155                                if (hideMask.member(LA(1).Type))
156                                {
157                                        // attach the hidden token to the monitored in a chain
158                                        // link forwards
159                                        p.setHiddenAfter(LA(1));
160                                        // link backwards
161                                        if (p != monitored)
162                                        {
163                                                //hidden cannot point to monitored tokens
164                                                LA(1).setHiddenBefore(p);
165                                        }
166                                        p = (lastHiddenToken = LA(1));
167                                }
168                                consume();
169                        }
170                        return monitored;
171                }
172                public virtual void resetState()
173                {
174                        firstHidden                     = null;
175                        lastHiddenToken         = null;
176                        nextMonitoredToken      = null;
177                }
178        }
179}
Note: See TracBrowser for help on using the repository browser.