guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: regex-case


From: Matt Wette
Subject: Re: regex-case
Date: Mon, 08 Feb 2016 08:04:55 -0800



Matt Wette



On Feb 8, 2016, at 7:40 AM, Stefan Israelsson Tampe <address@hidden> wrote:
---------- Forwarded message ----------
From: Stefan Israelsson Tampe <address@hidden>
Date: Mon, Feb 8, 2016 at 4:31 PM
Subject: Re: regex-case
To: Ludovic Court├Ęs <address@hidden>


An interesting question is what can be done by ice-9/match

To provide background to the guile-devel list my prototype implementation for regex-case is as follows (w/ error messages suggested by Ludo now included):

;; helper macro for regex-case
;; (rx-let m (v ...) exp ...) => (let ((v (match:substring m 1)) ...) exp ...)
(define-syntax rx-let
  (lambda (x)
    (syntax-case x ()
      ((_ m (v ...) exp ...)
       (with-syntax (((i ...)
      (let f ((n 1) (vl #'(v ...))) ; fold (v ...) to (1 ...)
(if (null? vl) '() (cons n (f (1+ n) (cdr vl)))))))
 #'(let ((v (match:substring m i)) ...) exp ...))))))

(define-syntax regex-case
  (lambda (x)
    (syntax-case x (else)
      ((_ str ((pat v ...) exp ...) ...)
       (with-syntax (((id ...) (generate-temporaries #'(pat ...))))
 #'(let ((id (make-regexp pat)) ...)
     (cond
      ((regexp-exec id str) =>
       (lambda (m) (rx-let m (v ...) exp ...)))
      ...
      (else (error "regex-case: no match"))))))
      ((_ str ((pat v ...) exp ...) ... (else else-exp ...))
       (with-syntax (((id ...) (generate-temporaries #'(pat ...))))
 #'(let ((id (make-regexp pat)) ...)
     (cond
      ((regexp-exec id str) =>
       (lambda (m) (rx-let m (v ...) exp ...)))
      ...
     (else else-exp ...))))))))


reply via email to

[Prev in Thread] Current Thread [Next in Thread]