From c6cc8e57d1423791f497ea96d31e901f30ecae31 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Thu, 20 May 2010 17:08:03 -0400 Subject: [PATCH] added token classes and token class process() methods to Imbecile-generated parser --- Parser.cc | 8 +++++++- TokenDefinition.cc | 27 +++++++++++++++++++++++++++ TokenDefinition.h | 2 ++ tmpl/parser.cc | 6 ++++-- tmpl/parser.h | 8 ++++++-- 5 files changed, 46 insertions(+), 5 deletions(-) diff --git a/Parser.cc b/Parser.cc index 6e2460f..19d4cb1 100644 --- a/Parser.cc +++ b/Parser.cc @@ -41,7 +41,9 @@ bool Parser::write(const string & fname) ofstream header(header_fname.c_str()); ofstream body(body_fname.c_str()); - /* make some #define's */ + /* process data */ + refptr token_classes = new string(); + refptr token_classes_code = new string(); int i = 0; for (list::const_iterator it = m_tokens.begin(); it != m_tokens.end(); @@ -50,6 +52,8 @@ bool Parser::write(const string & fname) char buff[20]; sprintf(buff, "%d", i++); makeDefine(string("TK_") + (*it)->getName(), buff); + *token_classes += (*it)->getClassDefinition(); + *token_classes_code += (*it)->getProcessMethod(); } if (m_namespace != "") { @@ -64,6 +68,8 @@ bool Parser::write(const string & fname) setReplacement("token_code", m_token_code); setReplacement("token_data", m_token_data); setReplacement("defines", m_defines); + setReplacement("token_classes", token_classes); + setReplacement("token_classes_code", token_classes_code); /* write the header */ writeTmpl(header, (char *) tmpl_parser_h, tmpl_parser_h_len); diff --git a/TokenDefinition.cc b/TokenDefinition.cc index 5e2201d..fd1976a 100644 --- a/TokenDefinition.cc +++ b/TokenDefinition.cc @@ -96,3 +96,30 @@ string TokenDefinition::getCString() const { return c_escape(m_definition); } + +string TokenDefinition::getClassDefinition() const +{ + string ret = "class "; + ret += m_name + " : public Token {\n"; + ret += "public:\n"; + if (m_process) + { + ret += " virtual void process(Matches matches);\n"; + } + ret += "protected:\n"; + ret += m_data + "\n"; + ret += "};\n"; + return ret; +} + +string TokenDefinition::getProcessMethod() const +{ + string ret; + if (m_code != "") + { + ret += "void " + m_name + "::process(Matches matches) {\n"; + ret += m_code + "\n"; + ret += "}\n"; + } + return ret; +} diff --git a/TokenDefinition.h b/TokenDefinition.h index d13f8fc..3d94b2f 100644 --- a/TokenDefinition.h +++ b/TokenDefinition.h @@ -19,6 +19,8 @@ class TokenDefinition std::string getData() const { return m_data; } void addCode(const std::string & c) { m_code += c; m_process = true; } std::string getCode() const { return m_code; } + std::string getClassDefinition() const; + std::string getProcessMethod() const; protected: std::string m_name; diff --git a/tmpl/parser.cc b/tmpl/parser.cc index 53dfb83..6d57266 100644 --- a/tmpl/parser.cc +++ b/tmpl/parser.cc @@ -121,7 +121,7 @@ bool I_CLASSNAME::parse(istream & i) } if (longest_match_index >= 0) { - MatchesRef matches = new Matches(tokens[longest_match_index].re, + Matches matches(tokens[longest_match_index].re, &buff[0], ovector, ovector_size); TokenRef token = buildToken(longest_match_index); buff_pos += longest_match_length; @@ -149,7 +149,7 @@ refptr Node::operator[](const std::string & index) : NULL; } -void Token::process(MatchesRef matches) +void Token::process(Matches matches) { {%token_code%} } @@ -187,6 +187,8 @@ std::string Matches::operator[](const std::string & index) return ""; } +{%token_classes_code%} + #ifdef I_NAMESPACE }; #endif diff --git a/tmpl/parser.h b/tmpl/parser.h index fe657e1..331da83 100644 --- a/tmpl/parser.h +++ b/tmpl/parser.h @@ -3,11 +3,16 @@ #define IMBECILE_PARSER_HEADER #include +#include +#include +#include #include #include #include +{%user_includes%} + {%defines%} #ifdef I_NAMESPACE @@ -138,7 +143,6 @@ class Matches int * m_ovector; int m_ovec_size; }; -typedef refptr MatchesRef; class Node { @@ -155,7 +159,7 @@ typedef refptr NodeRef; class Token : public Node { public: - virtual void process(MatchesRef matches); + virtual void process(Matches matches); protected: {%token_data%}