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