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

« back to all changes in this revision

Viewing changes to lib/antlr/src/MismatchedTokenException.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
 
 
7
#include "antlr/MismatchedTokenException.hpp"
 
8
#include "antlr/String.hpp"
 
9
 
 
10
#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
 
11
namespace antlr {
 
12
#endif
 
13
 
 
14
MismatchedTokenException::MismatchedTokenException()
 
15
  : RecognitionException("Mismatched Token: expecting any AST node","<AST>",-1,-1)
 
16
  , token(0)
 
17
  , node(nullASTptr)
 
18
  , tokenNames(0)
 
19
  , numTokens(0)
 
20
{
 
21
}
 
22
 
 
23
// Expected range / not range
 
24
MismatchedTokenException::MismatchedTokenException(
 
25
        const char* const* tokenNames_,
 
26
        const int numTokens_,
 
27
        RefAST node_,
 
28
        int lower,
 
29
        int upper_,
 
30
        bool matchNot
 
31
) : RecognitionException("Mismatched Token","<AST>",-1,-1)
 
32
  , token(0)
 
33
  , node(node_)
 
34
  , tokenText( (node_ ? node_->toString(): ANTLR_USE_NAMESPACE(std)string("<empty tree>")) )
 
35
  , mismatchType(matchNot ? NOT_RANGE : RANGE)
 
36
  , expecting(lower)
 
37
  , upper(upper_)
 
38
  , tokenNames(tokenNames_)
 
39
  , numTokens(numTokens_)
 
40
{
 
41
}
 
42
 
 
43
// Expected token / not token
 
44
MismatchedTokenException::MismatchedTokenException(
 
45
        const char* const* tokenNames_,
 
46
        const int numTokens_,
 
47
        RefAST node_,
 
48
        int expecting_,
 
49
        bool matchNot
 
50
) : RecognitionException("Mismatched Token","<AST>",-1,-1)
 
51
  , token(0)
 
52
  , node(node_)
 
53
  , tokenText( (node_ ? node_->toString(): ANTLR_USE_NAMESPACE(std)string("<empty tree>")) )
 
54
  , mismatchType(matchNot ? NOT_TOKEN : TOKEN)
 
55
  , expecting(expecting_)
 
56
  , tokenNames(tokenNames_)
 
57
  , numTokens(numTokens_)
 
58
{
 
59
}
 
60
 
 
61
// Expected BitSet / not BitSet
 
62
MismatchedTokenException::MismatchedTokenException(
 
63
        const char* const* tokenNames_,
 
64
        const int numTokens_,
 
65
        RefAST node_,
 
66
        BitSet set_,
 
67
        bool matchNot
 
68
) : RecognitionException("Mismatched Token","<AST>",-1,-1)
 
69
  , token(0)
 
70
  , node(node_)
 
71
  , tokenText( (node_ ? node_->toString(): ANTLR_USE_NAMESPACE(std)string("<empty tree>")) )
 
72
  , mismatchType(matchNot ? NOT_SET : SET)
 
73
  , set(set_)
 
74
  , tokenNames(tokenNames_)
 
75
  , numTokens(numTokens_)
 
76
{
 
77
}
 
78
 
 
79
// Expected range / not range
 
80
MismatchedTokenException::MismatchedTokenException(
 
81
        const char* const* tokenNames_,
 
82
        const int numTokens_,
 
83
        RefToken token_,
 
84
        int lower,
 
85
        int upper_,
 
86
        bool matchNot,
 
87
        const ANTLR_USE_NAMESPACE(std)string& fileName_
 
88
) : RecognitionException("Mismatched Token",fileName_,token_->getLine(),token_->getColumn())
 
89
  , token(token_)
 
90
  , node(nullASTptr)
 
91
  , tokenText(token_->getText())
 
92
  , mismatchType(matchNot ? NOT_RANGE : RANGE)
 
93
  , expecting(lower)
 
94
  , upper(upper_)
 
95
  , tokenNames(tokenNames_)
 
96
  , numTokens(numTokens_)
 
97
{
 
98
}
 
99
 
 
100
// Expected token / not token
 
101
MismatchedTokenException::MismatchedTokenException(
 
102
        const char* const* tokenNames_,
 
103
        const int numTokens_,
 
104
        RefToken token_,
 
105
        int expecting_,
 
106
        bool matchNot,
 
107
        const ANTLR_USE_NAMESPACE(std)string& fileName_
 
108
) : RecognitionException("Mismatched Token",fileName_,token_->getLine(),token_->getColumn())
 
109
  , token(token_)
 
110
  , node(nullASTptr)
 
111
  , tokenText(token_->getText())
 
112
  , mismatchType(matchNot ? NOT_TOKEN : TOKEN)
 
113
  , expecting(expecting_)
 
114
  , tokenNames(tokenNames_)
 
115
  , numTokens(numTokens_)
 
116
{
 
117
}
 
118
 
 
119
// Expected BitSet / not BitSet
 
120
MismatchedTokenException::MismatchedTokenException(
 
121
        const char* const* tokenNames_,
 
122
        const int numTokens_,
 
123
        RefToken token_,
 
124
        BitSet set_,
 
125
        bool matchNot,
 
126
        const ANTLR_USE_NAMESPACE(std)string& fileName_
 
127
) : RecognitionException("Mismatched Token",fileName_,token_->getLine(),token_->getColumn())
 
128
  , token(token_)
 
129
  , node(nullASTptr)
 
130
  , tokenText(token_->getText())
 
131
  , mismatchType(matchNot ? NOT_SET : SET)
 
132
  , set(set_)
 
133
  , tokenNames(tokenNames_)
 
134
  , numTokens(numTokens_)
 
135
{
 
136
}
 
137
 
 
138
ANTLR_USE_NAMESPACE(std)string MismatchedTokenException::getMessage() const
 
139
{
 
140
        ANTLR_USE_NAMESPACE(std)string s;
 
141
        switch (mismatchType) {
 
142
        case TOKEN:
 
143
                s += "expecting " + tokenName(expecting) + ", found '" + tokenText + "'";
 
144
                break;
 
145
        case NOT_TOKEN:
 
146
                s += "expecting anything but " + tokenName(expecting) + "; got it anyway";
 
147
                break;
 
148
        case RANGE:
 
149
                s += "expecting token in range: " + tokenName(expecting) + ".." + tokenName(upper) + ", found '" + tokenText + "'";
 
150
                break;
 
151
        case NOT_RANGE:
 
152
                s += "expecting token NOT in range: " + tokenName(expecting) + ".." + tokenName(upper) + ", found '" + tokenText + "'";
 
153
                break;
 
154
        case SET:
 
155
        case NOT_SET:
 
156
                {
 
157
                        s += ANTLR_USE_NAMESPACE(std)string("expecting ") + (mismatchType == NOT_SET ? "NOT " : "") + "one of (";
 
158
                        ANTLR_USE_NAMESPACE(std)vector<unsigned int> elems = set.toArray();
 
159
                        for ( unsigned int i = 0; i < elems.size(); i++ )
 
160
                        {
 
161
                                s += " ";
 
162
                                s += tokenName(elems[i]);
 
163
                        }
 
164
                        s += "), found '" + tokenText + "'";
 
165
                }
 
166
                break;
 
167
        default:
 
168
                s = RecognitionException::getMessage();
 
169
                break;
 
170
        }
 
171
        return s;
 
172
}
 
173
 
 
174
ANTLR_USE_NAMESPACE(std)string MismatchedTokenException::tokenName(int tokenType) const
 
175
{
 
176
        if (tokenType == Token::INVALID_TYPE)
 
177
                return "<Set of tokens>";
 
178
        else if (tokenType < 0 || tokenType >= numTokens)
 
179
                return ANTLR_USE_NAMESPACE(std)string("<") + tokenType + ">";
 
180
        else
 
181
                return tokenNames[tokenType];
 
182
}
 
183
 
 
184
#ifndef NO_STATIC_CONSTS
 
185
const int MismatchedTokenException::TOKEN;
 
186
const int MismatchedTokenException::NOT_TOKEN;
 
187
const int MismatchedTokenException::RANGE;
 
188
const int MismatchedTokenException::NOT_RANGE;
 
189
const int MismatchedTokenException::SET;
 
190
const int MismatchedTokenException::NOT_SET;
 
191
#endif
 
192
 
 
193
#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
 
194
}
 
195
#endif