#include "UserTestTraits.hpp" #include "t007lexer.hpp" #include #include #include #include using namespace Antlr3Test; using namespace std; int testValid(string const& data); int testMalformedInput(string const& data); static t007lexer *lxr; struct TokenData { t007lexerTokens::Tokens type; unsigned start; unsigned stop; const char* text; }; static TokenData ExpectedTokens[] = { // "fofababbooabb" { t007lexerTokens::FOO, 0, 1, "fo"}, { t007lexerTokens::FOO, 2, 12, "fababbooabb"}, { t007lexerTokens::EOF_TOKEN, 13, 13, ""} }; int main (int argc, char *argv[]) { testValid("fofababbooabb"); testMalformedInput("foaboao"); return 0; } int testValid(string const& data) { t007lexerTraits::InputStreamType* input = new t007lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(), ANTLR_ENC_8BIT, data.length(), //strlen(data.c_str()), (ANTLR_UINT8*)"t007"); if (lxr == NULL) lxr = new t007lexer(input); else lxr->setCharStream(input); std::cout << "testValid: \"" << data << '"' <nextToken(); size_t startIndex = ((const char*)token->get_startIndex()) - data.c_str(); size_t stopIndex = ((const char*)token->get_stopIndex()) - data.c_str(); std::cout << token->getText() << '\t' << (token->getType() == ExpectedTokens[i].type ? "OK" : "Fail") << '\t' << (startIndex == ExpectedTokens[i].start ? "OK" : "Fail") << '\t' << (stopIndex == ExpectedTokens[i].stop ? "OK" : "Fail") << '\t' << (token->getText() == ExpectedTokens[i].text ? "OK" : "Fail") << std::endl; } delete lxr; lxr = NULL; delete input; return 0; } int testMalformedInput(string const& data) { t007lexerTraits::InputStreamType* input = new t007lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(), ANTLR_ENC_8BIT, data.length(), //strlen(data.c_str()), (ANTLR_UINT8*)"t007"); if (lxr == NULL) lxr = new t007lexer(input); else lxr->setCharStream(input); std::cout << "testMalformedInput: \"" << data << '"' <nextToken(); std::cout << token0->getText() << std::endl; //except antlr3.EarlyExitException as exc: // self.assertEqual(exc.unexpectedType, 'o') // self.assertEqual(exc.charPositionInLine, 6) // self.assertEqual(exc.line, 1) delete lxr; lxr = NULL; delete input; return 0; }