1 #include "UserTestTraits.hpp"
2 #include "t009lexer.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 t009lexer *lxr;
17 
18 struct TokenData
19 {
20 	t009lexerTokens::Tokens type;
21 	unsigned start;
22 	unsigned stop;
23 	const char* text;
24 };
25 
26 static TokenData ExpectedTokens[] =
27 {
28 	// "085"
29 	{ t009lexerTokens::DIGIT, 0, 0, "0"},
30 	{ t009lexerTokens::DIGIT, 1, 1, "8"},
31 	{ t009lexerTokens::DIGIT, 2, 2, "5"},
32 	{ t009lexerTokens::EOF_TOKEN, 3, 3, "<EOF>"}
33 };
34 
main(int argc,char * argv[])35 int main (int argc, char *argv[])
36 {
37 	testValid("085");
38 	testMalformedInput("2a");
39 	return 0;
40 }
41 
testValid(string const & data)42 int testValid(string const& data)
43 {
44 	t009lexerTraits::InputStreamType* input	= new t009lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
45 										       ANTLR_ENC_8BIT,
46 										       data.length(), //strlen(data.c_str()),
47 										       (ANTLR_UINT8*)"t009");
48 	if (lxr == NULL)
49 		lxr = new t009lexer(input);
50 	else
51 		lxr->setCharStream(input);
52 
53 	std::cout << "testValid: \"" << data << '"' <<std::endl;
54 
55 	std::cout << "Text:"  << '\t'
56 		  << "Type:"  << '\t'
57 		  << "Start:" << '\t'
58 		  << "Stop:"  << '\t'
59 		  << "Text:"  << '\t' << std::endl;
60 
61 	for(unsigned i = 0; i < sizeof(ExpectedTokens)/sizeof(TokenData) ; i++)
62 	{
63 		// nextToken does not allocate any new Token instance(the same instance is returned again and again)
64 		t009lexerTraits::CommonTokenType *token = lxr->nextToken();
65 
66 		size_t startIndex = ((const char*)token->get_startIndex()) - data.c_str();
67 		size_t stopIndex = ((const char*)token->get_stopIndex()) - data.c_str();
68 
69 		std::cout << token->getText()
70 			  << '\t' << (token->getType()       == ExpectedTokens[i].type ?  "OK" : "Fail")
71 			  << '\t' << (startIndex == ExpectedTokens[i].start ? "OK" : "Fail")
72 			  << '\t' << (stopIndex  == ExpectedTokens[i].stop ?  "OK" : "Fail")
73 			  << '\t' << (token->getText()       == ExpectedTokens[i].text ?  "OK" : "Fail")
74 			  << std::endl;
75 
76 	}
77 	delete lxr; lxr = NULL;
78 	delete input;
79 	return 0;
80 }
81 
testMalformedInput(string const & data)82 int testMalformedInput(string const& data)
83 {
84 	t009lexerTraits::InputStreamType* input	= new t009lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
85 										       ANTLR_ENC_8BIT,
86 										       data.length(), //strlen(data.c_str()),
87 										       (ANTLR_UINT8*)"t009");
88 	if (lxr == NULL)
89 		lxr = new t009lexer(input);
90 	else
91 		lxr->setCharStream(input);
92 
93 	std::cout << "testMalformedInput: \"" << data << '"' <<std::endl;
94 
95 	t009lexerTraits::CommonTokenType *token;
96 	token = lxr->nextToken();
97 	std::cout << token->getText() << std::endl;
98 	token = lxr->nextToken();
99 	std::cout << token->getText() << std::endl;
100 
101 	//except antlr3.MismatchedSetException as exc:
102 	//   # TODO: This should provide more useful information
103 	//   self.assertIsNone(exc.expecting)
104 	//   self.assertEqual(exc.unexpectedType, 'a')
105 	//   self.assertEqual(exc.charPositionInLine, 1)
106 	//   self.assertEqual(exc.line, 1)
107 
108 	delete lxr; lxr = NULL;
109 	delete input;
110 	return 0;
111 }
112