• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "UserTestTraits.hpp"
2 #include "t051lexer.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 static t051lexer* lxr;
14 
15 static string slurp(string const& fileName);
16 static void parseFile(const char* fName);
17 
main(int argc,char * argv[])18 int main (int argc, char *argv[])
19 {
20 	if (argc < 2 || argv[1] == NULL)
21 	{
22 		parseFile("./t051.input"); // Note in VS2005 debug, working directory must be configured
23 	}
24 	else
25 	{
26 		for (int i = 1; i < argc; i++)
27 		{
28 			parseFile(argv[i]);
29 		}
30 	}
31 
32 	printf("finished parsing OK\n");	// Finnish parking is pretty good - I think it is all the snow
33 
34 	return 0;
35 }
36 
parseFile(const char * fName)37 void parseFile(const char* fName)
38 {
39 	t051lexerTraits::InputStreamType* input;
40 	t051lexerTraits::TokenStreamType* tstream;
41 
42 	string data = slurp(fName);
43 
44 	input	= new t051lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
45 						       ANTLR_ENC_8BIT,
46 						       data.length(), //strlen(data.c_str()),
47 						       (ANTLR_UINT8*)fName);
48 
49 	input->setUcaseLA(true);
50 
51 	// Our input stream is now open and all set to go, so we can create a new instance of our
52 	// lexer and set the lexer input to our input stream:
53 	//  (file | memory | ?) --> inputstream -> lexer --> tokenstream --> parser ( --> treeparser )?
54 	//
55 	if (lxr == NULL)
56 	{
57 		lxr = new t051lexer(input);	    // javaLexerNew is generated by ANTLR
58 	}
59 	else
60 	{
61 		lxr->setCharStream(input);
62 	}
63 
64 	tstream = new t051lexerTraits::TokenStreamType(ANTLR_SIZE_HINT, lxr->get_tokSource());
65 
66 	putc('L', stdout); fflush(stdout);
67 	{
68 		ANTLR_INT32 T = 0;
69 		while	(T != t051lexer::EOF_TOKEN)
70 		{
71 			T = tstream->_LA(1);
72 			t051lexerTraits::CommonTokenType const* token = tstream->_LT(1);
73 
74 			printf("%d\t\"%s\"\n",
75 			       T,
76 			       tstream->_LT(1)->getText().c_str()
77 				);
78 			tstream->consume();
79 		}
80 	}
81 
82 	tstream->_LT(1);	// Don't do this mormally, just causes lexer to run for timings here
83 
84 	delete tstream;
85 	delete lxr; lxr = NULL;
86 	delete input;
87 }
88 
slurp(string const & fileName)89 string slurp(string const& fileName)
90 {
91 	ifstream ifs(fileName.c_str(), ios::in | ios::binary | ios::ate);
92 	ifstream::pos_type fileSize = ifs.tellg();
93 	ifs.seekg(0, ios::beg);
94 
95 	stringstream sstr;
96 	sstr << ifs.rdbuf();
97 	return sstr.str();
98 }
99