Arto,

How about this patch, which extends your syntax to allow arbitrary delimiters?

#;6> ,x #r/\/path\/to\/file:i
(regexp "/path/to/file" #t #f #f)

#;6> ,x #r:/path/to/file:i
(regexp "/path/to/file" #t #f #f)

#;6> ,x #r{/path/to/file}i
(regexp "/path/to/file" #t #f #f)

#;7> (string-split-fields #r:[^/]+: "/path/to/file")
("path" "to" "file")

#;10> (string-match #r/\/path\/to\/file/ "/path/to/file")
("/path/to/file")

#;8> (string-match #r:/path/(to)/file: "/path/to/file")
("/path/to/file" "to")

The drawback over Perl is that nestable delimiters don't nest, so
#r(hi(bye)) is not valid; you have to escape the parentheses with
backslash, or use a different delimiter if you want grouping.

On 6/27/07, Arto Bendiken <[EMAIL PROTECTED]> wrote:
First, the `regex-literals' egg [2] provides precompiled regular
expression literals of the
form `#/[a-z0-9]+/i', the Perl-like #/.../[ixu] syntax sure to be
instantly familiar to most everyone:
Index: regex-literals.scm
===================================================================
--- regex-literals.scm  (revision 4721)
+++ regex-literals.scm  (working copy)
@@ -26,9 +26,17 @@
   (declare
     (export read-regex-literal) ) )
 
-;;;; Exported procedures
+;;;; Internal procedures
 
-(define (read-regex-literal #!optional (port (current-output-port)))
+(define (delimiter c)
+  (let ((delimiters '((#\{ . #\})
+                      (#\( . #\))
+                      (#\[ . #\])
+                      (#\< . #\>)) ))
+    (cond ((assq c delimiters) => cdr)
+          (else c))))
+
+(define (read-regex-literal/delim delim #!optional (port 
(current-output-port)))
   (define (read-option)
     (let ((char (peek-char port)))
       (cond ((eq? char #\i) (read-char port) 'caseless)
@@ -37,7 +45,7 @@
             (else #f) ) ) )
   (let loop ((buffer '()))
     (let ((char (read-char port)))
-      (cond ((char=? char #\/)
+      (cond ((char=? char delim)
              (let ((options (list (read-option) (read-option) (read-option))))
                `(regexp ,(list->string (reverse buffer))
                         ,(not (not (memq 'caseless options)))
@@ -48,9 +56,18 @@
             (else
              (loop (cons char buffer)) ) ) ) ) )
 
+;;;; Exported procedures
+
+(define (read-regex-literal #!optional (port (current-output-port)))
+  (read-regex-literal/delim #\/ port))
+
 ;;;; Initialization
 
 (define-inline (init-regex-literals!)
-  (set-sharp-read-syntax! #\/ read-regex-literal) )
+  (set-sharp-read-syntax! #\/ read-regex-literal)
+  (set-sharp-read-syntax! #\r
+                          (lambda (port)
+                            (let ((c (read-char port)))
+                              (read-regex-literal/delim (delimiter c) port)))))
 
 (init-regex-literals!)
_______________________________________________
Chicken-users mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/chicken-users

Reply via email to