guile-user
[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: Sat, 06 Feb 2016 14:10:42 -0800

> On Feb 6, 2016, at 11:13 AM, Matt Wette <address@hidden> wrote:
> 
> I have always missed the ease provided by Perl in throwing a string at a list 
> of regular expressions.   I have thought it would be nice if the (ice-9 
> regex) module would provide something comparable .   So I started work on a 
> macro “regex-case”.    Code attached.
> Comments on syntax appreciated. — Matt

I have added the else case and cleaned up the fold in rx-let.  New code 
attached, and echoed partial here:

;;; Copyright (C) 2016 Matthew R. Wette
;;;
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Lesser General Public
;;; License as published by the Free Software Foundation; either
;;; version 3 of the License, or (at your option) any later version.

(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 ...)))
              ...))))
      ((_ 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 ...)))))
      )))

Attachment: regex-case.scm
Description: Binary data


reply via email to

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