source: trunk/yao/share/antlr-2.7.7/lib/csharp/antlr.runtime/antlr/TokenStreamSelector.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: 3.2 KB
Line 
1using System;
2using Hashtable         = System.Collections.Hashtable;
3using Stack             = System.Collections.Stack;
4       
5namespace antlr
6{
7        /*ANTLR Translator Generator
8        * Project led by Terence Parr at http://www.jGuru.com
9        * Software rights: http://www.antlr.org/license.html
10        *
11        * $Id:$
12        */
13
14        //
15        // ANTLR C# Code Generator by Micheal Jordan
16        //                            Kunle Odutola       : kunle UNDERSCORE odutola AT hotmail DOT com
17        //                            Anthony Oguntimehin
18        //
19        // With many thanks to Eric V. Smith from the ANTLR list.
20        //
21
22        /*A token stream MUX (multiplexor) knows about n token streams
23        *  and can multiplex them onto the same channel for use by token
24        *  stream consumer like a parser.  This is a way to have multiple
25        *  lexers break up the same input stream for a single parser.
26        *       Or, you can have multiple instances of the same lexer handle
27        *  multiple input streams; this works great for includes.
28        */
29        public class TokenStreamSelector : TokenStream
30        {
31                /*The set of inputs to the MUX */
32                protected internal Hashtable inputStreamNames;
33               
34                /*The currently-selected token stream input */
35                protected internal TokenStream input;
36               
37                /*Used to track stack of input streams */
38                protected internal Stack streamStack = new Stack();
39               
40                public TokenStreamSelector() : base()
41                {
42                        inputStreamNames = new Hashtable();
43                }
44                public virtual void  addInputStream(TokenStream stream, string key)
45                {
46                        inputStreamNames[key] = stream;
47                }
48                /*Return the stream from tokens are being pulled at
49                *  the moment.
50                */
51                public virtual TokenStream getCurrentStream()
52                {
53                        return input;
54                }
55                public virtual TokenStream getStream(string sname)
56                {
57                        TokenStream stream = (TokenStream) inputStreamNames[sname];
58                        if (stream == null)
59                        {
60                                throw new System.ArgumentException("TokenStream " + sname + " not found");
61                        }
62                        return stream;
63                }
64                public virtual IToken nextToken()
65                {
66                        // return input.nextToken();
67                        // keep looking for a token until you don't
68                        // get a retry exception.
69                         for (; ; )
70                        {
71                                try
72                                {
73                                        return input.nextToken();
74                                }
75                                catch (TokenStreamRetryException)
76                                {
77                                        // just retry "forever"
78                                }
79                        }
80                }
81                public virtual TokenStream pop()
82                {
83                        TokenStream stream = (TokenStream) streamStack.Pop();
84                        select(stream);
85                        return stream;
86                }
87                public virtual void  push(TokenStream stream)
88                {
89                        streamStack.Push(input); // save current stream
90                        select(stream);
91                }
92                public virtual void  push(string sname)
93                {
94                        streamStack.Push(input);
95                        select(sname);
96                }
97                /*Abort recognition of current Token and try again.
98                *  A stream can push a new stream (for include files
99                *  for example, and then retry(), which will cause
100                *  the current stream to abort back to this.nextToken().
101                *  this.nextToken() then asks for a token from the
102                *  current stream, which is the new "substream."
103                */
104                public virtual void  retry()
105                {
106                        throw new TokenStreamRetryException();
107                }
108                /*Set the stream without pushing old stream */
109                public virtual void  select(TokenStream stream)
110                {
111                        input = stream;
112                        if (input is CharScanner)
113                        {
114                                ((CharScanner) input).refresh();
115                        }
116                }
117                public virtual void  select(string sname)
118                {
119                        input = getStream(sname);
120                        if (input is CharScanner)
121                        {
122                                ((CharScanner) input).refresh();
123                        }
124                }
125        }
126}
Note: See TracBrowser for help on using the repository browser.