Synopsis - Cross-Reference

File: /src/tools/display-ptree.cc
 1//
 2// Copyright (C) 2005 Stefan Seefeld
 3// All rights reserved.
 4// Licensed to the public under the terms of the GNU LGPL (>= 2),
 5// see the file COPYING for details.
 6//
 7#include <Synopsis/Buffer.hh>
 8#include <Synopsis/Lexer.hh>
 9#include <Synopsis/SymbolFactory.hh>
10#include <Synopsis/Parser.hh>
11#include <Synopsis/PTree.hh>
12#include <Synopsis/PTree/Display.hh>
13#include <Synopsis/SymbolLookup.hh>
14#include <Synopsis/Trace.hh>
15#include <fstream>
16
17using namespace Synopsis;
18
19int usage(const char *command)
20{
21  std::cerr << "Usage: " << command << " [-c] [-g <filename>] [-d] [-r] [-e] <input>" << std::endl;
22  return -1;
23}
24
25int main(int argc, char **argv)
26{
27  bool encoding = false;
28  bool typeinfo = false;
29  bool debug = false;
30  int tokenset = Lexer::CXX | Lexer::GCC;
31  int ruleset = Parser::CXX | Parser::GCC;
32  std::string dotfile;
33  const char *input = argv[1];
34  if (argc == 1) return usage(argv[0]);
35  for (int i = 1; i < argc - 1; ++i)
36  {
37    if (argv[i] == std::string("-g"))
38    {
39      // make sure an argument is provided
40      if (++i == argc - 1 || argv[i][0] == '-')
41      {
42	usage(argv[0]);
43	return -1;
44      }
45      dotfile = argv[i];
46    }
47    else if (argv[i] == std::string("-c"))
48    {
49      tokenset = Lexer::C | Lexer::GCC;
50      ruleset = Parser::GCC;
51    }
52    else if (argv[i] == std::string("-e")) encoding = true;
53    else if (argv[i] == std::string("-r")) typeinfo = true;
54    else if (argv[i] == std::string("-d")) debug = true;
55    else return usage(argv[0]);
56  }
57  input = argv[argc - 1];
58  try
59  {
60    if (debug) Trace::enable(Trace::ALL);
61
62    PTree::Encoding::do_init_static();
63
64    std::ifstream ifs(input);
65    Buffer buffer(ifs.rdbuf(), input);
66    Lexer lexer(&buffer, tokenset);
67    SymbolFactory symbols;
68    Parser parser(lexer, symbols, ruleset);
69    PTree::Node *node = parser.parse();
70    const Parser::ErrorList &errors = parser.errors();
71    for (Parser::ErrorList::const_iterator i = errors.begin(); i != errors.end(); ++i)
72      (*i)->write(std::cerr);
73
74    if (!node) return -1;
75    if (dotfile.empty())
76    {
77      PTree::display(node, std::cout, encoding, typeinfo);
78    }
79    else
80    {
81      std::ofstream os(dotfile.c_str());
82      PTree::generate_dot_file(node, os);
83    }
84  }
85  catch (const std::exception &e)
86  {
87    std::cerr << "Caught exception : " << e.what() << std::endl;
88  }
89}