1 // Copyright 2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
5 package edu.berkeley.sbp.tib;
6 import edu.berkeley.sbp.*;
7 import edu.berkeley.sbp.misc.*;
8 import edu.berkeley.sbp.chr.*;
12 // TODO: multiple {{ }} for superquotation
17 * A slow, ugly, inefficient, inelegant, ad-hoc parser for TIB files.
19 * Despite being inelegant, this parser keeps all internal state on
20 * the stack (the only heap objects created are those which are
23 * This was written as an ad-hoc parser to facilitate
24 * experimentation with the TIB spec. Once the spec is finalized it
25 * should probably be rewritten.
27 public class Tib implements Input<Character> {
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 {
36 //System.out.println("\rparsing: \"" + cur.toString(0, -1) + "\"");
39 private String s = "";
45 public Input.Location getLocation() { return new Cartesian.Location(_col, _row); }
46 private BufferedReader br;
48 char left = CharRange.left;
49 char right = CharRange.right;
51 boolean waiting = false;
52 char waitingChar = ' ';
53 boolean indenting = true;
55 private ArrayList<Integer> istack = new ArrayList<Integer>();
56 public Character next() throws IOException {
57 Character ret = nextc();
58 if (ret==null) return null;
59 else if (ret==left) System.out.print("\033[31m{\033[0m");
60 else if (ret==right) System.out.print("\033[31m}\033[0m");
61 else System.out.print(ret);
65 Character waitingBrace = null;
66 public Character nextc() throws IOException {
68 if (waitingBrace != null) {
69 Character ret = waitingBrace;
79 if (istack.size() > 1) {
80 istack.remove(istack.size()-1);
86 if (c=='\n') { _row++; _col=0; }
90 if (c==' ') { indentation++; return done(c); }
91 if (c=='\n') { indentation = 0; if (blank) return nextc(); blank = true; waiting = true; waitingChar='\n'; return '\n'; }
92 int last = istack.size()==0 ? -1 : istack.get(istack.size()-1);
93 if (indentation==last) {
100 //return nextc(numstates);
109 if (indentation > last) {
111 istack.add(indentation);
112 //System.out.print("\033[31m+"+indentation+"+\033[0m");
114 } else /*if (indentation < last)*/ {
115 istack.remove(istack.size()-1);
116 //System.out.print("\033[31m-"+last+"-\033[0m");
122 if (c=='\n') { indenting=true; indentation = 0; }
127 public Character done(char c) {
129 case '{': return left;
130 case '}': return right;
134 boolean blank = false;
136 // Grammar //////////////////////////////////////////////////////////////////////////////
139 public static class Grammar extends ReflectiveGrammar {
140 private int anon = 0;
141 private final Element ws = Sequence.maximal0(getNonTerminal("w"));
142 public Grammar(Class c) { super(c); dropAll.add(ws); }
143 public Object walk(Tree<String> tree) {
144 String head = tree.head();
145 if (tree.numChildren()==0) return super.walk(tree);
146 if ("{".equals(head)) {
147 Union u = new Union("???");
148 Union u2 = ((PreSequence)walk(tree, 0)).sparse(ws).buildUnion();
149 u2.add(Sequence.singleton(new Element[] { u }, 0));
150 return anonymousNonTerminal(new Sequence[][] {
152 Sequence.singleton(new Element[] { CharRange.leftBrace,
161 return super.walk(tree);
167 public class Braces extends Union {
169 private static final Element left = string("{");
170 private static final Element right = string("}");
172 public static String join(Object[] e) {
173 StringBuffer ret = new StringBuffer();
174 for(int i=0; i<e.length; i++) {
175 if (i>0) ret.append(" ");
178 return ret.toString();
181 public Braces(Element[] e, Element sep) {
182 super("{"+join(e)+"}");
183 Element[] e2 = new Element[sep == null ? e.length+2 : e.length + 4];
185 e2[e2.length-1] = right;
188 e2[e2.length-2] = sep;
190 for(int i=0; i<e.length; i++) e2[i+(sep==null ? 1 : 2)] = e[i];
191 addAlternative(new Sequence.Singleton(e2));
192 addAlternative(new Sequence.Singleton(sep==null ? new Element[] { left, this, right} : new Element[] { left, sep, this, sep, right }));