checkpoint
authoradam <adam@megacz.com>
Thu, 15 Dec 2005 00:45:49 +0000 (19:45 -0500)
committeradam <adam@megacz.com>
Thu, 15 Dec 2005 00:45:49 +0000 (19:45 -0500)
darcs-hash:20051215004549-5007d-2d92f87866d5a101b963264b70eb38147ba8f0df.gz

src/edu/berkeley/sbp/misc/CharToken.java
src/edu/berkeley/sbp/misc/MetaGrammar.java
src/edu/berkeley/sbp/tib/Tib.java

index eccac6a..93ae7bb 100644 (file)
@@ -89,7 +89,7 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable {
 
     public final char c;
     public final Location location;
 
     public final char c;
     public final Location location;
-    private CharToken(char c, int line, int col)   { this(c, new CartesianLocation(line, col)); }
+    CharToken(char c, int line, int col)   { this(c, new CartesianLocation(line, col)); }
     private CharToken(char c, Location loc)        { this.c = c; this.location = loc; }
     public String result()                         { return c+""; }
     public Location getLocation()                  { return location; }
     private CharToken(char c, Location loc)        { this.c = c; this.location = loc; }
     public String result()                         { return c+""; }
     public Location getLocation()                  { return location; }
index d385a55..6eab9ed 100644 (file)
@@ -31,9 +31,17 @@ public class MetaGrammar extends ReflectiveWalker {
     private HashMap<String,Union> nt;
     private int anon = 0;
     private String startSymbol;
     private HashMap<String,Union> nt;
     private int anon = 0;
     private String startSymbol;
+    private boolean strings;
 
 
-    public MetaGrammar() { this("s"); }
-    public MetaGrammar(String s) { done(s); }
+    private Element  set(Range.Set r) { return CharToken.set(r); }
+    private Element  string(String s) { return CharToken.string(s); }
+    private Topology top()            { return CharToken.top(); }
+    private Atom     leftBrace()      { return CharToken.leftBrace; }
+    private Atom     rightBrace()     { return CharToken.rightBrace; }
+
+    public MetaGrammar() { this("s", false); }
+    public MetaGrammar(String s) { this(s, false); }
+    public MetaGrammar(String s, boolean strings) { this.strings = strings; done(s); }
     public Union done() { return done("s"); }
     public Union done(String str) {
         Union ret = g;
     public Union done() { return done("s"); }
     public Union done(String str) {
         Union ret = g;
@@ -86,9 +94,9 @@ public class MetaGrammar extends ReflectiveWalker {
         return nonTerminal("braced"+(anon++),
                            new PreSequence[][] {
                                new PreSequence[] {
         return nonTerminal("braced"+(anon++),
                            new PreSequence[][] {
                                new PreSequence[] {
-                                   new PreSequence(CharToken.leftBrace),
+                                   new PreSequence(leftBrace()),
                                    p,
                                    p,
-                                   new PreSequence(CharToken.rightBrace)
+                                   new PreSequence(rightBrace())
                                }
                            },
                            false,
                                }
                            },
                            false,
@@ -121,7 +129,7 @@ public class MetaGrammar extends ReflectiveWalker {
     public MetaGrammar grammar(Object o, Union[] u, Object x) { return this; }
     public char    _backslash_n() { return '\n'; }
     public char    _backslash_r() { return '\r'; }
     public MetaGrammar grammar(Object o, Union[] u, Object x) { return this; }
     public char    _backslash_n() { return '\n'; }
     public char    _backslash_r() { return '\r'; }
-    public Object    literal(String s) { return new MyDrop(CharToken.string(s)); }
+    public Object    literal(String s) { return new MyDrop(string(s)); }
     public Range     _minus_(char a, char b) { return new Range(a, b); }
     public Range     range(char a)         { return new Range(a, a); }
     public Element   _leftbracket_       (Range[] rr) { return ranges(true, rr); }
     public Range     _minus_(char a, char b) { return new Range(a, b); }
     public Range     range(char a)         { return new Range(a, a); }
     public Element   _leftbracket_       (Range[] rr) { return ranges(true, rr); }
@@ -132,19 +140,19 @@ public class MetaGrammar extends ReflectiveWalker {
             for(Range r : rr)
                 if (positive) ret.add(r);
                 else          ret.remove(r);
             for(Range r : rr)
                 if (positive) ret.add(r);
                 else          ret.remove(r);
-        return CharToken.set(ret);
+        return set(ret);
     }
 
     }
 
-    public static class MyLift {
+    public class MyLift {
         public final String s;
         public MyLift(String s) { this.s = s; }
     }
         public final String s;
         public MyLift(String s) { this.s = s; }
     }
-    public static class MyDrop {
+    public class MyDrop {
         public final Object o;
         public MyDrop(Object o) { this.o = o; }
     }
 
         public final Object o;
         public MyDrop(Object o) { this.o = o; }
     }
 
-    public static class PreSequence {
+    public class PreSequence {
         public PreSequence not(Sequence s) { not.add(s); return this; }
         public PreSequence and(Sequence s) { and.add(s); return this; }
         public final HashSet<Sequence> and  = new HashSet<Sequence>();
         public PreSequence not(Sequence s) { not.add(s); return this; }
         public PreSequence and(Sequence s) { and.add(s); return this; }
         public final HashSet<Sequence> and  = new HashSet<Sequence>();
@@ -176,7 +184,7 @@ public class MetaGrammar extends ReflectiveWalker {
                     if (oi instanceof MyDrop)      { oi = ((MyDrop)oi).o; drops[j] = true; }
 
                     if (oi==SELF)                    o2[j] = u.new Subset("(("+u+"))", set);
                     if (oi instanceof MyDrop)      { oi = ((MyDrop)oi).o; drops[j] = true; }
 
                     if (oi==SELF)                    o2[j] = u.new Subset("(("+u+"))", set);
-                    else if (oi instanceof MyLift) { o2[j] = CharToken.string(tag = ((MyLift)oi).s); drops[j] = o.length>1; }
+                    else if (oi instanceof MyLift) { o2[j] = string(tag = ((MyLift)oi).s); drops[j] = o.length>1; }
                     else                             o2[j] = (Element)oi;
 
                     if (MetaGrammar.dropAll.contains(o2[j])) drops[j] = true;
                     else                             o2[j] = (Element)oi;
 
                     if (MetaGrammar.dropAll.contains(o2[j])) drops[j] = true;
index 167ad8b..9a546e9 100644 (file)
@@ -25,18 +25,34 @@ import java.io.*;
  *   possible (it is unclear whether or not the associated grammar is
  *   context-free).
  */
  *   possible (it is unclear whether or not the associated grammar is
  *   context-free).
  */
-public class Tib /*implements Token.Stream<TreeToken<String>>*/ {
+public class Tib /*implements Token.Stream<CharToken>*/ {
     /*
     /*
-    public final String str;
-
     public Tib(String s) { this(new StringReader(s)); }
     public Tib(Reader r) { this(new BufferedReader(r)); }
     public Tib(InputStream is) { this(new BufferedReader(new InputStreamReader(is))); }
     public Tib(String s) { this(new StringReader(s)); }
     public Tib(Reader r) { this(new BufferedReader(r)); }
     public Tib(InputStream is) { this(new BufferedReader(new InputStreamReader(is))); }
-    public Tib(BufferedReader br) { str = parse(br).toString(0,-1); }
+    public Tib(BufferedReader br) { cur = parse(br).toString(0,-1); }
+
+    boolean left = false;
+    boolean right = false;
+    private Block cur;
+    int pos = 0;
 
     public Token next() throws IOException {
 
     public Token next() throws IOException {
-        if (pos >= str.length()) return Atom.EOF;
-        return Atom.fromChar(str.charAt(pos++));
+        if (left) { left = false; return new CharToken(-3); }
+        if (right) { right = false; return new CharToken(-4); }
+
+        if (pos >= cur.size()) {
+            pos = cur.iip+1;
+            cur = cur.parent;
+            return new CharToken(-4);
+        }
+
+        Object o = cur.child(pos++);
+        if (o instanceof String) return new StringToken((String)o);
+        if (o.isLiteral()) return ((Block.Literal)o).text();
+        cur = (Block)b;
+        pos = 0;
+        return new CharToken(-3);
     }
 
     public static Block parse(BufferedReader br) throws Invalid {
     }
 
     public static Block parse(BufferedReader br) throws Invalid {
@@ -92,6 +108,7 @@ public class Tib /*implements Token.Stream<TreeToken<String>>*/ {
                       Block  parent;
         public  final int    row;
         public  final int    col;
                       Block  parent;
         public  final int    row;
         public  final int    col;
+        public final int iip;
         private final Vec    children = new Vec();
         private       String pending  = "";
 
         private final Vec    children = new Vec();
         private       String pending  = "";
 
@@ -106,8 +123,18 @@ public class Tib /*implements Token.Stream<TreeToken<String>>*/ {
         public Object child(int i) { return children.elementAt(i); }
         public boolean isLiteral() {  return false; }
 
         public Object child(int i) { return children.elementAt(i); }
         public boolean isLiteral() {  return false; }
 
-        protected Block(int row, int col)                { this.row=row; this.col=col; parent = null; }
-        public    Block(Block parent, int row, int col)  { this.row=row; this.col=col; (this.parent = parent).add(this); }
+        protected Block(int row, int col)                {
+            this.row=row;
+            this.col=col;
+            this.iip = -1;
+            parent = null;
+        }
+        public    Block(Block parent, int row, int col)  {
+            this.row=row;
+            this.col=col;
+            this.iip = parent.size();
+            (this.parent = parent).add(this);
+        }
 
         public void    add(String s)        { children.addElement(s); }
         public void    add(char c)          {
 
         public void    add(String s)        { children.addElement(s); }
         public void    add(char c)          {
@@ -188,6 +215,7 @@ public class Tib /*implements Token.Stream<TreeToken<String>>*/ {
             public    Block  close() { return parent; }
             public    Block  closeIndent() { return close(); }
             public    void   add(String s) { if (content.length()>0) content.append('\n'); content.append(s); }
             public    Block  close() { return parent; }
             public    Block  closeIndent() { return close(); }
             public    void   add(String s) { if (content.length()>0) content.append('\n'); content.append(s); }
+            public    String text() { return content.toString(); }
             protected String toString(int indent, int justificationLimit) {
                 StringBuffer ret = new StringBuffer();
                 String s = content.toString();
             protected String toString(int indent, int justificationLimit) {
                 StringBuffer ret = new StringBuffer();
                 String s = content.toString();