added token classes and token class process() methods to Imbecile-generated parser
This commit is contained in:
parent
449a510671
commit
c6cc8e57d1
@ -41,7 +41,9 @@ 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 */
|
/* process data */
|
||||||
|
refptr<string> token_classes = new string();
|
||||||
|
refptr<string> token_classes_code = new string();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (list<TokenDefinitionRef>::const_iterator it = m_tokens.begin();
|
for (list<TokenDefinitionRef>::const_iterator it = m_tokens.begin();
|
||||||
it != m_tokens.end();
|
it != m_tokens.end();
|
||||||
@ -50,6 +52,8 @@ bool Parser::write(const string & fname)
|
|||||||
char buff[20];
|
char buff[20];
|
||||||
sprintf(buff, "%d", i++);
|
sprintf(buff, "%d", i++);
|
||||||
makeDefine(string("TK_") + (*it)->getName(), buff);
|
makeDefine(string("TK_") + (*it)->getName(), buff);
|
||||||
|
*token_classes += (*it)->getClassDefinition();
|
||||||
|
*token_classes_code += (*it)->getProcessMethod();
|
||||||
}
|
}
|
||||||
if (m_namespace != "")
|
if (m_namespace != "")
|
||||||
{
|
{
|
||||||
@ -64,6 +68,8 @@ bool Parser::write(const string & 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);
|
setReplacement("defines", m_defines);
|
||||||
|
setReplacement("token_classes", token_classes);
|
||||||
|
setReplacement("token_classes_code", token_classes_code);
|
||||||
|
|
||||||
/* write the header */
|
/* write the header */
|
||||||
writeTmpl(header, (char *) tmpl_parser_h, tmpl_parser_h_len);
|
writeTmpl(header, (char *) tmpl_parser_h, tmpl_parser_h_len);
|
||||||
|
@ -96,3 +96,30 @@ string TokenDefinition::getCString() const
|
|||||||
{
|
{
|
||||||
return c_escape(m_definition);
|
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;
|
||||||
|
}
|
||||||
|
@ -19,6 +19,8 @@ class TokenDefinition
|
|||||||
std::string getData() const { return m_data; }
|
std::string getData() const { return m_data; }
|
||||||
void addCode(const std::string & c) { m_code += c; m_process = true; }
|
void addCode(const std::string & c) { m_code += c; m_process = true; }
|
||||||
std::string getCode() const { return m_code; }
|
std::string getCode() const { return m_code; }
|
||||||
|
std::string getClassDefinition() const;
|
||||||
|
std::string getProcessMethod() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
|
@ -121,7 +121,7 @@ bool I_CLASSNAME::parse(istream & i)
|
|||||||
}
|
}
|
||||||
if (longest_match_index >= 0)
|
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);
|
&buff[0], ovector, ovector_size);
|
||||||
TokenRef token = buildToken(longest_match_index);
|
TokenRef token = buildToken(longest_match_index);
|
||||||
buff_pos += longest_match_length;
|
buff_pos += longest_match_length;
|
||||||
@ -149,7 +149,7 @@ refptr<Node> Node::operator[](const std::string & index)
|
|||||||
: NULL;
|
: NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Token::process(MatchesRef matches)
|
void Token::process(Matches matches)
|
||||||
{
|
{
|
||||||
{%token_code%}
|
{%token_code%}
|
||||||
}
|
}
|
||||||
@ -187,6 +187,8 @@ std::string Matches::operator[](const std::string & index)
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{%token_classes_code%}
|
||||||
|
|
||||||
#ifdef I_NAMESPACE
|
#ifdef I_NAMESPACE
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
@ -3,11 +3,16 @@
|
|||||||
#define IMBECILE_PARSER_HEADER
|
#define IMBECILE_PARSER_HEADER
|
||||||
|
|
||||||
#include <pcre.h>
|
#include <pcre.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
{%user_includes%}
|
||||||
|
|
||||||
{%defines%}
|
{%defines%}
|
||||||
|
|
||||||
#ifdef I_NAMESPACE
|
#ifdef I_NAMESPACE
|
||||||
@ -138,7 +143,6 @@ class Matches
|
|||||||
int * m_ovector;
|
int * m_ovector;
|
||||||
int m_ovec_size;
|
int m_ovec_size;
|
||||||
};
|
};
|
||||||
typedef refptr<Matches> MatchesRef;
|
|
||||||
|
|
||||||
class Node
|
class Node
|
||||||
{
|
{
|
||||||
@ -155,7 +159,7 @@ typedef refptr<Node> NodeRef;
|
|||||||
class Token : public Node
|
class Token : public Node
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual void process(MatchesRef matches);
|
virtual void process(Matches matches);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
{%token_data%}
|
{%token_data%}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user