01: // Copyright (c) 2001 Per M.A. Bothner
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.kawa.lispexpr;
05:
06: import gnu.text.*;
07: import gnu.mapping.InPort;
08:
09: public class ReaderString extends ReadTableEntry {
10: public Object read(Lexer in, int ch, int count)
11: throws java.io.IOException, SyntaxException {
12: int startPos = in.tokenBufferLength;
13: LineBufferedReader port = in.getPort();
14: char saveReadState = '\0';
15: int c = ch;
16: int prev;
17: if (port instanceof InPort) {
18: saveReadState = ((InPort) port).readState;
19: ((InPort) port).readState = (char) ch;
20: }
21: try {
22: for (;;) {
23: int next;
24:
25: prev = c;
26:
27: // Read next char - inline the common case.
28: if (prev == '\r') {
29: c = port.read();
30: if (c == '\n')
31: continue;
32: } else if (port.pos < port.limit && prev != '\n')
33: c = port.buffer[port.pos++];
34: else
35: c = port.read();
36: if (c == ch) {
37: break;
38: }
39: switch (c) {
40: case '\r':
41: in.tokenBufferAppend('\n');
42: continue;
43: case '\\':
44: if (in instanceof LispReader)
45: c = ((LispReader) in).readEscape();
46: else
47: c = port.read();
48: if (c == -2) {
49: c = '\n'; // So prev gets set ...
50: continue;
51: }
52: /* ... fall through ... */
53: default:
54: if (c < 0)
55: in.eofError("unexpected EOF in string literal");
56: in.tokenBufferAppend(c);
57: break;
58: }
59: }
60: return new gnu.lists.FString(in.tokenBuffer, startPos,
61: in.tokenBufferLength - startPos);
62: } finally {
63: in.tokenBufferLength = startPos;
64: if (port instanceof InPort)
65: ((InPort) port).readState = saveReadState;
66: }
67: }
68: }
|