summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsbrandt <sbrandt@17b73243-c579-4c4c-a9d2-2d5706c11dac>2013-03-22 19:46:56 +0000
committersbrandt <sbrandt@17b73243-c579-4c4c-a9d2-2d5706c11dac>2013-03-22 19:46:56 +0000
commitf74e867aab6e3a36824f5c33ed1d75bac90ef06f (patch)
treececf6e882ad4ef16dce092ac06dff36d16c84489
parent249587f1e95ea75de1e7a04161124751750e42c0 (diff)
Updates to Piraha:
1) Added some debug code 2) Fix logic bugs that only I noticed Still passes the ET testsuite. git-svn-id: http://svn.cactuscode.org/flesh/trunk@4988 17b73243-c579-4c4c-a9d2-2d5706c11dac
-rw-r--r--src/piraha/Bracket.cc45
-rw-r--r--src/piraha/Call.cc3
-rw-r--r--src/piraha/Literal.cc3
-rw-r--r--src/piraha/Matcher.cc9
-rw-r--r--src/piraha/Multi.cc14
-rw-r--r--src/piraha/Piraha.hpp68
6 files changed, 118 insertions, 24 deletions
diff --git a/src/piraha/Bracket.cc b/src/piraha/Bracket.cc
index d1c52632..47f5c443 100644
--- a/src/piraha/Bracket.cc
+++ b/src/piraha/Bracket.cc
@@ -1,4 +1,5 @@
#include "Piraha.hpp"
+#include <string.h>
using namespace piraha;
@@ -69,8 +70,9 @@ static void fail(Bracket *b,Matcher *m) {
}
bool Bracket::match(Matcher *m) {
- if(m->pos >= (int)m->input_size)
+ if(m->pos >= (int)m->input_size) {
return false;
+ }
for(range_iter r = ranges.begin();r != ranges.end(); ++r) {
if((*r)->match(m)) {
if(neg) {
@@ -93,24 +95,35 @@ bool Bracket::match(Matcher *m) {
}
}
-std::ostream& piraha::operator<<(std::ostream& o,Bracket& b) {
- for(range_iter r = b.ranges.begin();r != b.ranges.end(); ++r) {
+void insertc(std::ostream& o,char c) {
+ if(c == '-') {
+ o << "\\-";
+ } else if(c == '\n') {
+ o << "\\n";
+ } else if(c == '\r') {
+ o << "\\r";
+ } else if(c == '\t') {
+ o << "\\t";
+ } else if(strchr("[]-",c)>=0) {
+ o << "\\" << c;
+ } else {
+ o << c;
+ }
+}
+
+void Bracket::insert(std::ostream& o) {
+ o << "[";
+ if(neg)
+ o << "^";
+ for(range_iter r = ranges.begin();r != ranges.end(); ++r) {
char lo = (*r)->lo, hi = (*r)->hi;
if(lo == hi) {
- if(lo == '-') {
- o << "\\-";
- } else if(lo == '\n') {
- o << "\\n";
- } else if(lo == '\r') {
- o << "\\r";
- } else if(lo == '\t') {
- o << "\\t";
- } else {
- o << lo;
- }
+ insertc(o,lo);
} else {
- o << lo << '-' << hi;
+ insertc(o,lo);
+ o << '-';
+ insertc(o,hi);
}
}
- return o;
+ o << "]";
}
diff --git a/src/piraha/Call.cc b/src/piraha/Call.cc
index 6cc48528..3f507b93 100644
--- a/src/piraha/Call.cc
+++ b/src/piraha/Call.cc
@@ -713,6 +713,7 @@ void check_types(const char *thorn,int line,ValueType v,int t) {
extern "C" int cctk_PirahaParser(const char *buffer,unsigned long buffersize,int (*set_function)(const char *, const char *, int)) {
const char *par_file_src =
"skipper = ([ \\t\\r\\n]|\\#.*)*\n"
+ "# comment\n"
"skipeol = ([ \\t\\r]|\\#.*)*($|\\n)\n"
"any = [^]\n"
"stringcomment = #.*\n"
@@ -722,7 +723,7 @@ extern "C" int cctk_PirahaParser(const char *buffer,unsigned long buffersize,int
"# 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"
+ "dname = [0-9][a-zA-Z_]{2,}\n"
"inquot = ({var}|\\\\.|[^\\\\\"])*\n"
"fname = \\.?/[-\\./0-9a-zA-Z_]+\n"
"quot = \"{inquot}\"|{fname}\n"
diff --git a/src/piraha/Literal.cc b/src/piraha/Literal.cc
index f19f7455..62678266 100644
--- a/src/piraha/Literal.cc
+++ b/src/piraha/Literal.cc
@@ -3,8 +3,9 @@
using namespace piraha;
bool Literal::match(Matcher *m) {
- if(m->pos - m->input_size >= 0)
+ if(m->pos - m->input_size >= 0) {
return false;
+ }
if(m->input[m->pos] == c) {
m->max_pos = std::max(m->pos,m->max_pos);
m->pos++;
diff --git a/src/piraha/Matcher.cc b/src/piraha/Matcher.cc
index 55f042be..dd189042 100644
--- a/src/piraha/Matcher.cc
+++ b/src/piraha/Matcher.cc
@@ -55,8 +55,9 @@ void Matcher::showError() {
for(int i=0;i<input_size;i++) {
if(i == max_pos) {
error_pos = i;
+ int column = i - start_of_previous_line[0]+1;
std::cout << "In rule '" << inrule_max
- << "' Line=" << line << std::endl;
+ << "' Line=" << line << ", Column=" << column << std::endl;
while(input[i] == '\n'||input[i] == '\r') {
line++;
for(int j=1;j<num_previous_lines;j++) {
@@ -76,11 +77,15 @@ void Matcher::showError() {
start_of_line = i+1;
}
}
+ bool eol = false;
for(int i=start_of_previous_line[0];i<input_size;i++) {
std::cout << input[i];
- if(i > error_pos && input[i] == '\n')
+ if(i > error_pos && input[i] == '\n') {
+ eol = true;
break;
+ }
}
+ if(!eol) std::cout << std::endl;
for(int i=start_of_line;i<=error_pos;i++)
std::cout << ' ';
std::cout << "^" << std::endl;
diff --git a/src/piraha/Multi.cc b/src/piraha/Multi.cc
index 4d718d59..d0733618 100644
--- a/src/piraha/Multi.cc
+++ b/src/piraha/Multi.cc
@@ -6,14 +6,22 @@ using namespace piraha;
bool Multi::match(Matcher *m) {
unsigned int save;
int iter = 0;
+ vector<smart_ptr<Group> > chSave;
while(true) {
save = m->pos;
- if(!pattern->match(m))
+ chSave = m->children;
+ if(!pattern->match(m)) {
+ m->children = chSave;
+ m->pos = save;
break;
- if(save-m->pos == 0)
+ }
+ if(save - m->pos == 0) {
+ std::cout << "ZERO ADVANCE IN MULTI!" << std::endl;
return minv <= iter && iter <= maxv;
+ }
iter++;
+ if(iter == maxv)
+ break;
}
- m->pos = save;
return minv <= iter && iter <= maxv;
}
diff --git a/src/piraha/Piraha.hpp b/src/piraha/Piraha.hpp
index 3addbc83..4308b1fc 100644
--- a/src/piraha/Piraha.hpp
+++ b/src/piraha/Piraha.hpp
@@ -76,8 +76,14 @@ public:
Pattern() {}
virtual ~Pattern() {}
virtual std::string fmt() { return "blank"; }
+ virtual void insert(std::ostream& o) { o << "{?}"; }
};
+inline std::ostream& operator<<(std::ostream& o,Pattern& p) {
+ p.insert(o);
+ return o;
+}
+
class JMap {
map<std::string,smart_ptr<Pattern> > m;
public:
@@ -127,6 +133,10 @@ public:
Seq(vector<smart_ptr<Pattern> > patterns,bool ign,bool show);
virtual ~Seq() {}
bool match(Matcher *m);
+ virtual void insert(std::ostream& o) {
+ for(int i=0;i<patterns.size();i++)
+ o << *patterns[i];
+ }
};
class Or : public Pattern {
@@ -137,6 +147,14 @@ public:
Or(Pattern *p,...);
virtual ~Or() {}
bool match(Matcher *m);
+ virtual void insert(std::ostream& o) {
+ o << "(";
+ for(int i=0;i<patterns.size();i++) {
+ if(i > 0) o << "|";
+ o << *patterns[i];
+ }
+ o << ")";
+ }
};
class Literal : public Pattern {
@@ -149,6 +167,24 @@ public:
s += c+")";
return s;
}
+ virtual void insert(std::ostream& o) {
+ if(c == '\n')
+ o << "\\n";
+ else if(c == '\r')
+ o << "\\r";
+ else if(c == '\t')
+ o << "\\t";
+ else if(c == '\b')
+ o << "\\b";
+ else if(c >= 'a' && c <= 'z')
+ o << c;
+ else if(c >= 'A' && c <= 'Z')
+ o << c;
+ else if(c >= '0' && c <= '9')
+ o << c;
+ else
+ o << "\\" << c;
+ }
};
class ILiteral : public Pattern {
@@ -164,6 +200,27 @@ public:
s += ")";
return s;
}
+ virtual void insert(std::ostream& o) {
+ char c = lc;
+ if(lc != uc)
+ o << "[" << lc << uc << "]";
+ else if(c == '\n')
+ o << "\\n";
+ else if(c == '\r')
+ o << "\\r";
+ else if(c == '\t')
+ o << "\\t";
+ else if(c == '\b')
+ o << "\\b";
+ else if(c >= 'a' && c <= 'z')
+ o << c;
+ else if(c >= 'A' && c <= 'Z')
+ o << c;
+ else if(c >= '0' && c <= '9')
+ o << c;
+ else
+ o << "\\" << c;
+ }
};
class Lookup : public Pattern {
@@ -177,6 +234,9 @@ public:
std::string fmt() {
return "Literal:"+name;
}
+ virtual void insert(std::ostream& o) {
+ o << "{" << name << "}";
+ }
};
class Nothing : public Pattern {
@@ -189,18 +249,21 @@ class Start : public Pattern {
public:
Start() {}
bool match(Matcher *m);
+ virtual void insert(std::ostream& o) { o << "^"; }
};
class End : public Pattern {
public:
End() {}
bool match(Matcher *m);
+ virtual void insert(std::ostream& o) { o << "$"; }
};
class Dot : public Pattern {
public:
Dot() {}
bool match(Matcher *m);
+ virtual void insert(std::ostream& o) { o << "."; }
};
class Multi : public Pattern {
@@ -211,6 +274,9 @@ public:
Multi(Pattern *p,int min_,int max_) : minv(min_), maxv(max_), pattern(p) {}
virtual ~Multi() {}
bool match(Matcher *m);
+ virtual void insert(std::ostream& o) {
+ o << *pattern << "{" << minv << "," << maxv << "}";
+ }
};
class Range : public Pattern {
@@ -230,8 +296,8 @@ public:
Bracket *addRange(char lo,char hi);
Bracket *addRange(char lo,char hi,bool ign);
bool match(Matcher *m);
+ virtual void insert(std::ostream& o);
};
-extern std::ostream& operator<<(std::ostream&,Bracket&);
class NegLookAhead : public Pattern {
public: