X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Ftib%2FTib.java;h=41aab5cd869fdd8e4e0322916664170670bc02f6;hp=15c286a19e686905c2cbe126708a695af526c909;hb=dcf8aafe73cbd899d8aebd2ef92df5a75a2c2b69;hpb=6a2ea790f843e058c7e67d3c7d1deebadcfe1fd5 diff --git a/src/edu/berkeley/sbp/tib/Tib.java b/src/edu/berkeley/sbp/tib/Tib.java index 15c286a..41aab5c 100644 --- a/src/edu/berkeley/sbp/tib/Tib.java +++ b/src/edu/berkeley/sbp/tib/Tib.java @@ -1,6 +1,4 @@ -// 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.*; @@ -9,10 +7,6 @@ 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. * @@ -24,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 Input.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)); } @@ -35,35 +31,39 @@ public class Tib implements Input.Stream { //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 Input.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 = CharRange.left; - char right = CharRange.right; + 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==left) System.out.print("\033[31m{\033[0m"); - else if (ret==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.out.print("\033[31m{\033[0m"); + else if (ret==right) System.out.print("\033[31m}\033[0m"); + else System.out.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; @@ -83,12 +83,13 @@ public class Tib implements Input.Stream { 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) { @@ -135,35 +136,33 @@ public class Tib implements Input.Stream { // Grammar ////////////////////////////////////////////////////////////////////////////// - public static class Grammar extends MetaGrammar { + /* + public static class Grammar extends ReflectiveGrammar { private int anon = 0; - private final Element ws = Repeat.maximal0(nonTerminal("w")); - public Grammar() { dropAll.add(ws); } + private final Element ws = Sequence.maximal0(getNonTerminal("w")); + public Grammar(Class c) { super(c); 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 u = new Union("???"); 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[] { CharRange.leftBrace, - ws, - u2, - ws, - CharRange.rightBrace - }) - } - }, - false, - false); + u2.add(Sequence.singleton(new Element[] { u }, 0)); + return anonymousNonTerminal(new Sequence[][] { + new Sequence[] { + Sequence.singleton(new Element[] { CharAtom.leftBrace, + ws, + u2, + ws, + CharAtom.rightBrace + }, 2) + } + }); } return super.walk(tree); } } + */ /* public class Braces extends Union {