From b42790cd6e962db91eeac82842dda5f1c7a754b0 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 2 Oct 2013 16:41:05 +0200 Subject: [PATCH] preprocessor: replace dates with dynDates --- ParsingDriver.cc | 3 ++- Statement.cc | 69 +++++++++++++++++++++++++++++++++++++++++++++++- Statement.hh | 6 +++-- 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/ParsingDriver.cc b/ParsingDriver.cc index 38bf5a78..37029fdc 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -2449,7 +2449,8 @@ ParsingDriver::add_model_var_or_external_function(string *function_name, bool in void ParsingDriver::add_native(const string &s) { - mod_file->addStatement(new NativeStatement(s)); + string ss = string(s); + mod_file->addStatement(new NativeStatement(ss)); } void diff --git a/Statement.cc b/Statement.cc index 59021287..242c73a8 100644 --- a/Statement.cc +++ b/Statement.cc @@ -18,6 +18,7 @@ */ #include "Statement.hh" +#include ModFileStructure::ModFileStructure() : check_present(false), @@ -65,11 +66,77 @@ Statement::computingPass() { } -NativeStatement::NativeStatement(const string &native_statement_arg) : +NativeStatement::NativeStatement(string &native_statement_arg) : native_statement(native_statement_arg) { } +void +NativeStatement::computingPass() +{ + using namespace boost::xpressive; + // Return if this is a comment + sregex comment_expr = sregex::compile( "\%.*" ); + match_results results; + if (regex_match(native_statement, results, comment_expr)) + return; + + // Otherwise, look at the line and consider substituting date + size_t idx = -1; + vector apostrophes; + while((idx = native_statement.find("'", idx + 1)) != string::npos) + if (apostrophes.size() < 2) + apostrophes.push_back(idx); + else + if (idx == apostrophes.back() + 1) + apostrophes.pop_back(); + else + apostrophes.push_back(idx); + + if (apostrophes.size() % 2) + { + cerr << native_statement << + " seems to be invalid Matlab syntax (an odd number of apostrophes was encountered)" << endl; + exit(EXIT_FAILURE); + } + + bool skip = false; + string newstr = ""; + int lastidx = 0; + sregex date_expr = sregex::compile( "-?[0-9]+[Mm]([1-9]|1[0-2])|-?[0-9]+[Qq][1-4]|-?[0-9]+[Ww]([1-9]{1}|[1-4][0-9]|5[0-2])" ); + string format( "dynDate('$&')" ); + for (size_t i = 0; i < apostrophes.size(); i++) + if (apostrophes[i] == 0) + skip = true; + else + { + if (skip) + { + skip = false; + newstr.append(native_statement.substr(lastidx, apostrophes[i] - lastidx)); + } + else + { + skip = true; + newstr.append(regex_replace(native_statement.substr(lastidx, apostrophes[i] - lastidx), + date_expr, format)); + } + lastidx = apostrophes[i]; + } + + //Replace last (or only) element + if (apostrophes.empty()) + lastidx = 0; + newstr.append(regex_replace(native_statement.substr(lastidx, native_statement.size() - lastidx), + date_expr, format)); + native_statement = newstr; +} + +void +regexReplace() +{ +} + void NativeStatement::writeOutput(ostream &output, const string &basename) const { diff --git a/Statement.hh b/Statement.hh index cd3fd5cf..82f64fa7 100644 --- a/Statement.hh +++ b/Statement.hh @@ -121,10 +121,12 @@ public: class NativeStatement : public Statement { private: - const string native_statement; + string native_statement; public: - NativeStatement(const string &native_statement_arg); + NativeStatement(string &native_statement_arg); + virtual void computingPass(); virtual void writeOutput(ostream &output, const string &basename) const; + void regexReplace(); }; class OptionsList