added token classes and token class process() methods to Imbecile-generated parser

This commit is contained in:
Josh Holtrop 2010-05-20 17:08:03 -04:00
parent 449a510671
commit c6cc8e57d1
5 changed files with 46 additions and 5 deletions

View File

@ -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<string> token_classes = new string();
refptr<string> token_classes_code = new string();
int i = 0;
for (list<TokenDefinitionRef>::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);

View File

@ -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;
}

View File

@ -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;

View File

@ -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> 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

View File

@ -3,11 +3,16 @@
#define IMBECILE_PARSER_HEADER
#include <pcre.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <map>
#include <vector>
{%user_includes%}
{%defines%}
#ifdef I_NAMESPACE
@ -138,7 +143,6 @@ class Matches
int * m_ovector;
int m_ovec_size;
};
typedef refptr<Matches> MatchesRef;
class Node
{
@ -155,7 +159,7 @@ typedef refptr<Node> NodeRef;
class Token : public Node
{
public:
virtual void process(MatchesRef matches);
virtual void process(Matches matches);
protected:
{%token_data%}