Synopsis - Cross-Reference

File: /sandbox/bpl/Processor.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 <boost/python.hpp>
 12#include <boost/python/make_constructor.hpp>
 13#include <fstream>
 14
 15namespace bpl = boost::python;
 16using namespace Synopsis;
 17
 18namespace
 19{
 20
 21Buffer *construct_buffer(char const *filename)
 22{
 23  std::ifstream ifs(filename);
 24  return new Buffer(ifs.rdbuf(), filename);
 25}
 26
 27void write_buffer(Buffer const &buffer,
 28		  char const *output, std::string const &filename)
 29{
 30  std::ofstream ofs(output);
 31  buffer.write(ofs, filename);
 32}
 33
 34//. insert the given string after the given node
 35void insert_buffer(Buffer &buffer, PTree::Node *node, std::string const &string)
 36{
 37  char *str = static_cast<char *>(memmove(new (GC) char[string.length() + 1],
 38					  string.data(), string.length()));
 39  str[string.length()] = '\0';
 40  buffer.replace(node->end(), node->end(), str, string.length());
 41}
 42
 43//. replace the given node by the given string
 44void replace_buffer(Buffer &buffer, PTree::Node *node, std::string const &string)
 45{
 46  char *str = static_cast<char *>(memmove(new (GC) char[string.length() + 1],
 47					  string.data(), string.length()));
 48  str[string.length()] = '\0';
 49  buffer.replace(node->begin(), node->end(), str, string.length());
 50}
 51
 52Parser *construct_parser(Lexer &lexer, SymbolFactory &symbols, int rule_set)
 53{
 54  return new Parser(lexer, symbols, rule_set);
 55}
 56
 57PTree::Node *parse(Parser *parser)
 58{
 59  PTree::Node *node = parser->parse();
 60  const Parser::ErrorList &errors = parser->errors();
 61
 62  for (Parser::ErrorList::const_iterator i = errors.begin();
 63       i != errors.end();
 64       ++i)
 65    (*i)->write(std::cerr);
 66  return node;
 67}
 68}
 69
 70BOOST_PYTHON_MODULE(Processor)
 71{
 72  bpl::class_<Buffer> buffer("Buffer", bpl::no_init);
 73  buffer.def("__init__", bpl::make_constructor(construct_buffer));
 74  buffer.def("write", write_buffer);
 75  buffer.def("insert", insert_buffer);
 76  buffer.def("replace", replace_buffer);
 77
 78  bpl::enum_<Lexer::TokenSet> token_set("TokenSet");
 79  token_set.value("C", Lexer::C);
 80  token_set.value("CXX", Lexer::CXX);
 81  token_set.value("GCC", Lexer::GCC);
 82  token_set.value("MSVC", Lexer::MSVC);
 83
 84  bpl::class_<Lexer> lexer("Lexer", bpl::init<Buffer *, int>());
 85
 86  bpl::enum_<Parser::RuleSet> rule_set("RuleSet");
 87  rule_set.value("CXX", Parser::CXX);
 88  rule_set.value("MSVC", Parser::MSVC);
 89
 90  bpl::enum_<SymbolFactory::Language> language("Language");
 91  language.value("NONE", SymbolFactory::NONE);
 92  language.value("C99", SymbolFactory::C99);
 93  language.value("CXX", SymbolFactory::CXX);
 94
 95  bpl::class_<SymbolFactory> symbols("SymbolFactory", bpl::init<SymbolFactory::Language>());
 96
 97  bpl::class_<Parser> parser("Parser", bpl::no_init);
 98  // keep the symbol table hidden as it will be exported in its own module later
 99  parser.def("__init__", bpl::make_constructor(construct_parser));
100  // The PTree module uses garbage collection so just ignore memory management,
101  // at least for now.
102  parser.def("parse", parse, bpl::return_value_policy<bpl::reference_existing_object>());
103}