corrected the reporting+alignment of error locations
authoradam <adam@megacz.com>
Sun, 23 Jul 2006 06:09:06 +0000 (02:09 -0400)
committeradam <adam@megacz.com>
Sun, 23 Jul 2006 06:09:06 +0000 (02:09 -0400)
darcs-hash:20060723060906-5007d-e29b6a87fbc8628f05aef45d82a15b7aae9152be.gz

TODO
src/edu/berkeley/sbp/Ambiguous.java
src/edu/berkeley/sbp/Forest.java
src/edu/berkeley/sbp/GSS.java
src/edu/berkeley/sbp/Parser.java
src/edu/berkeley/sbp/chr/CharInput.java
src/edu/berkeley/sbp/misc/Cartesian.java
src/edu/berkeley/sbp/misc/Demo2.java

diff --git a/TODO b/TODO
index 9362404..4495d30 100644 (file)
--- a/TODO
+++ b/TODO
@@ -7,6 +7,7 @@ Immediately
   - Annotation Tutorial
 
   - MUST HAVE BETTER ERROR MESSAGES
+     - when ambiguity encountered, show text region (where-to-where)
      - use for developing java15.g
 
   - java15.g
@@ -21,6 +22,8 @@ Immediately
 ______________________________________________________________________________
 v1.1
 
+  - broader regression testing (for stuff like error messages, etc)
+
   - More topology untangling [later]
   - tib: use the lexer only for indentation increases/decreases
   - grammar highlighting?
