Skip to content

Commit

Permalink
Next version, plus new example code: Cpp
Browse files Browse the repository at this point in the history
git-svn-id: https://piraha-peg.googlecode.com/svn/trunk@19 1a70d7c6-ffe7-ef14-4583-3269c1a2e543
  • Loading branch information
steven.brandt committed Sep 22, 2010
1 parent 46a34f0 commit a4868fd
Show file tree
Hide file tree
Showing 10 changed files with 797 additions and 79 deletions.
31 changes: 10 additions & 21 deletions src/edu/lsu/cct/piraha/DebugOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,16 @@ public void print(Object o) {
newline=false;
}
String s = o.toString();
for(int i=0;i<s.length();i++) {
char c = s.charAt(i);
/*
if(c == '\n')
pw.print("\\n");
else if(c == '\r')
pw.print("\\r");
else if(c == '\t')
pw.print("\\t");
else if(c == '\b')
pw.print("\\b");
else if(c < 10 || c >= 128) {
String hex = Integer.toHexString(c);
while(hex.length() < 4)
hex = "0"+hex;
pw.printf("\\u"+hex);
} else
pw.print(c);
*/
pw.print(outc(c));
}
outs(s);
}
pw.flush();
}
public void outs(String s) {
for(int i=0;i<s.length();i++) {
char c = s.charAt(i);
pw.print(outc(c));
}
}
public void println(Object o) {
print(o);
println();
Expand All @@ -72,4 +58,7 @@ public void println() {
newline = true;
}
public final static DebugOutput out = new DebugOutput();
public void flush() {
pw.flush();
}
}
39 changes: 39 additions & 0 deletions src/edu/lsu/cct/piraha/Grammar.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -131,6 +134,42 @@ private void check() {
}
checked = true;
}

public List<String> extras(String pat) {
List<String> extraPatterns = new ArrayList<String>();
final Map<String,Boolean> visited = new HashMap<String,Boolean>();
visited.put(pat,Boolean.FALSE);
Visitor extraFinder = new Visitor() {
public void finishVisit(Pattern p) {
if(p instanceof Lookup) {
String name = ((Lookup)p).lookup;
if(!visited.containsKey(name))
visited.put(name,Boolean.FALSE);
}
}
};
while(true) {
boolean done = true;
Set<String> set = new HashSet<String>();
for(String p : visited.keySet()) {
if(visited.get(p) == Boolean.FALSE)
set.add(p);
}
for(String p : set) {
//System.out.println("visit "+p+" "+set);
visited.put(p, Boolean.TRUE);
patterns.get(p).visit(extraFinder);
done = false;
}
if(done)
break;
}
for(String p : patterns.keySet()) {
if(!visited.containsKey(p))
extraPatterns.add(p);
}
return extraPatterns;
}

