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