Skip to content

Commit

Permalink
Support for extrapolating expected input in
Browse files Browse the repository at this point in the history
the case of match failure.



git-svn-id: https://piraha-peg.googlecode.com/svn/trunk@85 1a70d7c6-ffe7-ef14-4583-3269c1a2e543
  • Loading branch information
[email protected] committed Oct 12, 2012
1 parent 20eca28 commit 4268cde
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/edu/lsu/cct/piraha/Bracket.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.lsu.cct.piraha;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

Expand Down Expand Up @@ -69,11 +70,17 @@ public boolean match(Matcher m) {
if(range.lo <= c && c <= range.hi) {
if(!neg)
m.incrTextPos(1);
if(!neg^true) {
m.expected(new Expected(this));
}
return neg^true;
}
}
if(neg)
m.incrTextPos(1);
if(!neg^false) {
m.expected(new Expected(this));
}
return neg^false;
}

Expand Down Expand Up @@ -228,4 +235,11 @@ public static void main(String[] args) {
public int compareTo(Bracket b) {
return hashCode() - b.hashCode();
}

@Override
public List<String> expected(int n) {
List<String> ex = new ArrayList<String>();
ex.add(decompile());
return ex;
}
}
55 changes: 55 additions & 0 deletions src/edu/lsu/cct/piraha/Expected.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package edu.lsu.cct.piraha;

import java.util.HashSet;
import java.util.Set;

public class Expected {
Seq seq;
int n;
Pattern pat;
int pos;
Set<String> possibilities = new HashSet<String>();
int epos;
Or or;

void build(int maxTextPos) {
pos = maxTextPos;
if(pat != null) {
possibilities.add(pat.decompile());
} else if(seq != null) {
StringBuffer sb = new StringBuffer();
for(int j=n;j<seq.patternList.size();j++) {
Pattern x = seq.patternList.get(j);
if(x instanceof Literal)
sb.append(x.decompile());
else if(x instanceof ILiteral)
sb.append(x.decompile());
else if(x instanceof Bracket)
sb.append(x.decompile());
else
break;
}
if(sb.length()>0)
possibilities.add(sb.toString());
}
}

public Expected() {
}

public Expected(Pattern pat) {
this.pat = pat;
}

public Expected(Seq seq,int n) {
this.seq = seq;
this.n = n;
}

public Expected(Or or,int pos) {
this.or = or;
this.epos = pos;
}

public String toString() { return possibilities.toString(); }
}
11 changes: 11 additions & 0 deletions src/edu/lsu/cct/piraha/ILiteral.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package edu.lsu.cct.piraha;

import java.util.ArrayList;
import java.util.List;

public class ILiteral extends Pattern {
char lch,uch;
public ILiteral(int val) {
Expand All @@ -16,6 +19,7 @@ public boolean match(Matcher m) {
return true;
}
}
m.expected(new Expected(this));
return false;
}
@Override
Expand All @@ -27,4 +31,11 @@ public boolean eq(Object obj) {
ILiteral ilit = (ILiteral)obj;
return ilit.lch == lch && ilit.uch == uch;
}

@Override
public List<String> expected(int n) {
List<String> ex = new ArrayList<String>();
ex.add(decompile());
return ex;
}
}
11 changes: 11 additions & 0 deletions src/edu/lsu/cct/piraha/Literal.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package edu.lsu.cct.piraha;

import java.util.ArrayList;
import java.util.List;

public class Literal extends Pattern {
final static String SPECIAL = "*{}[]+?()^$|.\\";
char ch;
Expand All @@ -13,6 +16,7 @@ public boolean match(Matcher m) {
m.incrTextPos(1);
return true;
} else {
m.expected(new Expected(this));
//System.out.println("false");
return false;
}
Expand All @@ -34,4 +38,11 @@ public boolean eq(Object obj) {
Literal lit = (Literal)obj;
return lit.ch == ch;
}

@Override
public List<String> expected(int n) {
List<String> ex = new ArrayList<String>();
ex.add(decompile());
return ex;
}
}
8 changes: 8 additions & 0 deletions src/edu/lsu/cct/piraha/Lookup.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package edu.lsu.cct.piraha;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


