Skip to content

Commit

Permalink
Simplify the code base. In part, so that it's easier to parse and tra…
Browse files Browse the repository at this point in the history
…nsform.

Also modifies the formatting capabilities of groups, including perl output.



git-svn-id: https://piraha-peg.googlecode.com/svn/trunk@54 1a70d7c6-ffe7-ef14-4583-3269c1a2e543
  • Loading branch information
[email protected] committed Mar 2, 2012
1 parent f44407e commit 9d6fa91
Show file tree
Hide file tree
Showing 14 changed files with 353 additions and 211 deletions.
15 changes: 10 additions & 5 deletions src/edu/lsu/cct/piraha/CheckVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,25 @@ Boolean getDefault(String key) {
@Override
public Visitor startVisit(Pattern p) {
if(p instanceof Multi) {
CheckVisitor cv = child = new CheckVisitor(patterns);
CheckVisitor cv = new CheckVisitor(patterns);
child = cv;
cv.checking = checking;
return cv;
} else if(p instanceof Or) {
CheckVisitor cv = child = new CheckVisitor(patterns);
CheckVisitor cv = new CheckVisitor(patterns);
child = cv;
cv.checking = checking;
return cv;
} else if(p instanceof Seq) {
CheckVisitor cv = child = new CheckVisitor(patterns);
CheckVisitor cv = new CheckVisitor(patterns);
child = cv;
cv.checking = checking;
return cv;
} else if(p instanceof Lookup) {
CheckVisitor cv = child = new CheckVisitor(patterns);
cv.checking = ((Lookup)p).lookup;
CheckVisitor cv = new CheckVisitor(patterns);
child = cv;
Lookup ll = (Lookup)p;
cv.checking = ll.lookup;
return cv;
}
return this;
Expand Down
12 changes: 8 additions & 4 deletions src/edu/lsu/cct/piraha/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public Pattern getPattern(boolean ignCase,boolean igcShow) {
this.i = i;
compile((char)0,false,false);
}
void compile(char term,final boolean ignCase,final boolean igcShow) {
void compile(char term,boolean ignCase,boolean igcShow) {
this.grammar.checked = false;
while(next(term,ignCase,igcShow))
;
}
boolean next(char term,final boolean ignCase,final boolean igcShow) {
boolean next(char term,boolean ignCase,boolean igcShow) {
if(i >= pattern.length()) {
if(term != 0)
throw new ParseException(""+term+" not found in "+patternName+":"+pattern);
Expand Down Expand Up @@ -90,8 +90,8 @@ else if(cfinal >= '1' && cfinal <= '9')
multi.pattern = patternList.remove(patternList.size()-1);
patternList.add(multi);
} else if(num && comma && !other) {
int n1 = sb1.length()==0 ? 0 : Integer.parseInt(sb1.toString());
int n2 = sb2.length()==0 ? Integer.MAX_VALUE : Integer.parseInt(sb2.toString());
int n1 = parseInt(sb1,0);//sb1.length()==0 ? 0 : Integer.parseInt(sb1.toString());
int n2 = parseInt(sb2,Integer.MAX_VALUE);//sb2.length()==0 ? Integer.MAX_VALUE : Integer.parseInt(sb2.toString());
if(n1 > n2)
throw new ParseException(""+n1+" > "+n2+" in pattern "+patternName);
Multi multi = new Multi(n1,n2);
Expand Down Expand Up @@ -234,6 +234,10 @@ else if(negLookAhead)
}
return true;
}
int parseInt(StringBuffer sb1,int def) {
if(sb1.length()==0) return def;
else return Integer.parseInt(sb1.toString());
}
public Pattern mkSeq(List<Pattern> patternList,boolean ignCase,boolean igcShow) {
int n = patternList.size();
if(n == 0)
Expand Down
15 changes: 10 additions & 5 deletions src/edu/lsu/cct/piraha/DebugVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ public Visitor startVisit(Pattern p) {
out.print(": ");
out.indent += 2;
if(p instanceof Literal) {
printChar(((Literal)p).ch);
Literal ll = (Literal)p;
printChar(ll.ch);
} else if(p instanceof ILiteral) {
printChar(((ILiteral)p).lch);
ILiteral il = (ILiteral)p;
printChar(il.lch);
} else if(p instanceof Name) {
out.print(((Name)p).name);
Name nm = (Name)p;
out.print(nm.name);
} else if(p instanceof Range) {
Range r = (Range)p;
if(r.lo == r.hi)
Expand All @@ -47,9 +50,11 @@ public Visitor startVisit(Pattern p) {
else
out.print(m.min+" to "+m.max);
} else if(p instanceof Lookup) {
out.print(((Lookup)p).lookup);
Lookup lo = (Lookup)p;
out.print(lo.lookup);
} else if(p instanceof Bracket) {
out.print("neg="+((Bracket)p).neg);
Bracket br = (Bracket)p;
out.print("neg="+br.neg);
}
out.println();
return this;
Expand Down
20 changes: 20 additions & 0 deletions src/edu/lsu/cct/piraha/ExtraVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package edu.lsu.cct.piraha;

import java.util.Map;

class ExtraVisitor extends Visitor {
final Map<String, Boolean> visited;

ExtraVisitor(Map<String, Boolean> visited) {
this.visited = visited;
}

public void finishVisit(Pattern p) {
if(p instanceof Lookup) {
Lookup ll = (Lookup)p;
String name = ll.lookup;
if(!visited.containsKey(name))
visited.put(name,Boolean.FALSE);
}
}
}
162 changes: 162 additions & 0 deletions src/edu/lsu/cct/piraha/FindPart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package edu.lsu.cct.piraha;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

class FindPart {
static int nextId = 1;
private static int getNextId() {
return nextId++;
}
final int id = getNextId();
boolean optional = false, star = false, stitch = true, capture = true;

@Override
public boolean equals(Object o) {
if(o instanceof FindPart) {
FindPart fb = (FindPart)o;
return id == fb.id;
}
return false;
}
@Override
public int hashCode() {
return id;
}

static void buildFinder(Group g,int index,List<FindPart> fl) {
Group sub = g.group(index);
String pn = sub.getPatternName();
if(pn.equals("alt")) {
FindPart fb = new FindPart();
for(int i=0;i<sub.groupCount();i++) {
buildFinder(sub,i,fb.next);
}
if(fb.next.size()==1)
fl.add(fb.next.get(0));
else
fl.add(fb);
} else if(pn.equals("sub")) {
if(sub.groupCount()==2) {
String quant = sub.group(1).substring();
if(quant.equals("*")) {
FindPart fb = new FindPart();
fb.star = true;
fl.add(fb);
buildFinder(sub,0,fb.follow);
} else if(quant.equals("?")){
FindPart fb = new FindPart();
fb.optional = true;
fl.add(fb);
buildFinder(sub,0,fb.follow);
} else {
throw new Error(quant);
}
} else {
buildFinder(sub,0,fl);
}
} else if(pn.equals("seq")||pn.equals("pat")||pn.equals("subseq")) {
FindPart fb = new FindPart();
fl.add(fb);
for(int i=0;i<sub.groupCount();i++) {
buildFinder(sub,i,fb.follow);
}
} else if(pn.equals("elem")) {
FindPart fb = new FindPart();
fb.elem = g.group(index);
fl.add(fb);
} else {
throw new Error(pn);
}
}

List<FindPart> next = new ArrayList<FindPart>();
List<FindPart> follow = new ArrayList<FindPart>();
Group elem;
public void find(Group input,Set<Group> results) {
if(elem != null) {
String pattern = elem.group(0).substring();
if(elem.groupCount()>1) {
System.out.println("searching:");
elem.dumpMatches();
}
if(Finder.wildMatch(pattern,input.getPatternName())) {
if(elem.groupCount()==2 && elem.group(1).getPatternName().equals("eq")) {
if(elem.group(2).getPatternName().equals("squote")) {
if(!elem.group(2).substring().equals(input.substring()))
return;
}
}
Iterator<FindPart> iter = next.iterator();
while(iter.hasNext()) {
FindPart fb = iter.next();
for(int j=0;j<input.groupCount();j++) {
fb.find(input.group(j),results);
}
}
if(next.size()==0 && capture) {
results.add(input);
}
}
} else if(next.size()==0) {
return;
} else {
//for(FindPart fb : next) {
Iterator<FindPart> iter = next.iterator();
while(iter.hasNext()) {
FindPart fb = iter.next();
fb.find(input,results);
}
}
}

public String toString() {
StringBuffer sb = new StringBuffer();
toString(new HashSet<FindPart>(),sb);
return sb.toString();
}

private void toString(HashSet<FindPart> used,StringBuffer sb) {
sb.append('$');
sb.append(id);
if(optional)
sb.append('~');
sb.append(':');
if(elem != null) {
sb.append(".");
sb.append(elem.substring());
}
if(used.contains(this)) {
sb.append('@');
return;
}
used.add(this);
if(next.size()==1) {
sb.append("->");
next.get(0).toString(used,sb);
} else if(next.size()>1) {
sb.append("a(");
next.get(0).toString(used,sb);
for(int i=1;i<next.size();i++) {
sb.append("|");
next.get(i).toString(used, sb);
}
sb.append(")");
} else if(elem == null && follow.size()==0) {
sb.append(".<EMPTY>");
}
if(follow.size()>0) {
sb.append("f[");
for(int i=0;i<follow.size();i++) {
if(i>0) {
sb.append(", ");
}
follow.get(i).toString(used,sb);
}
sb.append(']');
}
}
}
Loading

0 comments on commit 9d6fa91

Please sign in to comment.