001: package gnu.kawa.brl;
002:
003: import gnu.kawa.lispexpr.*;
004: import gnu.text.SyntaxException;
005: import gnu.mapping.*;
006: import gnu.text.SourceMessages;
007: import gnu.lists.*;
008:
009: /** A class to read Scheme forms (S-expressions). */
010:
011: public class BRLRead extends LispReader {
012: int nesting;
013:
014: /** True if in literal text (even if nested inside an escaped expression). */
015: public boolean inLiteral() {
016: return ((InPort) port).readState == ']';
017: }
018:
019: void init() {
020: initialColonIsKeyword = false;
021: ((InPort) port).readState = ']';
022: }
023:
024: public BRLRead(InPort port) {
025: super (port);
026: init();
027: }
028:
029: public BRLRead(InPort port, SourceMessages messages) {
030: super (port, messages);
031: init();
032: }
033:
034: public Object readObject() throws java.io.IOException,
035: SyntaxException {
036: int startPos = tokenBufferLength;
037: InPort port = (InPort) getPort();
038: int saveNesting = nesting;
039: try {
040: for (;;) {
041: int ch = port.read();
042: if (ch < 0) {
043: if (port.readState != ']' && !isInteractive())
044: error('e', expressionStartFile,
045: expressionStartLine + 1,
046: expressionStartColumn,
047: "an unmatched '[' was read");
048: return Sequence.eofValue; // FIXME;
049: }
050: if (port.readState == ']') {
051: port.unread();
052: Object value = BRL.brlReader.read(this , ']', 1);
053: if (ch == '[' && value == BRL.emptyForm)
054: continue;
055: return value;
056: } else {
057: if (ch == ']')
058: port.readState = ']';
059: else {
060: nesting++;
061: Object value = readValues(ch, ReadTable
062: .getCurrent());
063: if (value != Values.empty) {
064: if (value == gnu.expr.QuoteExp.voidExp)
065: value = Values.empty;
066: return value;
067: }
068: nesting = saveNesting;
069: }
070: }
071: }
072: } finally {
073: nesting = saveNesting;
074: tokenBufferLength = startPos;
075: //((InPort) port).readState = saveReadState;
076: }
077: }
078:
079: public static Object readObject(InPort port)
080: throws java.io.IOException, SyntaxException {
081: return (new BRLRead(port)).readObject();
082: }
083:
084: boolean brlCompatible = false;
085:
086: public boolean isBrlCompatible() {
087: return brlCompatible;
088: }
089:
090: public void setBrlCompatible(boolean compat) {
091: brlCompatible = compat;
092: initialColonIsKeyword = compat;
093: }
094:
095: /** Record '[' location for error messages. */
096: String expressionStartFile;
097: int expressionStartLine;
098: int expressionStartColumn;
099:
100: void saveExpressionStartPosition() {
101: expressionStartFile = port.getName();
102: expressionStartLine = port.getLineNumber();
103: expressionStartColumn = port.getColumnNumber();
104: }
105: }
|