KickJava   Java API By Example, From Geeks To Geeks.

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


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 import gnu.mapping.Values;
8 import gnu.lists.FVector;
9
10 public class ReaderVector extends ReadTableEntry
11 {
12   char close;
13
14   public ReaderVector(char close)
15   {
16     this.close = close;
17   }
18
19   public Object JavaDoc read (Lexer in, int ch, int count)
20     throws java.io.IOException JavaDoc, SyntaxException
21   {
22     return readVector((LispReader) in, in.getPort(), count, close);
23   }
24
25   public static FVector readVector(LispReader lexer, LineBufferedReader port, int count, char close)
26     throws java.io.IOException JavaDoc, SyntaxException
27   {
28     char saveReadState = ' ';
29     if (port instanceof InPort)
30       {
31     saveReadState = ((InPort) port).readState;
32     ((InPort) port).readState = close == ']' ? '[' : '(';
33       }
34      try
35        {
36      java.util.Vector JavaDoc vec = new java.util.Vector JavaDoc();
37          ReadTable rtable = ReadTable.getCurrent();
38      for (;;)
39        {
40          int ch = lexer.read();
41          if (ch < 0)
42            lexer.eofError("unexpected EOF in vector");
43          if (ch == close)
44            break;
45          Object JavaDoc value = lexer.readValues(ch, rtable);
46          if (value instanceof Values)
47            {
48          Object JavaDoc[] values = ((Values) value).getValues();
49          int n = values.length;
50          for (int i = 0; i < n; i++)
51            vec.addElement(values[i]);
52            }
53          else
54            {
55          if (value == gnu.expr.QuoteExp.voidExp)
56            value = Values.empty;
57          vec.addElement(value);
58            }
59        }
60      Object JavaDoc[] objs = new Object JavaDoc[vec.size()];
61      vec.copyInto(objs);
62      return new FVector(objs);
63        }
64      finally
65        {
66      if (port instanceof InPort)
67        ((InPort) port).readState = saveReadState;
68        }
69   }
70 }
71
Popular Tags