• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "UserTestTraits.hpp"
2 #include "t006lexer.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 t006lexer *lxr;
17 
18 struct TokenData
19 {
20 	t006lexerTokens::Tokens type;
21 	unsigned start;
22 	unsigned stop;
23 	const char* text;
24 };
25 
26 static TokenData ExpectedTokens[] =
27 {
28 	// "fofaaooa"
29 	{ t006lexerTokens::FOO, 0, 1, "fo"},
30 	{ t006lexerTokens::FOO, 2, 7, "faaooa"},
31 	{ t006lexerTokens::EOF_TOKEN, 8, 8, "<EOF>"}
32 };
33 
main(int argc,char * argv[])34 int main (int argc, char *argv[])
35 {
36 	testValid("fofaaooa");
37 	testMalformedInput("fofoaooaoa2");
38 	return 0;
39 }
40 
testValid(string const & data)41 int testValid(string const& data)
42 {
43 	t006lexerTraits::InputStreamType* input	= new t006lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
44 										       ANTLR_ENC_8BIT,
45 										       data.length(), //strlen(data.c_str()),
46 										       (ANTLR_UINT8*)"t006");
47 	if (lxr == NULL)
48 		lxr = new t006lexer(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 		t006lexerTraits::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 	t006lexerTraits::InputStreamType* input	= new t006lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
84 										       ANTLR_ENC_8BIT,
85 										       data.length(), //strlen(data.c_str()),
86 										       (ANTLR_UINT8*)"t006");
87 	if (lxr == NULL)
88 		lxr = new t006lexer(input);
89 	else
90 		lxr->setCharStream(input);
91 
92 	std::cout << "testMalformedInput: \"" << data << '"' <<std::endl;
93 
94 	t006lexerTraits::CommonTokenType *token;
95 	token = lxr->nextToken();
96 	std::cout << token->getText() << std::endl;
97 	token = lxr->nextToken();
98 	std::cout << token->getText() << std::endl;
99 	token = lxr->nextToken();
100 	std::cout << token->getText() << std::endl;
101 
102     //except antlr3.MismatchedTokenException as exc:
103     //self.assertEqual(exc.expecting, 'f')
104     //self.assertEqual(exc.unexpectedType, '2')
105     //self.assertEqual(exc.charPositionInLine, 10)
106     //self.assertEqual(exc.line, 1)
107 
108 	delete lxr; lxr = NULL;
109 	delete input;
110 	return 0;
111 }
112