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.*;
11 // TODO: multiple {{ }} for superquotation
16 * A slow, ugly, inefficient, inelegant, ad-hoc parser for TIB files.
18 * Despite being inelegant, this parser keeps all internal state on
19 * the stack (the only heap objects created are those which are
22 * This was written as an ad-hoc parser to facilitate
23 * experimentation with the TIB spec. Once the spec is finalized it
24 * should probably be rewritten.
26 public class Tib implements Token.Stream<Character> {
28 public Tib(String s) throws IOException { this(new StringReader(s)); }
29 public Tib(Reader r) throws IOException { this(new BufferedReader(r)); }
30 public Tib(InputStream is) throws IOException { this(new BufferedReader(new InputStreamReader(is))); }
31 public Tib(BufferedReader br) throws IOException {
35 //System.out.println("\rparsing: \"" + cur.toString(0, -1) + "\"");
38 private String s = "";
44 public Token.Location getLocation() { return new CartesianInput.Location(_row, _col); }
45 private BufferedReader br;
47 char left = CharRange.left;
48 char right = CharRange.right;
50 boolean waiting = false;
51 char waitingChar = ' ';
52 boolean indenting = true;
54 private ArrayList<Integer> istack = new ArrayList<Integer>();
55 public Character next(int numstates, int resets, int waits) throws IOException {
56 Character ret = nextc(numstates, resets);
57 if (ret==left) System.out.print("\033[31m{\033[0m");
58 else if (ret==right) System.out.print("\033[31m}\033[0m");
59 else if (ret==null) return null;
60 else System.out.print(ret);
64 Character waitingBrace = null;
65 public Character nextc(int numstates, int resets) throws IOException {
67 if (waitingBrace != null) {
68 Character ret = waitingBrace;
78 if (istack.size() > 1) {
79 istack.remove(istack.size()-1);
86 if (c=='\n') { _row++; _col=0; }
89 if (c==' ') { indentation++; return done(c); }
90 if (c=='\n') { indentation = 0; if (blank) return nextc(numstates, resets); blank = true; waiting = true; waitingChar='\n'; return '\n'; }
91 int last = istack.size()==0 ? -1 : istack.get(istack.size()-1);
92 if (indentation==last) {
99 //return nextc(numstates);
108 if (indentation > last) {
110 istack.add(indentation);
111 //System.out.print("\033[31m+"+indentation+"+\033[0m");
113 } else /*if (indentation < last)*/ {
114 istack.remove(istack.size()-1);
115 //System.out.print("\033[31m-"+last+"-\033[0m");
121 if (c=='\n') { indenting=true; indentation = 0; }
126 public Character done(char c) {
128 case '{': return left;
129 case '}': return right;
133 boolean blank = false;
135 // Grammar //////////////////////////////////////////////////////////////////////////////
137 public static class Grammar extends MetaGrammar {
138 private int anon = 0;
139 private final Element ws = Repeat.maximal0(nonTerminal("w"));
140 public Grammar() { dropAll.add(ws); }
141 public Object walk(Tree<String> tree) {
142 String head = tree.head();
143 if (tree.numChildren()==0) return super.walk(tree);
144 if ("{".equals(head)) {
145 String s = "braced"+(anon++);
146 Union u = nonTerminal(s);
147 Union u2 = ((PreSequence)walk(tree, 0)).sparse(ws).buildUnion();
148 u2.add(Sequence.singleton(new Element[] { u }, 0, null, null));
149 return nonTerminal(s,
150 new PreSequence[][] {
152 new PreSequence(new Element[] { CharRange.leftBrace,
163 return super.walk(tree);
168 public class Braces extends Union {
170 private static final Element left = string("{");
171 private static final Element right = string("}");
173 public static String join(Object[] e) {
174 StringBuffer ret = new StringBuffer();
175 for(int i=0; i<e.length; i++) {
176 if (i>0) ret.append(" ");
179 return ret.toString();
182 public Braces(Element[] e, Element sep) {
183 super("{"+join(e)+"}");
184 Element[] e2 = new Element[sep == null ? e.length+2 : e.length + 4];
186 e2[e2.length-1] = right;
189 e2[e2.length-2] = sep;
191 for(int i=0; i<e.length; i++) e2[i+(sep==null ? 1 : 2)] = e[i];
192 addAlternative(new Sequence.Singleton(e2));
193 addAlternative(new Sequence.Singleton(sep==null ? new Element[] { left, this, right} : new Element[] { left, sep, this, sep, right }));