define_autoload.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright (C) 2000 Per M.A. Bothner.
  2. // This is free software; for terms and warranty disclaimer see ../../COPYING.
  3. package kawa.standard;
  4. import gnu.expr.*;
  5. import gnu.lists.*;
  6. import gnu.mapping.*;
  7. import gnu.kawa.io.InPort;
  8. import gnu.kawa.lispexpr.*;
  9. import kawa.lang.*;
  10. import java.io.File;
  11. public class define_autoload extends Syntax
  12. {
  13. public static final define_autoload define_autoload
  14. = new define_autoload("define-autoload", false);
  15. public static final define_autoload define_autoloads_from_file
  16. = new define_autoload("define-autoloads-from-file", true);
  17. boolean fromFile;
  18. public define_autoload (String name, boolean fromFile)
  19. {
  20. super(name);
  21. this.fromFile = fromFile;
  22. }
  23. @Override
  24. public boolean scanForDefinitions (Pair st, ScopeExp defs, Translator tr)
  25. {
  26. if (! (st.getCdr() instanceof Pair))
  27. return super.scanForDefinitions(st, defs, tr);
  28. Pair p = (Pair) st.getCdr();
  29. if (fromFile)
  30. {
  31. for (;;)
  32. {
  33. /* #ifdef use:java.lang.CharSequence */
  34. if (! (p.getCar() instanceof CharSequence))
  35. break;
  36. /* #else */
  37. // if (! (p.getCar() instanceof String || p.getCar() instanceof CharSeq))
  38. // break;
  39. /* #endif */
  40. if (! scanFile(p.getCar().toString(), defs, tr))
  41. return false;
  42. Object rest = p.getCdr();
  43. if (rest == LList.Empty)
  44. return true;
  45. if (! (rest instanceof Pair))
  46. break;
  47. p = (Pair) p.getCdr();
  48. }
  49. tr.syntaxError("invalid syntax for define-autoloads-from-file");
  50. return false;
  51. }
  52. Object names = p.getCar();
  53. Object filename = null;
  54. if (p.getCdr() instanceof Pair)
  55. {
  56. p = (Pair) p.getCdr();
  57. filename = p.getCar();
  58. return process(names, filename, defs, tr);
  59. }
  60. tr.syntaxError("invalid syntax for define-autoload");
  61. return false;
  62. }
  63. public boolean scanFile(String filespec, ScopeExp defs, Translator tr)
  64. {
  65. if (filespec.endsWith(".el"))
  66. ;
  67. File file = new File(filespec);
  68. if (! file.isAbsolute())
  69. file = new File(new File(tr.getFileName()).getParent(), filespec);
  70. String filename = file.getPath();
  71. int dot = filename.lastIndexOf('.');
  72. Language language;
  73. if (dot >= 0)
  74. {
  75. String extension = filename.substring(dot);
  76. language = Language.getInstance(extension);
  77. if (language == null)
  78. {
  79. tr.syntaxError("unknown extension for "+filename);
  80. return true;
  81. }
  82. gnu.text.Lexer lexer;
  83. /*
  84. // Since (module-name ...) is not handled in this pass,
  85. // we won't see it until its too late. FIXME.
  86. ModuleExp module = tr.getModule();
  87. String prefix = module == null ? null : module.getName();
  88. System.err.println("module:"+module);
  89. if (prefix == null)
  90. prefix = kawa.repl.compilationPrefix;
  91. else
  92. {
  93. int i = prefix.lastIndexOf('.');
  94. if (i < 0)
  95. prefix = null;
  96. else
  97. prefix = prefix.substring(0, i+1);
  98. }
  99. */
  100. String prefix = tr.classPrefix;
  101. int extlen = extension.length();
  102. int speclen = filespec.length();
  103. String cname = filespec.substring(0, speclen - extlen);
  104. while (cname.startsWith("../"))
  105. {
  106. int i = prefix.lastIndexOf('.', prefix.length() - 2);
  107. if (i < 0)
  108. {
  109. tr.syntaxError("cannot use relative filename \"" + filespec
  110. + "\" with simple prefix \"" + prefix + "\"");
  111. return false;
  112. }
  113. prefix = prefix.substring(0, i+1);
  114. cname = cname.substring(3);
  115. }
  116. String classname = (prefix + cname).replace('/', '.');
  117. try
  118. {
  119. InPort port = InPort.openFile(filename);
  120. lexer = language.getLexer(port, tr.getMessages());
  121. findAutoloadComments((LispReader) lexer, classname, defs, tr);
  122. }
  123. catch (Exception ex)
  124. {
  125. tr.syntaxError("error reading "+filename+": "+ex);
  126. return true;
  127. }
  128. }
  129. return true;
  130. }
  131. public static void findAutoloadComments (LispReader in, String filename,
  132. ScopeExp defs, Translator tr)
  133. throws java.io.IOException, gnu.text.SyntaxException
  134. {
  135. boolean lineStart = true;
  136. String magic = ";;;###autoload";
  137. int magicLength = magic.length();
  138. mainLoop:
  139. for (;;)
  140. {
  141. int ch = in.peek();
  142. if (ch < 0)
  143. return;
  144. if (ch == '\n' || ch == '\r')
  145. {
  146. in.read();
  147. lineStart = true;
  148. continue;
  149. }
  150. if (lineStart && ch == ';')
  151. {
  152. int i = 0;
  153. for (;;)
  154. {
  155. if (i == magicLength)
  156. break;
  157. ch = in.read();
  158. if (ch < 0)
  159. return;
  160. if (ch == '\n' || ch == '\r')
  161. {
  162. lineStart = true;
  163. continue mainLoop;
  164. }
  165. if (i < 0 || ch == magic.charAt(i++))
  166. continue;
  167. i = -1; // No match.
  168. }
  169. if (i > 0)
  170. {
  171. Object form = in.readObject();
  172. if (form instanceof Pair)
  173. {
  174. Pair pair = (Pair) form;
  175. Object value = null;
  176. String name = null;
  177. Object car = pair.getCar();
  178. String command
  179. = car instanceof String ? car.toString()
  180. : car instanceof Symbol ? ((Symbol) car).getName()
  181. : null;
  182. if (command == "defun")
  183. {
  184. name = ((Pair)pair.getCdr()).getCar().toString();
  185. value = new AutoloadProcedure(name, filename,
  186. tr.getLanguage());
  187. }
  188. else
  189. tr.error('w', "unsupported ;;;###autoload followed by: "
  190. + pair.getCar());
  191. if (value != null)
  192. {
  193. Declaration decl = defs.getDefine(name, tr);
  194. Expression ex = new QuoteExp(value);
  195. decl.setFlag(Declaration.IS_CONSTANT);
  196. decl.noteValue(ex);
  197. }
  198. }
  199. lineStart = false;
  200. continue;
  201. }
  202. }
  203. lineStart = false;
  204. in.skip();
  205. if (ch == '#')
  206. {
  207. if (in.peek() == '|')
  208. {
  209. in.skip();
  210. in.readNestedComment('#', '|', '|', '#');
  211. continue;
  212. }
  213. }
  214. if (Character.isWhitespace ((char)ch))
  215. ;
  216. else
  217. {
  218. Object skipped = in.readObject(ch);
  219. if (skipped == Sequence.eofValue)
  220. return;
  221. }
  222. }
  223. }
  224. public static boolean process(Object names, Object filename,
  225. ScopeExp defs, Translator tr)
  226. {
  227. if (names instanceof Pair)
  228. {
  229. Pair p = (Pair) names;
  230. return (process(p.getCar(), filename, defs, tr)
  231. && process(p.getCdr(), filename, defs, tr));
  232. }
  233. if (names == LList.Empty)
  234. return true;
  235. String fn;
  236. int len;
  237. /*
  238. if (names == "*" && filename instanceof String
  239. && (len = (fn = (String) filename).length()) > 2
  240. && fn.charAt(0) == '<' && fn.charAt(len-1) == '>')
  241. {
  242. fn = fn.substring(1, len-1);
  243. try
  244. {
  245. Class fclass = Class.forName(fn);
  246. Object instance = require.find(ctype, env);
  247. ...;
  248. }
  249. catch (ClassNotFoundException ex)
  250. {
  251. tr.syntaxError("class <"+fn+"> not found");
  252. return false;
  253. }
  254. }
  255. */
  256. if (names instanceof String || names instanceof Symbol)
  257. {
  258. String name = names.toString();
  259. Declaration decl = defs.getDefine(name, tr);
  260. if (filename instanceof SimpleSymbol
  261. && (len = (fn = filename.toString()).length()) > 2
  262. && fn.charAt(0) == '<' && fn.charAt(len-1) == '>')
  263. filename = fn.substring(1, len-1);
  264. Object value = new AutoloadProcedure(name, filename.toString(),
  265. tr.getLanguage());
  266. Expression ex = new QuoteExp(value);
  267. decl.setFlag(Declaration.IS_CONSTANT);
  268. decl.noteValue(ex);
  269. return true;
  270. }
  271. return false;
  272. }
  273. public Expression rewriteForm (Pair form, Translator tr)
  274. {
  275. return null;
  276. }
  277. }