This is guile-reader.info, produced by makeinfo version 6.8 from guile-reader.texi. This file documents Guile-Reader. Copyright (C) 2005, 2006, 2007, 2008, 2009, 2012, 2015, 2017 Ludovic Courtès Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions, except that this permission notice may be stated in a translation approved by the Free Software Foundation. INFO-DIR-SECTION The Algorithmic Language Scheme START-INFO-DIR-ENTRY * Guile Reader: (guile-reader). A Reader Framework for Guile. END-INFO-DIR-ENTRY  File: guile-reader.info, Node: Top, Next: Introduction, Prev: (dir), Up: (dir) A Reader Framework for Guile **************************** This document describes Guile-Reader version 0.6.3, for GNU Guile's 3.0 stable series, as well as the legacy 1.8, 2.0 and 2.2 series. It was last updated in July 2024. This documentation should be mostly complete. Details about the C API are omitted from this manual. They can be found in the public header files. * Menu: * Introduction:: The goals of Guile-Reader. * Overview:: How it works. * Quick Start:: For the impatient. * API Reference:: Using Guile-Reader. * Internals:: Performance considerations. * Concept Index:: Concepts. * Function Index:: Scheme procedures and C functions.  File: guile-reader.info, Node: Introduction, Next: Overview, Prev: Top, Up: Top 1 Introduction ************** Guile currently provides limited extensibility of its reader, by means of 'read-hash-extend' (*note 'read-hash-extend': (guile)Reader Extensions.), for instance, or 'read-enable' (*note 'read-enable': (guile)Scheme Read.). SRFI-10 (http://srfi.schemers.org/srfi-10/srfi-10.html) tries to propose a generic, portable, extension mechanism similar to 'read-hash-extend' but limited to '#,' sequences. Moreover, while this may not always be desirable, all these extension facilities have a global effect, changing the behavior of the sole reader implementation available at run-time. This makes it impossible to have, for instance, one module consider names starting with ':' as symbols, while another considers them as keywords. Extensions such as the read syntax for SRFI-4 numeric vectors (*note SRFI-4 vectors: (guile)Uniform Numeric Vectors.) had to be added to Guile's built-in C reader. Syntactic extensions that did not appeal the majority of users, like Emacs-Lisp vectors, are '#ifdef''d within the reader code and are not available by default. Moreover, some extensions are incompatible with each other, such as the DSSSL keyword syntax and SCSH block comments (*note SCSH block comments: (guile)Block Comments.). In short the current reader syntax is hardly extensible. The idea of Guile-Reader is to provide a framework allowing users to quickly define readers for whatever syntax (or rather: variant of the Scheme syntax) they like. Programs can then provide their own readers and, thanks to Guile's 'current-reader' mechanism, have their code read with this reader. When using Guile 2.0, readers produced by Guile-Reader are Unicode-capable; they can read from any ASCII-compatible encoding, such as UTF-8 or ISO-8859-1. While it is much simpler than a full-blown lexer generator such as Flex (http://www.gnu.org/software/flex/), Danny Dubé's SILex (http://www.iro.umontreal.ca/~dube/) and Bigloo's RGC (http://www.inria.fr/mimosa/fp/Bigloo), its simple programming interface should make it very straightforward to implement readers, especially for Scheme-like syntaxes. Best of all, Guile Reader comes with a library of components that can typically be used to construct a reader for the Scheme syntax. And each one of this components may be reused at will when creating other readers. On the other hand, one should be aware that this simpler API comes at the cost of a lack of consistency in some cases, as outlined later in this manual (*note Limitations::). Common Lisp has a similar mechanism to extend its reader which is called the "read table". Gambit Scheme (http://www.iro.umontreal.ca/~gambit/), for instance, also provides an implementation of read tables. However, it appears to have limitations similar to Guile's 'read-enable' and 'read-hash-extend' in terms of possibilities for syntax extension. On the other hand, it allows the reader and writer to be kept consistent, which Guile-Reader does not address.  File: guile-reader.info, Node: Overview, Next: Quick Start, Prev: Introduction, Up: Top 2 Overview ********** Guile-Reader allows for the construction of readers capable of understanding various syntactic variants. The simplest way to use it is through its "reader library" that allows one to pick and choose various commonly used syntactic extensions to the standard Scheme syntax (*note Reader Library::). However, Guile-Reader also provides a finer-grain programming interface allowing the construction of virtually any reader, with its own syntactic specificities. The following sections focus primarily on this capability. Before going into the details of the reader framework API, let us have a quick overview of what this is. Basically, Guile-Reader introduces two objects: "readers" and "token readers". Readers can be thought of, simply, as procedures like Scheme's 'read' (*note R5RS: (r5rs)Input.), i.e., procedures that take one (optional) argument, namely the port to read from. We will see later that readers as defined by Guile-Reader can actually receive two more arguments (*note Defining a New Reader::). A reader, like 'read', reads a sequence of characters (the "external representation" of some object) and returns a Scheme object. Token readers (TRs, for short) are the building block of a reader. A token reader is basically an association between a character or set of characters and a procedure to read and interpret a sequence of characters starting with one of the former. For instance, in a standard Scheme reader, the character '(' may be associated to a procedure that reads an S-expression. Likewise, lower-case and upper-case letters associated with the appropriate procedure form a token reader for symbols. In Guile-Reader, TRs may be written either in Scheme or in C, and they can even be a reader produced by Guile-Reader itself. Unless it is a reader, the procedure (or C function) used to create a TR will receive four arguments: * the character that was read and which triggered its call; in the S-exp example, this would be '('; * the port to read from; * the reader which performed this invocation; * the top-level reader which yielded this invocation. The next section shows how to get started with Guile-Reader, using a high-level API. Details about of the programming interface are given in the API reference (*note API Reference::).  File: guile-reader.info, Node: Quick Start, Next: API Reference, Prev: Overview, Up: Top 3 Quick Start ************* The simplest way to get started and to produce customized readers is through the high-level API provided by the '(system reader library)' module. As the name suggests, this module provides a library of readily usable readers. The 'make-alternate-guile-reader' procedure gives access to these readers. It can be passed a list of symbols describing reader options, such as whether you want support for DSSSL keywords, SRFI-62 comments (http://srfi.schemers.org/srfi-62/srfi-62.html), SRFI-30 block comments (http://srfi.schemers.org/srfi-30/srfi-30.html), etc. The following example binds to MY-READER a Scheme reader that supports DSSSL-style keywords and SRFI-62 comments, and that is case insensitive: (use-modules (system reader library)) (define my-reader (make-alternate-guile-reader '(dsssl-keywords srfi62-sexp-comments case-insensitive))) This reader can then be used like the regular 'read' procedure: (procedure? my-reader) => #t (with-input-from-string "some-symbol" my-reader) => some-symbol (my-reader (open-input-string "MiXeD-CaSe")) => mixed-case (my-reader (open-input-string "(an sexp with a #;srfi-62 comment)")) => (an sexp with a comment) (my-reader (open-input-string "#!some-DSSSL-keyword")) => #:some-dsssl-keyword Most of the time, you will want to use it as the current reader, at least in the module where you created it. Fortunately, Guile provides a mechanism for this, the 'current-reader' fluid (*note 'current-reader': (guile)Loading.). Changing the value of this fluid from a file that is being loaded will affect the reader use to load it. In general, you will want to modify 'current-reader' not only at run time, but also at compile time and when the code is evaluated, which can be achieved using 'eval-when' (*note 'eval-when': (guile)Eval When.). For instance: ;;; This is the beginning of my Scheme file. At this point, Guile's ;;; default reader is used (or, alternatively, the one that was ;;; passed as a second parameter to `load'). So, for instance, DSSSL ;;; keywords are _not_ recognized. ;; Let's create our own customized reader... (use-modules (system reader library)) ;; 'eval-when' here is needed to make 'my-reader' accessible at ;; compile time, and to have the 'current-reader' change take ;; effect at compile time. (eval-when (compile load eval) (define my-reader (make-alternate-guile-reader '(dsssl-keywords srfi62-sexp-comments case-insensitive))) ;; Let's make it the current reader. (fluid-set! current-reader my-reader)) ;; From now on, MY-READER is used to read the rest of this file. Thus ;; we can happily use the syntactic extensions it implements: DSSSL ;; keywords, SRFI-62 comments and case-insensitivity. (if (not (keyword? #!dsssl-keyword)) (error "Something went wrong, this should not happen!")) The nice thing is that 'current-reader' is reset to its initial value when the dynamic extent of 'load' is left. In other words, the loader of the file above is _not_ affected by the 'fluid-set!' statement. Reader changes are _hygienic_ and modules can use their own without risking to harm each other. The full list of options supported by 'make-alternate-guile-reader' is shown in *note Reader Library::. However, this option set is quite limited and you may find yourself wanting a syntactic extension not available here. In that case, you will want to build a new reader, possibly reusing existing reader components known as "token readers", as described in *note Defining a New Reader::.  File: guile-reader.info, Node: API Reference, Next: Internals, Prev: Quick Start, Up: Top 4 API Reference *************** All the Scheme procedures described below are exported by the '(system reader)' module. In order to be able to use them, you will need to import this module first: (use-modules (system reader)) A C variant is also available for most of them by including the declarations available in the '' header file. * Menu: * Token Readers:: The basic components of a reader. * Readers:: How to build readers.  File: guile-reader.info, Node: Token Readers, Next: Readers, Prev: API Reference, Up: API Reference 4.1 Token Readers ================= Basically, token readers are the association of a character or set of characters and a function that is able to interpret character sequences that start by one of these characters. We will see below how to define new token readers first, and then how to re-use existing ones. * Menu: * Defining a New Token Reader:: * TR Calling Convention:: * Invoking a Reader from a TR:: * Token Reader Library:: * Limitations::  File: guile-reader.info, Node: Defining a New Token Reader, Next: TR Calling Convention, Prev: Token Readers, Up: Token Readers 4.1.1 Defining a New Token Reader --------------------------------- A new token reader object can be created by calling the 'make-token-reader' procedure with a "character specification" and a procedure. A character specification defines the set of characters which should trigger an invocation of the corresponding procedure. The character specification may be either: * a single character; * a pair of characters, which is interpreted as a character range; * a list of characters, which is interpreted as a set of characters. The procedure passed to 'make-token-reader' may actually be either a C function or Scheme procedure that takes four arguments (*note TR Calling Convention::), any "object" returned by 'token-reader-procedure', or a reader. This last option turns out to be quite helpful. For example, this is very convenient when implementing the various Scheme read syntaxes prefixed by the '#' character: one can create a reader for '#', and then turn it into a token reader that is part of the top-level reader. The reference for 'make-token-reader' is given below: -- Scheme Procedure: make-token-reader spec proc [escape?] -- C Function: scm_make_token_reader (SCM spec, SCM proc, SCM escape_p) Use procedure (or reader) PROC as a token reader for the characters defined by SPEC. If ESCAPE_P is true, then the reader this token reader belongs to should return even if its result is undefined. The next section explains the token reader calling convention, i.e., how the PROC argument to 'make-token-reader' is invoked.  File: guile-reader.info, Node: TR Calling Convention, Next: Invoking a Reader from a TR, Prev: Defining a New Token Reader, Up: Token Readers 4.1.2 Token Reader Calling Convention ------------------------------------- A token reader's procedure is passed four arguments: * the character that was read and which triggered its call; in the S-exp example, this would be '('; * the port to read from; * the reader which performed this invocation, i.e., either an 'scm_reader_t' object (if the token reader is written in C) or a four-argument Scheme procedure (if the token reader is written in Scheme); * the top-level reader which yielded this invocation and which may be different from the previous argument in the case a token reader was made from a reader; the use of these two arguments will be detailed in the next section, *note Invoking a Reader from a TR::. It must return a Scheme object resulting from the interpretation of the characters read. It may as well raise an error if the input sequence is corrupt. Finally, it may return '*unspecified*', in which case the calling reader will not return and instead continue reading. This is particularly useful to define comment token readers: a TR that has just read a comment will obviously not have any sensible Scheme object to return, and a reader is not expected to return anything but a "real" Scheme object. A token reader for Scheme's ';' line comments may be defined as follows: (make-token-reader #\; read-a-line-and-return-unspecified) This behavior may, however, be overridden by passing 'make-token-reader' a third argument (called ESCAPE?): (make-token-reader #\; read-a-line-and-return-unspecified #t) A reader that includes this TR will return '*unspecified*' once a line comment has been read. This is particularly useful, for instance, when implementing '#!' block comments (*note SCSH block comments: (guile)Block Comments, for more information) as a TR attached to '#\!' within the '#\#' sub-reader (*note Defining a New Token Reader::). Finally, the procedure passed to 'make-token-reader' may be '#f', in which case the resulting TR will just have the effect of ignoring the characters it is associated to. For instance, handling white spaces may be done by defining a TR like this: (make-token-reader '(#\space #\newline #\tab) #f)  File: guile-reader.info, Node: Invoking a Reader from a TR, Next: Token Reader Library, Prev: TR Calling Convention, Up: Token Readers 4.1.3 Invoking a Reader from a Token Reader ------------------------------------------- As seen in section *Note Defining a New Token Reader::, token readers are systematically passed to readers when invoked. The reason why this may be useful may not be obvious at first sight. Consider an S-exp token reader. The TR itself doesn't have sufficient knowledge to read the objects that comprise an S-exp. So it needs to be able to call the reader that is being used to actually read those objects. The need for the TOP-LEVEL-READER argument passed to token readers may be illustrated looking at the implementation of the vector read syntax (*note (guile)Vector Syntax::). One may implement the vector reader as a token reader of the '#' sub-reader (*note Defining a New Token Reader::). The vector token reader may be implemented like this: (lambda (chr port reader top-level-reader) ;; At this point, `#' as already been read and CHR is `(', ;; so we can directly call the regular S-expression reader ;; and convert its result into a vector. (let ((sexp-read (token-reader-procedure (standard-token-reader 'sexp)))) (apply vector (sexp-read chr port reader)))) When this procedure is invoked, READER points to the '#' sub-reader.  File: guile-reader.info, Node: Token Reader Library, Next: Limitations, Prev: Invoking a Reader from a TR, Up: Token Readers 4.1.4 Token Reader Library -------------------------- Guile-Reader comes with a number of re-usable token readers. Together, they might be assembled to form a complete Scheme reader equivalent to that of Guile (*note Reader Library::). Or they can be used individually in any reader. The 'standard-token-reader' procedure takes a symbol that names a standard TR from the library and returns it (or '#f' if not found). Currently, the available TRs are: Token Reader Character Spec. Description --------------------------------------------------------------------------- 'boolean' 4 characters, This is a sharp token reader, i.e. '#\f'... '#\F' it reads an R5RS boolean ('#f' or '#F', '#t' or '#T') once a '#' character has been read. 'boolean-srfi-4' 3 characters, This is a sharp token reader, i.e. '#\t'... '#\F' it reads an R5RS boolean ('#t', '#T', '#F', but _not_ '#f') once a '#' character has been read. Compared to the 'boolean' token reader, this one is useful when SRFI-4 floating-point homogeneous vectors are to be used at the same time: the SRFI-4 TR will handle '#f' on its own (*note Overlapping Token Readers::). 'brace-free-number'13 characters, Return a number or a symbol, '#\-'... '#\9' considering curly braces as delimiters. 'brace-free-symbol-lower-case'from '#\a' toRead a symbol that starts with a '#\z' lower-case letter and return a symbol. This token reader recognizes braces as delimiters, unlike R5RS/R6RS. 'brace-free-symbol-misc-chars'17 characters,Read a symbol that starts with a '#\['... '#\$' non-alphanumeric character and return a symbol. This token reader recognizes braces as delimiters, unlike R5RS/R6RS. 'brace-free-symbol-upper-case'from '#\A' toRead a symbol that starts with an '#\Z' upper-case letter and return a symbol. This token reader recognizes braces as delimiters, unlike R5RS/R6RS. 'character' '#\\' This is a sharp token reader, i.e. it reads an R5RS character once a '#' character has been read. 'curly-brace-sexp' '#\{' Read an S-expression enclosed in square brackets. This is already permitted by a number of Scheme implementations and will soon be made compulsory by R6RS. 'guile-bit-vector' '#\*' This is a sharp token reader, i.e. it reads a bit vector following Guile's read syntax for bit vectors. See *note Guile's bit vectors: (guile)Bit Vectors, for details. 'guile-extended-symbol''#\{' This is a sharp token reader, i.e. it reads a symbol using Guile's extended symbol syntax assuming a '#' character was read. See *note Guile's extended read syntax for symbols: (guile)Symbol Read Syntax, for details. 'guile-number' 13 characters, Read a number following Guile's '#\-'... '#\9' fashion, that is, as in R5RS (*note R5RS' lexical structure: (r5rs)Lexical structure, for syntactic details). Because the syntaxes for numbers and symbols are closely tight in R5RS and Guile, this token reader may return either a number or a symbol. For instance, it will be invoked if the string '123.123.123' is passed to the reader but this will actually yield a symbol instead of a number (*note Overlapping Token Readers::). 'guile-symbol-lower-case'from '#\a' toRead a symbol that starts with a '#\z' lower-case letter in a case-sensitive fashion. 'guile-symbol-misc-chars'19 characters,Read a symbol that starts with a '#\['... '#\$' non-alphanumeric character in a case-sensitive fashion. Note that this token reader does _not_ consider square brackets as delimiters, as was the case with Guile 1.8 and earlier. 'guile-symbol-upper-case'from '#\A' toRead a symbol that starts with an '#\Z' upper-case letter in a case-sensitive fashion. 'keyword' '#\:' This token reader returns a keyword as found in Guile. It may be used either after a '#' character (to implement Guile's default keyword syntax, '#:kw') or within the top-level reader (to implement ':kw'-style keywords). It is worth noting that this token reader invokes its top-level in order to read the symbol subsequent to the ':' character. Therefore, it will adapt to the symbol delimiters currently in use (*note Token Delimiters::). 'number+radix' 12 characters, This is a sharp token reader, i.e. '#\b'... '#\E' it reads a number using the radix notation, like '#b01' for the binary notation, '#x1d' for the hexadecimal notation, etc., see *note Guile's number syntax: (guile)Number Syntax, for details. 'quote-quasiquote-unquote'3 characters,Read a quote, quasiquote, or '#\''... '#\,' unquote S-expression. 'r5rs-lower-case-number'13 characters,Return a number or a lower-case '#\-'... '#\9' symbol. 'r5rs-lower-case-symbol-lower-case'from '#\a' toRead a symbol that starts with a '#\z' lower-case letter and return a lower-case symbol, regardless of the case of the input. 'r5rs-lower-case-symbol-misc-chars'19 characters,Read a symbol that starts with a '#\['... '#\$' non-alphanumeric character and return a lower-case symbol, regardless of the case of the input. 'r5rs-lower-case-symbol-upper-case'from '#\A' toRead a symbol that starts with an '#\Z' upper-case letter and return a lower-case symbol, regardless of the case of the input. 'r5rs-upper-case-number'13 characters,Return a number or an upper-case '#\-'... '#\9' symbol. 'r6rs-number' 13 characters, Return a number or a symbol. This '#\-'... '#\9' token reader conforms to R6RS, i.e. it considers square brackets as delimiters. 'r6rs-symbol-lower-case'from '#\a' to Read a symbol that starts with a '#\z' lower-case letter and return a symbol. This token reader conforms with R6RS in that it is case-sensitive and recognizes square brackets as delimiters (*note Token Delimiters::). 'r6rs-symbol-misc-chars'17 characters,Read a symbol that starts with a '#\{'... '#\$' non-alphanumeric character and return a symbol. This token reader conforms with R6RS in that it is case-sensitive and recognizes square brackets as delimiters (*note Token Delimiters::). 'r6rs-symbol-upper-case'from '#\A' to Read a symbol that starts with an '#\Z' upper-case letter and return a symbol. This token reader conforms with R6RS in that it is case-sensitive and recognizes square brackets as delimiters (*note Token Delimiters::). 'r6rs-syntax-quote-quasiquote-unquote'3 characters,Read an R6RS-style syntax, '#\''... '#\,' quasisyntax, or unsyntax S-expression. 'scsh-block-comment''#\!' This is a sharp token reader, i.e. it reads a SCSH-style block comment (like '#! multi-line comment !#') and returns '*unspecified*', assuming a '#' character was read before. This token reader has its "escape" bit set, meaning that the reader that calls it will return '*unspecified*' to its parent reader. See also *note block comments: (guile)Block Comments, for details about SCSH block comments. 'semicolon-comment''#\;' Read an R5RS semicolon line-comment and return '*unspecified*'. Consequently, the calling reader will loop and ignore the comment. 'sexp' '#\(' Read a regular S-expression enclosed in parentheses. 'skribe-exp' '#\[' Read a Skribe markup expression. Skribe's expressions look like this: [Hello ,(bold [World])!] => ("Hello " (bold "World") "!") See the Skribe web site (http://www.inria.fr/mimosa/fp/Skribe) or the Skribilo web site (http://www.nongnu.org/skribilo/) for more details. 'square-bracket-sexp''#\[' Read an S-expression enclosed in square brackets. This is already permitted by a number of Scheme implementations and will soon be made compulsory by R6RS. 'srfi-4' 3 characters, This is a sharp token reader, i.e. '#\s'... '#\f' it reads an SRFI-4 homogenous numeric vector once a '#' character has been read. This token reader also handles the boolean values '#f'. 'srfi30-block-comment''#\|' This is a sharp token reader, i.e. it reads an SRFI-30 block comment (like '#| multi-line comment |#') and returns '*unspecified*', assuming a '#' character was read before. This token reader has its "escape" bit set. For more details about SRFI-30, see Nested Multi-line Comments (http://srfi.schemers.org/srfi-30/srfi-30.html). 'srfi62-sexp-comment''#\;' This is a sharp token reader, i.e. it reads an SRFI-62 comment S-expression (as in '(+ 2 #;(comment here) 2)') and returns '*unspecified*', assuming a '#' character was read before. This token reader has its "escape" bit set. For more details about SRFI-62, please see S-expression comments specifications (http://srfi.schemers.org/srfi-62/srfi-62.html). 'string' '#\"' Read an R5RS string. 'vector' '#\(' This is a sharp token reader, i.e. it reads an R5RS vector once a '#' character has been read. 'whitespace' from '#\soh' to This is a void token reader that '#\space' causes its calling reader to ignore (i.e. treat as whitespace) all ASCII characters ranging from 1 to 32. As can be inferred from the above two lists, reading character sequences starting with the '#' characters can easily be done by defining a sub-reader for that character. That reader can then be passed to 'make-token-reader' as the procedure attached to '#': (define sharp-reader (make-reader (map standard-token-reader '(boolean character number+radix keyword srfi-4 block-comment)))) (define top-level-reader (make-reader (list (make-token-reader #\# sharp-reader) ... ))) The procedures available to manipulate token readers are listed below: -- Scheme Procedure: token-reader-escape? tr -- C Function: scm_token_reader_escape_p (SCM tr) Return '#t' if token reader TR requires the readers that use it to return even if its return value is unspecified. -- Scheme Procedure: token-reader-specification tr -- C Function: scm_token_reader_spec (SCM tr) Return the specification, of token reader TR. -- Scheme Procedure: token-reader-procedure tr -- C Function: scm_token_reader_proc (SCM tr) Return the procedure attached to token reader TR. When '#f' is returned, the TR is a "fake" reader that does nothing. This is typically useful for whitespaces. -- Scheme Procedure: standard-token-reader name -- C Function: scm_standard_token_reader (SCM name) Lookup standard token reader named NAME (a symbol) and return it. If NAME is does not name a standard token reader, then an error is raised.  File: guile-reader.info, Node: Limitations, Prev: Token Reader Library, Up: Token Readers 4.1.5 Limitations ----------------- This section describes the main limitations and common pitfalls encountered when using Guile-Reader. * Menu: * Token Delimiters:: Dealing with token delimiters. * Overlapping Token Readers:: Token readers that syntactically overlap.  File: guile-reader.info, Node: Token Delimiters, Next: Overlapping Token Readers, Prev: Limitations, Up: Limitations 4.1.5.1 Token Delimiters ........................ As can be seen from the previous section, there exist, for instance, an surprisingly high number of symbol token readers. The reason for this is that different syntax variants define different "token delimiters". Token delimiters are characters that help the reader determine where tokens that require implicit termination do terminate. Quoting R5RS (*note R5RS' lexical structure: (r5rs)Lexical structure.): Tokens which require implicit termination (identifiers, numbers, characters, and dot) may be terminated by any , but not necessarily by anything else. R5RS defines token delimiters as one of the following: a whitespace, a parentheses, a quotation mark ('"') or a semi-colon (';') character. On the other hand, R6RS, which is to support the ability to use square brackets instead of parentheses for S-expressions, also considers square brackets as token delimiters. Likewise, if we were to support curly braces to enclose S-expressions, then curly braces would need to be considered as token delimiters too. For this reason, the token reader library comes with several symbol token readers: the 'guile-symbol-' family does not consider square brackets as delimiters while the 'r6rs-symbol-' family does, the 'brace-free-' TR family considers curly braces as delimiters but not square brackets, etc. Similarly, several variants of number TRs are available. This is due to the fact that number TRs may return symbols in corner cases like symbol names starting with a number. However, although keywords must also comply with the token delimiters rules, there is only one keyword TR (called 'keyword'). The reason for this is that this TR relies on the top-level reader's symbol reader to read the symbol that makes up the keyword being read. In the current design of Guile-Reader, this token delimiter issue creates a number of pitfalls when one is willing to change the current delimiters. In particular, one has to be very careful about using TRs that consistently assume the same token delimiters. A "real" lexer generator such as Danny Dubé's SILex avoids such issues because it allows the definition of tokens using regular expressions. However, its usage may be less trivial than that of Guile-Reader.  File: guile-reader.info, Node: Overlapping Token Readers, Prev: Token Delimiters, Up: Limitations 4.1.5.2 Overlapping Token Readers ................................. As can be seen from the descriptions of the standard token readers (*note Token Reader Library::), token readers sometimes "overlap", i.e., the set of input strings they match overlap. For instance, the 'boolean' token reader should match '#t', '#T', '#f' or '#F'. However, the 'srfi-4' token reader also needs to match floating-point numeric vectors such as '#f32(1.0 2.0 3.0)'. Similarly, strings like '1' are, logically, handled by the 'guile-number' (or similar) token reader; however, since a string like '1+' should be recognized as a _symbol_, rather than a number, it must then be passed to one of the symbol token readers. In those two cases, the input sets of those two token readers _overlap_. In order for the resulting reader to work as expected, the two overlapping token readers need to somehow _cooperate_. In the first example, this is achieved by having the 'srfi-4' TR read in strings starting with '#f' or '#F' and passing them to the 'boolean-srfi-4' TR if need be. In the second case, this is done by having number TRs (e.g., 'guile-number') explicitly check for non-digit characters and return a symbol instead of a number when a non-digit is encountered. It should be obvious from these two examples that this limitation impedes full separation of the various TRs. Fortunately, there are not so many cases where such overlapping occurs when implementing readers for R5RS-like syntaxes. The implementation of 'make-alternate-guile-reader' (*note Reader Library::) shows how such problems have been worked around. Lexer generators such as Flex (http://www.gnu.org/software/flex/), SILex (http://www.iro.umontreal.ca/~dube/) and Bigloo's RGC (*note Bigloo's RGC: (bigloo)Regular Parsing.) obviously do not have this problem: all possible "token" types are defined using regular expressions and the string-handling code (e.g., code that converts a string into a Scheme number) is only invoked once a full matching string has been found.  File: guile-reader.info, Node: Readers, Prev: Token Readers, Up: API Reference 4.2 Readers =========== Guile-Reader is about defining readers. Continuing to read this manual was definitely a good idea since we have finally reached the point where we will start talking about how to define new readers. * Menu: * Defining a New Reader:: * Reader Library:: * Compatibility and Confinement::  File: guile-reader.info, Node: Defining a New Reader, Next: Reader Library, Prev: Readers, Up: Readers 4.2.1 Defining a New Reader --------------------------- Roughly, a reader is no more than a loop which reads characters from a given port, and dispatches further interpretation to more specific procedures. Written in Scheme, it could resemble something like: (define (my-reader port) (let loop ((result *unspecified*)) (let ((the-char (getc port))) (case the-char ((#\() (my-sexp-token-reader the-char port my-reader))) ((#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9) (my-number-token-reader the-char port my-reader)) (else (error "unexpected character" the-char)))))) Using Guile-Reader, this is done simply by providing a list of token readers to the 'make-reader' procedure, as in the following example: (define my-reader (make-reader (list (make-token-reader #\( my-sexp-token-reader) (make-token-reader '(#\0 . #\9) my-number-token-reader)))) However, the procedure returned by 'make-reader' is different from the hand-written one above in that in takes two additional optional arguments which makes it look like this: (define (my-reader port faults-caller-handled? top-level-reader) (let loop ((the-char (getc port))) (case the-char ... (else (if (not faults-caller-handled?) (error "unexpected character" the-char) (ungetc the-char) ;; and return *unspecified* ))))) Therefore, by default, 'my-reader' will raise an error as soon as it reads a character that it does not know how to handle. However, if the caller passes '#t' as its 'faults-caller-handled?' argument, then 'my-reader' is expected to "unget" the faulty character and return '*unspecified*', thus allowing the caller to handle the situation. This is useful, for instance, in the S-exp token reader example: the S-exp token reader needs to call its calling reader in order to read the components between the opening and closing brackets; however, the calling reader may be unable to handle the '#\)' character so the S-exp token reader has to handle it by itself and needs to tell it to the reader. -- Scheme Procedure: %reader-standard-fault-handler chr port reader -- C Function: scm_reader_standard_fault_handler (SCM chr, SCM port, SCM reader) Throw a 'read-error' exception indicating that character CHR was read from PORT and could not be handled by READER. -- Scheme Procedure: make-reader token-readers [fault-handler-proc [flags...]] -- C Function: scm_make_reader (SCM token_readers, SCM fault_handler_proc, SCM flags) Create a reader made of the token readers listed in TOKEN_READERS. TOKEN_READERS should be a list of token readers returned by 'make-token-reader' or 'standard-token-reader' for instance. The FAULT_HANDLER_PROC argument is optional and may be a three-argument procedure to call when an unexpected character is read. When FAULT_HANDLER_PROC is invoked, it is passed the faulty character, input port, and reader; its return value, if any, is then returned by the reader. If FAULT_HANDLER_PROC is not specified, then '%reader-standard-fault-handler' is used. FLAGS is a rest argument which may contain a list of symbols representing reader compilation flags. Currently, the flags that may be passed to 'make-reader' are the following: * 'reader/record-positions' will yield a reader that records the position of the expression read, which is mostly useful for debugging purposes; this information may then be accessed via source properties (*note source properties: (guile)Procedure Properties.). * 'reader/lower-case' will have the yielded reader convert to lower-case all the letters that it reads; note that this is not sufficient to implement symbol case-insensitivity as shown in *note (guile)Reader options::. For this, the token reader(s) that read symbols must also convert all subsequent characters to lower-case. * 'reader/upper-case' will have the yielded reader convert to upper-case all the letters that it reads; again, that is not sufficient to implement case-insensitivity. * 'reader/debug' causes the generated reader to produce debugging output.  File: guile-reader.info, Node: Reader Library, Next: Compatibility and Confinement, Prev: Defining a New Reader, Up: Readers 4.2.2 Reader Library -------------------- The '(system reader)' module exports the 'default-reader' procedure which returns a reader equivalent to Guile's built-in default reader made of re-usable token readers written in C (*note Token Reader Library::). -- Scheme Procedure: default-sharp-reader-token-readers -- C Function: scm_default_sharp_reader_token_readers (void) Return the list of token readers that comprise Guile's default reader for the '#' character. -- Scheme Procedure: default-reader-token-readers -- C Function: scm_default_reader_token_readers (void) Return the list of token readers that comprise Guile's default reader. -- Scheme Procedure: default-sharp-reader -- C Function: scm_default_sharp_reader (void) Returns Guile's default reader for the '#' character. Additionally, the '(system reader library)' module exports a number of procedures that ease the re-use of readers. -- Scheme Procedure: make-guile-reader [fault-handler [flags...]] -- C Function: scm_make_guile_reader (SCM fault_handler, SCM flags) Make and return a new reader compatible with Guile 2.0's 'read', with its default settings. This function calls 'make-reader' with FLAGS. Note that the sharp reader used by the returned reader is also instantiated using FLAGS. The value of FAULT-HANDLER defaults to '%reader-standard-fault-handler'. -- Scheme Procedure: make-alternate-guile-reader Return a newly created Guile reader with options OPTIONS (a list of symbols, as for 'alternate-guile-reader-token-readers'), with fault handler FAULT-HANDLER and flags FLAGS. The FAULT-HANDLER and FLAGS arguments are the same as those passed to 'make-reader'. By default, FAULT-HANDLER is set to %READER-STANDARD-FAULT-HANDLER. -- Scheme Procedure: alternate-guile-reader-token-readers Given OPTIONS, a list of symbols describing reader options relative to the reader returned by '(default-reader)', return two lists of token readers: one for use as a sharp reader and the other for use as a top-level reader. Currently, the options supported are the following: 'no-sharp-keywords' Remove support for '#:kw'-style keywords. 'dsssl-keywords' Add support for DSSSL-style keywords, like '#!kw'. This option also has the same effect as 'no-scsh-block-comments'. 'colon-keywords' Add support for ':kw'-style keywords. This is equivalent to '(read-set! keywords 'prefix)'. 'no-scsh-block-comments' Disable SCSH-style block comments (see *note SCSH block comments: (guile)Block Comments, for details). 'srfi30-block-comments' Add support for SRFI-30 block comments, like: (+ 2 #| This is an #| SRFI-30 |# comment |# 2) 'srfi62-sexp-comments' Add support for SRFI-62 S-expression comments, like: (+ 2 #;(a comment) 2) 'case-insensitive' Read symbols in a case-insensitive way. 'square-bracket-sexps' 'square-brackets' Allow for square brackets around S-expressions. -- Scheme Procedure: read-options->extended-reader-options Read READ-OPTS, a list representing read options following Guile's built-in representation (see *note (guile)Scheme Read::, for details), and return a list of symbols represented "extended reader options" understood by 'make-alternate-guile-reader' et al.  File: guile-reader.info, Node: Compatibility and Confinement, Prev: Reader Library, Up: Readers 4.2.3 Compatibility and Confinement ----------------------------------- Guile's core read subsystem provides an interface to customize its reader, namely via the 'read-options' (*note (guile)Scheme Read::) and 'read-hash-extend' (*note 'read-hash-extend': (guile)Reader Extensions.) procedures. The main problem with this approach is that changing the reader's options using these procedures has a global effect since there is only one instance of 'read'. Changing the behavior of a single function at the scale of the whole is not very "schemey" and can be quite harmful. Suppose a module relies on case-insensitivity while another relies on case-sensitivity. If one tries to use both modules at the same time, chances are that at least one of them will not work as expected. Risks of conflicts are even higher when 'read-hash-extend' is used: imagine a module that uses DSSSL-style keywords, while another needs SCSH-style block comments. In '(system reader confinement)', guile-reader offers an implementation of 'read-option-interface' and 'read-hash-extend' that allows to _confine_ such settings on a per-module basis. In order to enable reader confinement, one just has to do this: (use-modules (system reader confinement)) Note that this must be done before the suspicious modules are loaded, that is, typically when your program starts. This will redefine 'read-options-interface' and 'read-hash-extend' so that any future modification performed via Guile's built-in reader option interface will be confined to the calling module. Starting from Guile 1.8.0, 'current-reader' is a core binding bound to a fluid whose value should be either '#f' or a reader (i.e., a 'read'-like procedure). The value of this fluid dictates the reader that is to be used by 'primitive-load' and its value can be changed dynamically (*note 'current-reader': (guile)Loading.). The confined variants of 'read-options-interface' and 'read-hash-extend' rely on this feature to make reader customizations local to the file being loaded. This way, invocations of these functions from within a file being loaded by 'primitive-load' take effect immediately.  File: guile-reader.info, Node: Internals, Next: Concept Index, Prev: API Reference, Up: Top 5 Internals *********** In order to not have to trade too much performance for flexibility, Guile-Reader dynamically compiles code for the readers defined using GNU _lightning_ (*note Introduction to GNU _lightning_: (lightning)Overview.). As of version 1.2c, GNU _lightning_ can generate code for the PowerPC, SPARC, and IA32 architectures. For other platforms, Guile-Reader provides an alternative (slower) C implementation that does not depend on it. Using the _lightning_-generated readers typically provides a 5% performance improvement over the static C implementation. However, note that _lightning_ 2.x is not supported yet. Re-using token readers written in C, as explained in *Note Token Reader Library::, does not imply any additional cost: the underlying C function will be called directly by the reader, without having to go through any marshalling/unmarshalling stage. Additionally, on the C side, token readers may be initialized _statically_ (except, obviously, token readers made out of a dynamically-compiled reader). Making good use of it can improve the startup time of a program. For example, 'make-guile-reader' (*note Reader Library::) is implemented in C and it uses statically initialized arrays of token readers. It still needs to invoke 'scm_c_make_reader ()', but at least, token readers themselves are "ready to use". Scanners as generated by Flex or similar tools should theoretically be able to provide better performance because the input reading and pattern matching loop is self-contained, may fit in cache, and only has to perform function calls once a pattern has been fully recognized.  File: guile-reader.info, Node: Concept Index, Next: Function Index, Prev: Internals, Up: Top Concept Index ************* [index] * Menu: * calling convention: TR Calling Convention. (line 6) * character specification: Defining a New Token Reader. (line 6) * confinement: Compatibility and Confinement. (line 22) * lexer: Introduction. (line 36) * lexer <1>: Overlapping Token Readers. (line 33) * pitfall: Token Delimiters. (line 38) * R5RS: Token Delimiters. (line 17) * R6RS: Token Delimiters. (line 17) * read table: Introduction. (line 48) * reader: Overview. (line 15) * reader <1>: Defining a New Reader. (line 6) * reader confinement: Compatibility and Confinement. (line 22) * reader library: Overview. (line 6) * reader library <1>: Reader Library. (line 6) * SCSH block comments: TR Calling Convention. (line 37) * SILex: Introduction. (line 36) * SRFI-30: Token Reader Library. (line 245) * SRFI-62: Token Reader Library. (line 256) * token delimiters: Token Delimiters. (line 6) * token reader: Overview. (line 15) * token reader <1>: Defining a New Token Reader. (line 6) * token reader library: Token Reader Library. (line 6) * top-level reader: TR Calling Convention. (line 14)  File: guile-reader.info, Node: Function Index, Prev: Concept Index, Up: Top Function Index ************** [index] * Menu: * %reader-standard-fault-handler: Defining a New Reader. (line 55) * alternate-guile-reader-token-readers: Reader Library. (line 43) * current-reader: Compatibility and Confinement. (line 35) * default-reader-token-readers: Reader Library. (line 16) * default-sharp-reader: Reader Library. (line 21) * default-sharp-reader-token-readers: Reader Library. (line 11) * make-alternate-guile-reader: Quick Start. (line 20) * make-alternate-guile-reader <1>: Reader Library. (line 36) * make-guile-reader: Reader Library. (line 28) * make-guile-reader <1>: Internals. (line 22) * make-reader: Defining a New Reader. (line 20) * make-reader <1>: Defining a New Reader. (line 61) * make-token-reader: Defining a New Token Reader. (line 6) * make-token-reader <1>: Defining a New Token Reader. (line 27) * read-disable: Compatibility and Confinement. (line 6) * read-enable: Compatibility and Confinement. (line 6) * read-hash-extend: Compatibility and Confinement. (line 6) * read-options: Compatibility and Confinement. (line 6) * read-options->extended-reader-options: Reader Library. (line 73) * read-options-interface: Compatibility and Confinement. (line 22) * read-set!: Compatibility and Confinement. (line 6) * scm_default_reader_token_readers: Reader Library. (line 17) * scm_default_sharp_reader: Reader Library. (line 22) * scm_default_sharp_reader_token_readers: Reader Library. (line 12) * scm_make_guile_reader: Reader Library. (line 29) * scm_make_reader: Defining a New Reader. (line 63) * scm_make_token_reader: Defining a New Token Reader. (line 28) * scm_reader_standard_fault_handler: Defining a New Reader. (line 56) * scm_standard_token_reader: Token Reader Library. (line 317) * scm_token_reader_escape_p: Token Reader Library. (line 302) * scm_token_reader_proc: Token Reader Library. (line 311) * scm_token_reader_spec: Token Reader Library. (line 307) * standard-token-reader: Token Reader Library. (line 316) * token-reader-escape?: Token Reader Library. (line 301) * token-reader-procedure: Token Reader Library. (line 310) * token-reader-specification: Token Reader Library. (line 306)  Tag Table: Node: Top1032 Node: Introduction1887 Node: Overview4970 Node: Quick Start7402 Node: API Reference11368 Node: Token Readers11967 Node: Defining a New Token Reader12530 Node: TR Calling Convention14247 Node: Invoking a Reader from a TR16651 Node: Token Reader Library18124 Node: Limitations37184 Node: Token Delimiters37566 Node: Overlapping Token Readers40011 Node: Readers42162 Node: Defining a New Reader42562 Node: Reader Library47122 Node: Compatibility and Confinement50748 Node: Internals53024 Node: Concept Index54767 Node: Function Index57394  End Tag Table  Local Variables: coding: utf-8 End: