BRLReaderString.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package gnu.kawa.brl;
  2. // BRLRead.java -- a class to read BRL forms
  3. // Copyright (C) 2001 Bruce R. Lewis and Eaton Vance Management
  4. // See the file COPYING for license terms.
  5. import gnu.text.*;
  6. import gnu.kawa.io.InPort;
  7. import gnu.lists.*;
  8. public class BRLReaderString extends gnu.kawa.lispexpr.ReadTableEntry
  9. {
  10. public Object read (Lexer in, int ch, int count)
  11. throws java.io.IOException
  12. {
  13. int startPos = in.tokenBufferLength;
  14. InPort port = in.getPort();
  15. char saveReadState = '\0';
  16. int c = ch;
  17. int prev;
  18. char endch;
  19. String startFile = port.getName();
  20. int startLine = port.getLineNumber();
  21. int startColumn = port.getColumnNumber();
  22. switch((char)ch)
  23. {
  24. case ']': // normal BRL case
  25. endch = '[';
  26. break;
  27. case '}':
  28. endch = '{';
  29. break;
  30. case '{':
  31. endch = '}';
  32. break;
  33. case '[':
  34. endch = ']';
  35. break;
  36. default:
  37. // By default, symmetric string delimiters
  38. endch = (char)ch;
  39. break;
  40. }
  41. if (port instanceof InPort)
  42. {
  43. saveReadState = ((InPort) port).readState;
  44. ((InPort) port).readState = (char) ch;
  45. }
  46. try
  47. {
  48. boolean inString = true;
  49. while (inString)
  50. {
  51. int next;
  52. prev = c;
  53. if (port.pos < port.limit
  54. && prev != '\r'
  55. && prev != '\n')
  56. c = port.buffer[port.pos++];
  57. else
  58. /* If no buffered data, or if port
  59. might update lineNumber */
  60. c = port.read();
  61. if (c == endch)
  62. {
  63. if (port.peek() == endch)
  64. {
  65. in.tokenBufferAppend(c);
  66. port.skip();
  67. }
  68. else
  69. {
  70. inString = false;
  71. saveReadState = '\n';
  72. if (in instanceof BRLRead)
  73. ((BRLRead) in).saveExpressionStartPosition();
  74. }
  75. }
  76. else if (c == '\n' && in.isInteractive()
  77. && ((BRLRead) in).nesting == 0)
  78. {
  79. port.unread();
  80. inString = false;
  81. in.tokenBufferAppend(c);
  82. }
  83. else
  84. {
  85. if (c < 0)
  86. {
  87. inString = false;
  88. if (! in.isInteractive()
  89. && ((BRLRead) in).nesting > 0)
  90. in.error('e',
  91. startFile, startLine + 1,
  92. startColumn,
  93. "nested literal text starting here was not ended by a '['");
  94. }
  95. else
  96. in.tokenBufferAppend(c);
  97. }
  98. }
  99. int length = in.tokenBufferLength - startPos;
  100. if (length == 0)
  101. return BRL.emptyForm;
  102. String str = new String(in.tokenBuffer, startPos, length);
  103. /* FIXME
  104. if (((BRLRead) in).isBrlCompatible())
  105. return str;
  106. else
  107. */
  108. return new UnescapedData(str);
  109. }
  110. finally
  111. {
  112. in.tokenBufferLength = startPos;
  113. if (port instanceof InPort)
  114. ((InPort) port).readState = saveReadState;
  115. }
  116. }
  117. }