index 1acd8cd..6138577 100644 (file)
@@ -19,7 +19,7 @@ public class Ambiguous extends Exception {
     public String toString() {
         // FIXME: print the input region that was ambiguously matched
         StringBuffer sb = new StringBuffer();
-        sb.append("unresolved ambiguity; shared subtrees are shown as \"*\" ");
+        sb.append("unresolved ambiguity at "+ambiguity.getRegion()+"; shared subtrees are shown as \"*\" ");
         for(Tree<?> result : ht) {
             sb.append("\n  possibility: ");
             StringBuffer sb2 = new StringBuffer();
index 4f79bb6..ee3bfbc 100644 (file)
@@ -34,7 +34,8 @@ public abstract class Forest<NodeType> implements GraphViz.ToGraphViz {
     abstract void gather(HashSet<Forest<NodeType>> ignore);
     abstract void edges(GraphViz.Node n);
     boolean ambiguous() { return false; }
-
+    
+    abstract Input.Region getRegion();
 
     // One //////////////////////////////////////////////////////////////////////////////
 
@@ -48,6 +49,8 @@ public abstract class Forest<NodeType> implements GraphViz.ToGraphViz {
         /** if true, the last child's children are considered children of this node */
         private final boolean           lift;
 
+        Input.Region getRegion() { return location; }
+
         private One(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift) {
             this.location = loc;
             this.head = head;
@@ -129,6 +132,8 @@ public abstract class Forest<NodeType> implements GraphViz.ToGraphViz {
 
         public Many() { }
 
+        Input.Region getRegion() { return hp.iterator().next().getRegion(); } // all should be identical
+
         public Tree<NodeType> expand1() throws Ambiguous {
             touched();
             if (hp.size() > 1) {
index 816c086..5ede971 100644 (file)
@@ -50,17 +50,19 @@ class GSS {
         private Phase next = null;
         private Phase prev;
         private Input.Location location;
+        private Input.Location nextLocation;
         public final Parser parser;
 
         private Forest forest;
 
-        public Phase(Phase prev, Parser parser, Phase previous, Tok token, Input.Location location, Forest forest) throws ParseFailed {
+        public Phase(Phase prev, Parser parser, Phase previous, Tok token, Input.Location location, Input.Location nextLocation, Forest forest) throws ParseFailed {
             this.prev = prev;
             this.forest = forest;
             this.parser = parser;
             this.pos = previous==null ? 0 : previous.pos+1;
             this.token = token;
             this.location = location;
+            this.nextLocation = nextLocation;
             performed.clear();
             reset();
         }
@@ -89,6 +91,7 @@ class GSS {
         }
 
         public Input.Location getLocation() { return location; }
+        public Input.Location getNextLocation() { return nextLocation; }
 
         /** add a new node (merging with existing nodes if possible)
          *  @param parent             the parent of the new node
@@ -343,7 +346,7 @@ class GSS {
                     for(Node child : ((Forest.Many<?>)result).parents) {
                         if (only != null && child!=only) continue;
                         holder[pos] = result;
-                        if (pos==0) child.finish(r, r.rewrite(child.phase().getLocation().createRegion(phase().getLocation())), target);
+                        if (pos==0) child.finish(r, r.rewrite(child.phase().getNextLocation().createRegion(target.getLocation())), target);
                         else        child.reduce(r, pos-1, target, null);
                     }
 
index e884a4b..c77ad6e 100644 (file)
@@ -25,15 +25,17 @@ public abstract class Parser<Token, NodeType> {
     public Forest<NodeType> parse(Input<Token> input) throws IOException, ParseFailed {
         GSS gss = new GSS();
         Input.Location loc = input.getLocation();
-        GSS.Phase current = gss.new Phase<Token>(null, this, null, input.next(), loc, null);
+        Token tok = input.next();
+        GSS.Phase current = gss.new Phase<Token>(null, this, null, tok, loc, input.getLocation(), null);
         current.newNode(null, Forest.create(null, null, null, false), pt.start, true);
         int count = 1;
         for(int idx=0;;idx++) {
             Input.Location oldloc = loc;
-            loc = input.getLocation();
             current.reduce();
             Forest forest = current.token==null ? null : shiftToken((Token)current.token, loc);
-            GSS.Phase next = gss.new Phase<Token>(current, this, current, input.next(), loc, forest);
+            loc = input.getLocation();
+            Token nextToken = input.next();
+            GSS.Phase next = gss.new Phase<Token>(current, this, current, nextToken, loc, input.getLocation(), forest);
             if (!helpgc) {
                 FileOutputStream fos = new FileOutputStream("out-"+idx+".dot");
                 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
index 530f25c..1a08f22 100644 (file)
@@ -20,7 +20,7 @@ public class CharInput extends Cartesian.Input<Character> {
     boolean cr = false;
     private int count = 0;
     public boolean   isCR() { return cr; }
-    public Character next() throws IOException {
+    public Character _next() throws IOException {
         cr = false;
         int i = r.read();
         if (i==-1) { System.err.print("\r...done       \r"); return null; }
index 23e5884..5665019 100644 (file)
@@ -11,21 +11,21 @@ public class Cartesian {
 
     public static abstract class Input<Token> implements edu.berkeley.sbp.Input<Token> {
 
-        public abstract Token   next() throws IOException;
+        public abstract Token   _next() throws IOException;
         public abstract boolean isCR();
 
         long then = 0;
         private Cartesian.Location location = new Cartesian.Location(0, 1);
         public  edu.berkeley.sbp.Input.Location    getLocation() { return location; }
 
-        public Token next(int numstates, int resets, int waits) throws IOException {
+        public Token next() throws IOException {
             int line  = location.getRow();
             int col   = location.getCol();
-            Token t = next();
+            Token t = _next();
             if (t==null) return null;
             String s = "  line "+line+", col " + col;
             while(s.length() < 20) s += " ";
-            s += "[ambiguity level: " + (numstates-1) + "] [resets: " + resets + "] [waits: " + waits + "]";
+            //s += "[ambiguity level: " + (numstates-1) + "] [resets: " + resets + "] [waits: " + waits + "]";
             long now = System.currentTimeMillis();
             if (now-then > 10) {
                 then = now;
index fbe0ecf..8d55c1e 100644 (file)
@@ -21,14 +21,14 @@ public class Demo2 {
         Sequence multSequence = Sequence.create("mult", mult, null, false);
 
         // uncomment this line to disambiguate
-        multSequence = multSequence.andnot(Sequence.create("add", add, null, false));
+        //multSequence = multSequence.andnot(Sequence.create("add", add, null, false));
 
         expr.add(Sequence.create(paren, 1));
         expr.add(addSequence);
         expr.add(multSequence);
         expr.add(Sequence.create(atom('0', '9')));
 
-        String input = "8+(1+3)*7";
+        String input = "(1+3*8)*7";
 
         System.out.println("input:  \""+input+"\"");