public Matcher matcher(String patternName, String text) {
if(!checked)
Expand Down
7 changes: 2 additions & 5 deletions src/edu/lsu/cct/piraha/GrammarToXML.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ public static void main(String[] args) throws Exception {
if(!m.didMatch) {
throw new Exception("Syntax error near line: "+m.near());
}
out.println("<"+rule+">");
out.indent++;
Matcher.dumpMatches(m.text, m.subMatches, out);
out.indent--;
out.println("</"+rule+">");
m.dumpMatchesXML(out);
}
out.flush();
}
}
83 changes: 79 additions & 4 deletions src/edu/lsu/cct/piraha/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ public int groupCount() {
public Group group(int n) {
return subMatches.get(n);
}

Group() {}

public final static LinkedList<Group> emptyList = new LinkedList<Group>();

static public Group make(String lookup, String value) {
return new Group(lookup,0,value.length(),emptyList,value);
}

public Group(String lookup, int before, int after, LinkedList<Group> matches,String text) {
this.patternName = lookup;
this.begin = before;
Expand All @@ -39,9 +45,8 @@ public Group(String patternName, Group m) {
this.text = m.text;
}

@Override
public Object clone() {
return new Group(patternName,begin,end,(LinkedList<Group>)subMatches.clone(),text);
public Group group() {
return new Group(patternName,begin,end,subMatches,text);
}

public String substring(String text) {
Expand Down Expand Up @@ -79,4 +84,74 @@ public String getText() {
public String toString() {
return substring();
}
public void dumpMatchesXML() {
dumpMatchesXML(DebugOutput.out);
DebugOutput.out.pw.flush();
}
public void dumpMatchesXML(DebugOutput out) {
out.print("<");
out.print(getPatternName());
out.print(">");
if(groupCount()==0) {
out.print(xmltext(substring()));
} else {
out.println();
out.indent++;
try {
for(Group match : subMatches) {
match.dumpMatchesXML(out);
}
} finally {
out.indent--;
}
}
out.print("</");
out.print(getPatternName());
out.println(">");
}
public void dumpMatches() {
dumpMatches(DebugOutput.out);
DebugOutput.out.pw.flush();
}
public void dumpMatches(DebugOutput out) {
out.print(getPatternName());
if(groupCount()==0) {
out.print("=(");
out.outs(substring());
out.println(")");
} else {
out.println(":");
out.indent+=2;
try {
for(int i=0;i<groupCount();i++) {
Group match = group(i);
out.print("[");
out.print(i);
out.print("] ");
match.dumpMatches(out);
}
} finally {
out.indent-=2;
}
}
}
private static String xmltext(String str) {
StringBuffer sb = new StringBuffer();
for(int i=0;i<str.length();i++) {
char c = str.charAt(i);
if(c == '<')
sb.append("&lt;");
else if(c == '>')
sb.append("&gt;");
else if(c == '&')
sb.append("&amp;");
else if(c == '"')
sb.append("&quot;");
else if(c <= 13 || c > 127)
sb.append("&#"+(int)c+";");
else
sb.append(c);
}
return sb.toString();
}
}
48 changes: 1 addition & 47 deletions src/edu/lsu/cct/piraha/Matcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,56 +75,10 @@ static boolean matchAll(Pattern pattern, Matcher m) {
public Group group() {
return new Group(patternName,getBegin(),getEnd(),subMatches,text);
}

public void dumpMatches() {
dumpMatches(text,subMatches);
}

public String getText() {
return text;
}
void dumpMatches(String text, LinkedList<Group> matches) {
dumpMatches(text,matches,DebugOutput.out);
}
static void dumpMatches(String text,LinkedList<Group> matches,DebugOutput out) {
for(Group match : matches) {
dumpMatches(text, match, out);
}
}
static void dumpMatches(String text, Group match, DebugOutput out) {
//out.println(match.lookup+" ["+match.before+" to "+match.after+"] \""+text.substring(match.before,match.after)+"\"");
out.println("<"+match.getPatternName()+">");
out.indent++;
try {
if(match.groupCount()==0)
out.println("<contents>"+xmltext(text.substring(match.getBegin(),match.getEnd()))+"</contents>");
dumpMatches(text,match.subMatches,out);
} finally {
out.indent--;
out.println("</"+match.patternName+">");
}
}
private static String xmltext(String str) {
StringBuffer sb = new StringBuffer();
for(int i=0;i<str.length();i++) {
char c = str.charAt(i);
if(c == '<')
sb.append("&lt;");
else if(c == '>')
sb.append("&gt;");
else if(c == '&')
sb.append("&amp;");
else if(c == '"')
sb.append("&quot;");
else if(c <= 13 || c > 127)
sb.append("&#"+(int)c+";");
else
sb.append(c);
}
return sb.toString();
}
static void dumpMatches(String text, Group match) {
dumpMatches(text,match,DebugOutput.out);
}

public static class Near {
String text,rule;
Expand Down
2 changes: 1 addition & 1 deletion src/edu/lsu/cct/piraha/examples/Calc.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void main(String[] args) {
boolean b = m.matches();
System.out.println("match? "+b);
if(b) {
m.dumpMatches();
m.dumpMatchesXML();
System.out.println("node count="+count(m));
System.out.println("eval="+evalExpr(m));
}
Expand Down
Loading

0 comments on commit a4868fd

Please sign in to comment.