KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > brl > BRLReaderString


1 package gnu.kawa.brl;
2
3 // BRLRead.java -- a class to read BRL forms
4
// Copyright (C) 2001 Bruce R. Lewis and Eaton Vance Management
5
// See the file COPYING for license terms.
6

7 import gnu.text.*;
8 import gnu.mapping.InPort;
9 import gnu.lists.*;
10
11 public class BRLReaderString extends gnu.kawa.lispexpr.ReadTableEntry
12 {
13   public Object JavaDoc read (Lexer in, int ch, int count)
14       throws java.io.IOException JavaDoc
15   {
16     int startPos = in.tokenBufferLength;
17     LineBufferedReader port = in.getPort();
18     char saveReadState = '\0';
19     int c = ch;
20     int prev;
21     char endch;
22     String JavaDoc startFile = port.getName();
23     int startLine = port.getLineNumber();
24     int startColumn = port.getColumnNumber();
25
26     switch((char)ch)
27         {
28         case ']': // normal BRL case
29
endch = '[';
30         break;
31         case '}':
32         endch = '{';
33         break;
34         case '{':
35         endch = '}';
36         break;
37         case '[':
38         endch = ']';
39         break;
40         default:
41         // By default, symmetric string delimiters
42
endch = (char)ch;
43         break;
44         }
45
46     if (port instanceof InPort)
47         {
48         saveReadState = ((InPort) port).readState;
49         ((InPort) port).readState = (char) ch;
50         }
51
52     try
53         {
54         boolean inString = true;
55         while (inString)
56             {
57             int next;
58
59             prev = c;
60
61             if (port.pos < port.limit
62                 && prev != '\r'
63                 && prev != '\n')
64                 c = port.buffer[port.pos++];
65             else
66                 /* If no buffered data, or if port
67                    might update lineNumber */

68                 c = port.read();
69             if (c == endch)
70                 {
71                 if (port.peek() == endch)
72                     {
73                     in.tokenBufferAppend(c);
74                     port.skip();
75                     }
76                 else
77                   {
78                     inString = false;
79                     saveReadState = '\n';
80                     if (in instanceof BRLRead)
81                       ((BRLRead) in).saveExpressionStartPosition();
82                   }
83                 }
84             else if (c == '\n' && in.isInteractive()
85                  && ((BRLRead) in).nesting == 0)
86               {
87                 port.unread();
88                 inString = false;
89                 in.tokenBufferAppend(c);
90               }
91             else
92                 {
93                 if (c < 0)
94                   {
95                     inString = false;
96                     if (! in.isInteractive()
97                     && ((BRLRead) in).nesting > 0)
98                       in.error('e',
99                            startFile, startLine + 1,
100                            startColumn,
101                            "nested literal text starting here was not ended by a '['");
102                   }
103                 else
104                     in.tokenBufferAppend(c);
105                 }
106             }
107     
108         int length = in.tokenBufferLength - startPos;
109         if (length == 0)
110           return BRL.emptyForm;
111         else if (((BRLRead) in).isBrlCompatible())
112           return new FString(in.tokenBuffer, startPos, length);
113         else
114           {
115             String JavaDoc str = new String JavaDoc(in.tokenBuffer, startPos, length);
116             return new UnescapedData(str);
117           }
118         }
119     finally
120         {
121         in.tokenBufferLength = startPos;
122         if (port instanceof InPort)
123             ((InPort) port).readState = saveReadState;
124         }
125     }
126 }
127
Popular Tags