001: package gnu.kawa.brl;
002:
003: // BRLRead.java -- a class to read BRL forms
004: // Copyright (C) 2001 Bruce R. Lewis and Eaton Vance Management
005: // See the file COPYING for license terms.
006:
007: import gnu.text.*;
008: import gnu.mapping.InPort;
009: import gnu.lists.*;
010:
011: public class BRLReaderString extends gnu.kawa.lispexpr.ReadTableEntry {
012: public Object read(Lexer in, int ch, int count)
013: throws java.io.IOException {
014: int startPos = in.tokenBufferLength;
015: LineBufferedReader port = in.getPort();
016: char saveReadState = '\0';
017: int c = ch;
018: int prev;
019: char endch;
020: String startFile = port.getName();
021: int startLine = port.getLineNumber();
022: int startColumn = port.getColumnNumber();
023:
024: switch ((char) ch) {
025: case ']': // normal BRL case
026: endch = '[';
027: break;
028: case '}':
029: endch = '{';
030: break;
031: case '{':
032: endch = '}';
033: break;
034: case '[':
035: endch = ']';
036: break;
037: default:
038: // By default, symmetric string delimiters
039: endch = (char) ch;
040: break;
041: }
042:
043: if (port instanceof InPort) {
044: saveReadState = ((InPort) port).readState;
045: ((InPort) port).readState = (char) ch;
046: }
047:
048: try {
049: boolean inString = true;
050: while (inString) {
051: int next;
052:
053: prev = c;
054:
055: if (port.pos < port.limit && prev != '\r'
056: && prev != '\n')
057: c = port.buffer[port.pos++];
058: else
059: /* If no buffered data, or if port
060: might update lineNumber */
061: c = port.read();
062: if (c == endch) {
063: if (port.peek() == endch) {
064: in.tokenBufferAppend(c);
065: port.skip();
066: } else {
067: inString = false;
068: saveReadState = '\n';
069: if (in instanceof BRLRead)
070: ((BRLRead) in)
071: .saveExpressionStartPosition();
072: }
073: } else if (c == '\n' && in.isInteractive()
074: && ((BRLRead) in).nesting == 0) {
075: port.unread();
076: inString = false;
077: in.tokenBufferAppend(c);
078: } else {
079: if (c < 0) {
080: inString = false;
081: if (!in.isInteractive()
082: && ((BRLRead) in).nesting > 0)
083: in
084: .error('e', startFile,
085: startLine + 1, startColumn,
086: "nested literal text starting here was not ended by a '['");
087: } else
088: in.tokenBufferAppend(c);
089: }
090: }
091:
092: int length = in.tokenBufferLength - startPos;
093: if (length == 0)
094: return BRL.emptyForm;
095: else if (((BRLRead) in).isBrlCompatible())
096: return new FString(in.tokenBuffer, startPos, length);
097: else {
098: String str = new String(in.tokenBuffer, startPos,
099: length);
100: return new UnescapedData(str);
101: }
102: } finally {
103: in.tokenBufferLength = startPos;
104: if (port instanceof InPort)
105: ((InPort) port).readState = saveReadState;
106: }
107: }
108: }
|