unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* [RFC PATCH 0/2] Strings interpolation
@ 2022-12-10 22:17 Olivier Dion
  2022-12-10 22:17 ` [RFC PATCH 1/2] Add reader extension for interpolated strings Olivier Dion
  2022-12-10 22:17 ` [RFC PATCH 2/2] Add tests for strings interpolation reader extension Olivier Dion
  0 siblings, 2 replies; 3+ messages in thread
From: Olivier Dion @ 2022-12-10 22:17 UTC (permalink / raw)
  To: guile-user; +Cc: rekado, Olivier Dion

Hi Guilers,

I've added a hash reader extension in ice-9 boot to do strings
interpolation.

One can interpolate expressions in a string by using the `@' character
before the expressions.  If that characters is doubled, then it acts as
an escaping to insert itself without evaluation.

The interpolation is done by extracting the expressions and putting them
in a call to `format'.  Expressions are all formatted with `~a', which
is perhaps a little bit limiting here.

As an optimization, if there's no expression to interpolate, then no call
to `format' is done and a string is returned instead.

Example of interpolations:

#"foo" -> "foo"

#"9^2=@(* 9 9)" -> (format #f "9^2=~a" (* 9 9))

#"one=@1" -> (format #f "one=~a" 1)

#"bar@'buz" -> (format #f "bar=~a" (quote buz))

#"@me@@gnu.org" -> (format #f "~a@gnu.org" me)

#"me@@gnu.org" -> "me@gnu.org"

#"@@no-suffix@" -> "@weird"

#"@@suffix@@" -> "@weird@"

Olivier dion (2):
  Add reader extension for interpolated strings.
  Add tests for strings interpolation reader extension.

 module/ice-9/boot-9.scm      | 37 ++++++++++++++++++++++++++++++++++++
 test-suite/tests/reader.test | 11 ++++++++++-
 2 files changed, 47 insertions(+), 1 deletion(-)

-- 
2.38.1




^ permalink raw reply	[flat|nested] 3+ messages in thread

* [RFC PATCH 1/2] Add reader extension for interpolated strings.
  2022-12-10 22:17 [RFC PATCH 0/2] Strings interpolation Olivier Dion
@ 2022-12-10 22:17 ` Olivier Dion
  2022-12-10 22:17 ` [RFC PATCH 2/2] Add tests for strings interpolation reader extension Olivier Dion
  1 sibling, 0 replies; 3+ messages in thread
From: Olivier Dion @ 2022-12-10 22:17 UTC (permalink / raw)
  To: guile-user; +Cc: rekado, Olivier dion

From: Olivier dion <olivier-dion@proton.me>

Interpolate strings of the form #"The string @(eval this)".

When no interpolation is required, simply return the raw string.  For
example, #"foo" will return "foo".

When interpolation is required, return an expression that format the
string with the embedded expressions withing it.  For example
#"1+1=@(+ 1 1)" will return (format #f "1+1=~a" (+ 1 1)).

* module/ice-9/boot-9.scm: Extend read hash for strings interpolation.
---
 module/ice-9/boot-9.scm | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/module/ice-9/boot-9.scm b/module/ice-9/boot-9.scm
index a46145ed5..5c0f2eef2 100644
--- a/module/ice-9/boot-9.scm
+++ b/module/ice-9/boot-9.scm
@@ -2254,6 +2254,43 @@ name extensions listed in %load-extensions."
                         (error
                          "#. read expansion found and read-eval? is #f."))))
 
+
+(letrec ((interpolate
+          (lambda (port chars exps)
+            (let ((char (read-char port)))
+              (cond
+               ((eof-object? char)
+                (values
+                 (reverse-list->string chars)
+                 (reverse exps)))
+               ((char=? char #\@)
+                (let ((ahead (peek-char port)))
+                  (cond
+                   ((eof-object? ahead)
+                    (interpolate port chars exps))
+                   ((char=? ahead #\@)
+                    (read-char port)
+                    (interpolate port
+                                 (cons #\@ chars)
+                                 exps))
+                   (else
+                    (interpolate port
+                                 (cons #\a (cons #\~ chars))
+                                 (cons (read port) exps))))))
+               (else
+                (interpolate port (cons char chars) exps)))))))
+  (read-hash-extend #\"
+                    (lambda (char port)
+                      (unread-char char port)
+                      (call-with-values
+                          (lambda () (call-with-input-string (read port)
+                                  (lambda (port)
+                                    (interpolate port '() '()))))
+                        (lambda (fmt args)
+                          (if (null? args)
+                              fmt
+                              `(format #f ,fmt ,@args)))))))
+
 \f
 
 ;;; {Low Level Modules}
-- 
2.38.1




^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [RFC PATCH 2/2] Add tests for strings interpolation reader extension.
  2022-12-10 22:17 [RFC PATCH 0/2] Strings interpolation Olivier Dion
  2022-12-10 22:17 ` [RFC PATCH 1/2] Add reader extension for interpolated strings Olivier Dion
@ 2022-12-10 22:17 ` Olivier Dion
  1 sibling, 0 replies; 3+ messages in thread
From: Olivier Dion @ 2022-12-10 22:17 UTC (permalink / raw)
  To: guile-user; +Cc: rekado, Olivier dion

From: Olivier dion <olivier-dion@proton.me>

* test-suite/tests/reader.test: Add the tests.
---
 test-suite/tests/reader.test | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/test-suite/tests/reader.test b/test-suite/tests/reader.test
index 27daf6106..fb31575dd 100644
--- a/test-suite/tests/reader.test
+++ b/test-suite/tests/reader.test
@@ -200,7 +200,16 @@
   (pass-if-equal '(#f) (read-string "(#FALSE)"))
   (pass-if-equal '(#f) (read-string "(#FaLsE)"))
 
-  (pass-if (eof-object? (read-string "#!!#"))))
+  (pass-if (eof-object? (read-string "#!!#")))
+
+  (pass-if-equal "raw" (read-string "#\"raw\""))
+  (pass-if-equal "@raw" (read-string "#\"@@raw\""))
+  (pass-if-equal "@raw" (read-string "#\"@@raw@\""))
+  (pass-if-equal "@raw@" (read-string "#\"@@raw@@\""))
+  (pass-if-equal '(format #f "true=~a" (or #f #t))
+      (read-string "#\"true=@(or #f #t)\""))
+  (pass-if-equal '(format #f "@@~a" '@)
+      (read-string "#\"@@@@@'@\"")))
 
 
 \f
-- 
2.38.1




^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-12-10 22:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-10 22:17 [RFC PATCH 0/2] Strings interpolation Olivier Dion
2022-12-10 22:17 ` [RFC PATCH 1/2] Add reader extension for interpolated strings Olivier Dion
2022-12-10 22:17 ` [RFC PATCH 2/2] Add tests for strings interpolation reader extension Olivier Dion

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).