diff options
author | Andy Wingo <wingo@pobox.com> | 2011-01-21 08:57:39 +0100 |
---|---|---|
committer | Andy Wingo <wingo@pobox.com> | 2011-01-21 09:24:32 +0100 |
commit | 684d664e39dad02f7a44f1c70a753a02d4d1856c (patch) | |
tree | bc4641a6d5d78b431a7ceb1042d1f7bca6549779 | |
parent | b04f841d5fa6d3448bb8f976bb27ee3e9a06f83c (diff) | |
download | guile-684d664e39dad02f7a44f1c70a753a02d4d1856c.tar.gz |
implement r6rs hungry escaped EOL
* libguile/private-options.h (SCM_HUNGRY_EOL_ESCAPES_P): New private
option.
* libguile/read.c: Define SCM_HUNGRY_EOL_ESCAPES_P, defaulting to #f.
(skip_intraline_whitespace): New helper.
(scm_read_string): If SCM_HUNGRY_EOL_ESCAPES_P,
skip_intraline_whitespace after an escaped EOL.
* test-suite/tests/reader.test ("read-options"): Add test.
-rw-r--r-- | libguile/private-options.h | 3 | ||||
-rw-r--r-- | libguile/read.c | 21 | ||||
-rw-r--r-- | test-suite/tests/reader.test | 22 |
3 files changed, 42 insertions, 4 deletions
diff --git a/libguile/private-options.h b/libguile/private-options.h index 2c27214ea..c095688c3 100644 --- a/libguile/private-options.h +++ b/libguile/private-options.h @@ -4,7 +4,7 @@ * We put this in a private header, since layout of data structures * is an implementation detail that we want to hide. * - * Copyright (C) 2007, 2009, 2010 Free Software Foundation, Inc. + * Copyright (C) 2007, 2009, 2010, 2011 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -63,6 +63,7 @@ SCM_INTERNAL scm_t_option scm_read_opts[]; #define SCM_KEYWORD_STYLE scm_read_opts[3].val #define SCM_R6RS_ESCAPES_P scm_read_opts[4].val #define SCM_SQUARE_BRACKETS_P scm_read_opts[5].val +#define SCM_HUNGRY_EOL_ESCAPES_P scm_read_opts[6].val #define SCM_N_READ_OPTIONS 6 diff --git a/libguile/read.c b/libguile/read.c index 54384fa5c..87eecfeb0 100644 --- a/libguile/read.c +++ b/libguile/read.c @@ -29,6 +29,7 @@ #include <string.h> #include <unistd.h> #include <unicase.h> +#include <unictype.h> #include "libguile/_scm.h" #include "libguile/bytevectors.h" @@ -75,6 +76,8 @@ scm_t_option scm_read_opts[] = { "Use R6RS variable-length character and string hex escapes."}, { SCM_OPTION_BOOLEAN, "square-brackets", 1, "Treat `[' and `]' as parentheses, for R6RS compatibility."}, + { SCM_OPTION_BOOLEAN, "hungry-eol-escapes", 0, + "In strings, consume leading whitespace after an escaped end-of-line."}, { 0, }, }; @@ -486,6 +489,22 @@ scm_read_sexp (scm_t_wchar chr, SCM port) } \ } while (0) +static void +skip_intraline_whitespace (SCM port) +{ + scm_t_wchar c; + + do + { + c = scm_getc (port); + if (c == EOF) + return; + } + while (c == '\t' || uc_is_general_category (c, UC_SPACE_SEPARATOR)); + + scm_ungetc (c, port); +} + static SCM scm_read_string (int chr, SCM port) #define FUNC_NAME "scm_lreadr" @@ -524,6 +543,8 @@ scm_read_string (int chr, SCM port) case '\\': break; case '\n': + if (SCM_HUNGRY_EOL_ESCAPES_P) + skip_intraline_whitespace (port); continue; case '0': c = '\0'; diff --git a/test-suite/tests/reader.test b/test-suite/tests/reader.test index 6f0ed1d59..13c852665 100644 --- a/test-suite/tests/reader.test +++ b/test-suite/tests/reader.test @@ -1,6 +1,6 @@ ;;;; reader.test --- Reader test. -*- coding: iso-8859-1; mode: scheme -*- ;;;; -;;;; Copyright (C) 1999, 2001, 2002, 2003, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. +;;;; Copyright (C) 1999, 2001, 2002, 2003, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. ;;;; Jim Blandy <jimb@red-bean.com> ;;;; ;;;; This library is free software; you can redistribute it and/or @@ -364,8 +364,24 @@ (with-output-to-string (lambda () (write (integer->char #x80)))))) - "#\\x80")))) - + "#\\x80"))) + + (with-test-prefix "hungry escapes" + (pass-if "default not hungry" + ;; Assume default setting of not hungry. + (equal? (with-input-from-string "\"foo\\\n bar\"" + read) + "foo bar")) + (pass-if "hungry" + (dynamic-wind + (lambda () + (read-enable 'hungry-eol-escapes)) + (lambda () + (equal? (with-input-from-string "\"foo\\\n bar\"" + read) + "foobar")) + (lambda () + (read-disable 'hungry-eol-escapes)))))) (with-test-prefix "#;" |