From: adam Date: Sun, 16 Jul 2006 08:09:33 +0000 (-0400) Subject: checkpoint X-Git-Tag: tag_for_25-Mar~117 X-Git-Url: http://git.megacz.com/?p=sbp.git;a=commitdiff_plain;h=82f9b8d4a2fadb14cb004e5cc9f14944458f2e6c checkpoint darcs-hash:20060716080933-5007d-06b2ffe4cd0b3126a1971fef050b70930484082d.gz --- diff --git a/src/edu/berkeley/sbp/Element.java b/src/edu/berkeley/sbp/Element.java index 0d7fb84..896540e 100644 --- a/src/edu/berkeley/sbp/Element.java +++ b/src/edu/berkeley/sbp/Element.java @@ -10,6 +10,9 @@ import java.lang.ref.*; /** the root superclass for all components of the grammar (terminals, nonterminals, literals, etc) */ public abstract class Element { + /** sorry, you can't make up new, custom elements */ + Element() { } + abstract StringBuffer toString(StringBuffer sb); /** returns the Forest resulting from matching this element against the empty string */ diff --git a/src/edu/berkeley/sbp/GSS.java b/src/edu/berkeley/sbp/GSS.java index 9075f41..1a8ef11 100644 --- a/src/edu/berkeley/sbp/GSS.java +++ b/src/edu/berkeley/sbp/GSS.java @@ -344,7 +344,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(new Input.Region(child.phase().getLocation(), phase().getLocation())), target); + if (pos==0) child.finish(r, r.rewrite(child.phase().getLocation().createRegion(phase().getLocation())), target); else child.reduce(r, pos-1, target, null); } diff --git a/src/edu/berkeley/sbp/Input.java b/src/edu/berkeley/sbp/Input.java index 407d843..9948ae4 100644 --- a/src/edu/berkeley/sbp/Input.java +++ b/src/edu/berkeley/sbp/Input.java @@ -18,21 +18,10 @@ public interface Input { /** a location between tokens in the input stream */ public static interface Location extends Comparable { public String toString(); + public Region createRegion(Location loc); } - public static class Region /* implements Topology> */ { - public final Loc start; - public final Loc end; - public String toString() { return start+"-"+end; } - public Region(Loc a, Loc b) { - switch(a.compareTo(b)) { - case -1: - case 0: start=a; end=b; return; - case 1: start=b; end=a; return; - default: throw new Error("impossible"); - } - } - } + public static interface Region /* implements Topology> */ { } } diff --git a/src/edu/berkeley/sbp/Parser.java b/src/edu/berkeley/sbp/Parser.java index 744b7c0..0504a11 100644 --- a/src/edu/berkeley/sbp/Parser.java +++ b/src/edu/berkeley/sbp/Parser.java @@ -15,7 +15,7 @@ public abstract class Parser { protected Parser(Table pt) { this.pt = pt; } /** implement this method to create the output forest corresponding to a lone shifted input token */ - protected abstract Forest shiftToken(Input.Location oldloc, Tok t, Input.Location newloc); + protected abstract Forest shiftToken(Tok t, Input.Location newloc); boolean helpgc = true; @@ -32,7 +32,7 @@ public abstract class Parser { Input.Location oldloc = loc; loc = input.getLocation(); current.reduce(); - Forest forest = current.token==null ? null : shiftToken(oldloc, (Tok)current.token, loc); + Forest forest = current.token==null ? null : shiftToken((Tok)current.token, loc); GSS.Phase next = gss.new Phase(current, this, current, input.next(), loc, forest); if (!helpgc) { FileOutputStream fos = new FileOutputStream("out-"+idx+".dot"); diff --git a/src/edu/berkeley/sbp/chr/CharParser.java b/src/edu/berkeley/sbp/chr/CharParser.java index 8630161..9bdadbc 100644 --- a/src/edu/berkeley/sbp/chr/CharParser.java +++ b/src/edu/berkeley/sbp/chr/CharParser.java @@ -15,8 +15,13 @@ public class CharParser extends Parser { public CharParser(Union u) { super(u, new CharTopology()); } - public Forest shiftToken(Location oldloc, Character ct, Location newloc) { - return Forest.create(new Input.Region(oldloc, newloc), ct.toString(), null, false); + private Location oldloc; + + public Forest shiftToken(Character ct, Location newloc) { + if (oldloc==null) oldloc = newloc; + Forest ret = Forest.create(oldloc.createRegion(newloc), ct.toString(), null, false); + oldloc = newloc; + return ret; } } diff --git a/src/edu/berkeley/sbp/misc/Cartesian.java b/src/edu/berkeley/sbp/misc/Cartesian.java index 676a4cf..23e5884 100644 --- a/src/edu/berkeley/sbp/misc/Cartesian.java +++ b/src/edu/berkeley/sbp/misc/Cartesian.java @@ -59,5 +59,24 @@ public class Cartesian { if (col > c.col) return 1; return 0; } + public Input.Region createRegion(Input.Location loc) { + return new Region(this, (Cartesian.Location)loc); } + } + + public static class Region implements Input.Region { + public final Location start; + public final Location end; + public String toString() { + if (start.row==end.row) return start.row+":"+(start.col+"-"+end.col); + return start+"-"+end; + } + public Region(Location a, Location b) { + switch(a.compareTo(b)) { + case -1: + case 0: start=a; end=b; return; + case 1: start=b; end=a; return; + default: throw new Error("impossible"); + } + } } }