• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "UserTestTraits.hpp"
2 #include "t011lexer.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 t011lexer *lxr;
17 
18 struct TokenData
19 {
20 	t011lexerTokens::Tokens type;
21 	unsigned start;
22 	unsigned stop;
23 	const char* text;
24 };
25 
26 static TokenData ExpectedTokens[] =
27 {
28 	// "foobar _Ab98 \n A12sdf"
29 	{ t011lexerTokens::IDENTIFIER,  0,   5, "foobar"},
30 	{ t011lexerTokens::WS,          6,   6, " "},
31 	{ t011lexerTokens::IDENTIFIER,  7,  11, "_Ab98"},
32 	{ t011lexerTokens::WS,         12, 14, " \n "},
33 	{ t011lexerTokens::IDENTIFIER, 15, 20, "A12sdf"},
34 	{ t011lexerTokens::EOF_TOKEN,  21, 21, "<EOF>"}
35 };
36 
main(int argc,char * argv[])37 int main (int argc, char *argv[])
38 {
39 	testValid("foobar _Ab98 \n A12sdf");
40 	testMalformedInput("a-b");
41 	return 0;
42 }
43 
testValid(string const & data)44 int testValid(string const& data)
45 {
46 	t011lexerTraits::InputStreamType* input	= new t011lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
47 										       ANTLR_ENC_8BIT,
48 										       data.length(), //strlen(data.c_str()),
49 										       (ANTLR_UINT8*)"t011");
50 	if (lxr == NULL)
51 		lxr = new t011lexer(input);
52 	else
53 		lxr->setCharStream(input);
54 
55 	std::cout << "testValid: \"" << data << '"' <<std::endl;
56 
57 	std::cout << "Text:"  << '\t'
58 		  << "Type:"  << '\t'
59 		  << "Start:" << '\t'
60 		  << "Stop:"  << '\t'
61 		  << "Text:"  << '\t' << std::endl;
62 
63 	for(unsigned i = 0; i < sizeof(ExpectedTokens)/sizeof(TokenData) ; i++)
64 	{
65 		// nextToken does not allocate any new Token instance(the same instance is returned again and again)
66 		t011lexerTraits::CommonTokenType *token = lxr->nextToken();
67 
68 		size_t startIndex = ((const char*)token->get_startIndex()) - data.c_str();
69 		size_t stopIndex = ((const char*)token->get_stopIndex()) - data.c_str();
70 
71 		std::cout << token->getText()
72 			  << '\t' << (token->getType()       == ExpectedTokens[i].type ?  "OK" : "Fail")
73 			  << '\t' << (startIndex == ExpectedTokens[i].start ? "OK" : "Fail")
74 			  << '\t' << (stopIndex  == ExpectedTokens[i].stop ?  "OK" : "Fail")
75 			  << '\t' << (token->getText()       == ExpectedTokens[i].text ?  "OK" : "Fail")
76 			  << std::endl;
77 
78 	}
79 	delete lxr; lxr = NULL;
80 	delete input;
81 	return 0;
82 }
83 
testMalformedInput(string const & data)84 int testMalformedInput(string const& data)
85 {
86 	t011lexerTraits::InputStreamType* input	= new t011lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
87 										       ANTLR_ENC_8BIT,
88 										       data.length(), //strlen(data.c_str()),
89 										       (ANTLR_UINT8*)"t011");
90 	if (lxr == NULL)
91 		lxr = new t011lexer(input);
92 	else
93 		lxr->setCharStream(input);
94 
95 	std::cout << "testMalformedInput: \"" << data << '"' <<std::endl;
96 
97 	t011lexerTraits::CommonTokenType *token;
98 	token = lxr->nextToken();
99 	std::cout << token->getText() << std::endl;
100 	token = lxr->nextToken();
101 	std::cout << token->getText() << std::endl;
102 	token = lxr->nextToken();
103 	std::cout << token->getText() << std::endl;
104 
105 	//except antlr3.NoViableAltException as exc:
106 	//    self.assertEqual(exc.unexpectedType, '-')
107 	//    self.assertEqual(exc.charPositionInLine, 1)
108 	//    self.assertEqual(exc.line, 1)
109 
110 	delete lxr; lxr = NULL;
111 	delete input;
112 	return 0;
113 }
114