checkpoint harmony
authoradam <adam@megacz.com>
Wed, 11 Jan 2006 08:12:05 +0000 (03:12 -0500)
committeradam <adam@megacz.com>
Wed, 11 Jan 2006 08:12:05 +0000 (03:12 -0500)
darcs-hash:20060111081205-5007d-b481bd9f6b56924bd3db420d6bc5c6d574aa2bc5.gz

src/edu/berkeley/sbp/GSS.java
src/edu/berkeley/sbp/ParseFailed.java
src/edu/berkeley/sbp/Parser.java
src/edu/berkeley/sbp/Token.java
src/edu/berkeley/sbp/misc/CharToken.java

index becea95..5bfbd52 100644 (file)
@@ -1,8 +1,8 @@
 package edu.berkeley.sbp;
 import edu.berkeley.sbp.*;
 import edu.berkeley.sbp.util.*;
+import edu.berkeley.sbp.Parser.Table.*;
 import edu.berkeley.sbp.Sequence.Position;
-import edu.berkeley.sbp.Parser.Table.State;
 import java.io.*;
 import java.util.*;
 import java.lang.reflect.*;
@@ -24,10 +24,14 @@ class GSS {
     public  Forest.Ref finalResult;
 
     /** corresponds to a positions <i>between tokens</i> the input stream; same as Tomita's U_i's */
-    public class Phase implements Invokable<State, Forest, GSS.Phase.Node>, IntegerMappable {
+    public class Phase<Tok> implements Invokable<State, Forest, Phase<Tok>.Node>, IntegerMappable {
+
+        public void invoke(State st, Forest result, Node n) {
+            good |= next.newNode(n, result, st, false);
+        }
 
         /** the token immediately after this phase */
-        final Token token;
+        final Tok token;
 
         private final int pos;
 
@@ -42,7 +46,7 @@ class GSS {
 
         private Forest forest;
 
-        public Phase(Phase prev, Parser parser, Phase previous, Token token, Token.Location location, Forest forest) {
+        public Phase(Phase prev, Parser parser, Phase previous, Tok token, Token.Location location, Forest forest) {
             this.prev = prev;
             this.forest = forest;
             this.parser = parser;
@@ -198,10 +202,6 @@ class GSS {
 
         class Reset extends RuntimeException { }
 
-        public void invoke(State st, Forest result, Node n) {
-            good |= next.newNode(n, result, st, false);
-        }
-
         /** perform all shift operations, adding promoted nodes to <tt>next</tt> */
         public void shift(Phase next, Forest result) throws ParseFailed {
             // this massively improves GC performance
@@ -257,7 +257,7 @@ class GSS {
             private boolean allqueued = false;
 
             /** what state this node is in */
-            public final State state;
+            public final Parser.Table<Tok>.State<Tok> state;
 
             /** which Phase this Node belongs to (node that Node is also a non-static inner class of Phase) */
             public  Phase phase() { return Phase.this; }
@@ -303,22 +303,22 @@ class GSS {
                 }
             }
 
-            public void reduce(Position r, int pos, GSS.Phase target, Forest[] holder) {
+            public void reduce(Position r, int pos, Phase target, Forest[] holder) {
                 Forest old = holder[pos];
                 holder[pos] = this.pending();
                 if (pos==0) {
                     System.arraycopy(holder, 0, r.holder, 0, holder.length);
                     for(int i=0; i<r.pos; i++) if (r.holder[i]==null) throw new Error("realbad");
                     Forest rex = r.rewrite(target.getLocation());
-                    for(GSS.Phase.Node child : this.parents()) child.finish(r, rex, target, holder);
+                    for(Node child : this.parents()) child.finish(r, rex, target, holder);
                 } else {
-                    for(GSS.Phase.Node child : this.parents()) child.reduce(r, pos-1, target, holder);
+                    for(Node child : this.parents()) child.reduce(r, pos-1, target, holder);
                 }
                 holder[pos] = old;
             }
 
-            public void finish(Position r, Forest result, GSS.Phase target, Forest[] holder) {
-                State state0 = state.gotoSetNonTerminals.get(r.owner());
+            public void finish(Position r, Forest result, Phase<Tok> target, Forest[] holder) {
+                Parser.Table<Tok>.State<Tok> state0 = state.gotoSetNonTerminals.get(r.owner());
                 if (result==null) throw new Error();
                 if (state0!=null)
                     target.newNode(this, result, state0, r.pos<=0, r);
index e5041d6..9c0a3ad 100644 (file)
@@ -16,7 +16,7 @@ public class ParseFailed extends RuntimeException {
     public Token.Location getLocation() { return location; }
     public String toString() { return message/* + (location==null ? "" : (" at " + location))*/; }
 
-    public static void complain(Node n, HashMap<String,HashSet<String>> errors, boolean force) {
+    public static <Tok> void complain(GSS.Phase<Tok>.Node n, HashMap<String,HashSet<String>> errors, boolean force) {
         //if (n.touched) return;
         //n.touched = true;
         for(Position p : n.state) {
@@ -44,7 +44,7 @@ public class ParseFailed extends RuntimeException {
         }
         return ANSI.purple(ret.toString());
     }
-    public static String error(String message, Token token, Iterable<Node> nodes) {
+    public static String error(String message, Object token, Iterable<Node> nodes) {
         String lookAhead = token==null ? "<EOF>" : token.toString();
         StringBuffer ret = new StringBuffer();
         ret.append("\n  ");
index d04c49f..3bafbfe 100644 (file)
@@ -6,31 +6,31 @@ import java.io.*;
 import java.util.*;
 
 /** a parser which translates streams of Tokens of type T into a Forest<R> */
-public abstract class Parser<T extends Token, R> {
+public abstract class Parser<Tok, Result> {
 
     private final Table pt;
 
     /** create a parser to parse the grammar with start symbol <tt>u</tt> */
-    protected Parser(Union u, Topology<T> top)  { this.pt = new Table(u, top); }
-    protected Parser(Table pt)                  { this.pt = pt; }
+    protected Parser(Union u, Topology<Tok> top)  { this.pt = new Table<Tok>(u, top); }
+    protected Parser(Table pt)                    { this.pt = pt; }
 
     /** implement this method to create the output forest corresponding to a lone shifted input token */
-    public abstract Forest<R> shiftedToken(T t, Token.Location loc);
+    public abstract Forest<Result> shiftToken(Tok t, Token.Location loc);
 
     /** parse <tt>input</tt>, using the table <tt>pt</tt> to drive the parser */
-    public Forest<R> parse(Token.Stream<T> input) throws IOException, ParseFailed {
+    public Forest<Result> parse(Token.Stream<Tok> input) throws IOException, ParseFailed {
         GSS gss = new GSS();
         Token.Location loc = input.getLocation();
-        GSS.Phase current = gss.new Phase(null, this, null, input.next(1, 0, 0), loc, null);
+        GSS.Phase current = gss.new Phase<Tok>(null, this, null, input.next(1, 0, 0), loc, null);
         current.newNode(null, Forest.leaf(null, null), pt.start, true);
         int count = 1;
         for(;;) {
             loc = input.getLocation();
             current.reduce();
-            Forest forest = current.token==null ? null : shiftedToken((T)current.token, loc);
-            GSS.Phase next = gss.new Phase(current, this, current, input.next(count, gss.resets, gss.waits), loc, forest);
+            Forest forest = current.token==null ? null : shiftToken((Tok)current.token, loc);
+            GSS.Phase next = gss.new Phase<Tok>(current, this, current, input.next(count, gss.resets, gss.waits), loc, forest);
             count = next.size();
-            if (current.isDone()) return (Forest<R>)gss.finalResult;
+            if (current.isDone()) return (Forest<Result>)gss.finalResult;
             current = next;
         }
     }
@@ -38,7 +38,7 @@ public abstract class Parser<T extends Token, R> {
     // Table //////////////////////////////////////////////////////////////////////////////
 
     /** an SLR(1) parse table which may contain conflicts */
-    static class Table extends Walk.Cache {
+    static class Table<Tok> extends Walk.Cache {
 
         public final Walk.Cache cache = this;
 
@@ -55,7 +55,7 @@ public abstract class Parser<T extends Token, R> {
         }
 
         /** the start state */
-        public final State   start;
+        public final State<Tok>   start;
 
         /** used to generate unique values for State.idx */
         private int master_state_idx = 0;
@@ -71,17 +71,17 @@ public abstract class Parser<T extends Token, R> {
             cache.eof.put(start0, true);
 
             // construct the set of states
-            HashMap<HashSet<Position>,State>   all_states    = new HashMap<HashSet<Position>,State>();
-            HashSet<Element>                   all_elements  = new HashSet<Element>();
+            HashMap<HashSet<Position>,State<Tok>>   all_states    = new HashMap<HashSet<Position>,State<Tok>>();
+            HashSet<Element>                        all_elements  = new HashSet<Element>();
             walk(start0, all_elements);
             for(Element e : all_elements)
                 cache.ys.addAll(e, new Walk.YieldSet(e, cache).walk());
             HashSet<Position> hp = new HashSet<Position>();
             reachable(start0, hp);
-            this.start = new State(hp, all_states, all_elements);
+            this.start = new State<Tok>(hp, all_states, all_elements);
 
             // for each state, fill in the corresponding "row" of the parse table
-            for(State state : all_states.values())
+            for(State<Tok> state : all_states.values())
                 for(Position p : state.hs) {
 
                     // the Grammar's designated "last position" is the only accepting state
@@ -102,7 +102,7 @@ public abstract class Parser<T extends Token, R> {
                     if (p.element() != null && p.element() instanceof Atom)
                         state.shifts.addAll(state.gotoSetTerminals.subset(((Atom)p.element())));
                 }
-            for(State state : all_states.values()) {
+            for(State<Tok> state : all_states.values()) {
                 state.oreductions = state.reductions.optimize();
                 state.oshifts = state.shifts.optimize();
             }
@@ -116,34 +116,34 @@ public abstract class Parser<T extends Token, R> {
 
         /** a single state in the LR table and the transitions possible from it */
 
-        public class State implements Comparable<Table.State>, IntegerMappable, Iterable<Position> {
+        public class State<Tok> implements Comparable<State<Tok>>, IntegerMappable, Iterable<Position> {
         
             public  final     int               idx    = master_state_idx++;
             private final     HashSet<Position> hs;
 
-            public transient HashMap<Element,State>          gotoSetNonTerminals = new HashMap<Element,State>();
-            private transient TopologicalBag<Token,State>     gotoSetTerminals    = new TopologicalBag<Token,State>();
+            public transient HashMap<Element,State<Tok>>          gotoSetNonTerminals = new HashMap<Element,State<Tok>>();
+            private transient TopologicalBag<Tok,State<Tok>>     gotoSetTerminals    = new TopologicalBag<Tok,State<Tok>>();
 
-            private           TopologicalBag<Token,Position> reductions          = new TopologicalBag<Token,Position>();
+            private           TopologicalBag<Tok,Position> reductions          = new TopologicalBag<Tok,Position>();
             private           HashSet<Position>              eofReductions       = new HashSet<Position>();
-            private           TopologicalBag<Token,State>     shifts              = new TopologicalBag<Token,State>();
+            private           TopologicalBag<Tok,State<Tok>>     shifts              = new TopologicalBag<Tok,State<Tok>>();
             private           boolean                         accept              = false;
 
-            private VisitableMap<Token,State> oshifts = null;
-            private VisitableMap<Token,Position> oreductions = null;
+            private VisitableMap<Tok,State<Tok>> oshifts = null;
+            private VisitableMap<Tok,Position> oreductions = null;
 
             // Interface Methods //////////////////////////////////////////////////////////////////////////////
 
             boolean             isAccepting()               { return accept; }
             public Iterator<Position>  iterator()                  { return hs.iterator(); }
 
-            boolean             canShift(Token t)           { return oshifts.contains(t); }
-            <B,C> void          invokeShifts(Token t, Invokable<State,B,C> irbc, B b, C c) {
+            boolean             canShift(Tok t)           { return oshifts.contains(t); }
+            <B,C> void          invokeShifts(Tok t, Invokable<State<Tok>,B,C> irbc, B b, C c) {
                 oshifts.invoke(t, irbc, b, c);
             }
 
-            boolean             canReduce(Token t)          { return t==null ? eofReductions.size()>0 : oreductions.contains(t); }
-            <B,C> void          invokeReductions(Token t, Invokable<Position,B,C> irbc, B b, C c) {
+            boolean             canReduce(Tok t)          { return t==null ? eofReductions.size()>0 : oreductions.contains(t); }
+            <B,C> void          invokeReductions(Tok t, Invokable<Position,B,C> irbc, B b, C c) {
                 if (t==null) for(Position r : eofReductions) irbc.invoke(r, b, c);
                 else         oreductions.invoke(t, irbc, b, c);
             }
@@ -173,7 +173,7 @@ public abstract class Parser<T extends Token, R> {
              *  </ul>
              */
             public State(HashSet<Position> hs,
-                         HashMap<HashSet<Position>,State> all_states,
+                         HashMap<HashSet<Position>,State<Tok>> all_states,
                          HashSet<Element> all_elements) {
                 this.hs = hs;
 
@@ -186,7 +186,7 @@ public abstract class Parser<T extends Token, R> {
                 //          of _new_ positions (positions after shifting).  These mappings are
                 //          collectively known as the _closure_
 
-                TopologicalBag<Token,Position> bag0 = new TopologicalBag<Token,Position>();
+                TopologicalBag<Tok,Position> bag0 = new TopologicalBag<Tok,Position>();
                 for(Position position : hs) {
                     if (position.isLast() || !(position.element() instanceof Atom)) continue;
                     Atom a = (Atom)position.element();
@@ -199,10 +199,10 @@ public abstract class Parser<T extends Token, R> {
                 //          set, add that character set to the goto table (with the State corresponding to the
                 //          computed next-position set).
 
-                for(Topology<Token> r : bag0) {
+                for(Topology<Tok> r : bag0) {
                     HashSet<Position> h = new HashSet<Position>();
                     for(Position p : bag0.getAll(r)) h.add(p);
-                    gotoSetTerminals.put(r, all_states.get(h) == null ? new State(h, all_states, all_elements) : all_states.get(h));
+                    gotoSetTerminals.put(r, all_states.get(h) == null ? new State<Tok>(h, all_states, all_elements) : all_states.get(h));
                 }
 
                 // Step 2: for every non-Atom element (ie every Element which has a corresponding reduction),
@@ -224,7 +224,7 @@ public abstract class Parser<T extends Token, R> {
                 }
                 for(Element y : move) {
                     HashSet<Position> h = move.getAll(y);
-                    State s = all_states.get(h) == null ? new State(h, all_states, all_elements) : all_states.get(h);
+                    State<Tok> s = all_states.get(h) == null ? new State<Tok>(h, all_states, all_elements) : all_states.get(h);
                     gotoSetNonTerminals.put(y, s);
                 }
             }
@@ -236,7 +236,7 @@ public abstract class Parser<T extends Token, R> {
                 return ret.toString();
             }
 
-            public int compareTo(Table.State s) { return idx==s.idx ? 0 : idx < s.idx ? -1 : 1; }
+            public int compareTo(State<Tok> s) { return idx==s.idx ? 0 : idx < s.idx ? -1 : 1; }
             public int toInt() { return idx; }
         }
     }
index 1014a55..10ab90f 100644 (file)
@@ -14,8 +14,8 @@ public interface Token {
     public abstract String toString();
 
     /** a sequence of input tokens; returns null when EOF is reached */
-    public static interface Stream<T extends Token> {
-        public T next(int numstates, int resets, int waits) throws IOException;
+    public static interface Stream<Tok> {
+        public Tok next(int numstates, int resets, int waits) throws IOException;
         public abstract Location getLocation();
     }
 
index 87be340..2042145 100644 (file)
@@ -15,7 +15,7 @@ public class CharToken implements Token, IntegerMappable {
 
     public static class CharToStringParser extends Parser<CharToken,String> {
         public CharToStringParser(Union u) { super(u, new IntegerTopology<CharToken>()); }
-        public Forest<String> shiftedToken(CharToken ct, Token.Location loc) {
+        public Forest<String> shiftToken(CharToken ct, Token.Location loc) {
             return Forest.create(loc, ct.result(), null, false, false);
         }
     }