ReadTable.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright (c) 2001, 2003, 2006 Per M.A. Bothner
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.kawa.lispexpr;
  4. import gnu.kawa.util.RangeTable;
  5. import gnu.mapping.*;
  6. import gnu.expr.CommandCompleter;
  7. import gnu.expr.Language;
  8. import gnu.kawa.reflect.StaticFieldLocation;
  9. import gnu.bytecode.Type;
  10. public class ReadTable extends RangeTable
  11. {
  12. /** Kinds of characters. */
  13. public static final int ILLEGAL = 0;
  14. public static final int WHITESPACE = 1;
  15. public static final int CONSTITUENT = 2;
  16. public static final int SINGLE_ESCAPE = 3;
  17. public static final int MULTIPLE_ESCAPE = 4;
  18. public static final int TERMINATING_MACRO = 5;
  19. public static final int NON_TERMINATING_MACRO = 6;
  20. /** Default value to pass to setBracketMode() unless overridden. */
  21. public static int defaultBracketMode = -2;
  22. /** A character {@code X} such that {@code PreXWord -> ($lookup$ Pre 'Word)}, if {@code X > 0}. */
  23. public char postfixLookupOperator = (char) (-1);
  24. /** True if ":IDENTIFIER" should be treated as a keyword. */
  25. protected boolean initialColonIsKeyword;
  26. /** True if "IDENTIFIER:" should be treated as a keyword. */
  27. protected boolean finalColonIsKeyword;
  28. public int extraFlags;
  29. /** Control whether we should handle R6RS inline hex escape.
  30. * I.e. {@code "\x"<hexdigits>";"}.
  31. */
  32. protected boolean hexEscapeAfterBackslash = true;
  33. /** Set whether ":IDENTIFIER" should be treated as a keyword. */
  34. public void setInitialColonIsKeyword (boolean whenInitial)
  35. {
  36. initialColonIsKeyword = whenInitial;
  37. }
  38. /** Set whether "IDENTIFIER:" should be treated as a keyword. */
  39. public void setFinalColonIsKeyword (boolean whenFinal)
  40. {
  41. finalColonIsKeyword = whenFinal;
  42. }
  43. static final ThreadLocation current = new ThreadLocation("read-table");
  44. public ReadTable()
  45. {
  46. }
  47. public void initialize(boolean sharpIsTerminating)
  48. {
  49. ReadTableEntry entry;
  50. entry = ReadTableEntry.getWhitespaceInstance();
  51. set(' ', entry);
  52. set('\t', entry);
  53. set('\n', entry);
  54. set('\r', entry);
  55. set('\f', entry);
  56. //set('\v', entry);
  57. set('|', ReadTableEntry.getMultipleEscapeInstance());
  58. set('\\', ReadTableEntry.getSingleEscapeInstance());
  59. set('0', '9', ReadTableEntry.getDigitInstance());
  60. entry = ReadTableEntry.getConstituentInstance();
  61. set('a', 'z', entry);
  62. set('A', 'Z', entry);
  63. set('!', entry);
  64. set('$', entry);
  65. set('%', entry);
  66. set('&', ReadTableEntry.ampersand);
  67. set('*', entry);
  68. set('+', entry);
  69. set('-', entry);
  70. set('.', entry);
  71. set('/', entry);
  72. set('=', entry);
  73. set('>', entry);
  74. set('?', entry);
  75. set('@', entry);
  76. set('^', entry);
  77. set('_', entry);
  78. set('{', ReadTableEntry.brace);
  79. set('~', entry);
  80. set('\177',entry);
  81. set('\b', entry);
  82. set(':', new ReaderColon());
  83. set('\"', new ReaderString());
  84. set('#', ReaderDispatch.create(this, ! sharpIsTerminating));
  85. set(';', ReaderIgnoreRestOfLine.getInstance());
  86. set('(', ReaderParens.getInstance('(', ')'));
  87. set('\'', new ReaderQuote(makeSymbol(LispLanguage.quote_str)));
  88. set('`', new ReaderQuote(makeSymbol(LispLanguage.quasiquote_str)));
  89. ReaderQuote unquoteEntry =
  90. new ReaderQuote(makeSymbol(LispLanguage.unquote_str),
  91. '@', makeSymbol(LispLanguage.unquotesplicing_str),
  92. ReadTable.TERMINATING_MACRO);
  93. set(',', unquoteEntry);
  94. setBracketMode(); // Sets the entries for '[', ']', and '<'.
  95. }
  96. /** Create a new ReadTable and initialize it appropriately for Common Lisp. */
  97. public static ReadTable createInitial ()
  98. {
  99. ReadTable tab = new ReadTable();
  100. tab.initialize(true);
  101. return tab;
  102. }
  103. /** Specify how {@code '['} and {@code ']'} (and {@code '<'}) are handled.
  104. * The value -2 means {@code [a b c]} is {@code ($bracket-list$ a b c)}
  105. * and {@code f[a b]} is {@code ($bracket-apply$ f a b)}.
  106. * The value -1 means that '[' and ']' are plain token constituents.
  107. * The value 0 means that '[' and ']' are equivalent to '(' and ')'.
  108. * The value 1 means that '[' and ']' are equivalent to '(' and ')', except
  109. * within a token starting with {@code '<',} in which case they are constituents.
  110. * This is so {@code '['} is non-terminating when reading say {@code '<char[]>'}
  111. */
  112. public void setBracketMode(int mode)
  113. {
  114. if (mode == -2)
  115. {
  116. set('[', ReaderParens.getInstance('[', ']', ReadTable.TERMINATING_MACRO,
  117. LispLanguage.bracket_list_sym));
  118. set('<', new ReaderTypespec());
  119. }
  120. else if (mode <= 0)
  121. {
  122. ReadTableEntry token = ReadTableEntry.getConstituentInstance();
  123. set('<', token);
  124. if (mode < 0)
  125. {
  126. set('[', token);
  127. set(']', token);
  128. }
  129. }
  130. else
  131. set('<', new ReaderTypespec());
  132. if (mode >= 0)
  133. {
  134. set('[', ReaderParens.getInstance('[', ']'));
  135. remove(']');
  136. }
  137. }
  138. /** Specify how '[' and ']' are handled.
  139. * Unless overridden, uses defaultBracketMode. */
  140. public void setBracketMode()
  141. {
  142. setBracketMode(defaultBracketMode);
  143. }
  144. /** A table mapping constructor tags to functions, as in SRFI-10. */
  145. Environment ctorTable = null;
  146. void initCtorTable ()
  147. {
  148. if (ctorTable == null)
  149. ctorTable = Environment.make();
  150. }
  151. /** Add a mapping for a SRFI-10 constructor tag. */
  152. public synchronized void putReaderCtor (String key, Procedure proc)
  153. {
  154. initCtorTable();
  155. ctorTable.put(key, proc);
  156. }
  157. /** Add a mapping for a SRFI-10 constructor tag. */
  158. public synchronized void putReaderCtor (String key, Type type)
  159. {
  160. initCtorTable();
  161. ctorTable.put(key, type);
  162. }
  163. /** Map a SRFI-10 constructor tag to Procedure-valued lazy field */
  164. public synchronized void putReaderCtorFld (String key,
  165. String cname, String fname)
  166. {
  167. initCtorTable();
  168. Symbol symbol = ctorTable.getSymbol(key);
  169. StaticFieldLocation.define(ctorTable, symbol, null, cname, fname);
  170. }
  171. /** Resolve a SRFI-10 constructor tags to a functions. */
  172. public synchronized Object getReaderCtor (String key)
  173. {
  174. initCtorTable();
  175. return ctorTable.get(key, null);
  176. }
  177. public static ReadTable getCurrent()
  178. {
  179. ReadTable table = (ReadTable) current.get(null);
  180. if (table == null)
  181. {
  182. Language language = Language.getDefaultLanguage();
  183. if (language instanceof LispLanguage)
  184. {
  185. LispLanguage llanguage = (LispLanguage) language;
  186. synchronized (llanguage)
  187. {
  188. table = llanguage.defaultReadTable;
  189. if (table == null)
  190. {
  191. table = llanguage.createReadTable();
  192. llanguage.defaultReadTable = table;
  193. }
  194. }
  195. }
  196. else
  197. table = ReadTable.createInitial();
  198. current.set(table);
  199. }
  200. return table;
  201. }
  202. public static void setCurrent(ReadTable rt)
  203. {
  204. current.set(rt);
  205. }
  206. public ReadTableEntry lookup (int ch)
  207. {
  208. ReadTableEntry entry = (ReadTableEntry) lookup(ch, null);
  209. if (entry == null && ch >= 0 && ch < 0x10000)
  210. {
  211. if (Character.isDigit((char) ch))
  212. entry = (ReadTableEntry) lookup('0', null);
  213. else if (Character.isLowerCase((char) ch))
  214. entry = (ReadTableEntry) lookup('a', null);
  215. else if (Character.isLetter((char) ch)
  216. || ch == CommandCompleter.COMPLETE_REQUEST)
  217. entry = (ReadTableEntry) lookup('A', null);
  218. else if (Character.isWhitespace((char) ch))
  219. entry = (ReadTableEntry) lookup(' ', null);
  220. // Current code assumes lookup(')') returns null.
  221. if (entry == null && ch >= 128)
  222. entry = ReadTableEntry.getConstituentInstance();
  223. if (entry == null)
  224. entry = ReadTableEntry.getIllegalInstance();
  225. }
  226. return entry;
  227. }
  228. protected Object makeSymbol (String name)
  229. {
  230. return Symbol.valueOf(name);
  231. }
  232. }