-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git-svn-id: https://piraha-peg.googlecode.com/svn/trunk@2 1a70d7c6-ffe7-ef14-4583-3269c1a2e543
- Loading branch information
steven.brandt
committed
Aug 31, 2010
1 parent
8245eb7
commit 4a71b49
Showing
33 changed files
with
2,164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package edu.lsu.cct.piraha; | ||
|
||
public class BackRef extends Pattern { | ||
final int num; | ||
final boolean ignCase; | ||
|
||
public BackRef(int num,boolean ignCase) { | ||
this.num = num-1; | ||
this.ignCase = ignCase; | ||
} | ||
|
||
@Override | ||
public boolean match(Matcher m) { | ||
//System.out.println("num="+num+" mc="+m.matchCount()); | ||
if(num >= m.groupCount()) | ||
return false; | ||
Group backRef = m.getMatch(num); | ||
int begin = backRef.getBegin(); | ||
int end = backRef.getEnd(); | ||
int n = end-begin; | ||
int pos = m.getTextPos(); | ||
if(pos + n > m.text.length()) | ||
return false; | ||
if (ignCase) { | ||
for (int i = 0; i < n; i++) { | ||
int nn = pos + i; | ||
char c1 = Character.toLowerCase(m.text.charAt(i+begin)); | ||
char c2 = Character.toLowerCase(m.text.charAt(nn)); | ||
//System.out.println("ign: "+c1+" <=> "+c2); | ||
if (c1 != c2) { | ||
return false; | ||
} | ||
} | ||
} else { | ||
for (int i = 0; i < n; i++) { | ||
int nn = pos + i; | ||
char c1 = m.text.charAt(i+begin); | ||
char c2 = m.text.charAt(nn); | ||
//System.out.println("!ign: "+c1+" <=> "+c2); | ||
if (c1 != c2) { | ||
return false; | ||
} | ||
} | ||
} | ||
m.setTextPos(pos+n); | ||
return true; | ||
} | ||
|
||
@Override | ||
public String decompile() { | ||
return "\\"+num; | ||
} | ||
|
||
@Override | ||
public boolean eq(Object obj) { | ||
BackRef ref = (BackRef)obj; | ||
return ref.num == num; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package edu.lsu.cct.piraha; | ||
|
||
public class Boundary extends Pattern { | ||
|
||
@Override | ||
public boolean match(Matcher m) { | ||
int n = m.getTextPos(); | ||
if(n==0 || n==m.text.length()) { | ||
return true; | ||
} | ||
if(!identc(m.text.charAt(n)) || !identc(m.text.charAt(n-1))) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private boolean identc(char c) { | ||
if(c >= 'a' && c <= 'z') | ||
return true; | ||
if(c >= 'A' && c <= 'Z') | ||
return true; | ||
if(c >= '0' && c <= '9') | ||
return true; | ||
return c == '_'; | ||
} | ||
|
||
@Override | ||
public String decompile() { | ||
return "\\b"; | ||
} | ||
|
||
@Override | ||
public boolean eq(Object obj) { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package edu.lsu.cct.piraha; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
public class Bracket extends Pattern { | ||
boolean neg; | ||
List<Range> ranges = new LinkedList<Range>(); | ||
|
||
public void addRange(char lo,char hi) { | ||
if(lo > hi) throw new ParseException("Invalid range "+lo+" > "+hi); | ||
ranges.add(new Range(lo,hi)); | ||
} | ||
|
||
public void addChar(char v) { | ||
ranges.add(new Range(v,v)); | ||
} | ||
|
||
@Override | ||
public boolean match(Matcher m) { | ||
if(m.getTextPos() >= m.text.length()) | ||
return false; | ||
char c = m.text.charAt(m.getTextPos()); | ||
for(Range range : ranges) { | ||
if(range.lo <= c && c <= range.hi) { | ||
if(!neg) | ||
m.incrTextPos(1); | ||
return neg^true; | ||
} | ||
} | ||
if(neg) | ||
m.incrTextPos(1); | ||
return neg^false; | ||
} | ||
|
||
@Override public void visit(Visitor v) { | ||
Visitor vv = v.startVisit(this); | ||
for(Range range : ranges) { | ||
range.visit(vv); | ||
} | ||
v.finishVisit(this); | ||
} | ||
|
||
@Override | ||
public String decompile() { | ||
StringBuffer sb = new StringBuffer(); | ||
sb.append("["); | ||
if(neg) | ||
sb.append("^"); | ||
for(Range range : ranges) { | ||
if(range.lo != range.hi) { | ||
outc(sb,range.lo); | ||
sb.append("-"); | ||
outc(sb,range.hi); | ||
} else { | ||
outc(sb,range.lo); | ||
} | ||
} | ||
sb.append("]"); | ||
return sb.toString(); | ||
} | ||
|
||
private void outc(StringBuffer sb, char c) { | ||
if(c == '-' || c == ']') { | ||
sb.append("\\"); | ||
} | ||
sb.append(Literal.outc(c)); | ||
} | ||
|
||
@Override | ||
public boolean eq(Object obj) { | ||
Bracket b = (Bracket)obj; | ||
if(ranges.size() != b.ranges.size()) | ||
return false; | ||
for(int i=0;i<ranges.size();i++) { | ||
Range brange = b.ranges.get(i); | ||
Range range = ranges.get(i); | ||
if(brange.lo != range.lo) | ||
return false; | ||
if(brange.hi != range.hi) | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package edu.lsu.cct.piraha; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class CheckVisitor extends Visitor { | ||
Map<String,Boolean> patterns; | ||
CheckVisitor child = null; | ||
List<Boolean> canBeZero = new ArrayList<Boolean>(); | ||
String checking; | ||
boolean retry = true; | ||
public Map<String, Boolean> defaults; | ||
|
||
public CheckVisitor() { | ||
patterns = new HashMap<String,Boolean>(); | ||
} | ||
private CheckVisitor(Map<String,Boolean> patterns) { | ||
this.patterns = patterns; | ||
} | ||
|
||
Boolean getDefault(String key) { | ||
if(defaults != null && defaults.containsKey(key)) { | ||
return defaults.get(key); | ||
} | ||
return Boolean.FALSE; | ||
} | ||
|
||
@Override | ||
public Visitor startVisit(Pattern p) { | ||
if(p instanceof Multi) { | ||
CheckVisitor cv = child = new CheckVisitor(patterns); | ||
cv.checking = checking; | ||
return cv; | ||
} else if(p instanceof Or) { | ||
CheckVisitor cv = child = new CheckVisitor(patterns); | ||
cv.checking = checking; | ||
return cv; | ||
} else if(p instanceof Seq) { | ||
CheckVisitor cv = child = new CheckVisitor(patterns); | ||
cv.checking = checking; | ||
return cv; | ||
} else if(p instanceof Lookup) { | ||
CheckVisitor cv = child = new CheckVisitor(patterns); | ||
cv.checking = ((Lookup)p).lookup; | ||
return cv; | ||
} | ||
return this; | ||
} | ||
|
||
private boolean andZero() { | ||
for(Boolean b : child.canBeZero) { | ||
if(!b) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private boolean orZero() { | ||
for(Boolean b : child.canBeZero) { | ||
if(b) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public void finishVisit(Pattern p) { | ||
if(p instanceof Multi) { | ||
Multi m = (Multi)p; | ||
if(m.max > 1 && andZero()) { | ||
System.out.println(child.canBeZero); | ||
System.out.println(patterns); | ||
//p.visit(new DebugVisitor()); | ||
throw new ParseException(checking+ | ||
": cannot have zero length pattern in quantifier: "+p.decompile()); | ||
} | ||
if(m.min==0) | ||
canBeZero.add(Boolean.TRUE); | ||
else | ||
canBeZero.add(andZero()); | ||
} else if(p instanceof Nothing) { | ||
canBeZero.add(Boolean.TRUE); | ||
} else if(p instanceof LookAhead) { | ||
canBeZero.add(Boolean.TRUE); | ||
} else if(p instanceof NegLookAhead) { | ||
canBeZero.add(Boolean.TRUE); | ||
} else if(p instanceof End) { | ||
canBeZero.add(Boolean.TRUE); | ||
} else if(p instanceof Start) { | ||
canBeZero.add(Boolean.TRUE); | ||
} else if(p instanceof Seq) { | ||
//p.visit(new DebugVisitor()); | ||
canBeZero.add(andZero()); | ||
} else if(p instanceof Or) { | ||
canBeZero.add(orZero()); | ||
} else if(p instanceof Lookup) { | ||
Lookup l = (Lookup)p; | ||
if(patterns.containsKey(l.lookup)) { | ||
canBeZero.add(patterns.get(l.lookup)); | ||
} else { | ||
Boolean defaultValue = getDefault(l.lookup); | ||
patterns.put(l.lookup,defaultValue); | ||
l.pattern.visit(child); | ||
boolean res = andZero(); | ||
if(res) { | ||
patterns.put(l.lookup,Boolean.TRUE); | ||
} | ||
if(defaultValue ^ res) | ||
retry = true; | ||
canBeZero.add(res); | ||
} | ||
} else { | ||
canBeZero.add(Boolean.FALSE); | ||
} | ||
} | ||
} |
Oops, something went wrong.