| 1 |
This is guile-reader.info, produced by makeinfo version 6.8 from |
| 2 |
guile-reader.texi. |
| 3 |
|
| 4 |
This file documents Guile-Reader. |
| 5 |
|
| 6 |
Copyright (C) 2005, 2006, 2007, 2008, 2009, 2012, 2015, 2017 Ludovic |
| 7 |
Courtès |
| 8 |
|
| 9 |
Permission is granted to make and distribute verbatim copies of this |
| 10 |
manual provided the copyright notice and this permission notice are |
| 11 |
preserved on all copies. |
| 12 |
|
| 13 |
Permission is granted to copy and distribute modified versions of |
| 14 |
this manual under the conditions for verbatim copying, provided that the |
| 15 |
entire resulting derived work is distributed under the terms of a |
| 16 |
permission notice identical to this one. |
| 17 |
|
| 18 |
Permission is granted to copy and distribute translations of this |
| 19 |
manual into another language, under the above conditions for modified |
| 20 |
versions, except that this permission notice may be stated in a |
| 21 |
translation approved by the Free Software Foundation. |
| 22 |
INFO-DIR-SECTION The Algorithmic Language Scheme |
| 23 |
START-INFO-DIR-ENTRY |
| 24 |
* Guile Reader: (guile-reader). A Reader Framework for Guile. |
| 25 |
END-INFO-DIR-ENTRY |
| 26 |
|
| 27 |
|
| 28 |
File: guile-reader.info, Node: Top, Next: Introduction, Prev: (dir), Up: (dir) |
| 29 |
|
| 30 |
A Reader Framework for Guile |
| 31 |
**************************** |
| 32 |
|
| 33 |
This document describes Guile-Reader version 0.6.3, for GNU Guile's 3.0 |
| 34 |
stable series, as well as the legacy 1.8, 2.0 and 2.2 series. It was |
| 35 |
last updated in July 2024. |
| 36 |
|
| 37 |
This documentation should be mostly complete. Details about the C |
| 38 |
API are omitted from this manual. They can be found in the public |
| 39 |
header files. |
| 40 |
|
| 41 |
* Menu: |
| 42 |
|
| 43 |
* Introduction:: The goals of Guile-Reader. |
| 44 |
* Overview:: How it works. |
| 45 |
* Quick Start:: For the impatient. |
| 46 |
* API Reference:: Using Guile-Reader. |
| 47 |
* Internals:: Performance considerations. |
| 48 |
|
| 49 |
* Concept Index:: Concepts. |
| 50 |
* Function Index:: Scheme procedures and C functions. |
| 51 |
|
| 52 |
|
| 53 |
File: guile-reader.info, Node: Introduction, Next: Overview, Prev: Top, Up: Top |
| 54 |
|
| 55 |
1 Introduction |
| 56 |
************** |
| 57 |
|
| 58 |
Guile currently provides limited extensibility of its reader, by means |
| 59 |
of 'read-hash-extend' (*note 'read-hash-extend': (guile)Reader |
| 60 |
Extensions.), for instance, or 'read-enable' (*note 'read-enable': |
| 61 |
(guile)Scheme Read.). SRFI-10 |
| 62 |
(http://srfi.schemers.org/srfi-10/srfi-10.html) tries to propose a |
| 63 |
generic, portable, extension mechanism similar to 'read-hash-extend' but |
| 64 |
limited to '#,' sequences. Moreover, while this may not always be |
| 65 |
desirable, all these extension facilities have a global effect, changing |
| 66 |
the behavior of the sole reader implementation available at run-time. |
| 67 |
This makes it impossible to have, for instance, one module consider |
| 68 |
names starting with ':' as symbols, while another considers them as |
| 69 |
keywords. |
| 70 |
|
| 71 |
Extensions such as the read syntax for SRFI-4 numeric vectors (*note |
| 72 |
SRFI-4 vectors: (guile)Uniform Numeric Vectors.) had to be added to |
| 73 |
Guile's built-in C reader. Syntactic extensions that did not appeal the |
| 74 |
majority of users, like Emacs-Lisp vectors, are '#ifdef''d within the |
| 75 |
reader code and are not available by default. Moreover, some extensions |
| 76 |
are incompatible with each other, such as the DSSSL keyword syntax and |
| 77 |
SCSH block comments (*note SCSH block comments: (guile)Block Comments.). |
| 78 |
In short the current reader syntax is hardly extensible. |
| 79 |
|
| 80 |
The idea of Guile-Reader is to provide a framework allowing users to |
| 81 |
quickly define readers for whatever syntax (or rather: variant of the |
| 82 |
Scheme syntax) they like. Programs can then provide their own readers |
| 83 |
and, thanks to Guile's 'current-reader' mechanism, have their code read |
| 84 |
with this reader. When using Guile 2.0, readers produced by |
| 85 |
Guile-Reader are Unicode-capable; they can read from any |
| 86 |
ASCII-compatible encoding, such as UTF-8 or ISO-8859-1. |
| 87 |
|
| 88 |
While it is much simpler than a full-blown lexer generator such as |
| 89 |
Flex (http://www.gnu.org/software/flex/), Danny Dubé's SILex |
| 90 |
(http://www.iro.umontreal.ca/~dube/) and Bigloo's RGC |
| 91 |
(http://www.inria.fr/mimosa/fp/Bigloo), its simple programming interface |
| 92 |
should make it very straightforward to implement readers, especially for |
| 93 |
Scheme-like syntaxes. Best of all, Guile Reader comes with a library of |
| 94 |
components that can typically be used to construct a reader for the |
| 95 |
Scheme syntax. And each one of this components may be reused at will |
| 96 |
when creating other readers. On the other hand, one should be aware |
| 97 |
that this simpler API comes at the cost of a lack of consistency in some |
| 98 |
cases, as outlined later in this manual (*note Limitations::). |
| 99 |
|
| 100 |
Common Lisp has a similar mechanism to extend its reader which is |
| 101 |
called the "read table". Gambit Scheme |
| 102 |
(http://www.iro.umontreal.ca/~gambit/), for instance, also provides an |
| 103 |
implementation of read tables. However, it appears to have limitations |
| 104 |
similar to Guile's 'read-enable' and 'read-hash-extend' in terms of |
| 105 |
possibilities for syntax extension. On the other hand, it allows the |
| 106 |
reader and writer to be kept consistent, which Guile-Reader does not |
| 107 |
address. |
| 108 |
|
| 109 |
|
| 110 |
File: guile-reader.info, Node: Overview, Next: Quick Start, Prev: Introduction, Up: Top |
| 111 |
|
| 112 |
2 Overview |
| 113 |
********** |
| 114 |
|
| 115 |
Guile-Reader allows for the construction of readers capable of |
| 116 |
understanding various syntactic variants. The simplest way to use it is |
| 117 |
through its "reader library" that allows one to pick and choose various |
| 118 |
commonly used syntactic extensions to the standard Scheme syntax (*note |
| 119 |
Reader Library::). However, Guile-Reader also provides a finer-grain |
| 120 |
programming interface allowing the construction of virtually any reader, |
| 121 |
with its own syntactic specificities. The following sections focus |
| 122 |
primarily on this capability. |
| 123 |
|
| 124 |
Before going into the details of the reader framework API, let us |
| 125 |
have a quick overview of what this is. Basically, Guile-Reader |
| 126 |
introduces two objects: "readers" and "token readers". Readers can be |
| 127 |
thought of, simply, as procedures like Scheme's 'read' (*note R5RS: |
| 128 |
(r5rs)Input.), i.e., procedures that take one (optional) argument, |
| 129 |
namely the port to read from. We will see later that readers as defined |
| 130 |
by Guile-Reader can actually receive two more arguments (*note Defining |
| 131 |
a New Reader::). A reader, like 'read', reads a sequence of characters |
| 132 |
(the "external representation" of some object) and returns a Scheme |
| 133 |
object. |
| 134 |
|
| 135 |
Token readers (TRs, for short) are the building block of a reader. A |
| 136 |
token reader is basically an association between a character or set of |
| 137 |
characters and a procedure to read and interpret a sequence of |
| 138 |
characters starting with one of the former. For instance, in a standard |
| 139 |
Scheme reader, the character '(' may be associated to a procedure that |
| 140 |
reads an S-expression. Likewise, lower-case and upper-case letters |
| 141 |
associated with the appropriate procedure form a token reader for |
| 142 |
symbols. |
| 143 |
|
| 144 |
In Guile-Reader, TRs may be written either in Scheme or in C, and |
| 145 |
they can even be a reader produced by Guile-Reader itself. Unless it is |
| 146 |
a reader, the procedure (or C function) used to create a TR will receive |
| 147 |
four arguments: |
| 148 |
|
| 149 |
* the character that was read and which triggered its call; in the |
| 150 |
S-exp example, this would be '('; |
| 151 |
* the port to read from; |
| 152 |
* the reader which performed this invocation; |
| 153 |
* the top-level reader which yielded this invocation. |
| 154 |
|
| 155 |
The next section shows how to get started with Guile-Reader, using a |
| 156 |
high-level API. Details about of the programming interface are given in |
| 157 |
the API reference (*note API Reference::). |
| 158 |
|
| 159 |
|
| 160 |
File: guile-reader.info, Node: Quick Start, Next: API Reference, Prev: Overview, Up: Top |
| 161 |
|
| 162 |
3 Quick Start |
| 163 |
************* |
| 164 |
|
| 165 |
The simplest way to get started and to produce customized readers is |
| 166 |
through the high-level API provided by the '(system reader library)' |
| 167 |
module. As the name suggests, this module provides a library of readily |
| 168 |
usable readers. The 'make-alternate-guile-reader' procedure gives |
| 169 |
access to these readers. It can be passed a list of symbols describing |
| 170 |
reader options, such as whether you want support for DSSSL keywords, |
| 171 |
SRFI-62 comments (http://srfi.schemers.org/srfi-62/srfi-62.html), |
| 172 |
SRFI-30 block comments (http://srfi.schemers.org/srfi-30/srfi-30.html), |
| 173 |
etc. |
| 174 |
|
| 175 |
The following example binds to MY-READER a Scheme reader that |
| 176 |
supports DSSSL-style keywords and SRFI-62 comments, and that is case |
| 177 |
insensitive: |
| 178 |
|
| 179 |
(use-modules (system reader library)) |
| 180 |
|
| 181 |
(define my-reader |
| 182 |
(make-alternate-guile-reader '(dsssl-keywords |
| 183 |
srfi62-sexp-comments |
| 184 |
case-insensitive))) |
| 185 |
|
| 186 |
This reader can then be used like the regular 'read' procedure: |
| 187 |
|
| 188 |
(procedure? my-reader) |
| 189 |
=> #t |
| 190 |
|
| 191 |
(with-input-from-string "some-symbol" my-reader) |
| 192 |
=> some-symbol |
| 193 |
|
| 194 |
(my-reader (open-input-string "MiXeD-CaSe")) |
| 195 |
=> mixed-case |
| 196 |
|
| 197 |
(my-reader (open-input-string "(an sexp with a #;srfi-62 comment)")) |
| 198 |
=> (an sexp with a comment) |
| 199 |
|
| 200 |
(my-reader (open-input-string "#!some-DSSSL-keyword")) |
| 201 |
=> #:some-dsssl-keyword |
| 202 |
|
| 203 |
Most of the time, you will want to use it as the current reader, at |
| 204 |
least in the module where you created it. Fortunately, Guile provides a |
| 205 |
mechanism for this, the 'current-reader' fluid (*note 'current-reader': |
| 206 |
(guile)Loading.). Changing the value of this fluid from a file that is |
| 207 |
being loaded will affect the reader use to load it. In general, you |
| 208 |
will want to modify 'current-reader' not only at run time, but also at |
| 209 |
compile time and when the code is evaluated, which can be achieved using |
| 210 |
'eval-when' (*note 'eval-when': (guile)Eval When.). For instance: |
| 211 |
|
| 212 |
;;; This is the beginning of my Scheme file. At this point, Guile's |
| 213 |
;;; default reader is used (or, alternatively, the one that was |
| 214 |
;;; passed as a second parameter to `load'). So, for instance, DSSSL |
| 215 |
;;; keywords are _not_ recognized. |
| 216 |
|
| 217 |
;; Let's create our own customized reader... |
| 218 |
(use-modules (system reader library)) |
| 219 |
|
| 220 |
;; 'eval-when' here is needed to make 'my-reader' accessible at |
| 221 |
;; compile time, and to have the 'current-reader' change take |
| 222 |
;; effect at compile time. |
| 223 |
(eval-when (compile load eval) |
| 224 |
(define my-reader |
| 225 |
(make-alternate-guile-reader '(dsssl-keywords |
| 226 |
srfi62-sexp-comments |
| 227 |
case-insensitive))) |
| 228 |
|
| 229 |
;; Let's make it the current reader. |
| 230 |
(fluid-set! current-reader my-reader)) |
| 231 |
|
| 232 |
;; From now on, MY-READER is used to read the rest of this file. Thus |
| 233 |
;; we can happily use the syntactic extensions it implements: DSSSL |
| 234 |
;; keywords, SRFI-62 comments and case-insensitivity. |
| 235 |
|
| 236 |
(if (not (keyword? #!dsssl-keyword)) |
| 237 |
(error "Something went wrong, this should not happen!")) |
| 238 |
|
| 239 |
The nice thing is that 'current-reader' is reset to its initial value |
| 240 |
when the dynamic extent of 'load' is left. In other words, the loader |
| 241 |
of the file above is _not_ affected by the 'fluid-set!' statement. |
| 242 |
Reader changes are _hygienic_ and modules can use their own without |
| 243 |
risking to harm each other. |
| 244 |
|
| 245 |
The full list of options supported by 'make-alternate-guile-reader' |
| 246 |
is shown in *note Reader Library::. However, this option set is quite |
| 247 |
limited and you may find yourself wanting a syntactic extension not |
| 248 |
available here. In that case, you will want to build a new reader, |
| 249 |
possibly reusing existing reader components known as "token readers", as |
| 250 |
described in *note Defining a New Reader::. |
| 251 |
|
| 252 |
|
| 253 |
File: guile-reader.info, Node: API Reference, Next: Internals, Prev: Quick Start, Up: Top |
| 254 |
|
| 255 |
4 API Reference |
| 256 |
*************** |
| 257 |
|
| 258 |
All the Scheme procedures described below are exported by the '(system |
| 259 |
reader)' module. In order to be able to use them, you will need to |
| 260 |
import this module first: |
| 261 |
|
| 262 |
(use-modules (system reader)) |
| 263 |
|
| 264 |
A C variant is also available for most of them by including the |
| 265 |
declarations available in the '<guile-reader/reader.h>' header file. |
| 266 |
|
| 267 |
* Menu: |
| 268 |
|
| 269 |
* Token Readers:: The basic components of a reader. |
| 270 |
* Readers:: How to build readers. |
| 271 |
|
| 272 |
|
| 273 |
File: guile-reader.info, Node: Token Readers, Next: Readers, Prev: API Reference, Up: API Reference |
| 274 |
|
| 275 |
4.1 Token Readers |
| 276 |
================= |
| 277 |
|
| 278 |
Basically, token readers are the association of a character or set of |
| 279 |
characters and a function that is able to interpret character sequences |
| 280 |
that start by one of these characters. We will see below how to define |
| 281 |
new token readers first, and then how to re-use existing ones. |
| 282 |
|
| 283 |
* Menu: |
| 284 |
|
| 285 |
* Defining a New Token Reader:: |
| 286 |
* TR Calling Convention:: |
| 287 |
* Invoking a Reader from a TR:: |
| 288 |
* Token Reader Library:: |
| 289 |
* Limitations:: |
| 290 |
|
| 291 |
|
| 292 |
File: guile-reader.info, Node: Defining a New Token Reader, Next: TR Calling Convention, Prev: Token Readers, Up: Token Readers |
| 293 |
|
| 294 |
4.1.1 Defining a New Token Reader |
| 295 |
--------------------------------- |
| 296 |
|
| 297 |
A new token reader object can be created by calling the |
| 298 |
'make-token-reader' procedure with a "character specification" and a |
| 299 |
procedure. A character specification defines the set of characters |
| 300 |
which should trigger an invocation of the corresponding procedure. The |
| 301 |
character specification may be either: |
| 302 |
|
| 303 |
* a single character; |
| 304 |
* a pair of characters, which is interpreted as a character range; |
| 305 |
* a list of characters, which is interpreted as a set of characters. |
| 306 |
|
| 307 |
The procedure passed to 'make-token-reader' may actually be either a |
| 308 |
C function or Scheme procedure that takes four arguments (*note TR |
| 309 |
Calling Convention::), any "object" returned by |
| 310 |
'token-reader-procedure', or a reader. This last option turns out to be |
| 311 |
quite helpful. For example, this is very convenient when implementing |
| 312 |
the various Scheme read syntaxes prefixed by the '#' character: one can |
| 313 |
create a reader for '#', and then turn it into a token reader that is |
| 314 |
part of the top-level reader. |
| 315 |
|
| 316 |
The reference for 'make-token-reader' is given below: |
| 317 |
|
| 318 |
-- Scheme Procedure: make-token-reader spec proc [escape?] |
| 319 |
-- C Function: scm_make_token_reader (SCM spec, SCM proc, SCM escape_p) |
| 320 |
Use procedure (or reader) PROC as a token reader for the characters |
| 321 |
defined by SPEC. If ESCAPE_P is true, then the reader this token |
| 322 |
reader belongs to should return even if its result is undefined. |
| 323 |
|
| 324 |
The next section explains the token reader calling convention, i.e., |
| 325 |
how the PROC argument to 'make-token-reader' is invoked. |
| 326 |
|
| 327 |
|
| 328 |
File: guile-reader.info, Node: TR Calling Convention, Next: Invoking a Reader from a TR, Prev: Defining a New Token Reader, Up: Token Readers |
| 329 |
|
| 330 |
4.1.2 Token Reader Calling Convention |
| 331 |
------------------------------------- |
| 332 |
|
| 333 |
A token reader's procedure is passed four arguments: |
| 334 |
|
| 335 |
* the character that was read and which triggered its call; in the |
| 336 |
S-exp example, this would be '('; |
| 337 |
* the port to read from; |
| 338 |
* the reader which performed this invocation, i.e., either an |
| 339 |
'scm_reader_t' object (if the token reader is written in C) or a |
| 340 |
four-argument Scheme procedure (if the token reader is written in |
| 341 |
Scheme); |
| 342 |
* the top-level reader which yielded this invocation and which may be |
| 343 |
different from the previous argument in the case a token reader was |
| 344 |
made from a reader; the use of these two arguments will be detailed |
| 345 |
in the next section, *note Invoking a Reader from a TR::. |
| 346 |
|
| 347 |
It must return a Scheme object resulting from the interpretation of |
| 348 |
the characters read. It may as well raise an error if the input |
| 349 |
sequence is corrupt. Finally, it may return '*unspecified*', in which |
| 350 |
case the calling reader will not return and instead continue reading. |
| 351 |
This is particularly useful to define comment token readers: a TR that |
| 352 |
has just read a comment will obviously not have any sensible Scheme |
| 353 |
object to return, and a reader is not expected to return anything but a |
| 354 |
"real" Scheme object. A token reader for Scheme's ';' line comments may |
| 355 |
be defined as follows: |
| 356 |
|
| 357 |
(make-token-reader #\; read-a-line-and-return-unspecified) |
| 358 |
|
| 359 |
This behavior may, however, be overridden by passing |
| 360 |
'make-token-reader' a third argument (called ESCAPE?): |
| 361 |
|
| 362 |
(make-token-reader #\; read-a-line-and-return-unspecified #t) |
| 363 |
|
| 364 |
A reader that includes this TR will return '*unspecified*' once a |
| 365 |
line comment has been read. This is particularly useful, for instance, |
| 366 |
when implementing '#!' block comments (*note SCSH block comments: |
| 367 |
(guile)Block Comments, for more information) as a TR attached to '#\!' |
| 368 |
within the '#\#' sub-reader (*note Defining a New Token Reader::). |
| 369 |
|
| 370 |
Finally, the procedure passed to 'make-token-reader' may be '#f', in |
| 371 |
which case the resulting TR will just have the effect of ignoring the |
| 372 |
characters it is associated to. For instance, handling white spaces may |
| 373 |
be done by defining a TR like this: |
| 374 |
|
| 375 |
(make-token-reader '(#\space #\newline #\tab) #f) |
| 376 |
|
| 377 |
|
| 378 |
File: guile-reader.info, Node: Invoking a Reader from a TR, Next: Token Reader Library, Prev: TR Calling Convention, Up: Token Readers |
| 379 |
|
| 380 |
4.1.3 Invoking a Reader from a Token Reader |
| 381 |
------------------------------------------- |
| 382 |
|
| 383 |
As seen in section *Note Defining a New Token Reader::, token readers |
| 384 |
are systematically passed to readers when invoked. The reason why this |
| 385 |
may be useful may not be obvious at first sight. |
| 386 |
|
| 387 |
Consider an S-exp token reader. The TR itself doesn't have |
| 388 |
sufficient knowledge to read the objects that comprise an S-exp. So it |
| 389 |
needs to be able to call the reader that is being used to actually read |
| 390 |
those objects. |
| 391 |
|
| 392 |
The need for the TOP-LEVEL-READER argument passed to token readers |
| 393 |
may be illustrated looking at the implementation of the vector read |
| 394 |
syntax (*note (guile)Vector Syntax::). One may implement the vector |
| 395 |
reader as a token reader of the '#' sub-reader (*note Defining a New |
| 396 |
Token Reader::). The vector token reader may be implemented like this: |
| 397 |
|
| 398 |
(lambda (chr port reader top-level-reader) |
| 399 |
;; At this point, `#' as already been read and CHR is `(', |
| 400 |
;; so we can directly call the regular S-expression reader |
| 401 |
;; and convert its result into a vector. |
| 402 |
(let ((sexp-read (token-reader-procedure |
| 403 |
(standard-token-reader 'sexp)))) |
| 404 |
(apply vector |
| 405 |
(sexp-read chr port reader)))) |
| 406 |
|
| 407 |
When this procedure is invoked, READER points to the '#' sub-reader. |
| 408 |
|
| 409 |
|
| 410 |
File: guile-reader.info, Node: Token Reader Library, Next: Limitations, Prev: Invoking a Reader from a TR, Up: Token Readers |
| 411 |
|
| 412 |
4.1.4 Token Reader Library |
| 413 |
-------------------------- |
| 414 |
|
| 415 |
Guile-Reader comes with a number of re-usable token readers. Together, |
| 416 |
they might be assembled to form a complete Scheme reader equivalent to |
| 417 |
that of Guile (*note Reader Library::). Or they can be used |
| 418 |
individually in any reader. |
| 419 |
|
| 420 |
The 'standard-token-reader' procedure takes a symbol that names a |
| 421 |
standard TR from the library and returns it (or '#f' if not found). |
| 422 |
Currently, the available TRs are: |
| 423 |
|
| 424 |
Token Reader Character Spec. Description |
| 425 |
|
| 426 |
--------------------------------------------------------------------------- |
| 427 |
'boolean' 4 characters, This is a sharp token reader, i.e. |
| 428 |
'#\f'... '#\F' it reads an R5RS boolean ('#f' or |
| 429 |
'#F', '#t' or '#T') once a '#' |
| 430 |
character has been read. |
| 431 |
|
| 432 |
'boolean-srfi-4' 3 characters, This is a sharp token reader, i.e. |
| 433 |
'#\t'... '#\F' it reads an R5RS boolean ('#t', |
| 434 |
'#T', '#F', but _not_ '#f') once a |
| 435 |
'#' character has been read. |
| 436 |
Compared to the 'boolean' token |
| 437 |
reader, this one is useful when |
| 438 |
SRFI-4 floating-point homogeneous |
| 439 |
vectors are to be used at the same |
| 440 |
time: the SRFI-4 TR will handle |
| 441 |
'#f' on its own |
| 442 |
(*note Overlapping Token Readers::). |
| 443 |
|
| 444 |
'brace-free-number'13 characters, Return a number or a symbol, |
| 445 |
'#\-'... '#\9' considering curly braces as |
| 446 |
delimiters. |
| 447 |
|
| 448 |
'brace-free-symbol-lower-case'from '#\a' toRead a symbol that starts with a |
| 449 |
'#\z' lower-case letter and return a |
| 450 |
symbol. This token reader |
| 451 |
recognizes braces as delimiters, |
| 452 |
unlike R5RS/R6RS. |
| 453 |
|
| 454 |
'brace-free-symbol-misc-chars'17 characters,Read a symbol that starts with a |
| 455 |
'#\['... '#\$' non-alphanumeric character and |
| 456 |
return a symbol. This token |
| 457 |
reader recognizes braces as |
| 458 |
delimiters, unlike R5RS/R6RS. |
| 459 |
|
| 460 |
'brace-free-symbol-upper-case'from '#\A' toRead a symbol that starts with an |
| 461 |
'#\Z' upper-case letter and return a |
| 462 |
symbol. This token reader |
| 463 |
recognizes braces as delimiters, |
| 464 |
unlike R5RS/R6RS. |
| 465 |
|
| 466 |
'character' '#\\' This is a sharp token reader, i.e. |
| 467 |
it reads an R5RS character once a |
| 468 |
'#' character has been read. |
| 469 |
|
| 470 |
'curly-brace-sexp' '#\{' Read an S-expression enclosed in |
| 471 |
square brackets. This is already |
| 472 |
permitted by a number of Scheme |
| 473 |
implementations and will soon be |
| 474 |
made compulsory by R6RS. |
| 475 |
|
| 476 |
'guile-bit-vector' '#\*' This is a sharp token reader, i.e. |
| 477 |
it reads a bit vector following |
| 478 |
Guile's read syntax for bit |
| 479 |
vectors. See |
| 480 |
*note Guile's bit vectors: (guile)Bit Vectors, |
| 481 |
for details. |
| 482 |
|
| 483 |
'guile-extended-symbol''#\{' This is a sharp token reader, i.e. |
| 484 |
it reads a symbol using Guile's |
| 485 |
extended symbol syntax assuming a |
| 486 |
'#' character was read. See |
| 487 |
*note Guile's extended read syntax for symbols: (guile)Symbol Read Syntax, |
| 488 |
for details. |
| 489 |
|
| 490 |
'guile-number' 13 characters, Read a number following Guile's |
| 491 |
'#\-'... '#\9' fashion, that is, as in R5RS |
| 492 |
(*note R5RS' lexical structure: (r5rs)Lexical structure, |
| 493 |
for syntactic details). Because |
| 494 |
the syntaxes for numbers and |
| 495 |
symbols are closely tight in R5RS |
| 496 |
and Guile, this token reader may |
| 497 |
return either a number or a |
| 498 |
symbol. For instance, it will be |
| 499 |
invoked if the string |
| 500 |
'123.123.123' is passed to the |
| 501 |
reader but this will actually |
| 502 |
yield a symbol instead of a number |
| 503 |
(*note Overlapping Token Readers::). |
| 504 |
|
| 505 |
'guile-symbol-lower-case'from '#\a' toRead a symbol that starts with a |
| 506 |
'#\z' lower-case letter in a |
| 507 |
case-sensitive fashion. |
| 508 |
|
| 509 |
'guile-symbol-misc-chars'19 characters,Read a symbol that starts with a |
| 510 |
'#\['... '#\$' non-alphanumeric character in a |
| 511 |
case-sensitive fashion. Note that |
| 512 |
this token reader does _not_ |
| 513 |
consider square brackets as |
| 514 |
delimiters, as was the case with |
| 515 |
Guile 1.8 and earlier. |
| 516 |
|
| 517 |
'guile-symbol-upper-case'from '#\A' toRead a symbol that starts with an |
| 518 |
'#\Z' upper-case letter in a |
| 519 |
case-sensitive fashion. |
| 520 |
|
| 521 |
'keyword' '#\:' This token reader returns a |
| 522 |
keyword as found in Guile. It may |
| 523 |
be used either after a '#' |
| 524 |
character (to implement Guile's |
| 525 |
default keyword syntax, '#:kw') or |
| 526 |
within the top-level reader (to |
| 527 |
implement ':kw'-style keywords). |
| 528 |
|
| 529 |
It is worth noting that this token |
| 530 |
reader invokes its top-level in |
| 531 |
order to read the symbol |
| 532 |
subsequent to the ':' character. |
| 533 |
Therefore, it will adapt to the |
| 534 |
symbol delimiters currently in use |
| 535 |
(*note Token Delimiters::). |
| 536 |
|
| 537 |
'number+radix' 12 characters, This is a sharp token reader, i.e. |
| 538 |
'#\b'... '#\E' it reads a number using the radix |
| 539 |
notation, like '#b01' for the |
| 540 |
binary notation, '#x1d' for the |
| 541 |
hexadecimal notation, etc., see |
| 542 |
*note Guile's number syntax: (guile)Number Syntax, |
| 543 |
for details. |
| 544 |
|
| 545 |
'quote-quasiquote-unquote'3 characters,Read a quote, quasiquote, or |
| 546 |
'#\''... '#\,' unquote S-expression. |
| 547 |
|
| 548 |
'r5rs-lower-case-number'13 characters,Return a number or a lower-case |
| 549 |
'#\-'... '#\9' symbol. |
| 550 |
|
| 551 |
'r5rs-lower-case-symbol-lower-case'from '#\a' toRead a symbol that starts with a |
| 552 |
'#\z' lower-case letter and return a |
| 553 |
lower-case symbol, regardless of |
| 554 |
the case of the input. |
| 555 |
|
| 556 |
'r5rs-lower-case-symbol-misc-chars'19 characters,Read a symbol that starts with a |
| 557 |
'#\['... '#\$' non-alphanumeric character and |
| 558 |
return a lower-case symbol, |
| 559 |
regardless of the case of the |
| 560 |
input. |
| 561 |
|
| 562 |
'r5rs-lower-case-symbol-upper-case'from '#\A' toRead a symbol that starts with an |
| 563 |
'#\Z' upper-case letter and return a |
| 564 |
lower-case symbol, regardless of |
| 565 |
the case of the input. |
| 566 |
|
| 567 |
'r5rs-upper-case-number'13 characters,Return a number or an upper-case |
| 568 |
'#\-'... '#\9' symbol. |
| 569 |
|
| 570 |
'r6rs-number' 13 characters, Return a number or a symbol. This |
| 571 |
'#\-'... '#\9' token reader conforms to R6RS, |
| 572 |
i.e. it considers square brackets |
| 573 |
as delimiters. |
| 574 |
|
| 575 |
'r6rs-symbol-lower-case'from '#\a' to Read a symbol that starts with a |
| 576 |
'#\z' lower-case letter and return a |
| 577 |
symbol. This token reader |
| 578 |
conforms with R6RS in that it is |
| 579 |
case-sensitive and recognizes |
| 580 |
square brackets as delimiters |
| 581 |
(*note Token Delimiters::). |
| 582 |
|
| 583 |
'r6rs-symbol-misc-chars'17 characters,Read a symbol that starts with a |
| 584 |
'#\{'... '#\$' non-alphanumeric character and |
| 585 |
return a symbol. This token |
| 586 |
reader conforms with R6RS in that |
| 587 |
it is case-sensitive and |
| 588 |
recognizes square brackets as |
| 589 |
delimiters |
| 590 |
(*note Token Delimiters::). |
| 591 |
|
| 592 |
'r6rs-symbol-upper-case'from '#\A' to Read a symbol that starts with an |
| 593 |
'#\Z' upper-case letter and return a |
| 594 |
symbol. This token reader |
| 595 |
conforms with R6RS in that it is |
| 596 |
case-sensitive and recognizes |
| 597 |
square brackets as delimiters |
| 598 |
(*note Token Delimiters::). |
| 599 |
|
| 600 |
'r6rs-syntax-quote-quasiquote-unquote'3 characters,Read an R6RS-style syntax, |
| 601 |
'#\''... '#\,' quasisyntax, or unsyntax |
| 602 |
S-expression. |
| 603 |
|
| 604 |
'scsh-block-comment''#\!' This is a sharp token reader, i.e. |
| 605 |
it reads a SCSH-style block |
| 606 |
comment (like '#! multi-line |
| 607 |
comment !#') and returns |
| 608 |
'*unspecified*', assuming a '#' |
| 609 |
character was read before. This |
| 610 |
token reader has its "escape" bit |
| 611 |
set, meaning that the reader that |
| 612 |
calls it will return |
| 613 |
'*unspecified*' to its parent |
| 614 |
reader. See also |
| 615 |
*note block comments: (guile)Block Comments, |
| 616 |
for details about SCSH block |
| 617 |
comments. |
| 618 |
|
| 619 |
'semicolon-comment''#\;' Read an R5RS semicolon |
| 620 |
line-comment and return |
| 621 |
'*unspecified*'. Consequently, |
| 622 |
the calling reader will loop and |
| 623 |
ignore the comment. |
| 624 |
|
| 625 |
'sexp' '#\(' Read a regular S-expression |
| 626 |
enclosed in parentheses. |
| 627 |
|
| 628 |
'skribe-exp' '#\[' Read a Skribe markup expression. |
| 629 |
Skribe's expressions look like |
| 630 |
this: |
| 631 |
|
| 632 |
[Hello ,(bold [World])!] |
| 633 |
=> ("Hello " (bold "World") "!") |
| 634 |
|
| 635 |
See the Skribe web site |
| 636 |
(http://www.inria.fr/mimosa/fp/Skribe) |
| 637 |
or the Skribilo web site |
| 638 |
(http://www.nongnu.org/skribilo/) |
| 639 |
for more details. |
| 640 |
|
| 641 |
'square-bracket-sexp''#\[' Read an S-expression enclosed in |
| 642 |
square brackets. This is already |
| 643 |
permitted by a number of Scheme |
| 644 |
implementations and will soon be |
| 645 |
made compulsory by R6RS. |
| 646 |
|
| 647 |
'srfi-4' 3 characters, This is a sharp token reader, i.e. |
| 648 |
'#\s'... '#\f' it reads an SRFI-4 homogenous |
| 649 |
numeric vector once a '#' |
| 650 |
character has been read. This |
| 651 |
token reader also handles the |
| 652 |
boolean values '#f'. |
| 653 |
|
| 654 |
'srfi30-block-comment''#\|' This is a sharp token reader, i.e. |
| 655 |
it reads an SRFI-30 block comment |
| 656 |
(like '#| multi-line comment |#') |
| 657 |
and returns '*unspecified*', |
| 658 |
assuming a '#' character was read |
| 659 |
before. This token reader has its |
| 660 |
"escape" bit set. For more |
| 661 |
details about SRFI-30, see Nested |
| 662 |
Multi-line Comments |
| 663 |
(http://srfi.schemers.org/srfi-30/srfi-30.html). |
| 664 |
|
| 665 |
'srfi62-sexp-comment''#\;' This is a sharp token reader, i.e. |
| 666 |
it reads an SRFI-62 comment |
| 667 |
S-expression (as in '(+ 2 |
| 668 |
#;(comment here) 2)') and returns |
| 669 |
'*unspecified*', assuming a '#' |
| 670 |
character was read before. This |
| 671 |
token reader has its "escape" bit |
| 672 |
set. For more details about |
| 673 |
SRFI-62, please see S-expression |
| 674 |
comments specifications |
| 675 |
(http://srfi.schemers.org/srfi-62/srfi-62.html). |
| 676 |
|
| 677 |
'string' '#\"' Read an R5RS string. |
| 678 |
|
| 679 |
'vector' '#\(' This is a sharp token reader, i.e. |
| 680 |
it reads an R5RS vector once a '#' |
| 681 |
character has been read. |
| 682 |
|
| 683 |
'whitespace' from '#\soh' to This is a void token reader that |
| 684 |
'#\space' causes its calling reader to |
| 685 |
ignore (i.e. treat as whitespace) |
| 686 |
all ASCII characters ranging from |
| 687 |
1 to 32. |
| 688 |
|
| 689 |
|
| 690 |
As can be inferred from the above two lists, reading character |
| 691 |
sequences starting with the '#' characters can easily be done by |
| 692 |
defining a sub-reader for that character. That reader can then be |
| 693 |
passed to 'make-token-reader' as the procedure attached to '#': |
| 694 |
|
| 695 |
(define sharp-reader |
| 696 |
(make-reader (map standard-token-reader |
| 697 |
'(boolean character |
| 698 |
number+radix keyword |
| 699 |
srfi-4 |
| 700 |
block-comment)))) |
| 701 |
|
| 702 |
(define top-level-reader |
| 703 |
(make-reader (list (make-token-reader #\# sharp-reader) |
| 704 |
... |
| 705 |
))) |
| 706 |
|
| 707 |
The procedures available to manipulate token readers are listed |
| 708 |
below: |
| 709 |
|
| 710 |
-- Scheme Procedure: token-reader-escape? tr |
| 711 |
-- C Function: scm_token_reader_escape_p (SCM tr) |
| 712 |
Return '#t' if token reader TR requires the readers that use it to |
| 713 |
return even if its return value is unspecified. |
| 714 |
|
| 715 |
-- Scheme Procedure: token-reader-specification tr |
| 716 |
-- C Function: scm_token_reader_spec (SCM tr) |
| 717 |
Return the specification, of token reader TR. |
| 718 |
|
| 719 |
-- Scheme Procedure: token-reader-procedure tr |
| 720 |
-- C Function: scm_token_reader_proc (SCM tr) |
| 721 |
Return the procedure attached to token reader TR. When '#f' is |
| 722 |
returned, the TR is a "fake" reader that does nothing. This is |
| 723 |
typically useful for whitespaces. |
| 724 |
|
| 725 |
-- Scheme Procedure: standard-token-reader name |
| 726 |
-- C Function: scm_standard_token_reader (SCM name) |
| 727 |
Lookup standard token reader named NAME (a symbol) and return it. |
| 728 |
If NAME is does not name a standard token reader, then an error is |
| 729 |
raised. |
| 730 |
|
| 731 |
|
| 732 |
File: guile-reader.info, Node: Limitations, Prev: Token Reader Library, Up: Token Readers |
| 733 |
|
| 734 |
4.1.5 Limitations |
| 735 |
----------------- |
| 736 |
|
| 737 |
This section describes the main limitations and common pitfalls |
| 738 |
encountered when using Guile-Reader. |
| 739 |
|
| 740 |
* Menu: |
| 741 |
|
| 742 |
* Token Delimiters:: Dealing with token delimiters. |
| 743 |
* Overlapping Token Readers:: Token readers that syntactically overlap. |
| 744 |
|
| 745 |
|
| 746 |
File: guile-reader.info, Node: Token Delimiters, Next: Overlapping Token Readers, Prev: Limitations, Up: Limitations |
| 747 |
|
| 748 |
4.1.5.1 Token Delimiters |
| 749 |
........................ |
| 750 |
|
| 751 |
As can be seen from the previous section, there exist, for instance, an |
| 752 |
surprisingly high number of symbol token readers. The reason for this |
| 753 |
is that different syntax variants define different "token delimiters". |
| 754 |
Token delimiters are characters that help the reader determine where |
| 755 |
tokens that require implicit termination do terminate. Quoting R5RS |
| 756 |
(*note R5RS' lexical structure: (r5rs)Lexical structure.): |
| 757 |
|
| 758 |
Tokens which require implicit termination (identifiers, numbers, |
| 759 |
characters, and dot) may be terminated by any <delimiter>, but not |
| 760 |
necessarily by anything else. |
| 761 |
|
| 762 |
R5RS defines token delimiters as one of the following: a whitespace, |
| 763 |
a parentheses, a quotation mark ('"') or a semi-colon (';') character. |
| 764 |
On the other hand, R6RS, which is to support the ability to use square |
| 765 |
brackets instead of parentheses for S-expressions, also considers square |
| 766 |
brackets as token delimiters. Likewise, if we were to support curly |
| 767 |
braces to enclose S-expressions, then curly braces would need to be |
| 768 |
considered as token delimiters too. |
| 769 |
|
| 770 |
For this reason, the token reader library comes with several symbol |
| 771 |
token readers: the 'guile-symbol-' family does not consider square |
| 772 |
brackets as delimiters while the 'r6rs-symbol-' family does, the |
| 773 |
'brace-free-' TR family considers curly braces as delimiters but not |
| 774 |
square brackets, etc. Similarly, several variants of number TRs are |
| 775 |
available. This is due to the fact that number TRs may return symbols |
| 776 |
in corner cases like symbol names starting with a number. |
| 777 |
|
| 778 |
However, although keywords must also comply with the token delimiters |
| 779 |
rules, there is only one keyword TR (called 'keyword'). The reason for |
| 780 |
this is that this TR relies on the top-level reader's symbol reader to |
| 781 |
read the symbol that makes up the keyword being read. |
| 782 |
|
| 783 |
In the current design of Guile-Reader, this token delimiter issue |
| 784 |
creates a number of pitfalls when one is willing to change the current |
| 785 |
delimiters. In particular, one has to be very careful about using TRs |
| 786 |
that consistently assume the same token delimiters. |
| 787 |
|
| 788 |
A "real" lexer generator such as Danny Dubé's SILex avoids such |
| 789 |
issues because it allows the definition of tokens using regular |
| 790 |
expressions. However, its usage may be less trivial than that of |
| 791 |
Guile-Reader. |
| 792 |
|
| 793 |
|
| 794 |
File: guile-reader.info, Node: Overlapping Token Readers, Prev: Token Delimiters, Up: Limitations |
| 795 |
|
| 796 |
4.1.5.2 Overlapping Token Readers |
| 797 |
................................. |
| 798 |
|
| 799 |
As can be seen from the descriptions of the standard token readers |
| 800 |
(*note Token Reader Library::), token readers sometimes "overlap", i.e., |
| 801 |
the set of input strings they match overlap. For instance, the |
| 802 |
'boolean' token reader should match '#t', '#T', '#f' or '#F'. However, |
| 803 |
the 'srfi-4' token reader also needs to match floating-point numeric |
| 804 |
vectors such as '#f32(1.0 2.0 3.0)'. Similarly, strings like '1' are, |
| 805 |
logically, handled by the 'guile-number' (or similar) token reader; |
| 806 |
however, since a string like '1+' should be recognized as a _symbol_, |
| 807 |
rather than a number, it must then be passed to one of the symbol token |
| 808 |
readers. |
| 809 |
|
| 810 |
In those two cases, the input sets of those two token readers |
| 811 |
_overlap_. In order for the resulting reader to work as expected, the |
| 812 |
two overlapping token readers need to somehow _cooperate_. In the first |
| 813 |
example, this is achieved by having the 'srfi-4' TR read in strings |
| 814 |
starting with '#f' or '#F' and passing them to the 'boolean-srfi-4' TR |
| 815 |
if need be. In the second case, this is done by having number TRs |
| 816 |
(e.g., 'guile-number') explicitly check for non-digit characters and |
| 817 |
return a symbol instead of a number when a non-digit is encountered. |
| 818 |
|
| 819 |
It should be obvious from these two examples that this limitation |
| 820 |
impedes full separation of the various TRs. Fortunately, there are not |
| 821 |
so many cases where such overlapping occurs when implementing readers |
| 822 |
for R5RS-like syntaxes. The implementation of |
| 823 |
'make-alternate-guile-reader' (*note Reader Library::) shows how such |
| 824 |
problems have been worked around. |
| 825 |
|
| 826 |
Lexer generators such as Flex (http://www.gnu.org/software/flex/), |
| 827 |
SILex (http://www.iro.umontreal.ca/~dube/) and Bigloo's RGC (*note |
| 828 |
Bigloo's RGC: (bigloo)Regular Parsing.) obviously do not have this |
| 829 |
problem: all possible "token" types are defined using regular |
| 830 |
expressions and the string-handling code (e.g., code that converts a |
| 831 |
string into a Scheme number) is only invoked once a full matching string |
| 832 |
has been found. |
| 833 |
|
| 834 |
|
| 835 |
File: guile-reader.info, Node: Readers, Prev: Token Readers, Up: API Reference |
| 836 |
|
| 837 |
4.2 Readers |
| 838 |
=========== |
| 839 |
|
| 840 |
Guile-Reader is about defining readers. Continuing to read this manual |
| 841 |
was definitely a good idea since we have finally reached the point where |
| 842 |
we will start talking about how to define new readers. |
| 843 |
|
| 844 |
* Menu: |
| 845 |
|
| 846 |
* Defining a New Reader:: |
| 847 |
* Reader Library:: |
| 848 |
* Compatibility and Confinement:: |
| 849 |
|
| 850 |
|
| 851 |
File: guile-reader.info, Node: Defining a New Reader, Next: Reader Library, Prev: Readers, Up: Readers |
| 852 |
|
| 853 |
4.2.1 Defining a New Reader |
| 854 |
--------------------------- |
| 855 |
|
| 856 |
Roughly, a reader is no more than a loop which reads characters from a |
| 857 |
given port, and dispatches further interpretation to more specific |
| 858 |
procedures. Written in Scheme, it could resemble something like: |
| 859 |
|
| 860 |
(define (my-reader port) |
| 861 |
(let loop ((result *unspecified*)) |
| 862 |
(let ((the-char (getc port))) |
| 863 |
(case the-char |
| 864 |
((#\() (my-sexp-token-reader the-char port my-reader))) |
| 865 |
((#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9) |
| 866 |
(my-number-token-reader the-char port my-reader)) |
| 867 |
(else |
| 868 |
(error "unexpected character" the-char)))))) |
| 869 |
|
| 870 |
Using Guile-Reader, this is done simply by providing a list of token |
| 871 |
readers to the 'make-reader' procedure, as in the following example: |
| 872 |
|
| 873 |
(define my-reader |
| 874 |
(make-reader (list (make-token-reader #\( my-sexp-token-reader) |
| 875 |
(make-token-reader '(#\0 . #\9) |
| 876 |
my-number-token-reader)))) |
| 877 |
|
| 878 |
However, the procedure returned by 'make-reader' is different from |
| 879 |
the hand-written one above in that in takes two additional optional |
| 880 |
arguments which makes it look like this: |
| 881 |
|
| 882 |
(define (my-reader port faults-caller-handled? top-level-reader) |
| 883 |
(let loop ((the-char (getc port))) |
| 884 |
(case the-char |
| 885 |
... |
| 886 |
(else |
| 887 |
(if (not faults-caller-handled?) |
| 888 |
(error "unexpected character" the-char) |
| 889 |
(ungetc the-char) ;; and return *unspecified* |
| 890 |
))))) |
| 891 |
|
| 892 |
Therefore, by default, 'my-reader' will raise an error as soon as it |
| 893 |
reads a character that it does not know how to handle. However, if the |
| 894 |
caller passes '#t' as its 'faults-caller-handled?' argument, then |
| 895 |
'my-reader' is expected to "unget" the faulty character and return |
| 896 |
'*unspecified*', thus allowing the caller to handle the situation. |
| 897 |
|
| 898 |
This is useful, for instance, in the S-exp token reader example: the |
| 899 |
S-exp token reader needs to call its calling reader in order to read the |
| 900 |
components between the opening and closing brackets; however, the |
| 901 |
calling reader may be unable to handle the '#\)' character so the S-exp |
| 902 |
token reader has to handle it by itself and needs to tell it to the |
| 903 |
reader. |
| 904 |
|
| 905 |
-- Scheme Procedure: %reader-standard-fault-handler chr port reader |
| 906 |
-- C Function: scm_reader_standard_fault_handler (SCM chr, SCM port, |
| 907 |
SCM reader) |
| 908 |
Throw a 'read-error' exception indicating that character CHR was |
| 909 |
read from PORT and could not be handled by READER. |
| 910 |
|
| 911 |
-- Scheme Procedure: make-reader token-readers [fault-handler-proc |
| 912 |
[flags...]] |
| 913 |
-- C Function: scm_make_reader (SCM token_readers, SCM |
| 914 |
fault_handler_proc, SCM flags) |
| 915 |
Create a reader made of the token readers listed in TOKEN_READERS. |
| 916 |
TOKEN_READERS should be a list of token readers returned by |
| 917 |
'make-token-reader' or 'standard-token-reader' for instance. The |
| 918 |
FAULT_HANDLER_PROC argument is optional and may be a three-argument |
| 919 |
procedure to call when an unexpected character is read. When |
| 920 |
FAULT_HANDLER_PROC is invoked, it is passed the faulty character, |
| 921 |
input port, and reader; its return value, if any, is then returned |
| 922 |
by the reader. If FAULT_HANDLER_PROC is not specified, then |
| 923 |
'%reader-standard-fault-handler' is used. FLAGS is a rest argument |
| 924 |
which may contain a list of symbols representing reader compilation |
| 925 |
flags. |
| 926 |
|
| 927 |
Currently, the flags that may be passed to 'make-reader' are the |
| 928 |
following: |
| 929 |
|
| 930 |
* 'reader/record-positions' will yield a reader that records the |
| 931 |
position of the expression read, which is mostly useful for |
| 932 |
debugging purposes; this information may then be accessed via |
| 933 |
source properties (*note source properties: (guile)Procedure |
| 934 |
Properties.). |
| 935 |
|
| 936 |
* 'reader/lower-case' will have the yielded reader convert to |
| 937 |
lower-case all the letters that it reads; note that this is not |
| 938 |
sufficient to implement symbol case-insensitivity as shown in *note |
| 939 |
(guile)Reader options::. For this, the token reader(s) that read |
| 940 |
symbols must also convert all subsequent characters to lower-case. |
| 941 |
|
| 942 |
* 'reader/upper-case' will have the yielded reader convert to |
| 943 |
upper-case all the letters that it reads; again, that is not |
| 944 |
sufficient to implement case-insensitivity. |
| 945 |
|
| 946 |
* 'reader/debug' causes the generated reader to produce debugging |
| 947 |
output. |
| 948 |
|
| 949 |
|
| 950 |
File: guile-reader.info, Node: Reader Library, Next: Compatibility and Confinement, Prev: Defining a New Reader, Up: Readers |
| 951 |
|
| 952 |
4.2.2 Reader Library |
| 953 |
-------------------- |
| 954 |
|
| 955 |
The '(system reader)' module exports the 'default-reader' procedure |
| 956 |
which returns a reader equivalent to Guile's built-in default reader |
| 957 |
made of re-usable token readers written in C (*note Token Reader |
| 958 |
Library::). |
| 959 |
|
| 960 |
-- Scheme Procedure: default-sharp-reader-token-readers |
| 961 |
-- C Function: scm_default_sharp_reader_token_readers (void) |
| 962 |
Return the list of token readers that comprise Guile's default |
| 963 |
reader for the '#' character. |
| 964 |
|
| 965 |
-- Scheme Procedure: default-reader-token-readers |
| 966 |
-- C Function: scm_default_reader_token_readers (void) |
| 967 |
Return the list of token readers that comprise Guile's default |
| 968 |
reader. |
| 969 |
|
| 970 |
-- Scheme Procedure: default-sharp-reader |
| 971 |
-- C Function: scm_default_sharp_reader (void) |
| 972 |
Returns Guile's default reader for the '#' character. |
| 973 |
|
| 974 |
Additionally, the '(system reader library)' module exports a number |
| 975 |
of procedures that ease the re-use of readers. |
| 976 |
|
| 977 |
-- Scheme Procedure: make-guile-reader [fault-handler [flags...]] |
| 978 |
-- C Function: scm_make_guile_reader (SCM fault_handler, SCM flags) |
| 979 |
Make and return a new reader compatible with Guile 2.0's 'read', |
| 980 |
with its default settings. This function calls 'make-reader' with |
| 981 |
FLAGS. Note that the sharp reader used by the returned reader is |
| 982 |
also instantiated using FLAGS. The value of FAULT-HANDLER defaults |
| 983 |
to '%reader-standard-fault-handler'. |
| 984 |
|
| 985 |
-- Scheme Procedure: make-alternate-guile-reader |
| 986 |
Return a newly created Guile reader with options OPTIONS (a list of |
| 987 |
symbols, as for 'alternate-guile-reader-token-readers'), with fault |
| 988 |
handler FAULT-HANDLER and flags FLAGS. The FAULT-HANDLER and FLAGS |
| 989 |
arguments are the same as those passed to 'make-reader'. By |
| 990 |
default, FAULT-HANDLER is set to %READER-STANDARD-FAULT-HANDLER. |
| 991 |
|
| 992 |
-- Scheme Procedure: alternate-guile-reader-token-readers |
| 993 |
Given OPTIONS, a list of symbols describing reader options relative |
| 994 |
to the reader returned by '(default-reader)', return two lists of |
| 995 |
token readers: one for use as a sharp reader and the other for use |
| 996 |
as a top-level reader. Currently, the options supported are the |
| 997 |
following: |
| 998 |
|
| 999 |
'no-sharp-keywords' |
| 1000 |
Remove support for '#:kw'-style keywords. |
| 1001 |
'dsssl-keywords' |
| 1002 |
Add support for DSSSL-style keywords, like '#!kw'. This |
| 1003 |
option also has the same effect as 'no-scsh-block-comments'. |
| 1004 |
'colon-keywords' |
| 1005 |
Add support for ':kw'-style keywords. This is equivalent to |
| 1006 |
'(read-set! keywords 'prefix)'. |
| 1007 |
'no-scsh-block-comments' |
| 1008 |
Disable SCSH-style block comments (see *note SCSH block |
| 1009 |
comments: (guile)Block Comments, for details). |
| 1010 |
'srfi30-block-comments' |
| 1011 |
Add support for SRFI-30 block comments, like: |
| 1012 |
(+ 2 #| This is an #| SRFI-30 |# comment |# 2) |
| 1013 |
'srfi62-sexp-comments' |
| 1014 |
Add support for SRFI-62 S-expression comments, like: |
| 1015 |
(+ 2 #;(a comment) 2) |
| 1016 |
'case-insensitive' |
| 1017 |
Read symbols in a case-insensitive way. |
| 1018 |
'square-bracket-sexps' |
| 1019 |
'square-brackets' |
| 1020 |
Allow for square brackets around S-expressions. |
| 1021 |
|
| 1022 |
-- Scheme Procedure: read-options->extended-reader-options |
| 1023 |
Read READ-OPTS, a list representing read options following Guile's |
| 1024 |
built-in representation (see *note (guile)Scheme Read::, for |
| 1025 |
details), and return a list of symbols represented "extended reader |
| 1026 |
options" understood by 'make-alternate-guile-reader' et al. |
| 1027 |
|
| 1028 |
|
| 1029 |
File: guile-reader.info, Node: Compatibility and Confinement, Prev: Reader Library, Up: Readers |
| 1030 |
|
| 1031 |
4.2.3 Compatibility and Confinement |
| 1032 |
----------------------------------- |
| 1033 |
|
| 1034 |
Guile's core read subsystem provides an interface to customize its |
| 1035 |
reader, namely via the 'read-options' (*note (guile)Scheme Read::) and |
| 1036 |
'read-hash-extend' (*note 'read-hash-extend': (guile)Reader Extensions.) |
| 1037 |
procedures. |
| 1038 |
|
| 1039 |
The main problem with this approach is that changing the reader's |
| 1040 |
options using these procedures has a global effect since there is only |
| 1041 |
one instance of 'read'. Changing the behavior of a single function at |
| 1042 |
the scale of the whole is not very "schemey" and can be quite harmful. |
| 1043 |
Suppose a module relies on case-insensitivity while another relies on |
| 1044 |
case-sensitivity. If one tries to use both modules at the same time, |
| 1045 |
chances are that at least one of them will not work as expected. Risks |
| 1046 |
of conflicts are even higher when 'read-hash-extend' is used: imagine a |
| 1047 |
module that uses DSSSL-style keywords, while another needs SCSH-style |
| 1048 |
block comments. |
| 1049 |
|
| 1050 |
In '(system reader confinement)', guile-reader offers an |
| 1051 |
implementation of 'read-option-interface' and 'read-hash-extend' that |
| 1052 |
allows to _confine_ such settings on a per-module basis. In order to |
| 1053 |
enable reader confinement, one just has to do this: |
| 1054 |
|
| 1055 |
(use-modules (system reader confinement)) |
| 1056 |
|
| 1057 |
Note that this must be done before the suspicious modules are loaded, |
| 1058 |
that is, typically when your program starts. This will redefine |
| 1059 |
'read-options-interface' and 'read-hash-extend' so that any future |
| 1060 |
modification performed via Guile's built-in reader option interface will |
| 1061 |
be confined to the calling module. |
| 1062 |
|
| 1063 |
Starting from Guile 1.8.0, 'current-reader' is a core binding bound |
| 1064 |
to a fluid whose value should be either '#f' or a reader (i.e., a |
| 1065 |
'read'-like procedure). The value of this fluid dictates the reader |
| 1066 |
that is to be used by 'primitive-load' and its value can be changed |
| 1067 |
dynamically (*note 'current-reader': (guile)Loading.). |
| 1068 |
|
| 1069 |
The confined variants of 'read-options-interface' and |
| 1070 |
'read-hash-extend' rely on this feature to make reader customizations |
| 1071 |
local to the file being loaded. This way, invocations of these |
| 1072 |
functions from within a file being loaded by 'primitive-load' take |
| 1073 |
effect immediately. |
| 1074 |
|
| 1075 |
|
| 1076 |
File: guile-reader.info, Node: Internals, Next: Concept Index, Prev: API Reference, Up: Top |
| 1077 |
|
| 1078 |
5 Internals |
| 1079 |
*********** |
| 1080 |
|
| 1081 |
In order to not have to trade too much performance for flexibility, |
| 1082 |
Guile-Reader dynamically compiles code for the readers defined using GNU |
| 1083 |
_lightning_ (*note Introduction to GNU _lightning_: |
| 1084 |
(lightning)Overview.). As of version 1.2c, GNU _lightning_ can generate |
| 1085 |
code for the PowerPC, SPARC, and IA32 architectures. For other |
| 1086 |
platforms, Guile-Reader provides an alternative (slower) C |
| 1087 |
implementation that does not depend on it. Using the |
| 1088 |
_lightning_-generated readers typically provides a 5% performance |
| 1089 |
improvement over the static C implementation. However, note that |
| 1090 |
_lightning_ 2.x is not supported yet. |
| 1091 |
|
| 1092 |
Re-using token readers written in C, as explained in *Note Token |
| 1093 |
Reader Library::, does not imply any additional cost: the underlying C |
| 1094 |
function will be called directly by the reader, without having to go |
| 1095 |
through any marshalling/unmarshalling stage. |
| 1096 |
|
| 1097 |
Additionally, on the C side, token readers may be initialized |
| 1098 |
_statically_ (except, obviously, token readers made out of a |
| 1099 |
dynamically-compiled reader). Making good use of it can improve the |
| 1100 |
startup time of a program. For example, 'make-guile-reader' (*note |
| 1101 |
Reader Library::) is implemented in C and it uses statically initialized |
| 1102 |
arrays of token readers. It still needs to invoke 'scm_c_make_reader |
| 1103 |
()', but at least, token readers themselves are "ready to use". |
| 1104 |
|
| 1105 |
Scanners as generated by Flex or similar tools should theoretically |
| 1106 |
be able to provide better performance because the input reading and |
| 1107 |
pattern matching loop is self-contained, may fit in cache, and only has |
| 1108 |
to perform function calls once a pattern has been fully recognized. |
| 1109 |
|
| 1110 |
|
| 1111 |
File: guile-reader.info, Node: Concept Index, Next: Function Index, Prev: Internals, Up: Top |
| 1112 |
|
| 1113 |
Concept Index |
| 1114 |
************* |
| 1115 |
|
| 1116 |
|