added token_data and token_code replacements for global Token information; added debug message for missing replacements

This commit is contained in:
Josh Holtrop 2010-05-20 15:27:08 -04:00
parent 62fd96ad9b
commit 4ec500a2fb
4 changed files with 27 additions and 14 deletions

View File

@ -16,8 +16,11 @@
using namespace std; using namespace std;
#define DEBUG
Parser::Parser() Parser::Parser()
: m_classname("Parser"), m_namespace(""), m_extension("cc") : m_classname("Parser"), m_namespace(""), m_extension("cc"),
m_token_data(new string()), m_token_code(new string())
{ {
} }
@ -41,24 +44,29 @@ bool Parser::write(const string & fname)
ofstream header(header_fname.c_str()); ofstream header(header_fname.c_str());
ofstream body(body_fname.c_str()); ofstream body(body_fname.c_str());
/* set up replacements */
setReplacement("token_list", buildTokenList());
setReplacement("header_name",
new string(string("\"") + header_fname + "\""));
setReplacement("token_code", m_token_code);
setReplacement("token_data", m_token_data);
/* write the header */ /* write the header */
if (m_namespace != "") if (m_namespace != "")
{ {
writeDefine(header, "I_NAMESPACE", m_namespace); writeDefine(header, "I_NAMESPACE", m_namespace);
} }
writeDefine(header, "I_CLASSNAME", m_classname); writeDefine(header, "I_CLASSNAME", m_classname);
setReplacement("token_list", buildTokenList());
header << endl; header << endl;
writeTmpl(header, (char *) tmpl_parser_h, tmpl_parser_h_len); writeTmpl(header, (char *) tmpl_parser_h, tmpl_parser_h_len);
/* write the body */ /* write the body */
setReplacement("header_name",
new string(string("\"") + header_fname + "\""));
body << endl; body << endl;
writeTmpl(body, (char *) tmpl_parser_cc, tmpl_parser_cc_len); writeTmpl(body, (char *) tmpl_parser_cc, tmpl_parser_cc_len);
header.close(); header.close();
body.close(); body.close();
return true; return true;
} }
@ -103,6 +111,9 @@ refptr<std::string> Parser::getReplacement(const std::string & name)
{ {
return m_replacements[name]; return m_replacements[name];
} }
#ifdef DEBUG
cerr << "No replacement found for \"" << name << "\"" << endl;
#endif
return new string(""); return new string("");
} }
@ -119,7 +130,7 @@ refptr<string> Parser::buildTokenList()
+ (*t)->getCString() + "\", " + (*t)->getCString() + "\", "
+ ((*t)->getProcessFlag() ? "true" : "false") + " }"; + ((*t)->getProcessFlag() ? "true" : "false") + " }";
if (({typeof(t) tmp = t; ++tmp;}) != m_tokens.end()) if (({typeof(t) tmp = t; ++tmp;}) != m_tokens.end())
*tokenlist += ", \\\n"; *tokenlist += ",\n";
} }
return tokenlist; return tokenlist;
} }
@ -265,9 +276,7 @@ bool Parser::parseInputFile(char * buff, int size)
continue_line = true; continue_line = true;
if (current_token.isNull()) if (current_token.isNull())
{ {
cerr << "Data section with no corresponding " *m_token_data += gather;
"token definition on line " << lineno << endl;
return false;
} }
else else
{ {
@ -291,9 +300,7 @@ bool Parser::parseInputFile(char * buff, int size)
continue_line = true; continue_line = true;
if (current_token.isNull()) if (current_token.isNull())
{ {
cerr << "Code section with no corresponding " *m_token_code += gather;
"token definition on line " << lineno << endl;
return false;
} }
else else
{ {

View File

@ -50,6 +50,8 @@ class Parser
std::string m_namespace; std::string m_namespace;
std::string m_extension; std::string m_extension;
std::map< std::string, refptr<std::string> > m_replacements; std::map< std::string, refptr<std::string> > m_replacements;
refptr<std::string> m_token_data;
refptr<std::string> m_token_code;
}; };
#endif #endif

View File

@ -23,7 +23,7 @@ static TokenRef buildToken(int typeindex)
TokenRef token; TokenRef token;
switch (typeindex) switch (typeindex)
{ {
//%buildToken% {%buildToken%}
} }
return token; return token;
} }
@ -149,8 +149,9 @@ refptr<Node> Node::operator[](const std::string & index)
: NULL; : NULL;
} }
void Token::process() void Token::process(MatchesRef matches)
{ {
{%token_code%}
} }
Matches::Matches(pcre * re, const char * data, int * ovector, int ovec_size) Matches::Matches(pcre * re, const char * data, int * ovector, int ovec_size)

View File

@ -153,12 +153,15 @@ typedef refptr<Node> NodeRef;
class Token : public Node class Token : public Node
{ {
public: public:
virtual void process(); virtual void process(MatchesRef matches);
protected: protected:
{%token_data%}
}; };
typedef refptr<Token> TokenRef; typedef refptr<Token> TokenRef;
{%token_classes%}
#ifdef I_NAMESPACE #ifdef I_NAMESPACE
}; };
#endif #endif