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