Synopsis - Cross-Reference

File: /tests/Cxx/src/Lexer.cc
 1#include <Synopsis/Trace.hh>
 2#include <Synopsis/Buffer.hh>
 3#include <Synopsis/Lexer.hh>
 4#include <iostream>
 5#include <iomanip>
 6#include <fstream>
 7#include <string>
 8
 9using namespace Synopsis;
10
11int main(int argc, char **argv)
12{
13  if (argc < 3)
14  {
15    std::cerr << "Usage: " << argv[0] << " [-d] <output> <input>" << std::endl;
16    exit(-1);
17  }
18  try
19  {
20    std::string output;
21    std::string input;
22    if (argv[1] == std::string("-d"))
23    {
24      Trace::enable(Trace::ALL);
25      output = argv[2];
26      input = argv[3];
27    }
28    else
29    {
30      output = argv[1];
31      input = argv[2];
32    }
33    std::ofstream ofs(output.c_str());
34    std::ifstream ifs(input.c_str());
35    Buffer buffer(ifs.rdbuf(), input);
36    Lexer lexer(&buffer);
37    int i = 0;
38    while(true)
39    {
40      Token token;
41
42      int t = lexer.look_ahead(i++, token);
43      if(!t) break;
44      else
45      {
46	ofs << std::setw(3);
47	if(t < 128)
48	  ofs << t << " '" << static_cast<char>(t) << "': ";
49	else
50	  ofs << t << "    : ";
51	ofs.put('"');
52	while(token.length-- > 0) ofs.put(*token.ptr++);
53	ofs << "\"\n";
54      }
55    }
56  }
57  catch (std::exception const &e)
58  {
59    std::cerr << "Caught exception : " << e.what() << std::endl;
60  }
61}
62