00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #include <algorithm>
00049
00050 #include "debug.h"
00051 #include "parser_spec.hh"
00052 #include "itoken_spec.hh"
00053 #include "ebnf_la_node.hh"
00054 #include "ebnf_node_builder.hh"
00055
00056 ParserSpec::ParserSpec (PropRegistry ®istry_, ITokenSpec &tokens_) :
00057 tokens(tokens_),
00058 nodeBuilder(*new EbnfNodeBuilder(registry_, tokens_))
00059 {}
00060
00061
00062 IEbnfNodeBuilder& ParserSpec::getEbnfNodeBuilder()
00063 {
00064 return nodeBuilder;
00065 }
00066
00067
00068 void ParserSpec::addProduction (const string &name,
00069 const string &retTypeName,
00070 const Position &retTypePos,
00071 const string &formalArgs,
00072 const Position &formalArgsPos,
00073 const string &exceptList,
00074 const Position &exceptListPos,
00075 const string &preambleCode,
00076 const Position &preambleCodePos,
00077 EbnfNode *expansion,
00078 const Position &pos)
00079 throw (ParseException)
00080 {
00081 if (isProduction(name))
00082 throw ParseException(pos, string("Duplicate production name ") +
00083 name + ".");
00084 productions.push_back(ProductionSpec(name, retTypeName, retTypePos,
00085 formalArgs, formalArgsPos,
00086 exceptList, exceptListPos,
00087 preambleCode, preambleCodePos,
00088 dynamic_cast<EbnfLaNode*>(expansion),
00089 pos));
00090 }
00091
00092
00093 void ParserSpec::setPreambleCode (const string &block, const Position &pos)
00094 {
00095 preambleCode.set(block, pos);
00096 }
00097
00098
00099 void ParserSpec::setClassName (const string &className_)
00100 {
00101 className = className_;
00102 }
00103
00104
00105 void ParserSpec::setInheritance (const string &inheritance_,
00106 const Position &pos)
00107 {
00108 inheritance.set(inheritance_, pos);
00109 }
00110
00111
00112 void ParserSpec::addCodeBlock (const string &block, const Position &pos)
00113 {
00114 userCode.push_back(CodeChunk(pos, block));
00115 }
00116
00117
00118 bool operator == (ProductionSpec &p, const string &prodName)
00119 {
00120 return p.name == prodName;
00121 }
00122
00123
00124 bool ParserSpec::isProduction (const string &name)
00125 {
00126 return(find(productions.begin(), productions.end(), name) !=
00127 productions.end());
00128 }
00129
00130
00131 ProductionSpec& ParserSpec::getProduction (const string &name)
00132 throw (ParseException)
00133 {
00134 vector<ProductionSpec>::iterator i = find(productions.begin(),
00135 productions.end(),
00136 name);
00137 if (i == productions.end())
00138 throw ParseException(string("Production \"") + name + "\" not found");
00139
00140 return *i;
00141 }
00142
00143 #ifdef DEBUG
00144 void ParserSpec::dump (ostream &os) const
00145 {
00146 os << "ParseSpec dump: " << endl
00147 << "className: " << className << endl
00148 << "inheritance: " << inheritance.code << endl
00149 << "preambleCode:" << endl
00150 << preambleCode.code << endl
00151 << "----------------------------------------------------------" << endl
00152 << "userCode:" << endl;
00153 for (int i = 0; i < userCode.size(); i++)
00154 os << userCode[i].code << endl;
00155 os << "----------------------------------------------------------" << endl
00156 << "Productions: " << endl;
00157 for (int i = 0; i < productions.size(); i++) {
00158 productions[i].dump(os);
00159 os << endl;
00160 }
00161 os << "----------------------------------------------------------" << endl;
00162 }
00163 #endif