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<CharToken> {
28 public Tib(String s) throws IOException, Invalid { this(new StringReader(s)); }
29 public Tib(Reader r) throws IOException, Invalid { this(new BufferedReader(r)); }
30 public Tib(InputStream is) throws IOException, Invalid { this(new BufferedReader(new InputStreamReader(is))); }
31 public Tib(BufferedReader br) throws IOException, Invalid {
35 //System.out.println("\rparsing: \"" + cur.toString(0, -1) + "\"");
39 private String s = "";
45 public Token.Location getLocation() { return new CharToken.CartesianLocation(_row, _col); }
46 private BufferedReader br;
48 boolean waiting = false;
49 char waitingChar = ' ';
50 boolean indenting = true;
52 private ArrayList<Integer> istack = new ArrayList<Integer>();
53 public CharToken next(int numstates) throws IOException {
54 CharToken ret = nextc(numstates);
55 if (ret==CharToken.left) System.out.print("\033[31m{\033[0m");
56 else if (ret==CharToken.right) System.out.print("\033[31m}\033[0m");
57 else if (ret==null) return null;
58 else System.out.print(ret.c);
62 CharToken waitingBrace = null;
63 public CharToken nextc(int numstates) throws IOException {
65 if (waitingBrace != null) {
66 CharToken ret = waitingBrace;
76 if (istack.size() > 1) {
77 istack.remove(istack.size()-1);
78 return CharToken.right;
84 if (c=='\n') { _row++; _col=0; }
87 if (c==' ') { indentation++; return done(c); }
88 if (c=='\n') { indentation = 0; if (blank) return nextc(numstates); blank = true; waiting = true; waitingChar='\n'; return new CharToken('\n'); }
89 int last = istack.size()==0 ? -1 : istack.get(istack.size()-1);
90 if (indentation==last) {
95 waitingBrace = CharToken.left;
96 return CharToken.right;
97 //return nextc(numstates);
106 if (indentation > last) {
108 istack.add(indentation);
109 System.out.print("\033[31m+"+indentation+"+\033[0m");
110 return CharToken.left;
111 } else /*if (indentation < last)*/ {
112 istack.remove(istack.size()-1);
113 System.out.print("\033[31m-"+last+"-\033[0m");
115 return CharToken.right;
119 if (c=='\n') { indenting=true; indentation = 0; }
123 public CharToken done(char c) {
125 case '{': return CharToken.left;
126 case '}': return CharToken.right;
127 default: return new CharToken(c);
130 boolean blank = false;
132 public CharToken next(int numstates) throws IOException {
133 if (cur==null) return null;
135 if (spos < s.length()) {
136 char c = s.charAt(spos++);
137 if (c=='\n') { _row++; _col = 0; }
139 return new CharToken(c);
143 if (pos >= cur.size()) {
148 if (cur==null) return null;
149 return CharToken.right;
151 Object o = cur.child(pos++);
152 if (o instanceof String) {
155 return next(numstates);
157 if (o instanceof Block) {
162 if (((Block)o).isLiteral()) {
164 s = ((Block.Literal)o).text();
165 return next(numstates);
169 return CharToken.left;
172 public static Block parse(BufferedReader br) throws Invalid, IOException {
175 boolean blankLine = false;
176 Block top = new Block.Root();
177 for(String s = br.readLine(); s != null; s = br.readLine()) {
180 while (s.length() > 0 &&
181 s.charAt(0) == ' ' &&
182 (!(top instanceof Block.Literal) || col < top.col)) { col++; s = s.substring(1); }
183 if ((top instanceof Block.Literal) && col >= top.col) { top.add(s); continue; }
184 if (s.length()==0) { blankLine = true; continue; }
185 while (col < top.col) {
186 if (s.startsWith("{}") && top instanceof Block.Literal && ((Block.Literal)top).braceCol == col) break;
190 top = top.closeIndent();
192 if (s.startsWith("{}")) {
194 boolean append = top instanceof Block.Literal && ((Block.Literal)top).braceCol == bc;
197 while (s.length() > 0 && s.charAt(0) == ' ' && !(append && col >= top.col) ) { col++; s = s.substring(1); }
198 if (append) top.add(s); else (top = new Block.Literal(top, row, col, bc)).add(s);
201 while (s.length() > 0 && s.charAt(s.length()-1)==' ') { s = s.substring(0, s.length()-1); }
202 if (col > top.col) top = new Block.Indent(top, row, col);
203 else if (blankLine) { top.endrow=row; top.endcol=col; top = top.closeIndent(); top = new Block.Indent(top, row, col); }
205 for(int i=0; i<s.length(); i++) {
206 top.add(s.charAt(i));
207 switch(s.charAt(i)) {
208 case '{': top = new Block.Brace(top, row, col); break;
209 case '}': top.endrow=row; top.endcol=col; top = top.closeBrace(); break;
217 while (ret.parent != null) ret = ret.parent;
219 } catch (InternalException ie) {
220 throw new Invalid(ie, row, col);
224 public static class Block {
226 public final int row;
227 public final int col;
230 public final int iip;
231 private final Vector children = new Vector();
232 private String pending = "";
234 public int size() { return children.size(); }
235 public Object child(int i) { return children.elementAt(i); }
236 public boolean isLiteral() { return false; }
238 protected Block(int row, int col) {
244 public Block(Block parent, int row, int col) {
247 this.iip = parent.size();
248 (this.parent = parent).add(this);
251 public void add(String s) { children.addElement(s); }
252 public void add(char c) {
253 if (c == '{' || c == '}') { finishWord(); return; }
254 if (c != ' ') { pending += c; return; }
255 if (pending.length() > 0) { finishWord(); add(" "); return; }
256 if (size()==0) return;
257 if (child(size()-1).equals(" ")) return;
262 public void add(Block b) { children.addElement(b); }
263 public Block promote() { parent.parent.replaceLast(this); return close(); }
264 public Object lastChild() { return children.lastElement(); }
265 public Block lastChildAsBlock() { return (Block)lastChild(); }
266 public void replaceLast(Block b) { children.setElementAt(b, children.size()-1); b.parent = this; }
268 public void finishWord() { if (pending.length() > 0) { add(pending); pending = ""; } }
270 public Block closeBrace() { throw new InternalException("attempt to closeBrace() a "+getClass().getName()); }
271 public Block closeIndent() { throw new InternalException("attempt to closeIndent() a "+getClass().getName()); }
272 public Block close() {
273 while(size() > 0 && child(size()-1).equals(" ")) children.setSize(children.size()-1);
274 if (size()==0) throw new InternalException("PARSER BUG: attempt to close an empty block (should never happen)");
275 if (size() > 1 || !(lastChild() instanceof Block)) return parent;
276 return lastChildAsBlock().promote();
278 public String toString() { return toString(80); }
279 public String toString(int justificationLimit) { return toString(0, 80); }
280 protected String toString(int indent, int justificationLimit) {
281 StringBuffer ret = new StringBuffer();
282 StringBuffer line = new StringBuffer();
283 for(int i=0; i<children.size(); i++) {
284 Object o = children.elementAt(i);
285 if (i>0 && children.elementAt(i-1) instanceof Block && justificationLimit!=-1) ret.append("\n");
286 if (o instanceof Block) {
287 ret.append(justify(line.toString(), indent, justificationLimit));
289 if (justificationLimit==-1) {
291 ret.append(((Block)o).toString(indent+2, justificationLimit));
294 ret.append(((Block)o).toString(indent+2, justificationLimit));
297 line.append(o.toString());
300 ret.append(justify(line.toString(), indent, justificationLimit));
301 return ret.toString();
304 private static class Root extends Block {
305 public Root() { super(0, Integer.MIN_VALUE); }
306 public Block close() { throw new InternalException("attempted to close top block"); }
307 public String toString(int justificationLimit) { return toString(-2, justificationLimit); }
310 private static class Brace extends Block {
311 public Brace(Block parent, int row, int col) { super(parent, row, col); }
312 public Block closeBrace() { return super.close(); }
315 private static class Indent extends Block {
316 public Indent(Block parent, int row, int col) { super(parent, row, col); }
317 public Block closeIndent() { return super.close(); }
320 private static class Literal extends Block {
321 private StringBuffer content = new StringBuffer();
322 public final int braceCol;
323 public Literal(Block parent, int row, int col, int braceCol) { super(parent,row,col); this.braceCol = braceCol; }
324 public boolean isLiteral() { return true; }
325 public int size() { return 1; }
326 public Object child(int i) { return i==0 ? content.toString() : null; }
327 public Block close() { return parent; }
328 public Block closeIndent() { return close(); }
329 public void add(String s) { if (content.length()>0) content.append('\n'); content.append(s); }
330 public String text() { return content.toString(); }
331 protected String toString(int indent, int justificationLimit) {
332 StringBuffer ret = new StringBuffer();
333 String s = content.toString();
334 while(s.length() > 0) {
335 int nl = s.indexOf('\n');
336 if (nl==-1) nl = s.length();
337 ret.append(spaces(indent));
339 ret.append(s.substring(0, nl));
340 s = s.substring(Math.min(s.length(),nl+1));
343 return ret.toString();
349 // Exceptions //////////////////////////////////////////////////////////////////////////////
351 private static class InternalException extends RuntimeException { public InternalException(String s) { super(s); } }
352 public static class Invalid extends /*IOException*/RuntimeException {
353 public Invalid(InternalException ie, int row, int col) {
354 super(ie.getMessage() + " at " + row + ":" + col);
358 // Testing //////////////////////////////////////////////////////////////////////////////
360 public static void main(String[] s) throws Exception {
361 System.out.println(parse(new BufferedReader(new InputStreamReader(System.in))).toString(-1)); }
363 // Utilities //////////////////////////////////////////////////////////////////////////////
365 public static String spaces(int i) { if (i<=0) return ""; return " " + spaces(i-1); }
367 private static String justify(String s, int indent, int justificationLimit) {
368 if (s.length() == 0) return "";
369 if (justificationLimit==-1) return s;
370 StringBuffer ret = new StringBuffer();
371 while(s.length() > 0) {
372 if (s.charAt(0) == ' ') { s = s.substring(1); continue; }
373 ret.append(spaces(indent));
374 int i = s.indexOf(' ');
375 if (i==-1) i = s.length();
376 while(s.indexOf(' ', i+1) != -1 && s.indexOf(' ', i+1) < justificationLimit-indent) i = s.indexOf(' ', i+1);
377 if (s.length() + indent < justificationLimit) i = s.length();
378 ret.append(s.substring(0, i));
382 return ret.toString();
385 // Grammar //////////////////////////////////////////////////////////////////////////////
387 public static class Grammar extends MetaGrammar {
388 private int anon = 0;
389 private final Element ws = Repeat.maximal0(nonTerminal("w"));
390 public Grammar() { dropAll.add(ws); }
391 public Object walk(Tree<String> tree) {
392 String head = tree.head();
393 if (tree.numChildren()==0) return super.walk(tree);
394 if ("{".equals(head)) {
395 String s = "braced"+(anon++);
396 Union u = nonTerminal(s);
397 Union u2 = ((PreSequence)walk(tree, 0)).sparse(ws).buildUnion();
398 u2.add(Sequence.singleton(new Element[] { u }, 0, null, null));
399 return nonTerminal(s,
400 new PreSequence[][] {
402 new PreSequence(new Element[] { CharToken.leftBrace,
413 return super.walk(tree);