X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Ftib%2FTib.java;h=387f5edda2c3064c1514956377d1b365aa9155bb;hp=e4123fcd689e28e092a63e59074a9cb8b4c9c63a;hb=f65ffd84486b1deb993d17bcdee5c3a9f5638a8c;hpb=ad3d28238e215a2dee9224c146a1928671c2ba89 diff --git a/src/edu/berkeley/sbp/tib/Tib.java b/src/edu/berkeley/sbp/tib/Tib.java index e4123fc..387f5ed 100644 --- a/src/edu/berkeley/sbp/tib/Tib.java +++ b/src/edu/berkeley/sbp/tib/Tib.java @@ -1,17 +1,12 @@ -// Copyright 2005 the Contributors, as shown in the revision logs. -// Licensed under the Apache Public Source License 2.0 ("the License"). -// You may not use this file except in compliance with the License. +// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license package edu.berkeley.sbp.tib; import edu.berkeley.sbp.*; import edu.berkeley.sbp.misc.*; +import edu.berkeley.sbp.chr.*; import java.util.*; import java.io.*; -// TODO: multiple {{ }} for superquotation -// TODO: strings -// TODO: comments - /** * A slow, ugly, inefficient, inelegant, ad-hoc parser for TIB files. * @@ -23,7 +18,9 @@ import java.io.*; * experimentation with the TIB spec. Once the spec is finalized it * should probably be rewritten. */ -public class Tib implements Token.Stream { +public class Tib implements Input { + + public String showRegion(Region r) { return ""; } public Tib(String s) throws IOException { this(new StringReader(s)); } public Tib(Reader r) throws IOException { this(new BufferedReader(r)); } @@ -31,35 +28,40 @@ public class Tib implements Token.Stream { public Tib(BufferedReader br) throws IOException { this.br = br; istack.add(-1); - //cur = parse(br); - //System.out.println("\rparsing: \"" + cur.toString(0, -1) + "\""); } - + public String getName() { return null; } private String s = ""; int pos = 0; int spos = 0; int _row = 1; int _col = 0; - public Token.Location getLocation() { return new CartesianInput.Location(_row, _col); } + int _scalar = 0; + public Input.Location getLocation() { return new Cartesian.Location(_col, _row, _scalar); } private BufferedReader br; + char left = CharAtom.left; + char right = CharAtom.right; + boolean waiting = false; char waitingChar = ' '; boolean indenting = true; int indentation = 0; private ArrayList istack = new ArrayList(); - public Character next(int numstates, int resets, int waits) throws IOException { - Character ret = nextc(numstates, resets); - if (ret==CharToken.left) System.out.print("\033[31m{\033[0m"); - else if (ret==CharToken.right) System.out.print("\033[31m}\033[0m"); - else if (ret==null) return null; - else System.out.print(ret); + private static boolean debug = "true".equals(System.getProperty("tib.debug", "false")); + public Character next() throws IOException { + Character ret = nextc(); + if (debug) { + if (ret==null) return null; + else if (ret==left) System.err.print("\033[31m{\033[0m"); + else if (ret==right) System.err.print("\033[31m}\033[0m"); + else System.err.print(ret); + } return ret; } Character waitingBrace = null; - public Character nextc(int numstates, int resets) throws IOException { + public Character nextc() throws IOException { char c; if (waitingBrace != null) { Character ret = waitingBrace; @@ -74,25 +76,26 @@ public class Tib implements Token.Stream { if (i==-1) { if (istack.size() > 1) { istack.remove(istack.size()-1); - return CharToken.right; + return right; } return null; } c = (char)i; + _scalar++; + if (c=='\n') { _row++; _col=0; } + else _col++; } - if (c=='\n') { _row++; _col=0; } - else _col++; if (indenting) { if (c==' ') { indentation++; return done(c); } - if (c=='\n') { indentation = 0; if (blank) return nextc(numstates, resets); blank = true; waiting = true; waitingChar='\n'; return '\n'; } + if (c=='\n') { indentation = 0; if (blank) return nextc(); blank = true; waiting = true; waitingChar='\n'; return '\n'; } int last = istack.size()==0 ? -1 : istack.get(istack.size()-1); if (indentation==last) { if (blank) { indenting = false; waitingChar = c; waiting = true; - waitingBrace = CharToken.left; - return CharToken.right; + waitingBrace = left; + return right; //return nextc(numstates); } blank = false; @@ -106,12 +109,12 @@ public class Tib implements Token.Stream { indenting = false; istack.add(indentation); //System.out.print("\033[31m+"+indentation+"+\033[0m"); - return CharToken.left; + return left; } else /*if (indentation < last)*/ { istack.remove(istack.size()-1); //System.out.print("\033[31m-"+last+"-\033[0m"); blank = true; - return CharToken.right; + return right; } } else { blank = false; @@ -122,75 +125,12 @@ public class Tib implements Token.Stream { public Character done(char c) { switch(c) { - case '{': return CharToken.left; - case '}': return CharToken.right; + case '{': return left; + case '}': return right; default: return c; } } boolean blank = false; - // Grammar ////////////////////////////////////////////////////////////////////////////// - - public static class Grammar extends MetaGrammar { - private int anon = 0; - private final Element ws = Repeat.maximal0(nonTerminal("w")); - public Grammar() { dropAll.add(ws); } - public Object walk(Tree tree) { - String head = tree.head(); - if (tree.numChildren()==0) return super.walk(tree); - if ("{".equals(head)) { - String s = "braced"+(anon++); - Union u = nonTerminal(s); - Union u2 = ((PreSequence)walk(tree, 0)).sparse(ws).buildUnion(); - u2.add(Sequence.singleton(new Element[] { u }, 0, null, null)); - return nonTerminal(s, - new PreSequence[][] { - new PreSequence[] { - new PreSequence(new Element[] { CharToken.leftBrace, - ws, - u2, - ws, - CharToken.rightBrace - }) - } - }, - false, - false); - } - return super.walk(tree); - } - } - - /* -public class Braces extends Union { - - private static final Element left = CharToken.string("{"); - private static final Element right = CharToken.string("}"); - - public static String join(Object[] e) { - StringBuffer ret = new StringBuffer(); - for(int i=0; i0) ret.append(" "); - ret.append(e[i]); - } - return ret.toString(); - } - - public Braces(Element[] e, Element sep) { - super("{"+join(e)+"}"); - Element[] e2 = new Element[sep == null ? e.length+2 : e.length + 4]; - e2[0] = left; - e2[e2.length-1] = right; - if (sep != null) { - e2[1] = sep; - e2[e2.length-2] = sep; - } - for(int i=0; i