KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > lispexpr > ReaderString


1 // Copyright (c) 2001 Per M.A. Bothner
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.kawa.lispexpr;
5 import gnu.text.*;
6 import gnu.mapping.InPort;
7
8 public class ReaderString extends ReadTableEntry
9 {
10   public Object JavaDoc read (Lexer in, int ch, int count)
11     throws java.io.IOException JavaDoc, SyntaxException
12   {
13     int startPos = in.tokenBufferLength;
14     LineBufferedReader port = in.getPort();
15     char saveReadState = '\0';
16     int c = ch;
17     int prev;
18     if (port instanceof InPort)
19       {
20     saveReadState = ((InPort) port).readState;
21     ((InPort) port).readState = (char) ch;
22       }
23     try
24       {
25     for (;;)
26       {
27         int next;
28
29         prev = c;
30
31         // Read next char - inline the common case.
32
if (prev == '\r')
33           {
34         c = port.read();
35         if (c == '\n')
36           continue;
37           }
38         else if (port.pos < port.limit && prev != '\n')
39           c = port.buffer[port.pos++];
40         else
41           c = port.read();
42         if (c == ch)
43           {
44         break;
45           }
46         switch (c)
47           {
48           case '\r':
49         in.tokenBufferAppend('\n');
50         continue;
51           case '\\':
52         if (in instanceof LispReader)
53           c = ((LispReader) in).readEscape();
54         else
55           c = port.read();
56         if (c == -2)
57           {
58             c = '\n'; // So prev gets set ...
59
continue;
60           }
61         /* ... fall through ... */
62           default:
63         if (c < 0)
64           in.eofError("unexpected EOF in string literal");
65         in.tokenBufferAppend(c);
66         break;
67           }
68       }
69     return new gnu.lists.FString (in.tokenBuffer, startPos,
70                       in.tokenBufferLength - startPos);
71       }
72     finally
73       {
74     in.tokenBufferLength = startPos;
75     if (port instanceof InPort)
76       ((InPort) port).readState = saveReadState;
77       }
78   }
79 }
80
Popular Tags