read.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271
  1. /* Copyright (C) 1995, 1996, 1997, 1999, 2000, 2001, 2003, 2004, 2006,
  2. * 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <unicase.h>
  26. #include <unictype.h>
  27. #include <c-strcase.h>
  28. #include "libguile/_scm.h"
  29. #include "libguile/bytevectors.h"
  30. #include "libguile/chars.h"
  31. #include "libguile/eval.h"
  32. #include "libguile/arrays.h"
  33. #include "libguile/bitvectors.h"
  34. #include "libguile/keywords.h"
  35. #include "libguile/alist.h"
  36. #include "libguile/srcprop.h"
  37. #include "libguile/hashtab.h"
  38. #include "libguile/hash.h"
  39. #include "libguile/ports.h"
  40. #include "libguile/ports-internal.h"
  41. #include "libguile/fports.h"
  42. #include "libguile/root.h"
  43. #include "libguile/strings.h"
  44. #include "libguile/strports.h"
  45. #include "libguile/vectors.h"
  46. #include "libguile/validate.h"
  47. #include "libguile/srfi-4.h"
  48. #include "libguile/srfi-13.h"
  49. #include "libguile/read.h"
  50. #include "libguile/private-options.h"
  51. SCM_GLOBAL_SYMBOL (scm_sym_dot, ".");
  52. SCM_SYMBOL (scm_keyword_prefix, "prefix");
  53. SCM_SYMBOL (scm_keyword_postfix, "postfix");
  54. SCM_SYMBOL (sym_nil, "nil");
  55. /* SRFI-105 curly infix expression support */
  56. SCM_SYMBOL (sym_nfx, "$nfx$");
  57. SCM_SYMBOL (sym_bracket_list, "$bracket-list$");
  58. SCM_SYMBOL (sym_bracket_apply, "$bracket-apply$");
  59. scm_t_option scm_read_opts[] =
  60. {
  61. { SCM_OPTION_BOOLEAN, "copy", 0,
  62. "Copy source code expressions." },
  63. { SCM_OPTION_BOOLEAN, "positions", 1,
  64. "Record positions of source code expressions." },
  65. { SCM_OPTION_BOOLEAN, "case-insensitive", 0,
  66. "Convert symbols to lower case."},
  67. { SCM_OPTION_SCM, "keywords", (scm_t_bits) SCM_BOOL_F_BITS,
  68. "Style of keyword recognition: #f, 'prefix or 'postfix."},
  69. { SCM_OPTION_BOOLEAN, "r6rs-hex-escapes", 0,
  70. "Use R6RS variable-length character and string hex escapes."},
  71. { SCM_OPTION_BOOLEAN, "square-brackets", 1,
  72. "Treat `[' and `]' as parentheses, for R6RS compatibility."},
  73. { SCM_OPTION_BOOLEAN, "hungry-eol-escapes", 0,
  74. "In strings, consume leading whitespace after an escaped end-of-line."},
  75. { SCM_OPTION_BOOLEAN, "curly-infix", 0,
  76. "Support SRFI-105 curly infix expressions."},
  77. { 0, },
  78. };
  79. /* Internal read options structure. This is initialized by 'scm_read'
  80. from the global and per-port read options, and a pointer is passed
  81. down to all helper functions. */
  82. enum t_keyword_style
  83. {
  84. KEYWORD_STYLE_HASH_PREFIX,
  85. KEYWORD_STYLE_PREFIX,
  86. KEYWORD_STYLE_POSTFIX
  87. };
  88. struct t_read_opts
  89. {
  90. enum t_keyword_style keyword_style;
  91. unsigned int copy_source_p : 1;
  92. unsigned int record_positions_p : 1;
  93. unsigned int case_insensitive_p : 1;
  94. unsigned int r6rs_escapes_p : 1;
  95. unsigned int square_brackets_p : 1;
  96. unsigned int hungry_eol_escapes_p : 1;
  97. unsigned int curly_infix_p : 1;
  98. unsigned int neoteric_p : 1;
  99. };
  100. typedef struct t_read_opts scm_t_read_opts;
  101. /*
  102. Give meaningful error messages for errors
  103. We use the format
  104. FILE:LINE:COL: MESSAGE
  105. This happened in ....
  106. This is not standard GNU format, but the test-suite likes the real
  107. message to be in front.
  108. */
  109. void
  110. scm_i_input_error (char const *function,
  111. SCM port, const char *message, SCM arg)
  112. {
  113. SCM fn = (scm_is_string (SCM_FILENAME(port))
  114. ? SCM_FILENAME(port)
  115. : scm_from_locale_string ("#<unknown port>"));
  116. SCM string_port = scm_open_output_string ();
  117. SCM string = SCM_EOL;
  118. scm_simple_format (string_port,
  119. scm_from_locale_string ("~A:~S:~S: ~A"),
  120. scm_list_4 (fn,
  121. scm_from_long (SCM_LINUM (port) + 1),
  122. scm_from_int (SCM_COL (port) + 1),
  123. scm_from_locale_string (message)));
  124. string = scm_get_output_string (string_port);
  125. scm_close_output_port (string_port);
  126. scm_error_scm (scm_from_latin1_symbol ("read-error"),
  127. function? scm_from_locale_string (function) : SCM_BOOL_F,
  128. string,
  129. arg,
  130. SCM_BOOL_F);
  131. }
  132. SCM_DEFINE (scm_read_options, "read-options-interface", 0, 1, 0,
  133. (SCM setting),
  134. "Option interface for the read options. Instead of using\n"
  135. "this procedure directly, use the procedures @code{read-enable},\n"
  136. "@code{read-disable}, @code{read-set!} and @code{read-options}.")
  137. #define FUNC_NAME s_scm_read_options
  138. {
  139. SCM ans = scm_options (setting,
  140. scm_read_opts,
  141. FUNC_NAME);
  142. if (SCM_COPY_SOURCE_P)
  143. SCM_RECORD_POSITIONS_P = 1;
  144. return ans;
  145. }
  146. #undef FUNC_NAME
  147. /* A fluid referring to an association list mapping extra hash
  148. characters to procedures. */
  149. static SCM *scm_i_read_hash_procedures;
  150. static SCM
  151. scm_i_read_hash_procedures_ref (void)
  152. {
  153. return scm_fluid_ref (*scm_i_read_hash_procedures);
  154. }
  155. static void
  156. scm_i_read_hash_procedures_set_x (SCM value)
  157. {
  158. scm_fluid_set_x (*scm_i_read_hash_procedures, value);
  159. }
  160. /* Token readers. */
  161. /* Size of the C buffer used to read symbols and numbers. */
  162. #define READER_BUFFER_SIZE 128
  163. /* Number of 32-bit codepoints in the buffer used to read strings. */
  164. #define READER_STRING_BUFFER_SIZE 128
  165. /* The maximum size of Scheme character names. */
  166. #define READER_CHAR_NAME_MAX_SIZE 50
  167. /* The maximum size of reader directive names. */
  168. #define READER_DIRECTIVE_NAME_MAX_SIZE 50
  169. /* `isblank' is only in C99. */
  170. #define CHAR_IS_BLANK_(_chr) \
  171. (((_chr) == ' ') || ((_chr) == '\t') || ((_chr) == '\n') \
  172. || ((_chr) == '\f') || ((_chr) == '\r'))
  173. #ifdef MSDOS
  174. # define CHAR_IS_BLANK(_chr) \
  175. ((CHAR_IS_BLANK_ (chr)) || ((_chr) == 26))
  176. #else
  177. # define CHAR_IS_BLANK CHAR_IS_BLANK_
  178. #endif
  179. /* R5RS one-character delimiters (see section 7.1.1, ``Lexical
  180. structure''). */
  181. #define CHAR_IS_R5RS_DELIMITER(c) \
  182. (CHAR_IS_BLANK (c) \
  183. || (c) == ')' || (c) == '(' || (c) == ';' || (c) == '"')
  184. #define CHAR_IS_DELIMITER(c) \
  185. (CHAR_IS_R5RS_DELIMITER (c) \
  186. || (((c) == ']' || (c) == '[') && (opts->square_brackets_p \
  187. || opts->curly_infix_p)) \
  188. || (((c) == '}' || (c) == '{') && opts->curly_infix_p))
  189. /* Exponent markers, as defined in section 7.1.1 of R5RS, ``Lexical
  190. Structure''. */
  191. #define CHAR_IS_EXPONENT_MARKER(_chr) \
  192. (((_chr) == 'e') || ((_chr) == 's') || ((_chr) == 'f') \
  193. || ((_chr) == 'd') || ((_chr) == 'l'))
  194. /* Read an SCSH block comment. */
  195. static SCM scm_read_scsh_block_comment (scm_t_wchar, SCM);
  196. static SCM scm_read_r6rs_block_comment (scm_t_wchar, SCM);
  197. static SCM scm_read_commented_expression (scm_t_wchar, SCM, scm_t_read_opts *);
  198. static SCM scm_read_shebang (scm_t_wchar, SCM, scm_t_read_opts *);
  199. static SCM scm_get_hash_procedure (int);
  200. /* Read from PORT until a delimiter (e.g., a whitespace) is read. Put the
  201. result in the pre-allocated buffer BUF. Return zero if the whole token has
  202. fewer than BUF_SIZE bytes, non-zero otherwise. READ will be set the number of
  203. bytes actually read. */
  204. static int
  205. read_token (SCM port, scm_t_read_opts *opts,
  206. char *buf, size_t buf_size, size_t *read)
  207. {
  208. *read = 0;
  209. while (*read < buf_size)
  210. {
  211. int chr;
  212. chr = scm_get_byte_or_eof_unlocked (port);
  213. if (chr == EOF)
  214. return 0;
  215. else if (CHAR_IS_DELIMITER (chr))
  216. {
  217. scm_unget_byte_unlocked (chr, port);
  218. return 0;
  219. }
  220. else
  221. {
  222. *buf = (char) chr;
  223. buf++, (*read)++;
  224. }
  225. }
  226. return 1;
  227. }
  228. /* Like `read_token', but return either BUFFER, or a GC-allocated buffer
  229. if the token doesn't fit in BUFFER_SIZE bytes. */
  230. static char *
  231. read_complete_token (SCM port, scm_t_read_opts *opts,
  232. char *buffer, size_t buffer_size, size_t *read)
  233. {
  234. int overflow = 0;
  235. size_t bytes_read, overflow_size = 0;
  236. char *overflow_buffer = NULL;
  237. do
  238. {
  239. overflow = read_token (port, opts, buffer, buffer_size, &bytes_read);
  240. if (bytes_read == 0)
  241. break;
  242. if (overflow || overflow_size != 0)
  243. {
  244. if (overflow_size == 0)
  245. {
  246. overflow_buffer = scm_gc_malloc_pointerless (bytes_read, "read");
  247. memcpy (overflow_buffer, buffer, bytes_read);
  248. overflow_size = bytes_read;
  249. }
  250. else
  251. {
  252. char *new_buf =
  253. scm_gc_malloc_pointerless (overflow_size + bytes_read, "read");
  254. memcpy (new_buf, overflow_buffer, overflow_size);
  255. memcpy (new_buf + overflow_size, buffer, bytes_read);
  256. overflow_buffer = new_buf;
  257. overflow_size += bytes_read;
  258. }
  259. }
  260. }
  261. while (overflow);
  262. if (overflow_size)
  263. *read = overflow_size;
  264. else
  265. *read = bytes_read;
  266. return (overflow_size > 0 ? overflow_buffer : buffer);
  267. }
  268. /* Skip whitespace from PORT and return the first non-whitespace character
  269. read. Raise an error on end-of-file. */
  270. static int
  271. flush_ws (SCM port, scm_t_read_opts *opts, const char *eoferr)
  272. {
  273. scm_t_wchar c;
  274. while (1)
  275. switch (c = scm_getc_unlocked (port))
  276. {
  277. case EOF:
  278. goteof:
  279. if (eoferr)
  280. {
  281. scm_i_input_error (eoferr,
  282. port,
  283. "end of file",
  284. SCM_EOL);
  285. }
  286. return c;
  287. case ';':
  288. lp:
  289. switch (c = scm_getc_unlocked (port))
  290. {
  291. case EOF:
  292. goto goteof;
  293. default:
  294. goto lp;
  295. case SCM_LINE_INCREMENTORS:
  296. break;
  297. }
  298. break;
  299. case '#':
  300. switch (c = scm_getc_unlocked (port))
  301. {
  302. case EOF:
  303. eoferr = "read_sharp";
  304. goto goteof;
  305. case '!':
  306. scm_read_shebang (c, port, opts);
  307. break;
  308. case ';':
  309. scm_read_commented_expression (c, port, opts);
  310. break;
  311. case '|':
  312. if (scm_is_false (scm_get_hash_procedure (c)))
  313. {
  314. scm_read_r6rs_block_comment (c, port);
  315. break;
  316. }
  317. /* fall through */
  318. default:
  319. scm_ungetc_unlocked (c, port);
  320. return '#';
  321. }
  322. break;
  323. case SCM_LINE_INCREMENTORS:
  324. case SCM_SINGLE_SPACES:
  325. case '\t':
  326. break;
  327. default:
  328. return c;
  329. }
  330. return 0;
  331. }
  332. /* Token readers. */
  333. static SCM scm_read_expression (SCM port, scm_t_read_opts *opts);
  334. static SCM scm_read_sharp (int chr, SCM port, scm_t_read_opts *opts,
  335. long line, int column);
  336. static SCM
  337. maybe_annotate_source (SCM x, SCM port, scm_t_read_opts *opts,
  338. long line, int column)
  339. {
  340. if (opts->record_positions_p)
  341. scm_i_set_source_properties_x (x, line, column, SCM_FILENAME (port));
  342. return x;
  343. }
  344. static SCM
  345. scm_read_sexp (scm_t_wchar chr, SCM port, scm_t_read_opts *opts)
  346. #define FUNC_NAME "scm_i_lreadparen"
  347. {
  348. int c;
  349. SCM tmp, tl, ans = SCM_EOL;
  350. const int curly_list_p = (chr == '{') && opts->curly_infix_p;
  351. const int terminating_char = ((chr == '{') ? '}'
  352. : ((chr == '[') ? ']'
  353. : ')'));
  354. /* Need to capture line and column numbers here. */
  355. long line = SCM_LINUM (port);
  356. int column = SCM_COL (port) - 1;
  357. c = flush_ws (port, opts, FUNC_NAME);
  358. if (terminating_char == c)
  359. return SCM_EOL;
  360. scm_ungetc_unlocked (c, port);
  361. tmp = scm_read_expression (port, opts);
  362. /* Note that it is possible for scm_read_expression to return
  363. scm_sym_dot, but not as part of a dotted pair: as in #{.}#. So
  364. check that it's a real dot by checking `c'. */
  365. if (c == '.' && scm_is_eq (scm_sym_dot, tmp))
  366. {
  367. ans = scm_read_expression (port, opts);
  368. if (terminating_char != (c = flush_ws (port, opts, FUNC_NAME)))
  369. scm_i_input_error (FUNC_NAME, port, "missing close paren",
  370. SCM_EOL);
  371. return ans;
  372. }
  373. /* Build the head of the list structure. */
  374. ans = tl = scm_cons (tmp, SCM_EOL);
  375. while (terminating_char != (c = flush_ws (port, opts, FUNC_NAME)))
  376. {
  377. SCM new_tail;
  378. if (c == ')' || (c == ']' && opts->square_brackets_p)
  379. || ((c == '}' || c == ']') && opts->curly_infix_p))
  380. scm_i_input_error (FUNC_NAME, port,
  381. "in pair: mismatched close paren: ~A",
  382. scm_list_1 (SCM_MAKE_CHAR (c)));
  383. scm_ungetc_unlocked (c, port);
  384. tmp = scm_read_expression (port, opts);
  385. /* See above note about scm_sym_dot. */
  386. if (c == '.' && scm_is_eq (scm_sym_dot, tmp))
  387. {
  388. SCM_SETCDR (tl, scm_read_expression (port, opts));
  389. c = flush_ws (port, opts, FUNC_NAME);
  390. if (terminating_char != c)
  391. scm_i_input_error (FUNC_NAME, port,
  392. "in pair: missing close paren", SCM_EOL);
  393. break;
  394. }
  395. new_tail = scm_cons (tmp, SCM_EOL);
  396. SCM_SETCDR (tl, new_tail);
  397. tl = new_tail;
  398. }
  399. if (curly_list_p)
  400. {
  401. /* In addition to finding the length, 'scm_ilength' checks for
  402. improper or circular lists, in which case it returns -1. */
  403. int len = scm_ilength (ans);
  404. /* The (len == 0) case is handled above */
  405. if (len == 1)
  406. /* Return directly to avoid re-annotating the element's source
  407. location with the position of the outer brace. Also, it
  408. might not be possible to annotate the element. */
  409. return scm_car (ans); /* {e} => e */
  410. else if (len == 2)
  411. ; /* Leave the list unchanged: {e1 e2} => (e1 e2) */
  412. else if (len >= 3 && (len & 1))
  413. {
  414. /* It's a proper list whose length is odd and at least 3. If
  415. the elements at odd indices (the infix operator positions)
  416. are all 'equal?', then it's a simple curly-infix list.
  417. Otherwise it's a mixed curly-infix list. */
  418. SCM op = scm_cadr (ans);
  419. /* Check to see if the elements at odd indices are 'equal?' */
  420. for (tl = scm_cdddr (ans); ; tl = scm_cddr (tl))
  421. {
  422. if (scm_is_null (tl))
  423. {
  424. /* Convert simple curly-infix list to prefix:
  425. {a <op> b <op> ...} => (<op> a b ...) */
  426. tl = ans;
  427. while (scm_is_pair (scm_cdr (tl)))
  428. {
  429. tmp = scm_cddr (tl);
  430. SCM_SETCDR (tl, tmp);
  431. tl = tmp;
  432. }
  433. ans = scm_cons (op, ans);
  434. break;
  435. }
  436. else if (scm_is_false (scm_equal_p (op, scm_car (tl))))
  437. {
  438. /* Mixed curly-infix list: {e ...} => ($nfx$ e ...) */
  439. ans = scm_cons (sym_nfx, ans);
  440. break;
  441. }
  442. }
  443. }
  444. else
  445. /* Mixed curly-infix (possibly improper) list:
  446. {e . tail} => ($nfx$ e . tail) */
  447. ans = scm_cons (sym_nfx, ans);
  448. }
  449. return maybe_annotate_source (ans, port, opts, line, column);
  450. }
  451. #undef FUNC_NAME
  452. /* Read a hexadecimal number NDIGITS in length. Put its value into the variable
  453. C. If TERMINATOR is non-null, terminate early if the TERMINATOR character is
  454. found. */
  455. #define SCM_READ_HEX_ESCAPE(ndigits, terminator) \
  456. do \
  457. { \
  458. scm_t_wchar a; \
  459. size_t i = 0; \
  460. c = 0; \
  461. while (i < ndigits) \
  462. { \
  463. a = scm_getc_unlocked (port); \
  464. if (a == EOF) \
  465. goto str_eof; \
  466. if (terminator \
  467. && (a == (scm_t_wchar) terminator) \
  468. && (i > 0)) \
  469. break; \
  470. if ('0' <= a && a <= '9') \
  471. a -= '0'; \
  472. else if ('A' <= a && a <= 'F') \
  473. a = a - 'A' + 10; \
  474. else if ('a' <= a && a <= 'f') \
  475. a = a - 'a' + 10; \
  476. else \
  477. { \
  478. c = a; \
  479. goto bad_escaped; \
  480. } \
  481. c = c * 16 + a; \
  482. i ++; \
  483. } \
  484. } while (0)
  485. static void
  486. skip_intraline_whitespace (SCM port)
  487. {
  488. scm_t_wchar c;
  489. do
  490. {
  491. c = scm_getc_unlocked (port);
  492. if (c == EOF)
  493. return;
  494. }
  495. while (c == '\t' || uc_is_general_category (c, UC_SPACE_SEPARATOR));
  496. scm_ungetc_unlocked (c, port);
  497. }
  498. static SCM
  499. scm_read_string (int chr, SCM port, scm_t_read_opts *opts)
  500. #define FUNC_NAME "scm_lreadr"
  501. {
  502. /* For strings smaller than C_STR, this function creates only one Scheme
  503. object (the string returned). */
  504. SCM str = SCM_EOL;
  505. size_t c_str_len = 0;
  506. scm_t_wchar c, c_str[READER_STRING_BUFFER_SIZE];
  507. /* Need to capture line and column numbers here. */
  508. long line = SCM_LINUM (port);
  509. int column = SCM_COL (port) - 1;
  510. while ('"' != (c = scm_getc_unlocked (port)))
  511. {
  512. if (c == EOF)
  513. {
  514. str_eof:
  515. scm_i_input_error (FUNC_NAME, port,
  516. "end of file in string constant", SCM_EOL);
  517. }
  518. if (c_str_len + 1 >= READER_STRING_BUFFER_SIZE)
  519. {
  520. str = scm_cons (scm_from_utf32_stringn (c_str, c_str_len), str);
  521. c_str_len = 0;
  522. }
  523. if (c == '\\')
  524. {
  525. switch (c = scm_getc_unlocked (port))
  526. {
  527. case EOF:
  528. goto str_eof;
  529. case '"':
  530. case '\\':
  531. break;
  532. case '\n':
  533. if (opts->hungry_eol_escapes_p)
  534. skip_intraline_whitespace (port);
  535. continue;
  536. case '0':
  537. c = '\0';
  538. break;
  539. case 'f':
  540. c = '\f';
  541. break;
  542. case 'n':
  543. c = '\n';
  544. break;
  545. case 'r':
  546. c = '\r';
  547. break;
  548. case 't':
  549. c = '\t';
  550. break;
  551. case 'a':
  552. c = '\007';
  553. break;
  554. case 'v':
  555. c = '\v';
  556. break;
  557. case 'b':
  558. c = '\010';
  559. break;
  560. case 'x':
  561. if (opts->r6rs_escapes_p)
  562. SCM_READ_HEX_ESCAPE (10, ';');
  563. else
  564. SCM_READ_HEX_ESCAPE (2, '\0');
  565. break;
  566. case 'u':
  567. if (!opts->r6rs_escapes_p)
  568. {
  569. SCM_READ_HEX_ESCAPE (4, '\0');
  570. break;
  571. }
  572. case 'U':
  573. if (!opts->r6rs_escapes_p)
  574. {
  575. SCM_READ_HEX_ESCAPE (6, '\0');
  576. break;
  577. }
  578. default:
  579. bad_escaped:
  580. scm_i_input_error (FUNC_NAME, port,
  581. "illegal character in escape sequence: ~S",
  582. scm_list_1 (SCM_MAKE_CHAR (c)));
  583. }
  584. }
  585. c_str[c_str_len++] = c;
  586. }
  587. if (scm_is_null (str))
  588. /* Fast path: we got a string that fits in C_STR. */
  589. str = scm_from_utf32_stringn (c_str, c_str_len);
  590. else
  591. {
  592. if (c_str_len > 0)
  593. str = scm_cons (scm_from_utf32_stringn (c_str, c_str_len), str);
  594. str = scm_string_concatenate_reverse (str, SCM_UNDEFINED, SCM_UNDEFINED);
  595. }
  596. return maybe_annotate_source (str, port, opts, line, column);
  597. }
  598. #undef FUNC_NAME
  599. static SCM
  600. scm_read_number (scm_t_wchar chr, SCM port, scm_t_read_opts *opts)
  601. {
  602. SCM result, str = SCM_EOL;
  603. char local_buffer[READER_BUFFER_SIZE], *buffer;
  604. size_t bytes_read;
  605. /* Need to capture line and column numbers here. */
  606. long line = SCM_LINUM (port);
  607. int column = SCM_COL (port) - 1;
  608. scm_ungetc_unlocked (chr, port);
  609. buffer = read_complete_token (port, opts, local_buffer, sizeof local_buffer,
  610. &bytes_read);
  611. str = scm_from_port_stringn (buffer, bytes_read, port);
  612. result = scm_string_to_number (str, SCM_UNDEFINED);
  613. if (scm_is_false (result))
  614. {
  615. /* Return a symbol instead of a number */
  616. if (opts->case_insensitive_p)
  617. str = scm_string_downcase_x (str);
  618. result = scm_string_to_symbol (str);
  619. }
  620. else if (SCM_NIMP (result))
  621. result = maybe_annotate_source (result, port, opts, line, column);
  622. SCM_COL (port) += scm_i_string_length (str);
  623. return result;
  624. }
  625. static SCM
  626. scm_read_mixed_case_symbol (scm_t_wchar chr, SCM port, scm_t_read_opts *opts)
  627. {
  628. SCM result;
  629. int ends_with_colon = 0;
  630. size_t bytes_read;
  631. int postfix = (opts->keyword_style == KEYWORD_STYLE_POSTFIX);
  632. char local_buffer[READER_BUFFER_SIZE], *buffer;
  633. SCM str;
  634. scm_ungetc_unlocked (chr, port);
  635. buffer = read_complete_token (port, opts, local_buffer, sizeof local_buffer,
  636. &bytes_read);
  637. if (bytes_read > 0)
  638. ends_with_colon = buffer[bytes_read - 1] == ':';
  639. if (postfix && ends_with_colon && (bytes_read > 1))
  640. {
  641. str = scm_from_port_stringn (buffer, bytes_read - 1, port);
  642. if (opts->case_insensitive_p)
  643. str = scm_string_downcase_x (str);
  644. result = scm_symbol_to_keyword (scm_string_to_symbol (str));
  645. }
  646. else
  647. {
  648. str = scm_from_port_stringn (buffer, bytes_read, port);
  649. if (opts->case_insensitive_p)
  650. str = scm_string_downcase_x (str);
  651. result = scm_string_to_symbol (str);
  652. }
  653. SCM_COL (port) += scm_i_string_length (str);
  654. return result;
  655. }
  656. static SCM
  657. scm_read_number_and_radix (scm_t_wchar chr, SCM port, scm_t_read_opts *opts)
  658. #define FUNC_NAME "scm_lreadr"
  659. {
  660. SCM result;
  661. size_t read;
  662. char local_buffer[READER_BUFFER_SIZE], *buffer;
  663. unsigned int radix;
  664. SCM str;
  665. switch (chr)
  666. {
  667. case 'B':
  668. case 'b':
  669. radix = 2;
  670. break;
  671. case 'o':
  672. case 'O':
  673. radix = 8;
  674. break;
  675. case 'd':
  676. case 'D':
  677. radix = 10;
  678. break;
  679. case 'x':
  680. case 'X':
  681. radix = 16;
  682. break;
  683. default:
  684. scm_ungetc_unlocked (chr, port);
  685. scm_ungetc_unlocked ('#', port);
  686. radix = 10;
  687. }
  688. buffer = read_complete_token (port, opts, local_buffer, sizeof local_buffer,
  689. &read);
  690. str = scm_from_port_stringn (buffer, read, port);
  691. result = scm_string_to_number (str, scm_from_uint (radix));
  692. SCM_COL (port) += scm_i_string_length (str);
  693. if (scm_is_true (result))
  694. return result;
  695. scm_i_input_error (FUNC_NAME, port, "unknown # object", SCM_EOL);
  696. return SCM_BOOL_F;
  697. }
  698. #undef FUNC_NAME
  699. static SCM
  700. scm_read_quote (int chr, SCM port, scm_t_read_opts *opts)
  701. {
  702. SCM p;
  703. long line = SCM_LINUM (port);
  704. int column = SCM_COL (port) - 1;
  705. switch (chr)
  706. {
  707. case '`':
  708. p = scm_sym_quasiquote;
  709. break;
  710. case '\'':
  711. p = scm_sym_quote;
  712. break;
  713. case ',':
  714. {
  715. scm_t_wchar c;
  716. c = scm_getc_unlocked (port);
  717. if ('@' == c)
  718. p = scm_sym_uq_splicing;
  719. else
  720. {
  721. scm_ungetc_unlocked (c, port);
  722. p = scm_sym_unquote;
  723. }
  724. break;
  725. }
  726. default:
  727. fprintf (stderr, "%s: unhandled quote character (%i)\n",
  728. "scm_read_quote", chr);
  729. abort ();
  730. }
  731. p = scm_cons2 (p, scm_read_expression (port, opts), SCM_EOL);
  732. return maybe_annotate_source (p, port, opts, line, column);
  733. }
  734. SCM_SYMBOL (sym_syntax, "syntax");
  735. SCM_SYMBOL (sym_quasisyntax, "quasisyntax");
  736. SCM_SYMBOL (sym_unsyntax, "unsyntax");
  737. SCM_SYMBOL (sym_unsyntax_splicing, "unsyntax-splicing");
  738. static SCM
  739. scm_read_syntax (int chr, SCM port, scm_t_read_opts *opts)
  740. {
  741. SCM p;
  742. long line = SCM_LINUM (port);
  743. int column = SCM_COL (port) - 1;
  744. switch (chr)
  745. {
  746. case '`':
  747. p = sym_quasisyntax;
  748. break;
  749. case '\'':
  750. p = sym_syntax;
  751. break;
  752. case ',':
  753. {
  754. int c;
  755. c = scm_getc_unlocked (port);
  756. if ('@' == c)
  757. p = sym_unsyntax_splicing;
  758. else
  759. {
  760. scm_ungetc_unlocked (c, port);
  761. p = sym_unsyntax;
  762. }
  763. break;
  764. }
  765. default:
  766. fprintf (stderr, "%s: unhandled syntax character (%i)\n",
  767. "scm_read_syntax", chr);
  768. abort ();
  769. }
  770. p = scm_cons2 (p, scm_read_expression (port, opts), SCM_EOL);
  771. return maybe_annotate_source (p, port, opts, line, column);
  772. }
  773. static SCM
  774. scm_read_nil (int chr, SCM port, scm_t_read_opts *opts)
  775. {
  776. SCM id = scm_read_mixed_case_symbol (chr, port, opts);
  777. if (!scm_is_eq (id, sym_nil))
  778. scm_i_input_error ("scm_read_nil", port,
  779. "unexpected input while reading #nil: ~a",
  780. scm_list_1 (id));
  781. return SCM_ELISP_NIL;
  782. }
  783. static SCM
  784. scm_read_semicolon_comment (int chr, SCM port)
  785. {
  786. int c;
  787. /* We use the get_byte here because there is no need to get the
  788. locale correct with comment input. This presumes that newline
  789. always represents itself no matter what the encoding is. */
  790. for (c = scm_get_byte_or_eof_unlocked (port);
  791. (c != EOF) && (c != '\n');
  792. c = scm_get_byte_or_eof_unlocked (port));
  793. return SCM_UNSPECIFIED;
  794. }
  795. /* Sharp readers, i.e. readers called after a `#' sign has been read. */
  796. static SCM
  797. scm_read_boolean (int chr, SCM port)
  798. {
  799. switch (chr)
  800. {
  801. case 't':
  802. case 'T':
  803. return SCM_BOOL_T;
  804. case 'f':
  805. case 'F':
  806. return SCM_BOOL_F;
  807. }
  808. return SCM_UNSPECIFIED;
  809. }
  810. static SCM
  811. scm_read_character (scm_t_wchar chr, SCM port, scm_t_read_opts *opts)
  812. #define FUNC_NAME "scm_lreadr"
  813. {
  814. char buffer[READER_CHAR_NAME_MAX_SIZE];
  815. SCM charname;
  816. size_t charname_len, bytes_read;
  817. scm_t_wchar cp;
  818. int overflow;
  819. scm_t_port_internal *pti;
  820. overflow = read_token (port, opts, buffer, READER_CHAR_NAME_MAX_SIZE,
  821. &bytes_read);
  822. if (overflow)
  823. scm_i_input_error (FUNC_NAME, port, "character name too long", SCM_EOL);
  824. if (bytes_read == 0)
  825. {
  826. chr = scm_getc_unlocked (port);
  827. if (chr == EOF)
  828. scm_i_input_error (FUNC_NAME, port, "unexpected end of file "
  829. "while reading character", SCM_EOL);
  830. /* CHR must be a token delimiter, like a whitespace. */
  831. return (SCM_MAKE_CHAR (chr));
  832. }
  833. pti = SCM_PORT_GET_INTERNAL (port);
  834. /* Simple ASCII characters can be processed immediately. Also, simple
  835. ISO-8859-1 characters can be processed immediately if the encoding for this
  836. port is ISO-8859-1. */
  837. if (bytes_read == 1 &&
  838. ((unsigned char) buffer[0] <= 127
  839. || pti->encoding_mode == SCM_PORT_ENCODING_MODE_LATIN1))
  840. {
  841. SCM_COL (port) += 1;
  842. return SCM_MAKE_CHAR (buffer[0]);
  843. }
  844. /* Otherwise, convert the buffer into a proper scheme string for
  845. processing. */
  846. charname = scm_from_port_stringn (buffer, bytes_read, port);
  847. charname_len = scm_i_string_length (charname);
  848. SCM_COL (port) += charname_len;
  849. cp = scm_i_string_ref (charname, 0);
  850. if (charname_len == 1)
  851. return SCM_MAKE_CHAR (cp);
  852. /* Ignore dotted circles, which may be used to keep combining characters from
  853. combining with the backslash in #\charname. */
  854. if (cp == SCM_CODEPOINT_DOTTED_CIRCLE && charname_len == 2)
  855. return SCM_MAKE_CHAR (scm_i_string_ref (charname, 1));
  856. if (cp >= '0' && cp < '8')
  857. {
  858. /* Dirk:FIXME:: This type of character syntax is not R5RS
  859. * compliant. Further, it should be verified that the constant
  860. * does only consist of octal digits. */
  861. SCM p = scm_string_to_number (charname, scm_from_uint (8));
  862. if (SCM_I_INUMP (p))
  863. {
  864. scm_t_wchar c = scm_to_uint32 (p);
  865. if (SCM_IS_UNICODE_CHAR (c))
  866. return SCM_MAKE_CHAR (c);
  867. else
  868. scm_i_input_error (FUNC_NAME, port,
  869. "out-of-range octal character escape: ~a",
  870. scm_list_1 (charname));
  871. }
  872. }
  873. if (cp == 'x' && (charname_len > 1))
  874. {
  875. SCM p;
  876. /* Convert from hex, skipping the initial 'x' character in CHARNAME */
  877. p = scm_string_to_number (scm_c_substring (charname, 1, charname_len),
  878. scm_from_uint (16));
  879. if (SCM_I_INUMP (p))
  880. {
  881. scm_t_wchar c = scm_to_uint32 (p);
  882. if (SCM_IS_UNICODE_CHAR (c))
  883. return SCM_MAKE_CHAR (c);
  884. else
  885. scm_i_input_error (FUNC_NAME, port,
  886. "out-of-range hex character escape: ~a",
  887. scm_list_1 (charname));
  888. }
  889. }
  890. /* The names of characters should never have non-Latin1
  891. characters. */
  892. if (scm_i_is_narrow_string (charname)
  893. || scm_i_try_narrow_string (charname))
  894. { SCM ch = scm_i_charname_to_char (scm_i_string_chars (charname),
  895. charname_len);
  896. if (scm_is_true (ch))
  897. return ch;
  898. }
  899. scm_i_input_error (FUNC_NAME, port, "unknown character name ~a",
  900. scm_list_1 (charname));
  901. return SCM_UNSPECIFIED;
  902. }
  903. #undef FUNC_NAME
  904. static SCM
  905. scm_read_keyword (int chr, SCM port, scm_t_read_opts *opts)
  906. {
  907. SCM symbol;
  908. /* Read the symbol that comprises the keyword. Doing this instead of
  909. invoking a specific symbol reader function allows `scm_read_keyword ()'
  910. to adapt to the delimiters currently valid of symbols.
  911. XXX: This implementation allows sloppy syntaxes like `#: key'. */
  912. symbol = scm_read_expression (port, opts);
  913. if (!scm_is_symbol (symbol))
  914. scm_i_input_error ("scm_read_keyword", port,
  915. "keyword prefix `~a' not followed by a symbol: ~s",
  916. scm_list_2 (SCM_MAKE_CHAR (chr), symbol));
  917. return (scm_symbol_to_keyword (symbol));
  918. }
  919. static SCM
  920. scm_read_vector (int chr, SCM port, scm_t_read_opts *opts,
  921. long line, int column)
  922. {
  923. /* Note: We call `scm_read_sexp ()' rather than READER here in order to
  924. guarantee that it's going to do what we want. After all, this is an
  925. implementation detail of `scm_read_vector ()', not a desirable
  926. property. */
  927. return maybe_annotate_source (scm_vector (scm_read_sexp (chr, port, opts)),
  928. port, opts, line, column);
  929. }
  930. /* Helper used by scm_read_array */
  931. static int
  932. read_decimal_integer (SCM port, int c, ssize_t *resp)
  933. {
  934. ssize_t sign = 1;
  935. ssize_t res = 0;
  936. int got_it = 0;
  937. if (c == '-')
  938. {
  939. sign = -1;
  940. c = scm_getc_unlocked (port);
  941. }
  942. while ('0' <= c && c <= '9')
  943. {
  944. res = 10*res + c-'0';
  945. got_it = 1;
  946. c = scm_getc_unlocked (port);
  947. }
  948. if (got_it)
  949. *resp = sign * res;
  950. return c;
  951. }
  952. /* Read an array. This function can also read vectors and uniform
  953. vectors. Also, the conflict between '#f' and '#f32' and '#f64' is
  954. handled here.
  955. C is the first character read after the '#'. */
  956. static SCM
  957. scm_read_array (int c, SCM port, scm_t_read_opts *opts, long line, int column)
  958. {
  959. ssize_t rank;
  960. scm_t_wchar tag_buf[8];
  961. int tag_len;
  962. SCM tag, shape = SCM_BOOL_F, elements, array;
  963. /* XXX - shortcut for ordinary vectors. Shouldn't be necessary but
  964. the array code can not deal with zero-length dimensions yet, and
  965. we want to allow zero-length vectors, of course. */
  966. if (c == '(')
  967. return scm_read_vector (c, port, opts, line, column);
  968. /* Disambiguate between '#f' and uniform floating point vectors. */
  969. if (c == 'f')
  970. {
  971. c = scm_getc_unlocked (port);
  972. if (c != '3' && c != '6')
  973. {
  974. if (c != EOF)
  975. scm_ungetc_unlocked (c, port);
  976. return SCM_BOOL_F;
  977. }
  978. rank = 1;
  979. tag_buf[0] = 'f';
  980. tag_len = 1;
  981. goto continue_reading_tag;
  982. }
  983. /* Read rank. */
  984. rank = 1;
  985. c = read_decimal_integer (port, c, &rank);
  986. if (rank < 0)
  987. scm_i_input_error (NULL, port, "array rank must be non-negative",
  988. SCM_EOL);
  989. /* Read tag. */
  990. tag_len = 0;
  991. continue_reading_tag:
  992. while (c != EOF && c != '(' && c != '@' && c != ':'
  993. && tag_len < sizeof tag_buf / sizeof tag_buf[0])
  994. {
  995. tag_buf[tag_len++] = c;
  996. c = scm_getc_unlocked (port);
  997. }
  998. if (tag_len == 0)
  999. tag = SCM_BOOL_T;
  1000. else
  1001. {
  1002. tag = scm_string_to_symbol (scm_from_utf32_stringn (tag_buf, tag_len));
  1003. if (tag_len == sizeof tag_buf / sizeof tag_buf[0])
  1004. scm_i_input_error (NULL, port, "invalid array tag, starting with: ~a",
  1005. scm_list_1 (tag));
  1006. }
  1007. /* Read shape. */
  1008. if (c == '@' || c == ':')
  1009. {
  1010. shape = SCM_EOL;
  1011. do
  1012. {
  1013. ssize_t lbnd = 0, len = 0;
  1014. SCM s;
  1015. if (c == '@')
  1016. {
  1017. c = scm_getc_unlocked (port);
  1018. c = read_decimal_integer (port, c, &lbnd);
  1019. }
  1020. s = scm_from_ssize_t (lbnd);
  1021. if (c == ':')
  1022. {
  1023. c = scm_getc_unlocked (port);
  1024. c = read_decimal_integer (port, c, &len);
  1025. if (len < 0)
  1026. scm_i_input_error (NULL, port,
  1027. "array length must be non-negative",
  1028. SCM_EOL);
  1029. s = scm_list_2 (s, scm_from_ssize_t (lbnd+len-1));
  1030. }
  1031. shape = scm_cons (s, shape);
  1032. } while (c == '@' || c == ':');
  1033. shape = scm_reverse_x (shape, SCM_EOL);
  1034. }
  1035. /* Read nested lists of elements. */
  1036. if (c != '(')
  1037. scm_i_input_error (NULL, port,
  1038. "missing '(' in vector or array literal",
  1039. SCM_EOL);
  1040. elements = scm_read_sexp (c, port, opts);
  1041. if (scm_is_false (shape))
  1042. shape = scm_from_ssize_t (rank);
  1043. else if (scm_ilength (shape) != rank)
  1044. scm_i_input_error
  1045. (NULL, port,
  1046. "the number of shape specifications must match the array rank",
  1047. SCM_EOL);
  1048. /* Handle special print syntax of rank zero arrays; see
  1049. scm_i_print_array for a rationale. */
  1050. if (rank == 0)
  1051. {
  1052. if (!scm_is_pair (elements))
  1053. scm_i_input_error (NULL, port,
  1054. "too few elements in array literal, need 1",
  1055. SCM_EOL);
  1056. if (!scm_is_null (SCM_CDR (elements)))
  1057. scm_i_input_error (NULL, port,
  1058. "too many elements in array literal, want 1",
  1059. SCM_EOL);
  1060. elements = SCM_CAR (elements);
  1061. }
  1062. /* Construct array, annotate with source location, and return. */
  1063. array = scm_list_to_typed_array (tag, shape, elements);
  1064. return maybe_annotate_source (array, port, opts, line, column);
  1065. }
  1066. static SCM
  1067. scm_read_srfi4_vector (int chr, SCM port, scm_t_read_opts *opts,
  1068. long line, int column)
  1069. {
  1070. return scm_read_array (chr, port, opts, line, column);
  1071. }
  1072. static SCM
  1073. scm_read_bytevector (scm_t_wchar chr, SCM port, scm_t_read_opts *opts,
  1074. long line, int column)
  1075. {
  1076. chr = scm_getc_unlocked (port);
  1077. if (chr != 'u')
  1078. goto syntax;
  1079. chr = scm_getc_unlocked (port);
  1080. if (chr != '8')
  1081. goto syntax;
  1082. chr = scm_getc_unlocked (port);
  1083. if (chr != '(')
  1084. goto syntax;
  1085. return maybe_annotate_source
  1086. (scm_u8_list_to_bytevector (scm_read_sexp (chr, port, opts)),
  1087. port, opts, line, column);
  1088. syntax:
  1089. scm_i_input_error ("read_bytevector", port,
  1090. "invalid bytevector prefix",
  1091. SCM_MAKE_CHAR (chr));
  1092. return SCM_UNSPECIFIED;
  1093. }
  1094. static SCM
  1095. scm_read_guile_bit_vector (scm_t_wchar chr, SCM port, scm_t_read_opts *opts,
  1096. long line, int column)
  1097. {
  1098. /* Read the `#*10101'-style read syntax for bit vectors in Guile. This is
  1099. terribly inefficient but who cares? */
  1100. SCM s_bits = SCM_EOL;
  1101. for (chr = scm_getc_unlocked (port);
  1102. (chr != EOF) && ((chr == '0') || (chr == '1'));
  1103. chr = scm_getc_unlocked (port))
  1104. {
  1105. s_bits = scm_cons ((chr == '0') ? SCM_BOOL_F : SCM_BOOL_T, s_bits);
  1106. }
  1107. if (chr != EOF)
  1108. scm_ungetc_unlocked (chr, port);
  1109. return maybe_annotate_source
  1110. (scm_bitvector (scm_reverse_x (s_bits, SCM_EOL)),
  1111. port, opts, line, column);
  1112. }
  1113. static SCM
  1114. scm_read_scsh_block_comment (scm_t_wchar chr, SCM port)
  1115. {
  1116. int bang_seen = 0;
  1117. for (;;)
  1118. {
  1119. int c = scm_getc_unlocked (port);
  1120. if (c == EOF)
  1121. scm_i_input_error ("skip_block_comment", port,
  1122. "unterminated `#! ... !#' comment", SCM_EOL);
  1123. if (c == '!')
  1124. bang_seen = 1;
  1125. else if (c == '#' && bang_seen)
  1126. break;
  1127. else
  1128. bang_seen = 0;
  1129. }
  1130. return SCM_UNSPECIFIED;
  1131. }
  1132. static void set_port_case_insensitive_p (SCM port, scm_t_read_opts *opts,
  1133. int value);
  1134. static void set_port_square_brackets_p (SCM port, scm_t_read_opts *opts,
  1135. int value);
  1136. static void set_port_curly_infix_p (SCM port, scm_t_read_opts *opts,
  1137. int value);
  1138. static SCM
  1139. scm_read_shebang (scm_t_wchar chr, SCM port, scm_t_read_opts *opts)
  1140. {
  1141. char name[READER_DIRECTIVE_NAME_MAX_SIZE + 1];
  1142. int c;
  1143. int i = 0;
  1144. while (i <= READER_DIRECTIVE_NAME_MAX_SIZE)
  1145. {
  1146. c = scm_getc_unlocked (port);
  1147. if (c == EOF)
  1148. scm_i_input_error ("skip_block_comment", port,
  1149. "unterminated `#! ... !#' comment", SCM_EOL);
  1150. else if (('a' <= c && c <= 'z') || ('0' <= c && c <= '9') || c == '-')
  1151. name[i++] = c;
  1152. else if (CHAR_IS_DELIMITER (c))
  1153. {
  1154. scm_ungetc_unlocked (c, port);
  1155. name[i] = '\0';
  1156. if (0 == strcmp ("r6rs", name))
  1157. ; /* Silently ignore */
  1158. else if (0 == strcmp ("fold-case", name))
  1159. set_port_case_insensitive_p (port, opts, 1);
  1160. else if (0 == strcmp ("no-fold-case", name))
  1161. set_port_case_insensitive_p (port, opts, 0);
  1162. else if (0 == strcmp ("curly-infix", name))
  1163. set_port_curly_infix_p (port, opts, 1);
  1164. else if (0 == strcmp ("curly-infix-and-bracket-lists", name))
  1165. {
  1166. set_port_curly_infix_p (port, opts, 1);
  1167. set_port_square_brackets_p (port, opts, 0);
  1168. }
  1169. else
  1170. break;
  1171. return SCM_UNSPECIFIED;
  1172. }
  1173. else
  1174. {
  1175. scm_ungetc_unlocked (c, port);
  1176. break;
  1177. }
  1178. }
  1179. while (i > 0)
  1180. scm_ungetc_unlocked (name[--i], port);
  1181. return scm_read_scsh_block_comment (chr, port);
  1182. }
  1183. static SCM
  1184. scm_read_r6rs_block_comment (scm_t_wchar chr, SCM port)
  1185. {
  1186. /* Unlike SCSH-style block comments, SRFI-30/R6RS block comments may be
  1187. nested. So care must be taken. */
  1188. int nesting_level = 1;
  1189. int a = scm_getc_unlocked (port);
  1190. if (a == EOF)
  1191. scm_i_input_error ("scm_read_r6rs_block_comment", port,
  1192. "unterminated `#| ... |#' comment", SCM_EOL);
  1193. while (nesting_level > 0)
  1194. {
  1195. int b = scm_getc_unlocked (port);
  1196. if (b == EOF)
  1197. scm_i_input_error ("scm_read_r6rs_block_comment", port,
  1198. "unterminated `#| ... |#' comment", SCM_EOL);
  1199. if (a == '|' && b == '#')
  1200. {
  1201. nesting_level--;
  1202. b = EOF;
  1203. }
  1204. else if (a == '#' && b == '|')
  1205. {
  1206. nesting_level++;
  1207. b = EOF;
  1208. }
  1209. a = b;
  1210. }
  1211. return SCM_UNSPECIFIED;
  1212. }
  1213. static SCM
  1214. scm_read_commented_expression (scm_t_wchar chr, SCM port,
  1215. scm_t_read_opts *opts)
  1216. {
  1217. scm_t_wchar c;
  1218. c = flush_ws (port, opts, (char *) NULL);
  1219. if (EOF == c)
  1220. scm_i_input_error ("read_commented_expression", port,
  1221. "no expression after #; comment", SCM_EOL);
  1222. scm_ungetc_unlocked (c, port);
  1223. scm_read_expression (port, opts);
  1224. return SCM_UNSPECIFIED;
  1225. }
  1226. static SCM
  1227. scm_read_extended_symbol (scm_t_wchar chr, SCM port)
  1228. {
  1229. /* Guile's extended symbol read syntax looks like this:
  1230. #{This is all a symbol name}#
  1231. So here, CHR is expected to be `{'. */
  1232. int saw_brace = 0;
  1233. size_t len = 0;
  1234. SCM buf = scm_i_make_string (1024, NULL, 0);
  1235. buf = scm_i_string_start_writing (buf);
  1236. while ((chr = scm_getc_unlocked (port)) != EOF)
  1237. {
  1238. if (saw_brace)
  1239. {
  1240. if (chr == '#')
  1241. {
  1242. break;
  1243. }
  1244. else
  1245. {
  1246. saw_brace = 0;
  1247. scm_i_string_set_x (buf, len++, '}');
  1248. }
  1249. }
  1250. if (chr == '}')
  1251. saw_brace = 1;
  1252. else if (chr == '\\')
  1253. {
  1254. /* It used to be that print.c would print extended-read-syntax
  1255. symbols with backslashes before "non-standard" chars, but
  1256. this routine wouldn't do anything with those escapes.
  1257. Bummer. What we've done is to change print.c to output
  1258. R6RS hex escapes for those characters, relying on the fact
  1259. that the extended read syntax would never put a `\' before
  1260. an `x'. For now, we just ignore other instances of
  1261. backslash in the string. */
  1262. switch ((chr = scm_getc_unlocked (port)))
  1263. {
  1264. case EOF:
  1265. goto done;
  1266. case 'x':
  1267. {
  1268. scm_t_wchar c;
  1269. SCM_READ_HEX_ESCAPE (10, ';');
  1270. scm_i_string_set_x (buf, len++, c);
  1271. break;
  1272. str_eof:
  1273. chr = EOF;
  1274. goto done;
  1275. bad_escaped:
  1276. scm_i_string_stop_writing ();
  1277. scm_i_input_error ("scm_read_extended_symbol", port,
  1278. "illegal character in escape sequence: ~S",
  1279. scm_list_1 (SCM_MAKE_CHAR (c)));
  1280. break;
  1281. }
  1282. default:
  1283. scm_i_string_set_x (buf, len++, chr);
  1284. break;
  1285. }
  1286. }
  1287. else
  1288. scm_i_string_set_x (buf, len++, chr);
  1289. if (len >= scm_i_string_length (buf) - 2)
  1290. {
  1291. SCM addy;
  1292. scm_i_string_stop_writing ();
  1293. addy = scm_i_make_string (1024, NULL, 0);
  1294. buf = scm_string_append (scm_list_2 (buf, addy));
  1295. len = 0;
  1296. buf = scm_i_string_start_writing (buf);
  1297. }
  1298. }
  1299. done:
  1300. scm_i_string_stop_writing ();
  1301. if (chr == EOF)
  1302. scm_i_input_error ("scm_read_extended_symbol", port,
  1303. "end of file while reading symbol", SCM_EOL);
  1304. return (scm_string_to_symbol (scm_c_substring (buf, 0, len)));
  1305. }
  1306. /* Top-level token readers, i.e., dispatchers. */
  1307. static SCM
  1308. scm_read_sharp_extension (int chr, SCM port, scm_t_read_opts *opts)
  1309. {
  1310. SCM proc;
  1311. proc = scm_get_hash_procedure (chr);
  1312. if (scm_is_true (scm_procedure_p (proc)))
  1313. {
  1314. long line = SCM_LINUM (port);
  1315. int column = SCM_COL (port) - 2;
  1316. SCM got;
  1317. got = scm_call_2 (proc, SCM_MAKE_CHAR (chr), port);
  1318. if (opts->record_positions_p && SCM_NIMP (got)
  1319. && !scm_i_has_source_properties (got))
  1320. scm_i_set_source_properties_x (got, line, column, SCM_FILENAME (port));
  1321. return got;
  1322. }
  1323. return SCM_UNSPECIFIED;
  1324. }
  1325. /* The reader for the sharp `#' character. It basically dispatches reads
  1326. among the above token readers. */
  1327. static SCM
  1328. scm_read_sharp (scm_t_wchar chr, SCM port, scm_t_read_opts *opts,
  1329. long line, int column)
  1330. #define FUNC_NAME "scm_lreadr"
  1331. {
  1332. SCM result;
  1333. chr = scm_getc_unlocked (port);
  1334. result = scm_read_sharp_extension (chr, port, opts);
  1335. if (!scm_is_eq (result, SCM_UNSPECIFIED))
  1336. return result;
  1337. switch (chr)
  1338. {
  1339. case '\\':
  1340. return (scm_read_character (chr, port, opts));
  1341. case '(':
  1342. return (scm_read_vector (chr, port, opts, line, column));
  1343. case 's':
  1344. case 'u':
  1345. case 'f':
  1346. case 'c':
  1347. /* This one may return either a boolean or an SRFI-4 vector. */
  1348. return (scm_read_srfi4_vector (chr, port, opts, line, column));
  1349. case 'v':
  1350. return (scm_read_bytevector (chr, port, opts, line, column));
  1351. case '*':
  1352. return (scm_read_guile_bit_vector (chr, port, opts, line, column));
  1353. case 't':
  1354. case 'T':
  1355. case 'F':
  1356. return (scm_read_boolean (chr, port));
  1357. case ':':
  1358. return (scm_read_keyword (chr, port, opts));
  1359. case '0': case '1': case '2': case '3': case '4':
  1360. case '5': case '6': case '7': case '8': case '9':
  1361. case '@':
  1362. return (scm_read_array (chr, port, opts, line, column));
  1363. case 'i':
  1364. case 'e':
  1365. case 'b':
  1366. case 'B':
  1367. case 'o':
  1368. case 'O':
  1369. case 'd':
  1370. case 'D':
  1371. case 'x':
  1372. case 'X':
  1373. case 'I':
  1374. case 'E':
  1375. return (scm_read_number_and_radix (chr, port, opts));
  1376. case '{':
  1377. return (scm_read_extended_symbol (chr, port));
  1378. case '!':
  1379. return (scm_read_shebang (chr, port, opts));
  1380. case ';':
  1381. return (scm_read_commented_expression (chr, port, opts));
  1382. case '`':
  1383. case '\'':
  1384. case ',':
  1385. return (scm_read_syntax (chr, port, opts));
  1386. case 'n':
  1387. return (scm_read_nil (chr, port, opts));
  1388. default:
  1389. result = scm_read_sharp_extension (chr, port, opts);
  1390. if (scm_is_eq (result, SCM_UNSPECIFIED))
  1391. {
  1392. /* To remain compatible with 1.8 and earlier, the following
  1393. characters have lower precedence than `read-hash-extend'
  1394. characters. */
  1395. switch (chr)
  1396. {
  1397. case '|':
  1398. return scm_read_r6rs_block_comment (chr, port);
  1399. default:
  1400. scm_i_input_error (FUNC_NAME, port, "Unknown # object: ~S",
  1401. scm_list_1 (SCM_MAKE_CHAR (chr)));
  1402. }
  1403. }
  1404. else
  1405. return result;
  1406. }
  1407. return SCM_UNSPECIFIED;
  1408. }
  1409. #undef FUNC_NAME
  1410. static SCM
  1411. read_inner_expression (SCM port, scm_t_read_opts *opts)
  1412. #define FUNC_NAME "read_inner_expression"
  1413. {
  1414. while (1)
  1415. {
  1416. scm_t_wchar chr;
  1417. chr = scm_getc_unlocked (port);
  1418. switch (chr)
  1419. {
  1420. case SCM_WHITE_SPACES:
  1421. case SCM_LINE_INCREMENTORS:
  1422. break;
  1423. case ';':
  1424. (void) scm_read_semicolon_comment (chr, port);
  1425. break;
  1426. case '{':
  1427. if (opts->curly_infix_p)
  1428. {
  1429. if (opts->neoteric_p)
  1430. return scm_read_sexp (chr, port, opts);
  1431. else
  1432. {
  1433. SCM expr;
  1434. /* Enable neoteric expressions within curly braces */
  1435. opts->neoteric_p = 1;
  1436. expr = scm_read_sexp (chr, port, opts);
  1437. opts->neoteric_p = 0;
  1438. return expr;
  1439. }
  1440. }
  1441. else
  1442. return scm_read_mixed_case_symbol (chr, port, opts);
  1443. case '[':
  1444. if (opts->square_brackets_p)
  1445. return scm_read_sexp (chr, port, opts);
  1446. else if (opts->curly_infix_p)
  1447. {
  1448. /* The syntax of neoteric expressions requires that '[' be
  1449. a delimiter when curly-infix is enabled, so it cannot
  1450. be part of an unescaped symbol. We might as well do
  1451. something useful with it, so we adopt Kawa's convention:
  1452. [...] => ($bracket-list$ ...) */
  1453. long line = SCM_LINUM (port);
  1454. int column = SCM_COL (port) - 1;
  1455. return maybe_annotate_source
  1456. (scm_cons (sym_bracket_list, scm_read_sexp (chr, port, opts)),
  1457. port, opts, line, column);
  1458. }
  1459. else
  1460. return scm_read_mixed_case_symbol (chr, port, opts);
  1461. case '(':
  1462. return (scm_read_sexp (chr, port, opts));
  1463. case '"':
  1464. return (scm_read_string (chr, port, opts));
  1465. case '\'':
  1466. case '`':
  1467. case ',':
  1468. return (scm_read_quote (chr, port, opts));
  1469. case '#':
  1470. {
  1471. long line = SCM_LINUM (port);
  1472. int column = SCM_COL (port) - 1;
  1473. SCM result = scm_read_sharp (chr, port, opts, line, column);
  1474. if (scm_is_eq (result, SCM_UNSPECIFIED))
  1475. /* We read a comment or some such. */
  1476. break;
  1477. else
  1478. return result;
  1479. }
  1480. case ')':
  1481. scm_i_input_error (FUNC_NAME, port, "unexpected \")\"", SCM_EOL);
  1482. break;
  1483. case '}':
  1484. if (opts->curly_infix_p)
  1485. scm_i_input_error (FUNC_NAME, port, "unexpected \"}\"", SCM_EOL);
  1486. else
  1487. return scm_read_mixed_case_symbol (chr, port, opts);
  1488. case ']':
  1489. if (opts->square_brackets_p)
  1490. scm_i_input_error (FUNC_NAME, port, "unexpected \"]\"", SCM_EOL);
  1491. /* otherwise fall through */
  1492. case EOF:
  1493. return SCM_EOF_VAL;
  1494. case ':':
  1495. if (opts->keyword_style == KEYWORD_STYLE_PREFIX)
  1496. return scm_symbol_to_keyword (scm_read_expression (port, opts));
  1497. /* Fall through. */
  1498. default:
  1499. {
  1500. if (((chr >= '0') && (chr <= '9'))
  1501. || (strchr ("+-.", chr)))
  1502. return (scm_read_number (chr, port, opts));
  1503. else
  1504. return (scm_read_mixed_case_symbol (chr, port, opts));
  1505. }
  1506. }
  1507. }
  1508. }
  1509. #undef FUNC_NAME
  1510. static SCM
  1511. scm_read_expression (SCM port, scm_t_read_opts *opts)
  1512. #define FUNC_NAME "scm_read_expression"
  1513. {
  1514. if (!opts->neoteric_p)
  1515. return read_inner_expression (port, opts);
  1516. else
  1517. {
  1518. long line = 0;
  1519. int column = 0;
  1520. SCM expr;
  1521. if (opts->record_positions_p)
  1522. {
  1523. /* We need to get the position of the first non-whitespace
  1524. character in order to correctly annotate neoteric
  1525. expressions. For example, for the expression 'f(x)', the
  1526. first call to 'read_inner_expression' reads the 'f' (which
  1527. cannot be annotated), and then we later read the '(x)' and
  1528. use it to construct the new list (f x). */
  1529. int c = flush_ws (port, opts, (char *) NULL);
  1530. if (c == EOF)
  1531. return SCM_EOF_VAL;
  1532. scm_ungetc_unlocked (c, port);
  1533. line = SCM_LINUM (port);
  1534. column = SCM_COL (port);
  1535. }
  1536. expr = read_inner_expression (port, opts);
  1537. /* 'expr' is the first component of the neoteric expression. Now
  1538. we loop, and as long as the next character is '(', '[', or '{',
  1539. (without any intervening whitespace), we use it to construct a
  1540. new expression. For example, f{n - 1}(x) => ((f (- n 1)) x). */
  1541. for (;;)
  1542. {
  1543. int chr = scm_getc_unlocked (port);
  1544. if (chr == '(')
  1545. /* e(...) => (e ...) */
  1546. expr = scm_cons (expr, scm_read_sexp (chr, port, opts));
  1547. else if (chr == '[')
  1548. /* e[...] => ($bracket-apply$ e ...) */
  1549. expr = scm_cons (sym_bracket_apply,
  1550. scm_cons (expr,
  1551. scm_read_sexp (chr, port, opts)));
  1552. else if (chr == '{')
  1553. {
  1554. SCM arg = scm_read_sexp (chr, port, opts);
  1555. if (scm_is_null (arg))
  1556. expr = scm_list_1 (expr); /* e{} => (e) */
  1557. else
  1558. expr = scm_list_2 (expr, arg); /* e{...} => (e {...}) */
  1559. }
  1560. else
  1561. {
  1562. if (chr != EOF)
  1563. scm_ungetc_unlocked (chr, port);
  1564. break;
  1565. }
  1566. maybe_annotate_source (expr, port, opts, line, column);
  1567. }
  1568. return expr;
  1569. }
  1570. }
  1571. #undef FUNC_NAME
  1572. /* Actual reader. */
  1573. static void init_read_options (SCM port, scm_t_read_opts *opts);
  1574. SCM_DEFINE (scm_read, "read", 0, 1, 0,
  1575. (SCM port),
  1576. "Read an s-expression from the input port @var{port}, or from\n"
  1577. "the current input port if @var{port} is not specified.\n"
  1578. "Any whitespace before the next token is discarded.")
  1579. #define FUNC_NAME s_scm_read
  1580. {
  1581. scm_t_read_opts opts;
  1582. int c;
  1583. if (SCM_UNBNDP (port))
  1584. port = scm_current_input_port ();
  1585. SCM_VALIDATE_OPINPORT (1, port);
  1586. init_read_options (port, &opts);
  1587. c = flush_ws (port, &opts, (char *) NULL);
  1588. if (EOF == c)
  1589. return SCM_EOF_VAL;
  1590. scm_ungetc_unlocked (c, port);
  1591. return (scm_read_expression (port, &opts));
  1592. }
  1593. #undef FUNC_NAME
  1594. /* Manipulate the read-hash-procedures alist. This could be written in
  1595. Scheme, but maybe it will also be used by C code during initialisation. */
  1596. SCM_DEFINE (scm_read_hash_extend, "read-hash-extend", 2, 0, 0,
  1597. (SCM chr, SCM proc),
  1598. "Install the procedure @var{proc} for reading expressions\n"
  1599. "starting with the character sequence @code{#} and @var{chr}.\n"
  1600. "@var{proc} will be called with two arguments: the character\n"
  1601. "@var{chr} and the port to read further data from. The object\n"
  1602. "returned will be the return value of @code{read}. \n"
  1603. "Passing @code{#f} for @var{proc} will remove a previous setting. \n"
  1604. )
  1605. #define FUNC_NAME s_scm_read_hash_extend
  1606. {
  1607. SCM this;
  1608. SCM prev;
  1609. SCM_VALIDATE_CHAR (1, chr);
  1610. SCM_ASSERT (scm_is_false (proc)
  1611. || scm_is_eq (scm_procedure_p (proc), SCM_BOOL_T),
  1612. proc, SCM_ARG2, FUNC_NAME);
  1613. /* Check if chr is already in the alist. */
  1614. this = scm_i_read_hash_procedures_ref ();
  1615. prev = SCM_BOOL_F;
  1616. while (1)
  1617. {
  1618. if (scm_is_null (this))
  1619. {
  1620. /* not found, so add it to the beginning. */
  1621. if (scm_is_true (proc))
  1622. {
  1623. SCM new = scm_cons (scm_cons (chr, proc),
  1624. scm_i_read_hash_procedures_ref ());
  1625. scm_i_read_hash_procedures_set_x (new);
  1626. }
  1627. break;
  1628. }
  1629. if (scm_is_eq (chr, SCM_CAAR (this)))
  1630. {
  1631. /* already in the alist. */
  1632. if (scm_is_false (proc))
  1633. {
  1634. /* remove it. */
  1635. if (scm_is_false (prev))
  1636. {
  1637. SCM rest = SCM_CDR (scm_i_read_hash_procedures_ref ());
  1638. scm_i_read_hash_procedures_set_x (rest);
  1639. }
  1640. else
  1641. scm_set_cdr_x (prev, SCM_CDR (this));
  1642. }
  1643. else
  1644. {
  1645. /* replace it. */
  1646. scm_set_cdr_x (SCM_CAR (this), proc);
  1647. }
  1648. break;
  1649. }
  1650. prev = this;
  1651. this = SCM_CDR (this);
  1652. }
  1653. return SCM_UNSPECIFIED;
  1654. }
  1655. #undef FUNC_NAME
  1656. /* Recover the read-hash procedure corresponding to char c. */
  1657. static SCM
  1658. scm_get_hash_procedure (int c)
  1659. {
  1660. SCM rest = scm_i_read_hash_procedures_ref ();
  1661. while (1)
  1662. {
  1663. if (scm_is_null (rest))
  1664. return SCM_BOOL_F;
  1665. if (SCM_CHAR (SCM_CAAR (rest)) == c)
  1666. return SCM_CDAR (rest);
  1667. rest = SCM_CDR (rest);
  1668. }
  1669. }
  1670. #define SCM_ENCODING_SEARCH_SIZE (500)
  1671. static int
  1672. is_encoding_char (char c)
  1673. {
  1674. if (c >= 'a' && c <= 'z') return 1;
  1675. if (c >= 'A' && c <= 'Z') return 1;
  1676. if (c >= '0' && c <= '9') return 1;
  1677. return strchr ("_-.:/,+=()", c) != NULL;
  1678. }
  1679. /* Search the first few hundred characters of a file for an Emacs-like coding
  1680. declaration. Returns either NULL or a string whose storage has been
  1681. allocated with `scm_gc_malloc ()'. */
  1682. char *
  1683. scm_i_scan_for_encoding (SCM port)
  1684. {
  1685. scm_t_port *pt;
  1686. char header[SCM_ENCODING_SEARCH_SIZE+1];
  1687. size_t bytes_read, encoding_length, i;
  1688. char *encoding = NULL;
  1689. char *pos, *encoding_start;
  1690. int in_comment;
  1691. pt = SCM_PTAB_ENTRY (port);
  1692. if (pt->rw_active == SCM_PORT_WRITE)
  1693. scm_flush_unlocked (port);
  1694. if (pt->rw_random)
  1695. pt->rw_active = SCM_PORT_READ;
  1696. if (pt->read_pos == pt->read_end)
  1697. {
  1698. /* We can use the read buffer, and thus avoid a seek. */
  1699. if (scm_fill_input_unlocked (port) == EOF)
  1700. return NULL;
  1701. bytes_read = pt->read_end - pt->read_pos;
  1702. if (bytes_read > SCM_ENCODING_SEARCH_SIZE)
  1703. bytes_read = SCM_ENCODING_SEARCH_SIZE;
  1704. if (bytes_read <= 1)
  1705. /* An unbuffered port -- don't scan. */
  1706. return NULL;
  1707. memcpy (header, pt->read_pos, bytes_read);
  1708. header[bytes_read] = '\0';
  1709. }
  1710. else
  1711. {
  1712. /* Try to read some bytes and then seek back. Not all ports
  1713. support seeking back; and indeed some file ports (like
  1714. /dev/urandom) will succeed on an lseek (fd, 0, SEEK_CUR)---the
  1715. check performed by SCM_FPORT_FDES---but fail to seek
  1716. backwards. Hence this block comes second. We prefer to use
  1717. the read buffer in-place. */
  1718. if (SCM_FPORTP (port) && !SCM_FDES_RANDOM_P (SCM_FPORT_FDES (port)))
  1719. return NULL;
  1720. bytes_read = scm_c_read_unlocked (port, header, SCM_ENCODING_SEARCH_SIZE);
  1721. header[bytes_read] = '\0';
  1722. scm_seek (port, scm_from_int (0), scm_from_int (SEEK_SET));
  1723. }
  1724. /* search past "coding[:=]" */
  1725. pos = header;
  1726. while (1)
  1727. {
  1728. if ((pos = strstr(pos, "coding")) == NULL)
  1729. return NULL;
  1730. pos += strlen("coding");
  1731. if (pos - header >= SCM_ENCODING_SEARCH_SIZE ||
  1732. (*pos == ':' || *pos == '='))
  1733. {
  1734. pos ++;
  1735. break;
  1736. }
  1737. }
  1738. /* skip spaces */
  1739. while (pos - header <= SCM_ENCODING_SEARCH_SIZE &&
  1740. (*pos == ' ' || *pos == '\t'))
  1741. pos ++;
  1742. /* grab the next token */
  1743. encoding_start = pos;
  1744. i = 0;
  1745. while (encoding_start + i - header <= SCM_ENCODING_SEARCH_SIZE
  1746. && encoding_start + i - header < bytes_read
  1747. && is_encoding_char (encoding_start[i]))
  1748. i++;
  1749. encoding_length = i;
  1750. if (encoding_length == 0)
  1751. return NULL;
  1752. encoding = scm_gc_strndup (encoding_start, encoding_length, "encoding");
  1753. /* push backwards to make sure we were in a comment */
  1754. in_comment = 0;
  1755. pos = encoding_start;
  1756. while (pos >= header)
  1757. {
  1758. if (*pos == ';')
  1759. {
  1760. in_comment = 1;
  1761. break;
  1762. }
  1763. else if (*pos == '\n' || pos == header)
  1764. {
  1765. /* This wasn't in a semicolon comment. Check for a
  1766. hash-bang comment. */
  1767. char *beg = strstr (header, "#!");
  1768. char *end = strstr (header, "!#");
  1769. if (beg < encoding_start && encoding_start + encoding_length <= end)
  1770. in_comment = 1;
  1771. break;
  1772. }
  1773. else
  1774. {
  1775. pos --;
  1776. continue;
  1777. }
  1778. }
  1779. if (!in_comment)
  1780. /* This wasn't in a comment */
  1781. return NULL;
  1782. return encoding;
  1783. }
  1784. SCM_DEFINE (scm_file_encoding, "file-encoding", 1, 0, 0,
  1785. (SCM port),
  1786. "Scans the port for an Emacs-like character coding declaration\n"
  1787. "near the top of the contents of a port with random-accessible contents.\n"
  1788. "The coding declaration is of the form\n"
  1789. "@code{coding: XXXXX} and must appear in a scheme comment.\n"
  1790. "\n"
  1791. "Returns a string containing the character encoding of the file\n"
  1792. "if a declaration was found, or @code{#f} otherwise.\n")
  1793. #define FUNC_NAME s_scm_file_encoding
  1794. {
  1795. char *enc;
  1796. SCM s_enc;
  1797. SCM_VALIDATE_OPINPORT (SCM_ARG1, port);
  1798. enc = scm_i_scan_for_encoding (port);
  1799. if (enc == NULL)
  1800. return SCM_BOOL_F;
  1801. else
  1802. {
  1803. s_enc = scm_string_upcase (scm_from_locale_string (enc));
  1804. return s_enc;
  1805. }
  1806. return SCM_BOOL_F;
  1807. }
  1808. #undef FUNC_NAME
  1809. /* Per-port read options.
  1810. We store per-port read options in the 'port-read-options' key of the
  1811. port's alist, which is stored in the internal port structure. The
  1812. value stored in the alist is a single integer that contains a two-bit
  1813. field for each read option.
  1814. If a bit field contains READ_OPTION_INHERIT (3), that indicates that
  1815. the applicable value should be inherited from the corresponding
  1816. global read option. Otherwise, the bit field contains the value of
  1817. the read option. For boolean read options that have been set
  1818. per-port, the possible values are 0 or 1. If the 'keyword_style'
  1819. read option has been set per-port, its possible values are those in
  1820. 'enum t_keyword_style'. */
  1821. /* Key to read options in per-port alists. */
  1822. SCM_SYMBOL (sym_port_read_options, "port-read-options");
  1823. /* Offsets of bit fields for each per-port override */
  1824. #define READ_OPTION_COPY_SOURCE_P 0
  1825. #define READ_OPTION_RECORD_POSITIONS_P 2
  1826. #define READ_OPTION_CASE_INSENSITIVE_P 4
  1827. #define READ_OPTION_KEYWORD_STYLE 6
  1828. #define READ_OPTION_R6RS_ESCAPES_P 8
  1829. #define READ_OPTION_SQUARE_BRACKETS_P 10
  1830. #define READ_OPTION_HUNGRY_EOL_ESCAPES_P 12
  1831. #define READ_OPTION_CURLY_INFIX_P 14
  1832. /* The total width in bits of the per-port overrides */
  1833. #define READ_OPTIONS_NUM_BITS 16
  1834. #define READ_OPTIONS_INHERIT_ALL ((1UL << READ_OPTIONS_NUM_BITS) - 1)
  1835. #define READ_OPTIONS_MAX_VALUE READ_OPTIONS_INHERIT_ALL
  1836. #define READ_OPTION_MASK 3
  1837. #define READ_OPTION_INHERIT 3
  1838. static void
  1839. set_port_read_option (SCM port, int option, int new_value)
  1840. {
  1841. SCM alist, scm_read_options;
  1842. unsigned int read_options;
  1843. new_value &= READ_OPTION_MASK;
  1844. alist = scm_i_port_alist (port);
  1845. scm_read_options = scm_assq_ref (alist, sym_port_read_options);
  1846. if (scm_is_unsigned_integer (scm_read_options, 0, READ_OPTIONS_MAX_VALUE))
  1847. read_options = scm_to_uint (scm_read_options);
  1848. else
  1849. read_options = READ_OPTIONS_INHERIT_ALL;
  1850. read_options &= ~(READ_OPTION_MASK << option);
  1851. read_options |= new_value << option;
  1852. scm_read_options = scm_from_uint (read_options);
  1853. alist = scm_assq_set_x (alist, sym_port_read_options, scm_read_options);
  1854. scm_i_set_port_alist_x (port, alist);
  1855. }
  1856. /* Set OPTS and PORT's case-insensitivity according to VALUE. */
  1857. static void
  1858. set_port_case_insensitive_p (SCM port, scm_t_read_opts *opts, int value)
  1859. {
  1860. value = !!value;
  1861. opts->case_insensitive_p = value;
  1862. set_port_read_option (port, READ_OPTION_CASE_INSENSITIVE_P, value);
  1863. }
  1864. /* Set OPTS and PORT's square_brackets_p option according to VALUE. */
  1865. static void
  1866. set_port_square_brackets_p (SCM port, scm_t_read_opts *opts, int value)
  1867. {
  1868. value = !!value;
  1869. opts->square_brackets_p = value;
  1870. set_port_read_option (port, READ_OPTION_SQUARE_BRACKETS_P, value);
  1871. }
  1872. /* Set OPTS and PORT's curly_infix_p option according to VALUE. */
  1873. static void
  1874. set_port_curly_infix_p (SCM port, scm_t_read_opts *opts, int value)
  1875. {
  1876. value = !!value;
  1877. opts->curly_infix_p = value;
  1878. set_port_read_option (port, READ_OPTION_CURLY_INFIX_P, value);
  1879. }
  1880. /* Initialize OPTS based on PORT's read options and the global read
  1881. options. */
  1882. static void
  1883. init_read_options (SCM port, scm_t_read_opts *opts)
  1884. {
  1885. SCM alist, val, scm_read_options;
  1886. unsigned int read_options, x;
  1887. alist = scm_i_port_alist (port);
  1888. scm_read_options = scm_assq_ref (alist, sym_port_read_options);
  1889. if (scm_is_unsigned_integer (scm_read_options, 0, READ_OPTIONS_MAX_VALUE))
  1890. read_options = scm_to_uint (scm_read_options);
  1891. else
  1892. read_options = READ_OPTIONS_INHERIT_ALL;
  1893. x = READ_OPTION_MASK & (read_options >> READ_OPTION_KEYWORD_STYLE);
  1894. if (x == READ_OPTION_INHERIT)
  1895. {
  1896. val = SCM_PACK (SCM_KEYWORD_STYLE);
  1897. if (scm_is_eq (val, scm_keyword_prefix))
  1898. x = KEYWORD_STYLE_PREFIX;
  1899. else if (scm_is_eq (val, scm_keyword_postfix))
  1900. x = KEYWORD_STYLE_POSTFIX;
  1901. else
  1902. x = KEYWORD_STYLE_HASH_PREFIX;
  1903. }
  1904. opts->keyword_style = x;
  1905. #define RESOLVE_BOOLEAN_OPTION(NAME, name) \
  1906. do \
  1907. { \
  1908. x = READ_OPTION_MASK & (read_options >> READ_OPTION_ ## NAME); \
  1909. if (x == READ_OPTION_INHERIT) \
  1910. x = !!SCM_ ## NAME; \
  1911. opts->name = x; \
  1912. } \
  1913. while (0)
  1914. RESOLVE_BOOLEAN_OPTION (COPY_SOURCE_P, copy_source_p);
  1915. RESOLVE_BOOLEAN_OPTION (RECORD_POSITIONS_P, record_positions_p);
  1916. RESOLVE_BOOLEAN_OPTION (CASE_INSENSITIVE_P, case_insensitive_p);
  1917. RESOLVE_BOOLEAN_OPTION (R6RS_ESCAPES_P, r6rs_escapes_p);
  1918. RESOLVE_BOOLEAN_OPTION (SQUARE_BRACKETS_P, square_brackets_p);
  1919. RESOLVE_BOOLEAN_OPTION (HUNGRY_EOL_ESCAPES_P, hungry_eol_escapes_p);
  1920. RESOLVE_BOOLEAN_OPTION (CURLY_INFIX_P, curly_infix_p);
  1921. #undef RESOLVE_BOOLEAN_OPTION
  1922. opts->neoteric_p = 0;
  1923. }
  1924. void
  1925. scm_init_read ()
  1926. {
  1927. SCM read_hash_procs;
  1928. read_hash_procs = scm_make_fluid_with_default (SCM_EOL);
  1929. scm_i_read_hash_procedures =
  1930. SCM_VARIABLE_LOC (scm_c_define ("%read-hash-procedures", read_hash_procs));
  1931. scm_init_opts (scm_read_options, scm_read_opts);
  1932. #include "libguile/read.x"
  1933. }
  1934. /*
  1935. Local Variables:
  1936. c-file-style: "gnu"
  1937. End:
  1938. */