Added ability to order the execution of dictionary adapter behaviors.
[castle.git] / Experiments / Attic / Rook / Castle.Rook.Compiler / Parser / antlr / TokenStreamSelector.cs
blob5dc23dfde18e83736776dc40b546dead0c7a99d7
1 using System;
2 using Hashtable = System.Collections.Hashtable;
3 using Stack = System.Collections.Stack;
5 namespace antlr
7 /*ANTLR Translator Generator
8 * Project led by Terence Parr at http://www.jGuru.com
9 * Software rights: http://www.antlr.org/license.html
11 * $Id:$
15 // ANTLR C# Code Generator by Micheal Jordan
16 // Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com
17 // Anthony Oguntimehin
19 // With many thanks to Eric V. Smith from the ANTLR list.
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.
29 public class TokenStreamSelector : TokenStream
31 /*The set of inputs to the MUX */
32 protected internal Hashtable inputStreamNames;
34 /*The currently-selected token stream input */
35 protected internal TokenStream input;
37 /*Used to track stack of input streams */
38 protected internal Stack streamStack = new Stack();
40 public TokenStreamSelector() : base()
42 inputStreamNames = new Hashtable();
44 public virtual void addInputStream(TokenStream stream, string key)
46 inputStreamNames[key] = stream;
48 /*Return the stream from tokens are being pulled at
49 * the moment.
51 public virtual TokenStream getCurrentStream()
53 return input;
55 public virtual TokenStream getStream(string sname)
57 TokenStream stream = (TokenStream) inputStreamNames[sname];
58 if (stream == null)
60 throw new System.ArgumentException("TokenStream " + sname + " not found");
62 return stream;
64 public virtual IToken nextToken()
66 // return input.nextToken();
67 // keep looking for a token until you don't
68 // get a retry exception.
69 for (; ; )
71 try
73 return input.nextToken();
75 catch (TokenStreamRetryException)
77 // just retry "forever"
81 public virtual TokenStream pop()
83 TokenStream stream = (TokenStream) streamStack.Pop();
84 select(stream);
85 return stream;
87 public virtual void push(TokenStream stream)
89 streamStack.Push(input); // save current stream
90 select(stream);
92 public virtual void push(string sname)
94 streamStack.Push(input);
95 select(sname);
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."
104 public virtual void retry()
106 throw new TokenStreamRetryException();
108 /*Set the stream without pushing old stream */
109 public virtual void select(TokenStream stream)
111 input = stream;
113 public virtual void select(string sname)
115 input = getStream(sname);