public class Lookup extends Pattern {
Expand Down Expand Up @@ -106,4 +108,10 @@ public boolean eq(Object obj) {
Lookup lh = (Lookup)obj;
return lookup.equals(lh.lookup) && capture == lh.capture;
}

@Override
public List<String> expected(int n) {
setup();
return pattern.expected(n);
}
}
12 changes: 12 additions & 0 deletions src/edu/lsu/cct/piraha/Matcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ public final void setTextPos(int newTextPos) {
if(newTextPos > maxTextPos) {
maxTextPos = newTextPos;
maxLookup = lookStack.toString();
expected = null;
}
textPos = newTextPos;
}
public final void incrTextPos(int incr) {
setTextPos(getTextPos()+incr);
}
public boolean match(int pos) {
expected = null;
textPos = pos;
maxTextPos = pos;
savedMatches = new Stack<LinkedList<Group>>();
Expand Down Expand Up @@ -90,6 +92,7 @@ public Near near() {
Near near = new Near();
near.text = text;
near.rule = maxLookup;
near.expected = expected;
for(int i=0;i<text.length() && i<maxTextPos;i++) {
if(text.charAt(i)=='\n') {
near.lineNum++;
Expand Down Expand Up @@ -169,4 +172,13 @@ public void addPackRat(PackRat pr,boolean b, int after,
}
pr.after = after;
}

Expected expected;
public void expected(Expected ex) {
if(textPos == maxTextPos || ex.epos == maxTextPos) {
ex.build(maxTextPos);
if(ex.possibilities.size()>0)
expected = ex;
}
}
}
15 changes: 13 additions & 2 deletions src/edu/lsu/cct/piraha/Near.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
package edu.lsu.cct.piraha;


public class Near {
String text,rule;
Expected expected;
int lineNum=1, posInLine, startOfLine, endOfLine;
Near() {}
public String toString() {
String line = text.substring(startOfLine,endOfLine);
String str = line.substring(0,posInLine)+"|"+line.substring(posInLine);
String base = "";
if(expected != null) {
if(expected.possibilities.size()==1) {
base = "Expected: "+expected+" ";
} else {
base = "Expected one of: "+expected+" ";
}
}
if(rule != null)
return ""+lineNum+" in {"+rule+"}: '"+str.trim()+"'";
return base+lineNum+" in {"+rule+"}: '"+str.trim()+"'";
else
return ""+lineNum+": '"+str.trim()+"'";
return base+lineNum+": '"+str.trim()+"'";
}
}
30 changes: 30 additions & 0 deletions src/edu/lsu/cct/piraha/Or.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public Or(boolean ignCase,boolean igcShow,Pattern...pats) {
public boolean match(Matcher m) {
int posSave = m.getTextPos();
int sz = m.subMatches.size();
List<Expected> expecteds = new ArrayList<Expected>();
for(int i=0;i<patterns.size();i++) {
m.setTextPos(posSave);
int nsz = m.subMatches.size();
Expand All @@ -38,7 +39,25 @@ public boolean match(Matcher m) {
boolean b = Matcher.matchAll(patterns.get(i),m);
if(b)
return true;
else
expecteds.add(m.expected);
}
int max = -1;
for(Expected e : expecteds) {
if(e.pos > max && e.possibilities.size()>0) {
max = e.pos;
}
}
Expected ex = new Expected();
ex.epos = max;
for(Expected e : expecteds) {
if(e.pos == max) {
for(String s : e.possibilities) {
ex.possibilities.add(s);
}
}
}
m.expected(ex);
// new version
// LinkedList<Group> save = m.subMatches;
// m.subMatches = new LinkedList<Group>();
Expand Down Expand Up @@ -108,4 +127,15 @@ public boolean eq(Object obj) {
}
return true;
}

@Override
public List<String> expected(int n) {
List<String> ex = new ArrayList<String>();
for(int i=0;i<patterns.size();i++) {
List<String> li = patterns.get(i).expected(0);
for(String s : li)
ex.add(s);
}
return ex;
}
}
5 changes: 5 additions & 0 deletions src/edu/lsu/cct/piraha/Pattern.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package edu.lsu.cct.piraha;

import java.util.ArrayList;
import java.util.List;

public abstract class Pattern {
public abstract boolean match(Matcher m);

Expand All @@ -19,4 +22,6 @@ public final boolean equals(Object obj) {
}
return false;
}

public List<String> expected(int n) { return new ArrayList<String>(); }
}
23 changes: 21 additions & 2 deletions src/edu/lsu/cct/piraha/Seq.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ public Seq(boolean ignCase,boolean igcShow,Pattern...patterns) {
@Override
public boolean match(Matcher m) {
//for(Pattern x = pattern;x != null;x = x.next) {
for(Pattern x : patternList) {
for(int i=0;i<patternList.size();i++) {
Pattern x = patternList.get(i);
boolean b = x.match(m);
if(!b)
if(!b) {
m.expected(new Expected(this,i));
return false;
}
}
return true;
}
Expand Down Expand Up @@ -71,4 +74,20 @@ public boolean eq(Object obj) {
}
return true;
}

@Override
public List<String> expected(int n) {
List<String> ex = new ArrayList<String>();
for(int i=n;i<patternList.size();i++) {
List<String> nex = new ArrayList<String>();
List<String> li = patternList.get(i).expected(0);
for(int j=0;j<li.size();j++) {
for(int k=0;k<ex.size();k++) {
nex.add(ex.get(k)+li.get(j));
}
}
ex = nex;
}
return ex;
}
}

0 comments on commit 4268cde

Please sign in to comment.