#include <antlr3.h>
#include <iostream>
+#include <sstream>
+#include <stdexcept>
#include <string>
#include <vector>
/** Retrieve the text associated with a token. */
static std::string tokenText(pANTLR3_COMMON_TOKEN token);
+
+ /** Retrieve a substring of the text associated with a token.
+ *
+ * @param token the token
+ * @param index the index of the starting character of the substring
+ * @param n the size of the substring. If <code>n</code> is 0, then all of the
+ * characters up to the end of the token text will be included. If <code>n</code>
+ * would make the substring span past the end of the token text, only those
+ * characters up to the end of the token text will be included.
+ */
+ static std::string tokenTextSubstr(pANTLR3_COMMON_TOKEN token, size_t index, size_t n = 0);
+
/** Retrieve an unsigned from the text of a token */
static unsigned tokenToUnsigned( pANTLR3_COMMON_TOKEN token );
+
/** Retrieve an Integer from the text of a token */
static Integer tokenToInteger( pANTLR3_COMMON_TOKEN token );
+
/** Retrieve a Rational from the text of a token */
static Rational tokenToRational(pANTLR3_COMMON_TOKEN token);
return txt;
}
+inline std::string AntlrInput::tokenTextSubstr(pANTLR3_COMMON_TOKEN token,
+ size_t index,
+ size_t n) {
+ ANTLR3_MARKER start = token->getStartIndex(token);
+ ANTLR3_MARKER end = token->getStopIndex(token);
+ Assert( start < end );
+ if( index > (size_t) end - start ) {
+ stringstream ss;
+ ss << "Out-of-bounds substring index: " << index;
+ throw std::invalid_argument(ss.str());
+ }
+ start += index;
+ if( n==0 || n >= (size_t) end - start ) {
+ return std::string( (const char *)start + index, end-start+1 );
+ } else {
+ return std::string( (const char *)start + index, n );
+ }
+}
+
inline unsigned AntlrInput::tokenToUnsigned(pANTLR3_COMMON_TOKEN token) {
unsigned result;
std::stringstream ss;
{ // FIXME: This doesn't work because an SMT rational is not a valid GMP rational string
expr = MK_CONST( AntlrInput::tokenToRational($RATIONAL) ); }
| HEX_LITERAL
- { std::string hexString = AntlrInput::tokenText($HEX_LITERAL);
- AlwaysAssert( hexString.find("#x") == 0 );
- expr = MK_CONST( BitVector(hexString.erase(0,2), 16) ); }
+ { Assert( AntlrInput::tokenText($HEX_LITERAL).find("#x") == 0 );
+ std::string hexString = AntlrInput::tokenTextSubstr($HEX_LITERAL,2);
+ expr = MK_CONST( BitVector(hexString, 16) ); }
| BINARY_LITERAL
- { std::string binString = AntlrInput::tokenText($BINARY_LITERAL);
- AlwaysAssert( binString.find("#b") == 0 );
- expr = MK_CONST( BitVector(binString.erase(0,2), 2) ); }
+ { Assert( AntlrInput::tokenText($BINARY_LITERAL).find("#b") == 0 );
+ std::string binString = AntlrInput::tokenTextSubstr($BINARY_LITERAL,2);
+ expr = MK_CONST( BitVector(binString, 2) ); }
// NOTE: Theory constants go here
;