checkpoint
[sbp.git] / src / edu / berkeley / sbp / tib / Tib.java
1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp.tib;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.misc.*;
6 import edu.berkeley.sbp.chr.*;
7 import java.util.*;
8 import java.io.*;
9
10 // TODO: multiple {{ }} for superquotation
11 // TODO: strings
12 // TODO: comments
13
14 /**
15  *   A slow, ugly, inefficient, inelegant, ad-hoc parser for TIB files.
16  *
17  *   Despite being inelegant, this parser keeps all internal state on
18  *   the stack (the only heap objects created are those which are
19  *   returned).
20  *
21  *   This was written as an ad-hoc parser to facilitate
22  *   experimentation with the TIB spec.  Once the spec is finalized it
23  *   should probably be rewritten.
24  */
25 public class Tib implements Input<Character> {
26
27     public String showRegion(Region<Character> r) { return null; }
28
29     public Tib(String s) throws IOException { this(new StringReader(s)); }
30     public Tib(Reader r) throws IOException { this(new BufferedReader(r)); }
31     public Tib(InputStream is) throws IOException { this(new BufferedReader(new InputStreamReader(is))); }
32     public Tib(BufferedReader br) throws IOException {
33         this.br = br;
34         istack.add(-1);
35         //cur = parse(br);
36         //System.out.println("\rparsing: \"" + cur.toString(0, -1) + "\"");
37     }
38
39     private String s = "";
40     int pos = 0;
41     int spos = 0;
42
43     int _row = 1;
44     int _col = 0;
45     int _scalar = 0;
46     public Input.Location getLocation() { return new Cartesian.Location(_col, _row, _scalar); }
47     private BufferedReader br;
48
49     char left = CharAtom.left;
50     char right = CharAtom.right;
51
52     boolean waiting = false;
53     char waitingChar = ' ';
54     boolean indenting = true;
55     int indentation = 0;
56     private ArrayList<Integer> istack = new ArrayList<Integer>();
57     public Character next() throws IOException {
58         Character ret = nextc();
59         if      (ret==null) return null;
60         else if (ret==left)  System.out.print("\033[31m{\033[0m");
61         else if (ret==right) System.out.print("\033[31m}\033[0m");
62         else System.out.print(ret);
63         return ret;
64     }
65
66     Character waitingBrace = null;
67     public Character nextc() throws IOException {
68         char c;
69         if (waitingBrace != null) {
70             Character ret = waitingBrace;
71             waitingBrace = null;
72             return ret;
73         }
74         if (waiting) {
75             waiting = false;
76             c = waitingChar;
77         } else {
78             int i = br.read();
79             if (i==-1) {
80                 if (istack.size() > 1) {
81                     istack.remove(istack.size()-1);
82                     return right;
83                 }
84                 return null;
85             }
86             c = (char)i;
87             _scalar++;
88             if (c=='\n') { _row++; _col=0; }
89             else         _col++;
90         }
91         if (indenting) {
92             if (c==' ') { indentation++; return done(c); }
93             if (c=='\n') { indentation = 0; if (blank) return nextc(); blank = true; waiting = true; waitingChar='\n'; return '\n'; }
94             int last = istack.size()==0 ? -1 : istack.get(istack.size()-1);
95             if (indentation==last) {
96                 if (blank) {
97                     indenting = false;
98                     waitingChar = c;
99                     waiting = true;
100                     waitingBrace = left;
101                     return right;
102                     //return nextc(numstates);
103                 }
104                 blank = false;
105                 indenting = false;
106                 return done(c);
107             }
108             blank = false;
109             waitingChar = c;
110             waiting = true;
111             if (indentation > last) {
112                 indenting = false;
113                 istack.add(indentation);
114                 //System.out.print("\033[31m+"+indentation+"+\033[0m");
115                 return left;
116             } else /*if (indentation < last)*/ {
117                 istack.remove(istack.size()-1);
118                 //System.out.print("\033[31m-"+last+"-\033[0m");
119                 blank = true;
120                 return right;
121             }
122         } else {
123             blank = false;
124             if (c=='\n') { indenting=true; indentation = 0; }
125             return done(c);
126         }
127     }
128
129     public Character done(char c) {
130         switch(c) {
131             case '{': return left;
132             case '}': return right;
133             default: return c;
134         }
135     }
136     boolean blank = false;
137
138     // Grammar //////////////////////////////////////////////////////////////////////////////
139
140     /*
141     public static class Grammar extends ReflectiveGrammar {
142         private int anon = 0;
143         private final Element ws = Sequence.maximal0(getNonTerminal("w"));
144         public Grammar(Class c) { super(c); dropAll.add(ws); }
145         public Object walk(Tree<String> tree) {
146             String head = tree.head();
147             if (tree.numChildren()==0) return super.walk(tree);
148             if ("{".equals(head)) {
149                 Union u = new Union("???");
150                 Union u2 = ((PreSequence)walk(tree, 0)).sparse(ws).buildUnion();
151                 u2.add(Sequence.singleton(new Element[] { u }, 0));
152                 return anonymousNonTerminal(new Sequence[][] {
153                     new Sequence[] {
154                         Sequence.singleton(new Element[] { CharAtom.leftBrace,
155                                                            ws,
156                                                            u2,
157                                                            ws,
158                                                            CharAtom.rightBrace
159                         }, 2)
160                     }
161                 });
162             }
163             return super.walk(tree);
164         }
165     }
166     */
167
168     /*
169 public class Braces extends Union {
170
171     private static final Element left  = string("{");
172     private static final Element right = string("}");
173     
174     public static String join(Object[] e) {
175         StringBuffer ret = new StringBuffer();
176         for(int i=0; i<e.length; i++) {
177             if (i>0) ret.append(" ");
178             ret.append(e[i]);
179         }
180         return ret.toString();
181     }
182
183     public Braces(Element[] e, Element sep) {
184         super("{"+join(e)+"}");
185         Element[] e2 = new Element[sep == null ? e.length+2 : e.length + 4];
186         e2[0]           = left;
187         e2[e2.length-1] = right;
188         if (sep != null) {
189             e2[1] = sep;
190             e2[e2.length-2] = sep;
191         }
192         for(int i=0; i<e.length; i++) e2[i+(sep==null ? 1 : 2)] = e[i];
193         addAlternative(new Sequence.Singleton(e2));
194         addAlternative(new Sequence.Singleton(sep==null ? new Element[] { left, this, right} : new Element[] { left, sep, this, sep, right }));
195     }
196 }
197                       */
198
199 }
200