From 33ba7bb1a810b1965a4dee0039871236edbb5035 Mon Sep 17 00:00:00 2001 From: sbrandt Date: Wed, 6 Mar 2013 16:28:21 +0000 Subject: 1) Allow comments in quotes 2) expand variables in quotes 3) parse fortran format floats, e.g. 1.0e-3 4) allow some sequences of number/letter to be treated as a name even if they start with a digit. git-svn-id: http://svn.cactuscode.org/flesh/trunk@4978 17b73243-c579-4c4c-a9d2-2d5706c11dac --- src/piraha/Call.cc | 38 +++++++++++++++++++++++++++++++++----- src/piraha/Grammar.cc | 16 ++++++++-------- src/piraha/Piraha.hpp | 4 ++-- 3 files changed, 43 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/piraha/Call.cc b/src/piraha/Call.cc index f5082487..70d4adab 100644 --- a/src/piraha/Call.cc +++ b/src/piraha/Call.cc @@ -261,6 +261,28 @@ smart_ptr lookup_var(smart_ptr gr) { return ret; } + +std::string string_reparser(std::string s) { + smart_ptr m = new Matcher(par_file_grammar,"stringparser",s.c_str(),s.length()); + if(m->matches()) { + std::string out = ""; + for(int i=0;i < m->groupCount(); i++) { + std::string pn = m->group(i)->getPatternName(); + if(pn == "any" || pn == "name") { + out += m->group(i)->substring(); + } else if(pn == "stringcomment") { + ; + } else { + smart_ptr val = lookup_var(m->group(i)); + out += val->copy(); + } + } + return out; + } else { + return s; + } +} + /** * The meval() function takes any node within * the parse tree and creates a Value object @@ -380,7 +402,7 @@ smart_ptr meval(smart_ptr gr) { msg << "Unknown func: " << fn << "(" << val->type << ")" << std::endl; std::string par = get_parfile(); CCTK_Error(gr->line(),par.c_str(),current_thorn.c_str(),msg.str().c_str()); - } else if(pn == "name") { + } else if(pn == "name"||pn == "dname") { std::string s = gr->substring(); s = mklower(s); if(s == "no" || s == "false") { @@ -453,7 +475,7 @@ smart_ptr meval(smart_ptr gr) { } } else if(pn == "quot") { ret->type = PIR_STRING; - ret->sdata = gr->group(0)->substring(); + ret->sdata = string_reparser(gr->group(0)->substring()); } else if(pn == "inquot") { ret->type = PIR_STRING; ret->sdata = gr->substring(); @@ -602,7 +624,9 @@ smart_ptr meval(smart_ptr gr) { } else if(v1->type == PIR_STRING && v2->type == PIR_INT) { ret->type = PIR_STRING; if(mulop == "*") { - ret->sdata = v1->sdata + v2->sdata; + ret->sdata = ""; + for(int i=0;iidata;i++) + ret->sdata += v1->sdata; } else { std::ostringstream msg; msg << "Unknown mul operator: " << v1->type << mulop << v2->type << std::endl; @@ -692,15 +716,19 @@ extern "C" int cctk_PirahaParser(const char *buffer,unsigned long buffersize,int const char *par_file_src = "skipper = ([ \\t\\r\\n]|\\#.*)*\n" "skipeol = ([ \\t\\r]|\\#.*)*\\n\n" + "any = [^]\n" + "stringcomment = #.*\n" + "stringparser = ^({stringcomment}|{var}|{name}|{any})*$\n" "# Note that / occurs in some par files. It is my\n" "# feeling that this should require quote marks.\n" "name = [@a-zA-Z_][@/a-zA-Z0-9_-]*\n" + "dname = [01-9][ab-zA-Z_]{2,}\n" "inquot = ({var}|\\\\.|[^\\\\\"])*\n" "fname = \\.?/[-\\./0-9a-zA-Z_]+\n" "quot = \"{inquot}\"|{fname}\n" - "num = (inf|nan|([0-9]+(\\.[0-9]*|)|\\.[0-9]+)(e[+-]?[0-9]+|))\n" + "num = (inf|nan|([0-9]+(\\.[0-9]*|)|\\.[0-9]+)([ed][+-]?[0-9]+|))\n" "env = ENV\\{{name}\\}\n" "var = \\$({env}|{name}|\\{{name}\\})\n" @@ -722,7 +750,7 @@ extern "C" int cctk_PirahaParser(const char *buffer,unsigned long buffersize,int "func = {name} \\( {expr} \\)\n" "array = \\[ {expr}( , {expr})* \\]\n" - "value = {unop}?({par}|{func}|{paren}|{num}|{quot}|{name}|{var})\n" + "value = {unop}?({par}|{func}|{paren}|{dname}|{num}|{quot}|{name}|{var})\n" "unop = [-!]\n" "int = [0-9]+\n" diff --git a/src/piraha/Grammar.cc b/src/piraha/Grammar.cc index 9592e7c2..5a516358 100644 --- a/src/piraha/Grammar.cc +++ b/src/piraha/Grammar.cc @@ -6,18 +6,18 @@ extern smart_ptr compile(smart_ptr g,bool ignCase,smart_ptr pegGrammar = AutoGrammar::reparserGenerator(); -void Grammar::compile(std::string name,smart_ptr g) { - default_rule = name; - smart_ptr p = piraha::compile(g,false,this); - patterns.put(name,p); +void compile(smart_ptr thisg,std::string name,smart_ptr g) { + thisg->default_rule = name; + smart_ptr p = piraha::compile(g,false,thisg); + thisg->patterns.put(name,p); } -void Grammar::compile(std::string name,std::string pattern) { - default_rule = name; +void compile(smart_ptr thisg,std::string name,std::string pattern) { + thisg->default_rule = name; smart_ptr m = new Matcher(pegGrammar,"pattern",pattern.c_str()); smart_ptr g = m.dup(); if(m->matches()) { - smart_ptr p = piraha::compile(g,false,this); - patterns.put(name,p); + smart_ptr p = piraha::compile(g,false,thisg); + thisg->patterns.put(name,p); } else { std::cout << "Could not compile(" << name << "," << pattern << ")" << std::endl; std::cout << "pos = " << m->pos << std::endl; diff --git a/src/piraha/Piraha.hpp b/src/piraha/Piraha.hpp index f16b6ae0..3addbc83 100644 --- a/src/piraha/Piraha.hpp +++ b/src/piraha/Piraha.hpp @@ -117,8 +117,6 @@ public: Grammar() {} virtual ~Grammar() {} JMap patterns; - void compile(std::string name,std::string pattern); - void compile(std::string name,smart_ptr pattern); std::string default_rule; }; @@ -296,6 +294,8 @@ public: extern smart_ptr pegGrammar; extern smart_ptr compile(smart_ptr g,bool ignCase,smart_ptr gram); extern void compileFile(smart_ptr g,const char *buffer,signed long buffersize=-1); +void compile(smart_ptr thisg,std::string name,std::string pattern); +void compile(smart_ptr thisg,std::string name,smart_ptr pattern); } -- cgit v1.2.3