1 #include "UserTestTraits.hpp"
2 #include "t002lexer.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 testIteratorInterface(string const& data);
15 int testMalformedInput(string const& data);
16 
17 static t002lexer *lxr;
18 static t002lexerTokens::Tokens ExpectedTokens[] =
19   {
20     t002lexerTokens::ZERO,
21     t002lexerTokens::ONE,
22     t002lexerTokens::EOF_TOKEN
23   };
24 
main(int argc,char * argv[])25 int main (int argc, char *argv[])
26 {
27 	testValid("01");
28 	testIteratorInterface("01");
29 	testMalformedInput("2");
30 	return 0;
31 }
32 
testValid(string const & data)33 int testValid(string const& data)
34 {
35 	t002lexerTraits::InputStreamType* input	= new t002lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
36 										       ANTLR_ENC_8BIT,
37 										       data.length(), //strlen(data.c_str()),
38 										       (ANTLR_UINT8*)"t002");
39 	if (lxr == NULL)
40 		lxr = new t002lexer(input);
41 	else
42 		lxr->setCharStream(input);
43 
44 	std::cout << "testValid: \"" << data << '"' <<std::endl;
45 
46 	for(unsigned i = 0; i <= 2 ; i++)
47 	{
48 		// nextToken does not allocate any new Token instance(the same instance is returned again and again)
49 		t002lexerTraits::CommonTokenType *token = lxr->nextToken();
50 		std::cout << token->getText() << '\t'
51 			  << (token->getType() == ExpectedTokens[i] ? "OK" : "Fail")
52 			  << std::endl;
53 
54 	}
55 	delete lxr; lxr = NULL;
56 	delete input;
57 	return 0;
58 }
59 
testIteratorInterface(string const & data)60 int testIteratorInterface(string const& data)
61 {
62 	t002lexerTraits::InputStreamType* input	= new t002lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
63 										       ANTLR_ENC_8BIT,
64 										       data.length(), //strlen(data.c_str()),
65 										       (ANTLR_UINT8*)"t002");
66 	if (lxr == NULL)
67 		lxr = new t002lexer(input);
68 	else
69 		lxr->setCharStream(input);
70 
71 	std::cout << "testIteratorInterface: \"" << data << '"' <<std::endl;
72 
73 	t002lexerTraits::TokenStreamType *tstream = new t002lexerTraits::TokenStreamType(ANTLR_SIZE_HINT, lxr->get_tokSource());
74 	t002lexerTraits::CommonTokenType const *token0 = tstream->_LT(1);
75 	t002lexerTraits::CommonTokenType const *token1 = tstream->_LT(2);
76 	t002lexerTraits::CommonTokenType const *token2 = tstream->_LT(3);
77 
78 	std::cout << token0->getText() << std::endl;
79 	std::cout << token1->getText() << std::endl;
80 	std::cout << token2->getText() << std::endl;
81 
82 	delete tstream;
83 	delete lxr; lxr = NULL;
84 	delete input;
85 	return 0;
86 }
87 
testMalformedInput(string const & data)88 int testMalformedInput(string const& data)
89 {
90 	t002lexerTraits::InputStreamType* input	= new t002lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
91 										       ANTLR_ENC_8BIT,
92 										       data.length(), //strlen(data.c_str()),
93 										       (ANTLR_UINT8*)"t002");
94 	if (lxr == NULL)
95 		lxr = new t002lexer(input);
96 	else
97 		lxr->setCharStream(input);
98 
99 	std::cout << "testMalformedInput: \"" << data << '"' <<std::endl;
100 
101 	t002lexerTraits::CommonTokenType *token0 = lxr->nextToken();
102 	std::cout << token0->getText() << std::endl;
103 
104 	delete lxr; lxr = NULL;
105 	delete input;
106 	return 0;
107 }
108