Tib cleanups
[sbp.git] / src / edu / berkeley / sbp / tib / Tib.java
index 4805d1e..387f5ed 100644 (file)
@@ -1,6 +1,4 @@
-// Copyright 2005 the Contributors, as shown in the revision logs.
-// Licensed under the Apache Public Source License 2.0 ("the License").
-// You may not use this file except in compliance with the License.
+// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
 
 package edu.berkeley.sbp.tib;
 import edu.berkeley.sbp.*;
@@ -9,10 +7,6 @@ import edu.berkeley.sbp.chr.*;
 import java.util.*;
 import java.io.*;
 
-// TODO: multiple {{ }} for superquotation
-// TODO: strings
-// TODO: comments
-
 /**
  *   A slow, ugly, inefficient, inelegant, ad-hoc parser for TIB files.
  *
@@ -26,39 +20,43 @@ import java.io.*;
  */
 public class Tib implements Input<Character> {
 
+    public String showRegion(Region<Character> r) { return ""; }
+
     public Tib(String s) throws IOException { this(new StringReader(s)); }
     public Tib(Reader r) throws IOException { this(new BufferedReader(r)); }
     public Tib(InputStream is) throws IOException { this(new BufferedReader(new InputStreamReader(is))); }
     public Tib(BufferedReader br) throws IOException {
         this.br = br;
         istack.add(-1);
-        //cur = parse(br);
-        //System.out.println("\rparsing: \"" + cur.toString(0, -1) + "\"");
     }
-
+    public String getName() { return null; }
     private String s = "";
     int pos = 0;
     int spos = 0;
 
     int _row = 1;
     int _col = 0;
-    public Input.Location getLocation() { return new Input.Location.Cartesian(_col, _row); }
+    int _scalar = 0;
+    public Input.Location getLocation() { return new Cartesian.Location(_col, _row, _scalar); }
     private BufferedReader br;
 
-    char left = CharRange.left;
-    char right = CharRange.right;
+    char left = CharAtom.left;
+    char right = CharAtom.right;
 
     boolean waiting = false;
     char waitingChar = ' ';
     boolean indenting = true;
     int indentation = 0;
     private ArrayList<Integer> istack = new ArrayList<Integer>();
+    private static boolean debug = "true".equals(System.getProperty("tib.debug", "false"));
     public Character next() throws IOException {
         Character ret = nextc();
-        if      (ret==null) return null;
-        else if (ret==left)  System.out.print("\033[31m{\033[0m");
-        else if (ret==right) System.out.print("\033[31m}\033[0m");
-        else System.out.print(ret);
+        if (debug) {
+            if      (ret==null) return null;
+            else if (ret==left)  System.err.print("\033[31m{\033[0m");
+            else if (ret==right) System.err.print("\033[31m}\033[0m");
+            else System.err.print(ret);
+        }
         return ret;
     }
 
@@ -83,6 +81,7 @@ public class Tib implements Input<Character> {
                 return null;
             }
             c = (char)i;
+            _scalar++;
             if (c=='\n') { _row++; _col=0; }
             else         _col++;
         }
@@ -133,64 +132,5 @@ public class Tib implements Input<Character> {
     }
     boolean blank = false;
 
-    // Grammar //////////////////////////////////////////////////////////////////////////////
-
-    public static class Grammar extends ReflectiveGrammar {
-        private int anon = 0;
-        private final Element ws = Sequence.maximal0(getNonTerminal("w"));
-        public Grammar(Class c) { super(c); dropAll.add(ws); }
-        public Object walk(Tree<String> tree) {
-            String head = tree.head();
-            if (tree.numChildren()==0) return super.walk(tree);
-            if ("{".equals(head)) {
-                Union u = new Union("???");
-                Union u2 = ((PreSequence)walk(tree, 0)).sparse(ws).buildUnion();
-                u2.add(Sequence.singleton(new Element[] { u }, 0));
-                return anonymousNonTerminal(new Sequence[][] {
-                    new Sequence[] {
-                        Sequence.singleton(new Element[] { CharRange.leftBrace,
-                                                           ws,
-                                                           u2,
-                                                           ws,
-                                                           CharRange.rightBrace
-                        }, 2)
-                    }
-                });
-            }
-            return super.walk(tree);
-        }
-    }
-
-    /*
-public class Braces extends Union {
-
-    private static final Element left  = string("{");
-    private static final Element right = string("}");
-    
-    public static String join(Object[] e) {
-        StringBuffer ret = new StringBuffer();
-        for(int i=0; i<e.length; i++) {
-            if (i>0) ret.append(" ");
-            ret.append(e[i]);
-        }
-        return ret.toString();
-    }
-
-    public Braces(Element[] e, Element sep) {
-        super("{"+join(e)+"}");
-        Element[] e2 = new Element[sep == null ? e.length+2 : e.length + 4];
-        e2[0]           = left;
-        e2[e2.length-1] = right;
-        if (sep != null) {
-            e2[1] = sep;
-            e2[e2.length-2] = sep;
-        }
-        for(int i=0; i<e.length; i++) e2[i+(sep==null ? 1 : 2)] = e[i];
-        addAlternative(new Sequence.Singleton(e2));
-        addAlternative(new Sequence.Singleton(sep==null ? new Element[] { left, this, right} : new Element[] { left, sep, this, sep, right }));
-    }
-}
-                      */
-
 }