ReaderVector.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (c) 2001 Per M.A. Bothner
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.kawa.lispexpr;
  4. import gnu.text.*;
  5. import gnu.kawa.io.InPort;
  6. import gnu.mapping.Values;
  7. import gnu.lists.FVector;
  8. import gnu.lists.LList;
  9. import gnu.lists.Pair;
  10. public class ReaderVector extends ReadTableEntry
  11. {
  12. char close;
  13. public ReaderVector(char close)
  14. {
  15. this.close = close;
  16. }
  17. public Object read (Lexer in, int ch, int count, int sharingIndex)
  18. throws java.io.IOException, SyntaxException
  19. {
  20. return readVector((LispReader) in, in.getPort(), count, close, sharingIndex);
  21. }
  22. public static FVector readVector(LispReader lexer, InPort port, int count, char close, int sharingIndex)
  23. throws java.io.IOException, SyntaxException
  24. {
  25. char saveReadState = ' ';
  26. if (port instanceof InPort)
  27. {
  28. saveReadState = ((InPort) port).readState;
  29. ((InPort) port).readState = close == ']' ? '[' : '(';
  30. }
  31. int startLine = port.getLineNumber();
  32. int startColumn = port.getColumnNumber()-1;
  33. try
  34. {
  35. FVector result = new FVector();
  36. lexer.bindSharedObject(sharingIndex, result);
  37. ReadTable rtable = ReadTable.getCurrent();
  38. Pair head = new Pair(null, LList.Empty);
  39. Pair last = head;
  40. for (;;)
  41. {
  42. int ch = lexer.read();
  43. if (ch < 0)
  44. lexer.eofError("unexpected EOF in vector starting here",
  45. startLine + 1, startColumn);
  46. if (ch == close)
  47. break;
  48. last = lexer.readValuesAndAppend(ch, rtable, last);
  49. }
  50. result.replaceAll(((LList) head.getCdr()).toArray());
  51. result.setReadOnly();
  52. return result;
  53. }
  54. finally
  55. {
  56. if (port instanceof InPort)
  57. ((InPort) port).readState = saveReadState;
  58. }
  59. }
  60. }