read.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /* Copyright (C) 1995,1996,1997, 1999, 2000, 2002 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include <stdio.h>
  42. #include "libguile/_scm.h"
  43. #include "libguile/chars.h"
  44. #include "libguile/eval.h"
  45. #include "libguile/unif.h"
  46. #include "libguile/keywords.h"
  47. #include "libguile/alist.h"
  48. #include "libguile/srcprop.h"
  49. #include "libguile/hashtab.h"
  50. #include "libguile/hash.h"
  51. #include "libguile/ports.h"
  52. #include "libguile/root.h"
  53. #include "libguile/strings.h"
  54. #include "libguile/vectors.h"
  55. #include "libguile/validate.h"
  56. #include "libguile/read.h"
  57. SCM_SYMBOL (scm_keyword_prefix, "prefix");
  58. scm_option scm_read_opts[] = {
  59. { SCM_OPTION_BOOLEAN, "copy", 0,
  60. "Copy source code expressions." },
  61. { SCM_OPTION_BOOLEAN, "positions", 0,
  62. "Record positions of source code expressions." },
  63. { SCM_OPTION_BOOLEAN, "case-insensitive", 0,
  64. "Convert symbols to lower case."},
  65. { SCM_OPTION_SCM, "keywords", SCM_UNPACK (SCM_BOOL_F),
  66. "Style of keyword recognition: #f or 'prefix"}
  67. };
  68. SCM_DEFINE (scm_read_options, "read-options-interface", 0, 1, 0,
  69. (SCM setting),
  70. "")
  71. #define FUNC_NAME s_scm_read_options
  72. {
  73. SCM ans = scm_options (setting,
  74. scm_read_opts,
  75. SCM_N_READ_OPTIONS,
  76. FUNC_NAME);
  77. if (SCM_COPY_SOURCE_P)
  78. SCM_RECORD_POSITIONS_P = 1;
  79. return ans;
  80. }
  81. #undef FUNC_NAME
  82. /* An association list mapping extra hash characters to procedures. */
  83. static SCM *scm_read_hash_procedures;
  84. SCM_DEFINE (scm_read, "read", 0, 1, 0,
  85. (SCM port),
  86. "")
  87. #define FUNC_NAME s_scm_read
  88. {
  89. int c;
  90. SCM tok_buf, copy;
  91. if (SCM_UNBNDP (port))
  92. port = scm_cur_inp;
  93. SCM_VALIDATE_OPINPORT (1,port);
  94. c = scm_flush_ws (port, (char *) NULL);
  95. if (EOF == c)
  96. return SCM_EOF_VAL;
  97. scm_ungetc (c, port);
  98. tok_buf = scm_makstr (30L, 0);
  99. return scm_lreadr (&tok_buf, port, &copy);
  100. }
  101. #undef FUNC_NAME
  102. char *
  103. scm_grow_tok_buf (SCM *tok_buf)
  104. {
  105. scm_vector_set_length_x (*tok_buf, SCM_MAKINUM (2 * SCM_LENGTH (*tok_buf)));
  106. return SCM_CHARS (*tok_buf);
  107. }
  108. int
  109. scm_flush_ws (SCM port, const char *eoferr)
  110. {
  111. register int c;
  112. while (1)
  113. switch (c = scm_getc (port))
  114. {
  115. case EOF:
  116. goteof:
  117. if (eoferr)
  118. scm_wta (SCM_UNDEFINED, "end of file in ", eoferr);
  119. return c;
  120. case ';':
  121. lp:
  122. switch (c = scm_getc (port))
  123. {
  124. case EOF:
  125. goto goteof;
  126. default:
  127. goto lp;
  128. case SCM_LINE_INCREMENTORS:
  129. break;
  130. }
  131. break;
  132. case SCM_LINE_INCREMENTORS:
  133. case SCM_SINGLE_SPACES:
  134. case '\t':
  135. break;
  136. default:
  137. return c;
  138. }
  139. }
  140. int
  141. scm_casei_streq (char *s1, char *s2)
  142. {
  143. while (*s1 && *s2)
  144. if (scm_downcase((int)*s1) != scm_downcase((int)*s2))
  145. return 0;
  146. else
  147. {
  148. ++s1;
  149. ++s2;
  150. }
  151. return !(*s1 || *s2);
  152. }
  153. /* recsexpr is used when recording expressions
  154. * constructed by read:sharp.
  155. */
  156. #ifndef DEBUG_EXTENSIONS
  157. #define recsexpr(obj, line, column, filename) (obj)
  158. #else
  159. static SCM
  160. recsexpr (SCM obj,int line,int column,SCM filename)
  161. {
  162. if (!SCM_CONSP(obj)) {
  163. return obj;
  164. } else {
  165. SCM tmp = obj, copy;
  166. /* If this sexpr is visible in the read:sharp source, we want to
  167. keep that information, so only record non-constant cons cells
  168. which haven't previously been read by the reader. */
  169. if (SCM_FALSEP (scm_whash_lookup (scm_source_whash, obj)))
  170. {
  171. if (SCM_COPY_SOURCE_P)
  172. {
  173. copy = scm_cons (recsexpr (SCM_CAR (obj), line, column, filename),
  174. SCM_UNDEFINED);
  175. while ((tmp = SCM_CDR (tmp)) && SCM_CONSP (tmp))
  176. {
  177. SCM_SETCDR (copy, scm_cons (recsexpr (SCM_CAR (tmp),
  178. line,
  179. column,
  180. filename),
  181. SCM_UNDEFINED));
  182. copy = SCM_CDR (copy);
  183. }
  184. SCM_SETCDR (copy, tmp);
  185. }
  186. else
  187. {
  188. recsexpr (SCM_CAR (obj), line, column, filename);
  189. while ((tmp = SCM_CDR (tmp)) && SCM_CONSP (tmp))
  190. recsexpr (SCM_CAR (tmp), line, column, filename);
  191. copy = SCM_UNDEFINED;
  192. }
  193. scm_whash_insert (scm_source_whash,
  194. obj,
  195. scm_make_srcprops (line,
  196. column,
  197. filename,
  198. copy,
  199. SCM_EOL));
  200. }
  201. return obj;
  202. }
  203. }
  204. #endif
  205. /* Consume an SCSH-style block comment. Assume that we've already
  206. read the initial `#!', and eat characters until we get a
  207. newline/exclamation-point/sharp-sign/newline sequence. */
  208. static void
  209. skip_scsh_block_comment (SCM port)
  210. {
  211. /* Is this portable? Dear God, spare me from the non-eight-bit
  212. characters. But is it tasteful? */
  213. long history = 0;
  214. for (;;)
  215. {
  216. int c = scm_getc (port);
  217. if (c == EOF)
  218. scm_wta (SCM_UNDEFINED,
  219. "unterminated `#! ... !#' comment", "read");
  220. history = ((history << 8) | (c & 0xff)) & 0xffffffff;
  221. /* Were the last four characters read "\n!#\n"? */
  222. if (history == (('\n' << 24) | ('!' << 16) | ('#' << 8) | '\n'))
  223. return;
  224. }
  225. }
  226. static SCM scm_get_hash_procedure(int c);
  227. static char s_list[]="list";
  228. SCM
  229. scm_lreadr (SCM *tok_buf,SCM port,SCM *copy)
  230. {
  231. int c;
  232. scm_sizet j;
  233. SCM p;
  234. tryagain:
  235. c = scm_flush_ws (port, s_scm_read);
  236. tryagain_no_flush_ws:
  237. switch (c)
  238. {
  239. case EOF:
  240. return SCM_EOF_VAL;
  241. case '(':
  242. return SCM_RECORD_POSITIONS_P
  243. ? scm_lreadrecparen (tok_buf, port, s_list, copy)
  244. : scm_lreadparen (tok_buf, port, s_list, copy);
  245. case ')':
  246. scm_wta (SCM_UNDEFINED, "unexpected \")\"", "read");
  247. goto tryagain;
  248. case '\'':
  249. p = scm_sym_quote;
  250. goto recquote;
  251. case '`':
  252. p = scm_sym_quasiquote;
  253. goto recquote;
  254. case ',':
  255. c = scm_getc (port);
  256. if ('@' == c)
  257. p = scm_sym_uq_splicing;
  258. else
  259. {
  260. scm_ungetc (c, port);
  261. p = scm_sym_unquote;
  262. }
  263. recquote:
  264. p = scm_cons2 (p,
  265. scm_lreadr (tok_buf, port, copy),
  266. SCM_EOL);
  267. if (SCM_RECORD_POSITIONS_P)
  268. scm_whash_insert (scm_source_whash,
  269. p,
  270. scm_make_srcprops (SCM_LINUM (port),
  271. SCM_COL (port) - 1,
  272. SCM_FILENAME (port),
  273. SCM_COPY_SOURCE_P
  274. ? (*copy = scm_cons2 (SCM_CAR (p),
  275. SCM_CAR (SCM_CDR (p)),
  276. SCM_EOL))
  277. : SCM_UNDEFINED,
  278. SCM_EOL));
  279. return p;
  280. case '#':
  281. c = scm_getc (port);
  282. switch (c)
  283. {
  284. case '(':
  285. p = scm_lreadparen (tok_buf, port, "vector", copy);
  286. return SCM_NULLP (p) ? scm_nullvect : scm_vector (p);
  287. case 't':
  288. case 'T':
  289. return SCM_BOOL_T;
  290. case 'f':
  291. case 'F':
  292. return SCM_BOOL_F;
  293. case 'b':
  294. case 'B':
  295. case 'o':
  296. case 'O':
  297. case 'd':
  298. case 'D':
  299. case 'x':
  300. case 'X':
  301. case 'i':
  302. case 'I':
  303. case 'e':
  304. case 'E':
  305. scm_ungetc (c, port);
  306. c = '#';
  307. goto num;
  308. case '!':
  309. /* start of a shell script. Parse as a block comment,
  310. terminated by !#, just like SCSH. */
  311. skip_scsh_block_comment (port);
  312. /* EOF is not an error here */
  313. c = scm_flush_ws (port, (char *)NULL);
  314. goto tryagain_no_flush_ws;
  315. #ifdef HAVE_ARRAYS
  316. case '*':
  317. j = scm_read_token (c, tok_buf, port, 0);
  318. p = scm_istr2bve (SCM_CHARS (*tok_buf) + 1, (long) (j - 1));
  319. if (SCM_NFALSEP (p))
  320. return p;
  321. else
  322. goto unkshrp;
  323. #endif
  324. case '{':
  325. j = scm_read_token (c, tok_buf, port, 1);
  326. p = scm_intern (SCM_CHARS (*tok_buf), j);
  327. return SCM_CAR (p);
  328. case '\\':
  329. c = scm_getc (port);
  330. j = scm_read_token (c, tok_buf, port, 0);
  331. if (j == 1)
  332. return SCM_MAKE_CHAR (c);
  333. if (c >= '0' && c < '8')
  334. {
  335. p = scm_istr2int (SCM_CHARS (*tok_buf), (long) j, 8);
  336. if (SCM_NFALSEP (p))
  337. return SCM_MAKE_CHAR (SCM_INUM (p));
  338. }
  339. for (c = 0; c < scm_n_charnames; c++)
  340. if (scm_charnames[c]
  341. && (scm_casei_streq (scm_charnames[c], SCM_CHARS (*tok_buf))))
  342. return SCM_MAKE_CHAR (scm_charnums[c]);
  343. scm_wta (SCM_UNDEFINED, "unknown # object: #\\", SCM_CHARS (*tok_buf));
  344. /* #:SYMBOL is a syntax for keywords supported in all contexts. */
  345. case ':':
  346. j = scm_read_token ('-', tok_buf, port, 0);
  347. p = scm_intern (SCM_CHARS (*tok_buf), j);
  348. return scm_make_keyword_from_dash_symbol (SCM_CAR (p));
  349. default:
  350. callshrp:
  351. {
  352. SCM sharp = scm_get_hash_procedure (c);
  353. if (SCM_NIMP (sharp))
  354. {
  355. int line = SCM_LINUM (port);
  356. int column = SCM_COL (port) - 2;
  357. SCM got;
  358. got = scm_apply (sharp,
  359. SCM_MAKE_CHAR (c),
  360. scm_acons (port, SCM_EOL, SCM_EOL));
  361. if (SCM_EQ_P (got, SCM_UNSPECIFIED))
  362. goto unkshrp;
  363. if (SCM_RECORD_POSITIONS_P)
  364. return *copy = recsexpr (got, line, column,
  365. SCM_FILENAME (port));
  366. else
  367. return got;
  368. }
  369. }
  370. unkshrp:
  371. scm_misc_error (s_scm_read, "Unknown # object: ~S",
  372. scm_listify (SCM_MAKE_CHAR (c), SCM_UNDEFINED));
  373. }
  374. case '"':
  375. j = 0;
  376. while ('"' != (c = scm_getc (port)))
  377. {
  378. SCM_ASSERT (EOF != c, SCM_UNDEFINED, "end of file in ", "string");
  379. while (j + 2 >= SCM_LENGTH (*tok_buf))
  380. scm_grow_tok_buf (tok_buf);
  381. if (c == '\\')
  382. switch (c = scm_getc (port))
  383. {
  384. case '\n':
  385. continue;
  386. case '0':
  387. c = '\0';
  388. break;
  389. case 'f':
  390. c = '\f';
  391. break;
  392. case 'n':
  393. c = '\n';
  394. break;
  395. case 'r':
  396. c = '\r';
  397. break;
  398. case 't':
  399. c = '\t';
  400. break;
  401. case 'a':
  402. c = '\007';
  403. break;
  404. case 'v':
  405. c = '\v';
  406. break;
  407. }
  408. SCM_CHARS (*tok_buf)[j] = c;
  409. ++j;
  410. }
  411. if (j == 0)
  412. return scm_nullstr;
  413. SCM_CHARS (*tok_buf)[j] = 0;
  414. {
  415. SCM str;
  416. str = scm_makfromstr (SCM_CHARS (*tok_buf), j, 0);
  417. return str;
  418. }
  419. case'0':case '1':case '2':case '3':case '4':
  420. case '5':case '6':case '7':case '8':case '9':
  421. case '.':
  422. case '-':
  423. case '+':
  424. num:
  425. j = scm_read_token (c, tok_buf, port, 0);
  426. p = scm_istring2number (SCM_CHARS (*tok_buf), (long) j, 10L);
  427. if (SCM_NFALSEP (p))
  428. return p;
  429. if (c == '#')
  430. {
  431. if ((j == 2) && (scm_getc (port) == '('))
  432. {
  433. scm_ungetc ('(', port);
  434. c = SCM_CHARS (*tok_buf)[1];
  435. goto callshrp;
  436. }
  437. scm_wta (SCM_UNDEFINED, "unknown # object", SCM_CHARS (*tok_buf));
  438. }
  439. goto tok;
  440. case ':':
  441. if (SCM_EQ_P (SCM_PACK (SCM_KEYWORD_STYLE), scm_keyword_prefix))
  442. {
  443. j = scm_read_token ('-', tok_buf, port, 0);
  444. p = scm_intern (SCM_CHARS (*tok_buf), j);
  445. return scm_make_keyword_from_dash_symbol (SCM_CAR (p));
  446. }
  447. /* fallthrough */
  448. default:
  449. j = scm_read_token (c, tok_buf, port, 0);
  450. /* fallthrough */
  451. tok:
  452. p = scm_intern (SCM_CHARS (*tok_buf), j);
  453. return SCM_CAR (p);
  454. }
  455. }
  456. #ifdef _UNICOS
  457. _Pragma ("noopt"); /* # pragma _CRI noopt */
  458. #endif
  459. scm_sizet
  460. scm_read_token (int ic, SCM *tok_buf, SCM port, int weird)
  461. {
  462. register scm_sizet j;
  463. register int c;
  464. register char *p;
  465. c = (SCM_CASE_INSENSITIVE_P ? scm_downcase(ic) : ic);
  466. p = SCM_CHARS (*tok_buf);
  467. if (weird)
  468. j = 0;
  469. else
  470. {
  471. j = 0;
  472. while (j + 2 >= SCM_LENGTH (*tok_buf))
  473. p = scm_grow_tok_buf (tok_buf);
  474. p[j] = c;
  475. ++j;
  476. }
  477. while (1)
  478. {
  479. while (j + 2 >= SCM_LENGTH (*tok_buf))
  480. p = scm_grow_tok_buf (tok_buf);
  481. c = scm_getc (port);
  482. switch (c)
  483. {
  484. case '(':
  485. case ')':
  486. case '"':
  487. case ';':
  488. case SCM_WHITE_SPACES:
  489. case SCM_LINE_INCREMENTORS:
  490. if (weird)
  491. goto default_case;
  492. scm_ungetc (c, port);
  493. case EOF:
  494. eof_case:
  495. p[j] = 0;
  496. return j;
  497. case '\\':
  498. if (!weird)
  499. goto default_case;
  500. else
  501. {
  502. c = scm_getc (port);
  503. if (c == EOF)
  504. goto eof_case;
  505. else
  506. goto default_case;
  507. }
  508. case '}':
  509. if (!weird)
  510. goto default_case;
  511. c = scm_getc (port);
  512. if (c == '#')
  513. {
  514. p[j] = 0;
  515. return j;
  516. }
  517. else
  518. {
  519. scm_ungetc (c, port);
  520. c = '}';
  521. goto default_case;
  522. }
  523. default:
  524. default_case:
  525. {
  526. c = (SCM_CASE_INSENSITIVE_P ? scm_downcase(c) : c);
  527. p[j] = c;
  528. ++j;
  529. }
  530. }
  531. }
  532. }
  533. #ifdef _UNICOS
  534. _Pragma ("opt"); /* # pragma _CRI opt */
  535. #endif
  536. SCM
  537. scm_lreadparen (SCM *tok_buf, SCM port, char *name, SCM *copy)
  538. {
  539. SCM tmp;
  540. SCM tl;
  541. SCM ans;
  542. int c;
  543. c = scm_flush_ws (port, name);
  544. if (')' == c)
  545. return SCM_EOL;
  546. scm_ungetc (c, port);
  547. if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
  548. {
  549. ans = scm_lreadr (tok_buf, port, copy);
  550. closeit:
  551. if (')' != (c = scm_flush_ws (port, name)))
  552. scm_wta (SCM_UNDEFINED, "missing close paren", "");
  553. return ans;
  554. }
  555. ans = tl = scm_cons (tmp, SCM_EOL);
  556. while (')' != (c = scm_flush_ws (port, name)))
  557. {
  558. scm_ungetc (c, port);
  559. if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
  560. {
  561. SCM_SETCDR (tl, scm_lreadr (tok_buf, port, copy));
  562. goto closeit;
  563. }
  564. SCM_SETCDR (tl, scm_cons (tmp, SCM_EOL));
  565. tl = SCM_CDR (tl);
  566. }
  567. return ans;
  568. }
  569. SCM
  570. scm_lreadrecparen (SCM *tok_buf, SCM port, char *name, SCM *copy)
  571. {
  572. register int c;
  573. register SCM tmp;
  574. register SCM tl, tl2 = SCM_EOL;
  575. SCM ans, ans2 = SCM_EOL;
  576. /* Need to capture line and column numbers here. */
  577. int line = SCM_LINUM (port);
  578. int column = SCM_COL (port) - 1;
  579. c = scm_flush_ws (port, name);
  580. if (')' == c)
  581. return SCM_EOL;
  582. scm_ungetc (c, port);
  583. if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
  584. {
  585. ans = scm_lreadr (tok_buf, port, copy);
  586. if (')' != (c = scm_flush_ws (port, name)))
  587. scm_wta (SCM_UNDEFINED, "missing close paren", "");
  588. return ans;
  589. }
  590. /* Build the head of the list structure. */
  591. ans = tl = scm_cons (tmp, SCM_EOL);
  592. if (SCM_COPY_SOURCE_P)
  593. ans2 = tl2 = scm_cons (SCM_CONSP (tmp)
  594. ? *copy
  595. : tmp,
  596. SCM_EOL);
  597. while (')' != (c = scm_flush_ws (port, name)))
  598. {
  599. scm_ungetc (c, port);
  600. if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
  601. {
  602. SCM_SETCDR (tl, tmp = scm_lreadr (tok_buf, port, copy));
  603. if (SCM_COPY_SOURCE_P)
  604. SCM_SETCDR (tl2, scm_cons (SCM_CONSP (tmp)
  605. ? *copy
  606. : tmp,
  607. SCM_EOL));
  608. if (')' != (c = scm_flush_ws (port, name)))
  609. scm_wta (SCM_UNDEFINED, "missing close paren", "");
  610. goto exit;
  611. }
  612. tl = SCM_SETCDR (tl, scm_cons (tmp, SCM_EOL));
  613. if (SCM_COPY_SOURCE_P)
  614. tl2 = SCM_SETCDR (tl2, scm_cons (SCM_CONSP (tmp)
  615. ? *copy
  616. : tmp,
  617. SCM_EOL));
  618. }
  619. exit:
  620. scm_whash_insert (scm_source_whash,
  621. ans,
  622. scm_make_srcprops (line,
  623. column,
  624. SCM_FILENAME (port),
  625. SCM_COPY_SOURCE_P
  626. ? *copy = ans2
  627. : SCM_UNDEFINED,
  628. SCM_EOL));
  629. return ans;
  630. }
  631. /* Manipulate the read-hash-procedures alist. This could be written in
  632. Scheme, but maybe it will also be used by C code during initialisation. */
  633. SCM_DEFINE (scm_read_hash_extend, "read-hash-extend", 2, 0, 0,
  634. (SCM chr, SCM proc),
  635. "")
  636. #define FUNC_NAME s_scm_read_hash_extend
  637. {
  638. SCM this;
  639. SCM prev;
  640. SCM_VALIDATE_CHAR (1,chr);
  641. SCM_ASSERT (SCM_FALSEP (proc) || SCM_NIMP(proc), proc, SCM_ARG2,
  642. FUNC_NAME);
  643. /* Check if chr is already in the alist. */
  644. this = *scm_read_hash_procedures;
  645. prev = SCM_BOOL_F;
  646. while (1)
  647. {
  648. if (SCM_NULLP (this))
  649. {
  650. /* not found, so add it to the beginning. */
  651. if (SCM_NFALSEP (proc))
  652. {
  653. *scm_read_hash_procedures =
  654. scm_cons (scm_cons (chr, proc), *scm_read_hash_procedures);
  655. }
  656. break;
  657. }
  658. if (SCM_EQ_P (chr, SCM_CAAR (this)))
  659. {
  660. /* already in the alist. */
  661. if (SCM_FALSEP (proc))
  662. {
  663. /* remove it. */
  664. if (SCM_FALSEP (prev))
  665. {
  666. *scm_read_hash_procedures =
  667. SCM_CDR (*scm_read_hash_procedures);
  668. }
  669. else
  670. scm_set_cdr_x (prev, SCM_CDR (this));
  671. }
  672. else
  673. {
  674. /* replace it. */
  675. scm_set_cdr_x (SCM_CAR (this), proc);
  676. }
  677. break;
  678. }
  679. prev = this;
  680. this = SCM_CDR (this);
  681. }
  682. return SCM_UNSPECIFIED;
  683. }
  684. #undef FUNC_NAME
  685. /* Recover the read-hash procedure corresponding to char c. */
  686. static SCM
  687. scm_get_hash_procedure (int c)
  688. {
  689. SCM rest = *scm_read_hash_procedures;
  690. while (1)
  691. {
  692. if (SCM_NULLP (rest))
  693. return SCM_BOOL_F;
  694. if (SCM_CHAR (SCM_CAAR (rest)) == c)
  695. return SCM_CDAR (rest);
  696. rest = SCM_CDR (rest);
  697. }
  698. }
  699. void
  700. scm_init_read ()
  701. {
  702. scm_read_hash_procedures =
  703. SCM_CDRLOC (scm_sysintern ("read-hash-procedures", SCM_EOL));
  704. scm_init_opts (scm_read_options, scm_read_opts, SCM_N_READ_OPTIONS);
  705. #include "libguile/read.x"
  706. }
  707. /*
  708. Local Variables:
  709. c-file-style: "gnu"
  710. End:
  711. */