From ecfa1730f5967c1ca991f62213353c161f1967a6 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 19 May 2010 16:14:04 -0400 Subject: [PATCH] created replacement system for templates ("{%word%}" syntax) --- Parser.cc | 43 +++++++++++++++++++++++++++++++++++++++++-- Parser.h | 2 ++ tmpl/parser.cc | 15 ++++++++++++++- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/Parser.cc b/Parser.cc index d15e0a4..e1063fe 100644 --- a/Parser.cc +++ b/Parser.cc @@ -50,18 +50,57 @@ bool Parser::write(const string & fname) refptr 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 Parser::buildTokenList() { refptr tokenlist = new string(); diff --git a/Parser.h b/Parser.h index 16bb0d4..f63025b 100644 --- a/Parser.h +++ b/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; } diff --git a/tmpl/parser.cc b/tmpl/parser.cc index dbdab6d..9579764 100644 --- a/tmpl/parser.cc +++ b/tmpl/parser.cc @@ -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 & 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; } }