use >> and << for indent/dedent
[sbp.git] / src / edu / berkeley / sbp / Parser.java
index 64ad67a..a3730b6 100644 (file)
@@ -6,10 +6,6 @@ import edu.berkeley.sbp.Sequence.Position;
 import java.io.*;
 import java.util.*;
 
-// FEATURE: try harder to "fuse" states together along two dimensions:
-//   - identical (equivalent) states, or states that subsume each other
-//   - unnecessary intermediate states ("short cut" GLR)
-
 /** a parser which translates an Input&lt;Token&gt; into a Forest&lt;NodeType&gt; */
 public abstract class Parser<Token, NodeType> {
 
@@ -24,39 +20,27 @@ public abstract class Parser<Token, NodeType> {
     public abstract Topology<Token> emptyTopology();
 
     public String toString() { return pt.toString(); }
-    Cache cache() { return pt; }
+    Grammar cache() { return pt; }
 
     /** parse <tt>input</tt>, and return the shared packed parse forest (or throw an exception) */
     public Forest<NodeType> parse(Input<Token> input) throws IOException, ParseFailed {
         verbose = System.getProperty("sbp.verbose", null) != null;
         spinpos = 0;
+        GSS gss = new GSS(input, this);
         try {
-            GSS gss = new GSS(input, this);
             for(GSS.Phase current = gss.new Phase<Token>(pt.start); ;) {
-                
-                if (verbose) {
-                    // FIXME: clean this up
-                    String s;
-                    s = "  " + spin[spinpos++ % (spin.length)]+" parsing ";
-                    s += input.getName();
-                    s += " "+input.getLocation();
-                    while(s.indexOf(':') != -1 && s.indexOf(':') < 8) s = " " + s;
-                    String y = "@"+gss.viewPos+" ";
-                    while(y.length() < 9) y = " " + y;
-                    s += y;
-                    s += "   nodes="+gss.numOldNodes;
-                    while(s.length() < 50) s = s + " ";
-                    s += " shifted="+gss.numNewNodes;
-                    while(s.length() < 60) s = s + " ";
-                    s += " reductions="+gss.numReductions;
-                    System.err.print("\r"+s+ANSI.clreol()+"\r");
-                }
-                
+                if (verbose) debug(current.token, gss, input);
                 if (current.isDone()) return (Forest<NodeType>)current.finalResult;
-                Forest forest = shiftToken((Token)current.token, current.getRegion());
+                Input.Region region = current.getLocation().createRegion(current.getNextLocation());
+                Forest forest = shiftToken((Token)current.token, region);
                 current = gss.new Phase<Token>(current, forest);
             }
-        } finally { if (verbose) System.err.print("\r"+ANSI.clreol()); }
+        } finally {
+            if (verbose) {
+                System.err.print("\r"+ANSI.clreol());
+                debug(null, gss, input);
+            }
+        }
     }
 
     // Spinner //////////////////////////////////////////////////////////////////////////////
@@ -73,10 +57,56 @@ public abstract class Parser<Token, NodeType> {
         System.err.print("\r  " + spin[spinpos++ % (spin.length)]+"\r");
     }
 
+    private int _last = -1;
+    private String buf = "";
+    private void debug(Object t, GSS gss, Input input) {
+        //FIXME
+        int c = t==null ? -1 : ((t+"").charAt(0));
+        int last = _last;
+        _last = c;
+        switch(c) {
+            case edu.berkeley.sbp.chr.CharAtom.left:
+                buf += "\033[31m>\033[0m";
+                break;
+            case edu.berkeley.sbp.chr.CharAtom.right:
+                buf += "\033[31m<\033[0m";
+                break;
+            case -1: // FIXME 
+            case '\n':
+                if (verbose) {
+                    if (last==' ') buf += ANSI.blue("\\n");
+                    System.err.println("\r"+ANSI.clreol()+"\r"+buf);
+                    buf = "";
+                }
+                break;
+            default:
+                buf += ANSI.cyan(""+((char)c));
+                break;
+        }
+        if (t==null) return;
+
+        // FIXME: clean this up
+        String s;
+        s = "  " + spin[spinpos++ % (spin.length)]+" parsing ";
+        s += input.getName();
+        s += " "+input.getLocation();
+        while(s.indexOf(':') != -1 && s.indexOf(':') < 8) s = " " + s;
+        String y = "@"+gss.viewPos+" ";
+        while(y.length() < 9) y = " " + y;
+        s += y;
+        s += "   nodes="+gss.numOldNodes;
+        while(s.length() < 50) s = s + " ";
+        s += " shifted="+gss.numNewNodes;
+        while(s.length() < 60) s = s + " ";
+        s += " reductions="+gss.numReductions;
+        while(s.length() < 78) s = s + " ";
+        System.err.print("\r"+ANSI.invert(s+ANSI.clreol())+"\r");
+    }
+
     // Table //////////////////////////////////////////////////////////////////////////////
 
     /** an SLR(1) parse table which may contain conflicts */
-    class Table extends Cache<Token> {
+    class Table extends Grammar<Token> {
 
         /** the start state */
         final State<Token>   start;
@@ -253,6 +283,7 @@ public abstract class Parser<Token, NodeType> {
 
             // Interface Methods //////////////////////////////////////////////////////////////////////////////
 
+            public boolean doomed() { return doomed; }
             boolean                    isAccepting()           { return accept; }
             public Iterator<Position>  iterator()              { return hs.iterator(); }
             boolean                    canShift(Token t)       { return oshifts!=null && oshifts.contains(t); }
@@ -382,6 +413,12 @@ public abstract class Parser<Token, NodeType> {
             }
 
             public int toInt() { return idx; }
+            public String toString() {
+                StringBuffer ret = new StringBuffer();
+                for(Position p : hs)
+                    ret.append(p+"\n");
+                return ret.toString();
+            }
         }
 
     }