1 /* ANTLR Translator Generator
2 * Project led by Terence Parr at http://www.jGuru.com
3 * Software rights: http://www.antlr.org/license.html
5 * $Id: //depot/code/org.antlr/release/antlr-2.7.7/lib/cpp/src/TokenStreamSelector.cpp#2 $
7 #include "antlr/TokenStreamSelector.hpp"
8 #include "antlr/TokenStreamRetryException.hpp"
10 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
14 /** A token stream MUX (multiplexor) knows about n token streams
15 * and can multiplex them onto the same channel for use by token
16 * stream consumer like a parser. This is a way to have multiple
17 * lexers break up the same input stream for a single parser.
18 * Or, you can have multiple instances of the same lexer handle
19 * multiple input streams; this works great for includes.
22 TokenStreamSelector::TokenStreamSelector()
27 TokenStreamSelector::~TokenStreamSelector()
31 void TokenStreamSelector::addInputStream(TokenStream
* stream
, const ANTLR_USE_NAMESPACE(std
)string
& key
)
33 inputStreamNames
[key
] = stream
;
36 TokenStream
* TokenStreamSelector::getCurrentStream() const
41 TokenStream
* TokenStreamSelector::getStream(const ANTLR_USE_NAMESPACE(std
)string
& sname
) const
43 inputStreamNames_coll::const_iterator i
= inputStreamNames
.find(sname
);
44 if (i
== inputStreamNames
.end()) {
45 throw ANTLR_USE_NAMESPACE(std
)string("TokenStream ")+sname
+" not found";
50 RefToken
TokenStreamSelector::nextToken()
52 // keep looking for a token until you don't
53 // get a retry exception
56 return input
->nextToken();
58 catch (TokenStreamRetryException
&) {
59 // just retry "forever"
64 TokenStream
* TokenStreamSelector::pop()
66 TokenStream
* stream
= streamStack
.top();
72 void TokenStreamSelector::push(TokenStream
* stream
)
74 streamStack
.push(input
);
78 void TokenStreamSelector::push(const ANTLR_USE_NAMESPACE(std
)string
& sname
)
80 streamStack
.push(input
);
84 void TokenStreamSelector::retry()
86 throw TokenStreamRetryException();
89 /** Set the stream without pushing old stream */
90 void TokenStreamSelector::select(TokenStream
* stream
)
95 void TokenStreamSelector::select(const ANTLR_USE_NAMESPACE(std
)string
& sname
)
97 inputStreamNames_coll::const_iterator i
= inputStreamNames
.find(sname
);
98 if (i
== inputStreamNames
.end()) {
99 throw ANTLR_USE_NAMESPACE(std
)string("TokenStream ")+sname
+" not found";
104 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE