1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-11 22:31:12 +02:00

Add support for SRFI-88-like postfix keyword read syntax.

This commit is contained in:
Ludovic Courtès 2008-04-15 19:52:43 +02:00
parent cac3960028
commit ef4cbc08c8
8 changed files with 81 additions and 12 deletions

4
NEWS
View file

@ -46,6 +46,10 @@ The new repository can be accessed using
"git-clone git://git.sv.gnu.org/guile.git", or can be browsed on-line at
http://git.sv.gnu.org/gitweb/?p=guile.git . See `README' for details.
* New features (see the manual for details)
** New `postfix' read option, for SRFI-88 keyword syntax
* Bugs fixed
** `scm_add_slot ()' no longer segfaults (fixes bug #22369)

View file

@ -1,3 +1,10 @@
2008-04-15 Ludovic Courtès <ludo@gnu.org>
* api-data.texi (Keywords): Mention postfix syntax.
(Keyword Read Syntax): Document `postfix' read option.
* api-options.texi (Reader options): Update examples.
(Examples of option use): Likewise.
2008-03-28 Neil Jerram <neil@ossau.uklinux.net>
* libguile-concepts.texi (Multi-Threading): Fix typo.

View file

@ -4901,7 +4901,7 @@ makes them easy to type.
Guile's keyword support conforms to R5RS, and adds a (switchable) read
syntax extension to permit keywords to begin with @code{:} as well as
@code{#:}.
@code{#:}, or to end with @code{:}.
@menu
* Why Use Keywords?:: Motivation for keyword usage.
@ -5046,9 +5046,17 @@ If the @code{keyword} read option is set to @code{'prefix}, Guile also
recognizes the alternative read syntax @code{:NAME}. Otherwise, tokens
of the form @code{:NAME} are read as symbols, as required by R5RS.
@cindex SRFI-88 keyword syntax
If the @code{keyword} read option is set to @code{'postfix}, Guile
recognizes the @uref{http://srfi.schemers.org/srfi-88/srfi-88.html,
SRFI-88 read syntax} @code{NAME:}. Otherwise, tokens of this form are
read as symbols.
To enable and disable the alternative non-R5RS keyword syntax, you use
the @code{read-set!} procedure documented in @ref{User level options
interfaces} and @ref{Reader options}.
interfaces} and @ref{Reader options}. Note that the @code{prefix} and
@code{postfix} syntax are mutually exclusive.
@smalllisp
(read-set! keywords 'prefix)
@ -5061,6 +5069,16 @@ interfaces} and @ref{Reader options}.
@result{}
#:type
(read-set! keywords 'postfix)
type:
@result{}
#:type
:type
@result{}
:type
(read-set! keywords #f)
#:type

View file

@ -1,6 +1,6 @@
@c -*-texinfo-*-
@c This is part of the GNU Guile Reference Manual.
@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2005, 2006
@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2008
@c Free Software Foundation, Inc.
@c See the file guile.texi for copying conditions.
@ -491,7 +491,7 @@ Here is the list of reader options generated by typing
values.
@smalllisp
keywords #f Style of keyword recognition: #f or 'prefix
keywords #f Style of keyword recognition: #f, 'prefix or 'postfix
case-insensitive no Convert symbols to lower case.
positions yes Record positions of source code expressions.
copy no Copy source code expressions.
@ -729,7 +729,7 @@ ABORT: (misc-error)
Type "(backtrace)" to get more information.
guile> (read-options 'help)
keywords #f Style of keyword recognition: #f or 'prefix
keywords #f Style of keyword recognition: #f, 'prefix or 'postfix
case-insensitive no Convert symbols to lower case.
positions yes Record positions of source code expressions.
copy no Copy source code expressions.

View file

@ -1,3 +1,10 @@
2008-04-15 Ludovic Courtès <ludo@gnu.org>
Julian Graham <joolean@gmail.com>
* read.c (scm_keyword_postfix): New.
(scm_read_opts): Update docstring for `keywords'.
(scm_read_mixed_case_symbol): Add support for postfix keywords.
2008-04-13 Ludovic Courtès <ludo@gnu.org>
* inline.h (SCM_C_USE_EXTERN_INLINE): New macro. Use it to make

View file

@ -53,6 +53,7 @@
SCM_GLOBAL_SYMBOL (scm_sym_dot, ".");
SCM_SYMBOL (scm_keyword_prefix, "prefix");
SCM_SYMBOL (scm_keyword_postfix, "postfix");
scm_t_option scm_read_opts[] = {
{ SCM_OPTION_BOOLEAN, "copy", 0,
@ -62,7 +63,7 @@ scm_t_option scm_read_opts[] = {
{ SCM_OPTION_BOOLEAN, "case-insensitive", 0,
"Convert symbols to lower case."},
{ SCM_OPTION_SCM, "keywords", SCM_UNPACK (SCM_BOOL_F),
"Style of keyword recognition: #f or 'prefix."},
"Style of keyword recognition: #f, 'prefix or 'postfix."},
#if SCM_ENABLE_ELISP
{ SCM_OPTION_BOOLEAN, "elisp-vectors", 0,
"Support Elisp vector syntax, namely `[...]'."},
@ -531,15 +532,19 @@ static SCM
scm_read_mixed_case_symbol (int chr, SCM port)
{
SCM result, str = SCM_EOL;
int overflow = 0;
int overflow = 0, ends_with_colon = 0;
char buffer[READER_BUFFER_SIZE];
size_t read = 0;
int postfix = scm_is_eq (SCM_PACK (SCM_KEYWORD_STYLE), scm_keyword_postfix);
scm_ungetc (chr, port);
do
{
overflow = read_token (port, buffer, sizeof (buffer), &read);
if (read > 0)
ends_with_colon = (buffer[read - 1] == ':');
if ((overflow) || (scm_is_pair (str)))
str = scm_cons (scm_from_locale_stringn (buffer, read), str);
}
@ -549,12 +554,21 @@ scm_read_mixed_case_symbol (int chr, SCM port)
{
str = scm_string_concatenate (scm_reverse_x (str, SCM_EOL));
result = scm_string_to_symbol (str);
/* Per SRFI-88, `:' alone is an identifier, not a keyword. */
if (postfix && ends_with_colon && (scm_c_string_length (result) > 1))
result = scm_symbol_to_keyword (result);
}
else
/* For symbols smaller than `sizeof (buffer)', we don't need to recur to
Scheme strings. Therefore, we only create one Scheme object (a
symbol) per symbol read. */
result = scm_from_locale_symboln (buffer, read);
{
/* For symbols smaller than `sizeof (buffer)', we don't need to recur
to Scheme strings. Therefore, we only create one Scheme object (a
symbol) per symbol read. */
if (postfix && ends_with_colon && (read > 1))
result = scm_from_locale_keywordn (buffer, read - 1);
else
result = scm_from_locale_symboln (buffer, read);
}
return result;
}

View file

@ -1,3 +1,8 @@
2008-04-15 Ludovic Courtès <ludo@gnu.org>
* tests/reader.test (read-options)[prefix non-keywords, postfix
keywords, `:' is not a postfix keyword (per SRFI-88)]: New tests.
2008-04-13 Ludovic Courtès <ludo@gnu.org>
* tests/goops.test (defining classes)[interaction with

View file

@ -1,6 +1,6 @@
;;;; reader.test --- Exercise the reader. -*- Scheme -*-
;;;;
;;;; Copyright (C) 1999, 2001, 2002, 2003, 2007 Free Software Foundation, Inc.
;;;; Copyright (C) 1999, 2001, 2002, 2003, 2007, 2008 Free Software Foundation, Inc.
;;;; Jim Blandy <jimb@red-bean.com>
;;;;
;;;; This library is free software; you can redistribute it and/or
@ -149,6 +149,20 @@
(with-read-options '(keywords prefix case-insensitive)
(lambda ()
(read-string ":KeyWord")))))
(pass-if "prefix non-keywords"
(symbol? (with-read-options '(keywords prefix)
(lambda ()
(read-string "srfi88-keyword:")))))
(pass-if "postfix keywords"
(eq? #:keyword
(with-read-options '(keywords postfix)
(lambda ()
(read-string "keyword:")))))
(pass-if "`:' is not a postfix keyword (per SRFI-88)"
(eq? ':
(with-read-options '(keywords postfix)
(lambda ()
(read-string ":")))))
(pass-if "no positions"
(let ((sexp (with-read-options '()
(lambda ()