INTERFACE: #include // for PtreeArray #include "ptree_nodes.h" // forward declarations namespace Opencxx { class ProgramFile; class Program; class Lex; class Parser; class Walker; } /** Parsed source code in memory-resident database. This is a singleton class. */ class Source { private: Source (const Source&); //!< Declaration of undefined copy constructor. // DATA Opencxx::Program* _src_prog; Opencxx::Lex* _lex; Opencxx::Parser* _parser; PtreeArray _def; }; IMPLEMENTATION: #include #include #include #include #include #include "sstring.h" // // OpenC++ includes // #include #include #include #include #include using namespace std; using namespace Opencxx; PUBLIC static Source& Source::instance () { static Source i; return i; }; PRIVATE Source::Source () : _src_prog(0), _lex(0), _parser (0), _def (0) {} PRIVATE Source::~Source () {} PUBLIC void Source::parse (const char* filename) { ifstream src_stream (filename); if(!src_stream) throw string (filename) + ": " + strerror (errno); parse (filename, new ProgramFile(src_stream)); } PUBLIC void Source::parse (const char* filename, Opencxx::Program* program) { _src_prog = program; _lex = new Lex (_src_prog); _parser = new Parser (_lex); Ptree *program_fragment; while(_parser->rProgram(program_fragment)) { _def.Append (program_fragment); } if (_parser->NumOfErrors() != 0) { throw (Sstring() << _parser->NumOfErrors() << " found.\n").str(); } return; } PUBLIC void Source::dump () { _def.All()->Display2 (cout); } PUBLIC Opencxx::Parser* Source::parser() { return _parser; } PUBLIC Ptree* Source::translate (Opencxx::Walker *w) { PtreeArray* a = new PtreeArray; PtreeIter i (_def.All()); while (*i) { a->Append (w->Translate(*i)); i++; } return a->All(); } PUBLIC Opencxx::Program* Source::program() { return _src_prog; }