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