1
/* ANTLR Translator Generator
2
* Project led by Terence Parr at http://www.jGuru.com
3
* Software rights: http://www.antlr.org/RIGHTS.html
7
#include "antlr/MismatchedTokenException.hpp"
8
#include "antlr/String.hpp"
10
#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
14
MismatchedTokenException::MismatchedTokenException()
15
: RecognitionException("Mismatched Token: expecting any AST node","<AST>",-1,-1)
23
// Expected range / not range
24
MismatchedTokenException::MismatchedTokenException(
25
const char* const* tokenNames_,
31
) : RecognitionException("Mismatched Token","<AST>",-1,-1)
34
, tokenText( (node_ ? node_->toString(): ANTLR_USE_NAMESPACE(std)string("<empty tree>")) )
35
, mismatchType(matchNot ? NOT_RANGE : RANGE)
38
, tokenNames(tokenNames_)
39
, numTokens(numTokens_)
43
// Expected token / not token
44
MismatchedTokenException::MismatchedTokenException(
45
const char* const* tokenNames_,
50
) : RecognitionException("Mismatched Token","<AST>",-1,-1)
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_)
61
// Expected BitSet / not BitSet
62
MismatchedTokenException::MismatchedTokenException(
63
const char* const* tokenNames_,
68
) : RecognitionException("Mismatched Token","<AST>",-1,-1)
71
, tokenText( (node_ ? node_->toString(): ANTLR_USE_NAMESPACE(std)string("<empty tree>")) )
72
, mismatchType(matchNot ? NOT_SET : SET)
74
, tokenNames(tokenNames_)
75
, numTokens(numTokens_)
79
// Expected range / not range
80
MismatchedTokenException::MismatchedTokenException(
81
const char* const* tokenNames_,
87
const ANTLR_USE_NAMESPACE(std)string& fileName_
88
) : RecognitionException("Mismatched Token",fileName_,token_->getLine(),token_->getColumn())
91
, tokenText(token_->getText())
92
, mismatchType(matchNot ? NOT_RANGE : RANGE)
95
, tokenNames(tokenNames_)
96
, numTokens(numTokens_)
100
// Expected token / not token
101
MismatchedTokenException::MismatchedTokenException(
102
const char* const* tokenNames_,
103
const int numTokens_,
107
const ANTLR_USE_NAMESPACE(std)string& fileName_
108
) : RecognitionException("Mismatched Token",fileName_,token_->getLine(),token_->getColumn())
111
, tokenText(token_->getText())
112
, mismatchType(matchNot ? NOT_TOKEN : TOKEN)
113
, expecting(expecting_)
114
, tokenNames(tokenNames_)
115
, numTokens(numTokens_)
119
// Expected BitSet / not BitSet
120
MismatchedTokenException::MismatchedTokenException(
121
const char* const* tokenNames_,
122
const int numTokens_,
126
const ANTLR_USE_NAMESPACE(std)string& fileName_
127
) : RecognitionException("Mismatched Token",fileName_,token_->getLine(),token_->getColumn())
130
, tokenText(token_->getText())
131
, mismatchType(matchNot ? NOT_SET : SET)
133
, tokenNames(tokenNames_)
134
, numTokens(numTokens_)
138
ANTLR_USE_NAMESPACE(std)string MismatchedTokenException::getMessage() const
140
ANTLR_USE_NAMESPACE(std)string s;
141
switch (mismatchType) {
143
s += "expecting " + tokenName(expecting) + ", found '" + tokenText + "'";
146
s += "expecting anything but " + tokenName(expecting) + "; got it anyway";
149
s += "expecting token in range: " + tokenName(expecting) + ".." + tokenName(upper) + ", found '" + tokenText + "'";
152
s += "expecting token NOT in range: " + tokenName(expecting) + ".." + tokenName(upper) + ", found '" + tokenText + "'";
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++ )
162
s += tokenName(elems[i]);
164
s += "), found '" + tokenText + "'";
168
s = RecognitionException::getMessage();
174
ANTLR_USE_NAMESPACE(std)string MismatchedTokenException::tokenName(int tokenType) const
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 + ">";
181
return tokenNames[tokenType];
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;
193
#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE