re-worked defines in generated parser; writing TK_ #define's for token types now

This commit is contained in:
Josh Holtrop 2010-05-20 15:59:06 -04:00
parent 4ec500a2fb
commit 449a510671
3 changed files with 32 additions and 20 deletions

View File

@ -20,17 +20,14 @@ using namespace std;
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()) m_token_data(new string()), m_token_code(new string()),
m_defines(new string())
{ {
} }
static void writeDefine(ostream & out, void Parser::makeDefine(const string & defname, const string & definition)
const string & defname, const string & definition)
{ {
out << "#ifdef " << defname << endl; *m_defines += string("#define ") + defname + " " + definition + "\n";
out << "#undef " << defname << endl;
out << "#endif" << endl;
out << "#define " << defname << " " << definition << endl;
} }
bool Parser::write(const string & fname) bool Parser::write(const string & fname)
@ -44,24 +41,34 @@ 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());
/* make some #define's */
int i = 0;
for (list<TokenDefinitionRef>::const_iterator it = m_tokens.begin();
it != m_tokens.end();
it++)
{
char buff[20];
sprintf(buff, "%d", i++);
makeDefine(string("TK_") + (*it)->getName(), buff);
}
if (m_namespace != "")
{
makeDefine("I_NAMESPACE", m_namespace);
}
makeDefine("I_CLASSNAME", m_classname);
/* set up replacements */ /* set up replacements */
setReplacement("token_list", buildTokenList()); setReplacement("token_list", buildTokenList());
setReplacement("header_name", setReplacement("header_name",
new string(string("\"") + header_fname + "\"")); new string(string("\"") + header_fname + "\""));
setReplacement("token_code", m_token_code); setReplacement("token_code", m_token_code);
setReplacement("token_data", m_token_data); setReplacement("token_data", m_token_data);
setReplacement("defines", m_defines);
/* write the header */ /* write the header */
if (m_namespace != "")
{
writeDefine(header, "I_NAMESPACE", m_namespace);
}
writeDefine(header, "I_CLASSNAME", m_classname);
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 */
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();

View File

@ -24,12 +24,6 @@ class Parser
m_rules.push_back(rd); m_rules.push_back(rd);
} }
bool write(const std::string & fname); bool write(const std::string & fname);
bool writeTmpl(std::ostream & out, char * dat, int len);
refptr<std::string> getReplacement(const std::string & name);
void setReplacement(const std::string & name, refptr<std::string> val)
{
m_replacements[name] = val;
}
bool parseInputFile(char * buff, int size); bool parseInputFile(char * buff, int size);
void setClassName(const std::string & cn) { m_classname = cn; } void setClassName(const std::string & cn) { m_classname = cn; }
@ -43,6 +37,14 @@ class Parser
protected: protected:
refptr<std::string> buildTokenList(); refptr<std::string> buildTokenList();
bool writeTmpl(std::ostream & out, char * dat, int len);
refptr<std::string> getReplacement(const std::string & name);
void setReplacement(const std::string & name, refptr<std::string> val)
{
m_replacements[name] = val;
}
void makeDefine(const std::string & defname,
const std::string & definition);
std::list<TokenDefinitionRef> m_tokens; std::list<TokenDefinitionRef> m_tokens;
std::vector< refptr< RuleDefinition > > m_rules; std::vector< refptr< RuleDefinition > > m_rules;
@ -52,6 +54,7 @@ class Parser
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_data;
refptr<std::string> m_token_code; refptr<std::string> m_token_code;
refptr<std::string> m_defines;
}; };
#endif #endif

View File

@ -8,6 +8,8 @@
#include <map> #include <map>
#include <vector> #include <vector>
{%defines%}
#ifdef I_NAMESPACE #ifdef I_NAMESPACE
namespace I_NAMESPACE { namespace I_NAMESPACE {
#endif #endif