Skip to content

Commit

Permalink
The break pattern is now {brk}
Browse files Browse the repository at this point in the history
An error is captured if a non-existent pattern is given to the Matcher.

XML output now includes start and end positions within the text.



git-svn-id: https://piraha-peg.googlecode.com/svn/trunk@46 1a70d7c6-ffe7-ef14-4583-3269c1a2e543
  • Loading branch information
[email protected] committed Jan 25, 2012
1 parent 9cf70d2 commit 718bd95
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/edu/lsu/cct/piraha/Break.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public boolean match(Matcher m) {

@Override
public String decompile() {
return "{!}";
return "{brk}";
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/edu/lsu/cct/piraha/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ else if(cfinal >= '1' && cfinal <= '9')
s += ',' + sb2.toString();
if(s.startsWith("$"))
patternList.add(new Name(s.substring(1)));
else if(s.equals("!"))
else if(s.equals("brk"))
patternList.add(new Break());
else
patternList.add(new Lookup(s,this.grammar));
Expand Down Expand Up @@ -300,4 +300,4 @@ else if(d >= 'A' && d <= 'F')
}
return cfinal;
}
}
}
6 changes: 4 additions & 2 deletions src/edu/lsu/cct/piraha/Grammar.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
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;
Expand All @@ -18,8 +17,9 @@ public class Grammar {
Map<String,Pattern> patterns = new HashMap<String,Pattern>();
Map<String,Grammar> grammars = null;
public void importGrammar(String name,Grammar g) {
if(grammars == null)
if(grammars == null) {
grammars = new HashMap<String,Grammar>();
}
if(grammars.containsKey(name)) {
throw new ParseException("Grammar '"+name+"' is already defined");
}
Expand Down Expand Up @@ -177,6 +177,8 @@ public Matcher matcher(String patternName, String text) {
Matcher m = new Matcher();
m.text = text;
m.pattern = patterns.get(patternName);
if(m.pattern == null)
throw new ParseException("No such pattern: '"+patternName+"'");
m.patternName = patternName;
return m;
}
Expand Down
6 changes: 5 additions & 1 deletion src/edu/lsu/cct/piraha/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ public void dumpMatchesXML() {
public void dumpMatchesXML(DebugOutput out) {
out.print("<");
out.print(getPatternName());
out.print(">");
out.print(" startIndex='");
out.print(begin);
out.print("' endIndex='");
out.print(end);
out.print("'>");
if(groupCount()==0) {
out.print(xmltext(substring()));
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/edu/lsu/cct/piraha/Matcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,4 @@ public static void main(String[] args) {
}
}
*/
}
}
4 changes: 2 additions & 2 deletions src/edu/lsu/cct/piraha/examples/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ public static void main(String[] args) throws Exception {
test("[-a]+","a-a-",4);
test("(\\[(\\\\[^]|[^\\]\\\\])*\\]|\\\\[^]|[^ \t\r\n\b])+","xxx",3);
test("[a-zA-Z0-9\\.\\*]+|\"[^\"]*\"|'[^']*'","\"Failed password\"",17);
test("(b{!}|.)*","aaabaaa",4);
//test("[^ \t\r\n\b]+","abc",3);
test("(b{brk}|.)*","aaabaaa",4);
test("[^ \t\r\n\b]+","abc",3);

g = new Grammar();
g.compile("import", "import");
Expand Down

0 comments on commit 718bd95

Please sign in to comment.