1 #include "UserTestTraits.hpp"
2 #include "t007lexer.hpp"
3
4 #include <sys/types.h>
5
6 #include <iostream>
7 #include <sstream>
8 #include <fstream>
9
10 using namespace Antlr3Test;
11 using namespace std;
12
13 int testValid(string const& data);
14 int testMalformedInput(string const& data);
15
16 static t007lexer *lxr;
17
18 struct TokenData
19 {
20 t007lexerTokens::Tokens type;
21 unsigned start;
22 unsigned stop;
23 const char* text;
24 };
25
26 static TokenData ExpectedTokens[] =
27 {
28 // "fofababbooabb"
29 { t007lexerTokens::FOO, 0, 1, "fo"},
30 { t007lexerTokens::FOO, 2, 12, "fababbooabb"},
31 { t007lexerTokens::EOF_TOKEN, 13, 13, "<EOF>"}
32 };
33
main(int argc,char * argv[])34 int main (int argc, char *argv[])
35 {
36 testValid("fofababbooabb");
37 testMalformedInput("foaboao");
38 return 0;
39 }
40
testValid(string const & data)41 int testValid(string const& data)
42 {
43 t007lexerTraits::InputStreamType* input = new t007lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
44 ANTLR_ENC_8BIT,
45 data.length(), //strlen(data.c_str()),
46 (ANTLR_UINT8*)"t007");
47 if (lxr == NULL)
48 lxr = new t007lexer(input);
49 else
50 lxr->setCharStream(input);
51
52 std::cout << "testValid: \"" << data << '"' <<std::endl;
53
54 std::cout << "Text:" << '\t'
55 << "Type:" << '\t'
56 << "Start:" << '\t'
57 << "Stop:" << '\t'
58 << "Text:" << '\t' << std::endl;
59
60 for(unsigned i = 0; i < sizeof(ExpectedTokens)/sizeof(TokenData) ; i++)
61 {
62 // nextToken does not allocate any new Token instance(the same instance is returned again and again)
63 t007lexerTraits::CommonTokenType *token = lxr->nextToken();
64
65 size_t startIndex = ((const char*)token->get_startIndex()) - data.c_str();
66 size_t stopIndex = ((const char*)token->get_stopIndex()) - data.c_str();
67
68 std::cout << token->getText()
69 << '\t' << (token->getType() == ExpectedTokens[i].type ? "OK" : "Fail")
70 << '\t' << (startIndex == ExpectedTokens[i].start ? "OK" : "Fail")
71 << '\t' << (stopIndex == ExpectedTokens[i].stop ? "OK" : "Fail")
72 << '\t' << (token->getText() == ExpectedTokens[i].text ? "OK" : "Fail")
73 << std::endl;
74
75 }
76 delete lxr; lxr = NULL;
77 delete input;
78 return 0;
79 }
80
testMalformedInput(string const & data)81 int testMalformedInput(string const& data)
82 {
83 t007lexerTraits::InputStreamType* input = new t007lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
84 ANTLR_ENC_8BIT,
85 data.length(), //strlen(data.c_str()),
86 (ANTLR_UINT8*)"t007");
87 if (lxr == NULL)
88 lxr = new t007lexer(input);
89 else
90 lxr->setCharStream(input);
91
92 std::cout << "testMalformedInput: \"" << data << '"' <<std::endl;
93
94 t007lexerTraits::CommonTokenType *token0 = lxr->nextToken();
95 std::cout << token0->getText() << std::endl;
96
97 //except antlr3.EarlyExitException as exc:
98 // self.assertEqual(exc.unexpectedType, 'o')
99 // self.assertEqual(exc.charPositionInLine, 6)
100 // self.assertEqual(exc.line, 1)
101
102 delete lxr; lxr = NULL;
103 delete input;
104 return 0;
105 }
106