blob: 602e50dc840968f5d7eeabc6b5e2c4584166fd06 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/* ANTLR Translator Generator
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/license.html
*
* $Id$
*/
#include "antlr/TokenStreamSelector.hpp"
#include "antlr/TokenStreamRetryException.hpp"
#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
namespace antlr {
#endif
/** A token stream MUX (multiplexor) knows about n token streams
* and can multiplex them onto the same channel for use by token
* stream consumer like a parser. This is a way to have multiple
* lexers break up the same input stream for a single parser.
* Or, you can have multiple instances of the same lexer handle
* multiple input streams; this works great for includes.
*/
TokenStreamSelector::TokenStreamSelector()
: input(0)
{
}
TokenStreamSelector::~TokenStreamSelector()
{
}
void TokenStreamSelector::addInputStream(TokenStream* stream, const ANTLR_USE_NAMESPACE(std)string& key)
{
inputStreamNames[key] = stream;
}
TokenStream* TokenStreamSelector::getCurrentStream() const
{
return input;
}
TokenStream* TokenStreamSelector::getStream(const ANTLR_USE_NAMESPACE(std)string& sname) const
{
inputStreamNames_coll::const_iterator i = inputStreamNames.find(sname);
if (i == inputStreamNames.end()) {
throw ANTLR_USE_NAMESPACE(std)string("TokenStream ")+sname+" not found";
}
return (*i).second;
}
RefToken TokenStreamSelector::nextToken()
{
// keep looking for a token until you don't
// get a retry exception
for (;;) {
try {
return input->nextToken();
}
catch (TokenStreamRetryException&) {
// just retry "forever"
}
}
}
TokenStream* TokenStreamSelector::pop()
{
TokenStream* stream = streamStack.top();
streamStack.pop();
select(stream);
return stream;
}
void TokenStreamSelector::push(TokenStream* stream)
{
streamStack.push(input);
select(stream);
}
void TokenStreamSelector::push(const ANTLR_USE_NAMESPACE(std)string& sname)
{
streamStack.push(input);
select(sname);
}
void TokenStreamSelector::retry()
{
throw TokenStreamRetryException();
}
/** Set the stream without pushing old stream */
void TokenStreamSelector::select(TokenStream* stream)
{
input = stream;
}
void TokenStreamSelector::select(const ANTLR_USE_NAMESPACE(std)string& sname)
{
inputStreamNames_coll::const_iterator i = inputStreamNames.find(sname);
if (i == inputStreamNames.end()) {
throw ANTLR_USE_NAMESPACE(std)string("TokenStream ")+sname+" not found";
}
input = (*i).second;
}
#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
}
#endif
|