created replacement system for templates ("{%word%}" syntax)
This commit is contained in:
parent
55b1e1494a
commit
ecfa1730f5
43
Parser.cc
43
Parser.cc
@ -50,18 +50,57 @@ bool Parser::write(const string & fname)
|
||||
refptr<string> tokenlist = buildTokenList();
|
||||
writeDefine(header, "I_TOKENLIST", *tokenlist);
|
||||
header << endl;
|
||||
header.write((const char *) tmpl_parser_h, tmpl_parser_h_len);
|
||||
writeTmpl(header, (char *) tmpl_parser_h, tmpl_parser_h_len);
|
||||
|
||||
/* write the body */
|
||||
writeDefine(body, "I_HEADER_NAME", string("\"") + header_fname + "\"");
|
||||
body << endl;
|
||||
body.write((const char *) tmpl_parser_cc, tmpl_parser_cc_len);
|
||||
writeTmpl(body, (char *) tmpl_parser_cc, tmpl_parser_cc_len);
|
||||
|
||||
header.close();
|
||||
body.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Parser::writeTmpl(std::ostream & out, char * dat, int len)
|
||||
{
|
||||
char * newline;
|
||||
char * data = dat;
|
||||
const char * errptr;
|
||||
int erroffset;
|
||||
data[len-1] = '\n';
|
||||
const int ovec_size = 6;
|
||||
int ovector[ovec_size];
|
||||
pcre * replace = pcre_compile("{%(\\w+)%}", 0, &errptr, &erroffset, NULL);
|
||||
while (data < (dat + len) && (newline = strstr(data, "\n")) != NULL)
|
||||
{
|
||||
if (pcre_exec(replace, NULL, data, newline - data,
|
||||
0, 0, ovector, ovec_size) >= 0)
|
||||
{
|
||||
if (ovector[0] > 0)
|
||||
{
|
||||
out.write(data, ovector[0]);
|
||||
}
|
||||
out << getTmplReplacement(string(data, ovector[2],
|
||||
ovector[3] - ovector[2]));
|
||||
if (ovector[1] < newline - data)
|
||||
{
|
||||
out.write(data + ovector[1], newline - data - ovector[1]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
out.write(data, newline - data);
|
||||
}
|
||||
out << '\n';
|
||||
data = newline + 1;
|
||||
}
|
||||
}
|
||||
|
||||
std::string Parser::getTmplReplacement(const std::string & name)
|
||||
{
|
||||
}
|
||||
|
||||
refptr<string> Parser::buildTokenList()
|
||||
{
|
||||
refptr<string> tokenlist = new string();
|
||||
|
2
Parser.h
2
Parser.h
@ -23,6 +23,8 @@ class Parser
|
||||
m_rules.push_back(rd);
|
||||
}
|
||||
bool write(const std::string & fname);
|
||||
bool writeTmpl(std::ostream & out, char * dat, int len);
|
||||
std::string getTmplReplacement(const std::string & name);
|
||||
bool parseInputFile(char * buff, int size);
|
||||
|
||||
void setClassName(const std::string & cn) { m_classname = cn; }
|
||||
|
@ -18,6 +18,16 @@ I_CLASSNAME::I_CLASSNAME()
|
||||
{
|
||||
}
|
||||
|
||||
static TokenRef buildToken(int typeindex)
|
||||
{
|
||||
TokenRef token;
|
||||
switch (typeindex)
|
||||
{
|
||||
//%buildToken%
|
||||
}
|
||||
return token;
|
||||
}
|
||||
|
||||
static void read_istream(istream & i, vector<char> & buff, int & size)
|
||||
{
|
||||
size = 0;
|
||||
@ -111,12 +121,15 @@ bool I_CLASSNAME::parse(istream & i)
|
||||
}
|
||||
if (longest_match_index >= 0)
|
||||
{
|
||||
cout << "Matched a " << tokens[longest_match_index].name << endl;
|
||||
MatchesRef matches = new Matches(tokens[longest_match_index].re,
|
||||
&buff[0], ovector, ovector_size);
|
||||
TokenRef token = buildToken(longest_match_index);
|
||||
buff_pos += longest_match_length;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* no pattern matched the input at the current position */
|
||||
cerr << "Parse error" << endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user