Synopsis - Cross-Reference
File: /Synopsis/Parsers/C/ParserImpl.cc1// 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 8#include <Synopsis/ASG/ASGKit.hh> 9#include <Synopsis/Python/Module.hh> 10#include <Synopsis/Trace.hh> 11#include <Synopsis/PTree.hh> 12#include <Synopsis/PTree/Display.hh> 13#include <Synopsis/SymbolLookup.hh> 14#include <Synopsis/Buffer.hh> 15#include <Synopsis/Lexer.hh> 16#include <Synopsis/SymbolFactory.hh> 17#include <Synopsis/Parser.hh> 18#include "ASGTranslator.hh" 19// #include "SXRGenerator.hh" 20#include <Support/ErrorHandler.hh> 21#include <fstream> 22 23using namespace Synopsis; 24 25namespace 26{ 27PyObject *error; 28 29//. Override unexpected() to print a message before we abort 30void unexpected() 31{ 32 std::cout << "Warning: Aborting due to unexpected exception." << std::endl; 33 throw std::bad_exception(); 34} 35 36PyObject *parse(PyObject * /* self */, PyObject *args) 37{ 38 PyObject *py_ir; 39 const char *src, *cppfile; 40 char const * base_path = ""; 41 char const * sxr_prefix = 0; 42 int primary_file_only, verbose, debug; 43 if (!PyArg_ParseTuple(args, "Ossizzii", 44 &py_ir, &cppfile, &src, 45 &primary_file_only, 46 &base_path, 47 &sxr_prefix, 48 &verbose, 49 &debug)) 50 return 0; 51 52 Py_INCREF(py_ir); 53 IR ir(py_ir); 54 Py_INCREF(py_ir); 55 56 std::set_unexpected(unexpected); 57 ErrorHandler error_handler(); 58 59// if (verbose) ::verbose = true; 60 if (debug) Synopsis::Trace::enable(Trace::TRANSLATION); 61 62 if (!src || *src == '\0') 63 { 64 PyErr_SetString(PyExc_RuntimeError, "no input file"); 65 return 0; 66 } 67 try 68 { 69 std::ifstream ifs(cppfile); 70 Buffer buffer(ifs.rdbuf(), src); 71 Lexer lexer(&buffer, Lexer::GCC); 72 SymbolFactory symbols(SymbolFactory::NONE); 73 Parser parser(lexer, symbols, Parser::GCC); 74 PTree::Node *ptree = parser.parse(); 75 const Parser::ErrorList &errors = parser.errors(); 76 if (!errors.empty()) 77 { 78 for (Parser::ErrorList::const_iterator i = errors.begin(); i != errors.end(); ++i) 79 (*i)->write(std::cerr); 80 throw std::runtime_error("The input contains errors."); 81 } 82 else if (ptree) 83 { 84 ASGTranslator translator(src, base_path, primary_file_only, ir, verbose, debug); 85 translator.translate(ptree, buffer); 86// if (sxr_prefix) 87// { 88// SXRGenerator sxr_generator(src, base_path, primary_file_only, ir, verbose, debug); 89// srx_generator.generate(ptree, buffer); 90// } 91 } 92 } 93 catch (std::exception const &e) 94 { 95 PyErr_SetString(error, e.what()); 96 return 0; 97 } 98 return py_ir; 99} 100 101PyMethodDef methods[] = {{(char*)"parse", parse, METH_VARARGS}, 102 {0}}; 103} 104 105extern "C" void initParserImpl() 106{ 107 Python::Module module = Python::Module::define("ParserImpl", methods); 108 module.set_attr("version", "0.1"); 109 Python::Object processor = Python::Object::import("Synopsis.Processor"); 110 Python::Object error_base = processor.attr("Error"); 111 error = PyErr_NewException("ParserImpl.ParseError", error_base.ref(), 0); 112 module.set_attr("ParseError", error); 113}