~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to lib/antlr/src/TokenStreamSelector.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2006-05-23 18:39:42 UTC
  • Revision ID: james.westby@ubuntu.com-20060523183942-hucifbvh68k2bwz7
Tags: upstream-3.3.2
Import upstream version 3.3.2

Show diffs side-by-side

added added

removed removed

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