From: Matt Wette <matthew.wette@verizon.net>
To: Matthew Wette <matthew.wette@verizon.net>
Cc: guile-user@gnu.org
Subject: Re: regex-case
Date: Sat, 06 Feb 2016 14:10:42 -0800 [thread overview]
Message-ID: <E6F5D44A-7CE0-48E7-9894-F98382C70BE7@verizon.net> (raw)
In-Reply-To: <61E420AD-70B6-4DEA-A7DD-EB123E22EFD0@verizon.net>
[-- Attachment #1: Type: text/plain, Size: 1778 bytes --]
> On Feb 6, 2016, at 11:13 AM, Matt Wette <matthew.wette@verizon.net> 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 #2: regex-case.scm --]
[-- Type: application/octet-stream, Size: 1817 bytes --]
;; v160206c - M.Wette
;;; 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.
(use-modules (ice-9 pretty-print))
(use-modules (ice-9 regex))
;; 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 ...))))))
;; @example
;; (regex-case str
;; (("([a-z]+)" v) `(lower ,v))
;; (("([A-Z]+)" v) `(upper ,v))
;; (else (error "yuck")))
;; @end example
(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 ...)))
...))))
;; todo: pattern with "else"
((_ 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 ...)))))
)))
(define str "foo(3)")
(write
(regex-case str
(("^([a-z]+)\\(([0-9]+)\\)$" v i)
(list v i))
(("^([a-z]+)$" v)
(list v "1"))
(else
(error "not found"))
)
)
(newline)
;; --- last line ---
next prev parent reply other threads:[~2016-02-06 22:10 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-06 19:13 regex-case Matt Wette
2016-02-06 19:23 ` regex-case Matt Wette
2016-02-06 19:49 ` regex-case Marko Rauhamaa
2016-02-06 22:42 ` regex-case Matt Wette
2016-02-07 8:15 ` regex-case Marko Rauhamaa
2016-02-06 22:10 ` Matt Wette [this message]
2016-02-08 14:29 ` regex-case Ludovic Courtès
2016-02-11 1:19 ` regex-case Matt Wette
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=E6F5D44A-7CE0-48E7-9894-F98382C70BE7@verizon.net \
--to=matthew.wette@verizon.net \
--cc=guile-user@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).