From: Matt Wette <matthew.wette@verizon.net>
To: guile-user@gnu.org
Subject: regex-case
Date: Sat, 06 Feb 2016 11:13:25 -0800 [thread overview]
Message-ID: <61E420AD-70B6-4DEA-A7DD-EB123E22EFD0@verizon.net> (raw)
[-- Attachment #1: Type: text/plain, Size: 1761 bytes --]
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
=== test ================
(define str "foo")
(regex-case str
(("^([a-z]+)\\(([0-9]+)\\)$" v i)
(list v i))
(("^([a-z]+)$" v)
(list v "1”)))
=>
(“foo” “1”)
=== syntax ==============
(regex-case <string>
((<pattern> <var> <var> …) <body>)
((<pattern> <var> <var> …) <body>)
(else <body>)
Where <pattern> is a string form of a regular expression, <var> … are variables that are bound to the matched subexpressions, and <body> is a list of expressions. The return is the last expression of the matched case.
=== expansion ===========
The example shown above expands to:
(let ((t-292 (make-regexp "^([a-z]+)\\(([0-9]+)\\)$"))
(t-293 (make-regexp "^([a-z]+)$")))
(cond ((regexp-exec t-292 str)
=>
(lambda (m)
(let ((v (match:substring m 1))
(i (match:substring m 2)))
(list v i))))
((regexp-exec t-293 str)
=>
(lambda (m)
(let ((v (match:substring m 1))) (list v "1"))))))
I was thinking the above expansion has some chance (if it lives in the regex module?) to memoize the make-regexp part during optimization.
If not a macro could be written to generate a match function which can memoize the make-regexp part.
(define regex-matcher foo ((<pattern> …)
=>
(define (let ((t-123 (make-regex <pattern>)) …) (lambda (str) (cond ((regexp-exec t-123 str) ...
[-- Attachment #2: regex-case.scm --]
[-- Type: application/octet-stream, Size: 1511 bytes --]
;; v160206b - 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 ...) ; fold (v ...) to (1 ...)
(let f ((il '()) (n 1) (vl #'(v ...)))
(if (null? vl) (reverse il)
(f (cons n il) (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"
)))
(define str "foo")
(write
(regex-case str
(("^([a-z]+)\\(([0-9]+)\\)$" v i)
(list v i))
(("^([a-z]+)$" v)
(list v "1"))
)
)
(newline)
;; --- last line ---
[-- Attachment #3: Type: text/plain, Size: 3 bytes --]
next reply other threads:[~2016-02-06 19:13 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-06 19:13 Matt Wette [this message]
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 ` regex-case Matt Wette
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=61E420AD-70B6-4DEA-A7DD-EB123E22EFD0@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).