summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsbrandt <sbrandt@17b73243-c579-4c4c-a9d2-2d5706c11dac>2013-03-06 16:28:21 +0000
committersbrandt <sbrandt@17b73243-c579-4c4c-a9d2-2d5706c11dac>2013-03-06 16:28:21 +0000
commit33ba7bb1a810b1965a4dee0039871236edbb5035 (patch)
tree4cb517dbaa19854a1f01d1d8ee207b69333d0bfa /src
parent4c4e8cd402efafafcdcff354b8a6b277efca71f6 (diff)
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
Diffstat (limited to 'src')
-rw-r--r--src/piraha/Call.cc38
-rw-r--r--src/piraha/Grammar.cc16
-rw-r--r--src/piraha/Piraha.hpp4
3 files changed, 43 insertions, 15 deletions
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<Value> lookup_var(smart_ptr<Group> gr) {
return ret;
}
+
+std::string string_reparser(std::string s) {
+ smart_ptr<Matcher> 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<Value> 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<Value> meval(smart_ptr<Group> 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<Value> meval(smart_ptr<Group> 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<Value> meval(smart_ptr<Group> 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;i<v2->idata;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<Pattern> compile(smart_ptr<Group> g,bool ignCase,smart_ptr<Gram
smart_ptr<Grammar> pegGrammar = AutoGrammar::reparserGenerator();
-void Grammar::compile(std::string name,smart_ptr<Group> g) {
- default_rule = name;
- smart_ptr<Pattern> p = piraha::compile(g,false,this);
- patterns.put(name,p);
+void compile(smart_ptr<Grammar> thisg,std::string name,smart_ptr<Group> g) {
+ thisg->default_rule = name;
+ smart_ptr<Pattern> 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<Grammar> thisg,std::string name,std::string pattern) {
+ thisg->default_rule = name;
smart_ptr<Matcher> m = new Matcher(pegGrammar,"pattern",pattern.c_str());
smart_ptr<Group> g = m.dup<Group>();
if(m->matches()) {
- smart_ptr<Pattern> p = piraha::compile(g,false,this);
- patterns.put(name,p);
+ smart_ptr<Pattern> 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<Group> pattern);
std::string default_rule;
};
@@ -296,6 +294,8 @@ public:
extern smart_ptr<Grammar> pegGrammar;
extern smart_ptr<Pattern> compile(smart_ptr<Group> g,bool ignCase,smart_ptr<Grammar> gram);
extern void compileFile(smart_ptr<Grammar> g,const char *buffer,signed long buffersize=-1);
+void compile(smart_ptr<Grammar> thisg,std::string name,std::string pattern);
+void compile(smart_ptr<Grammar> thisg,std::string name,smart_ptr<Group> pattern);
}