1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
3 package edu.berkeley.sbp.tib;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.misc.*;
6 import edu.berkeley.sbp.chr.*;
11 * A slow, ugly, inefficient, inelegant, ad-hoc parser for TIB files.
13 * Despite being inelegant, this parser keeps all internal state on
14 * the stack (the only heap objects created are those which are
17 * This was written as an ad-hoc parser to facilitate
18 * experimentation with the TIB spec. Once the spec is finalized it
19 * should probably be rewritten.
21 public class Tib implements Input<Character> {
23 public String showRegion(Region<Character> r) { return ""; }
25 public Tib(String s) throws IOException { this(new StringReader(s)); }
26 public Tib(Reader r) throws IOException { this(new BufferedReader(r)); }
27 public Tib(InputStream is) throws IOException { this(new BufferedReader(new InputStreamReader(is))); }
28 public Tib(BufferedReader br) throws IOException {
32 public String getName() { return null; }
33 private String s = "";
40 public Input.Location getLocation() { return new Cartesian.Location(_col, _row, _scalar); }
41 private BufferedReader br;
43 char left = CharAtom.left;
44 char right = CharAtom.right;
46 boolean waiting = false;
47 char waitingChar = ' ';
48 boolean indenting = true;
50 private ArrayList<Integer> istack = new ArrayList<Integer>();
51 private static boolean debug = "true".equals(System.getProperty("tib.debug", "false"));
52 public Character next() throws IOException {
53 Character ret = nextc();
55 if (ret==null) return null;
56 else if (ret==left) System.err.print("\033[31m{\033[0m");
57 else if (ret==right) System.err.print("\033[31m}\033[0m");
58 else System.err.print(ret);
63 Character waitingBrace = null;
64 public Character nextc() throws IOException {
66 if (waitingBrace != null) {
67 Character ret = waitingBrace;
77 if (istack.size() > 1) {
78 istack.remove(istack.size()-1);
85 if (c=='\n') { _row++; _col=0; }
89 if (c==' ') { indentation++; return done(c); }
90 if (c=='\n') { indentation = 0; if (blank) return nextc(); 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;