• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "UserTestTraits.hpp"
2 #include "t004lexer.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 t004lexer *lxr;
17 
18 struct TokenData
19 {
20 	t004lexerTokens::Tokens type;
21 	unsigned start;
22 	unsigned stop;
23 	const char* text;
24 };
25 
26 static TokenData ExpectedTokens[] =
27 {
28 	{ t004lexerTokens::FOO, 0, 0, "f"},
29 	{ t004lexerTokens::FOO, 1, 2, "fo"},
30 	{ t004lexerTokens::FOO, 3, 5, "foo"},
31 	{ t004lexerTokens::FOO, 6, 9, "fooo"}
32 };
33 
main(int argc,char * argv[])34 int main (int argc, char *argv[])
35 {
36 	testValid("ffofoofooo");
37 	testMalformedInput("2");
38 	return 0;
39 }
40 
testValid(string const & data)41 int testValid(string const& data)
42 {
43 	t004lexerTraits::InputStreamType* input	= new t004lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
44 										       ANTLR_ENC_8BIT,
45 										       data.length(), //strlen(data.c_str()),
46 										       (ANTLR_UINT8*)"t004");
47 	if (lxr == NULL)
48 		lxr = new t004lexer(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 		t004lexerTraits::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 	t004lexerTraits::InputStreamType* input	= new t004lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
84 										       ANTLR_ENC_8BIT,
85 										       data.length(), //strlen(data.c_str()),
86 										       (ANTLR_UINT8*)"t004");
87 	if (lxr == NULL)
88 		lxr = new t004lexer(input);
89 	else
90 		lxr->setCharStream(input);
91 
92 	std::cout << "testMalformedInput: \"" << data << '"' <<std::endl;
93 
94 	t004lexerTraits::CommonTokenType *token0 = lxr->nextToken();
95 	std::cout << token0->getText() << std::endl;
96 
97 	delete lxr; lxr = NULL;
98 	delete input;
99 	return 0;
100 }
101