From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Matt Wette Newsgroups: gmane.lisp.guile.user Subject: Re: regex-case Date: Sat, 06 Feb 2016 14:10:42 -0800 Message-ID: References: <61E420AD-70B6-4DEA-A7DD-EB123E22EFD0@verizon.net> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2104\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_B6FF4B6C-888A-41BE-8E74-DD1AC1B44697" X-Trace: ger.gmane.org 1454796672 18952 80.91.229.3 (6 Feb 2016 22:11:12 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 6 Feb 2016 22:11:12 +0000 (UTC) Cc: guile-user@gnu.org To: Matthew Wette Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Sat Feb 06 23:11:03 2016 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1aSB4J-0005Mq-5O for guile-user@m.gmane.org; Sat, 06 Feb 2016 23:11:03 +0100 Original-Received: from localhost ([::1]:57152 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aSB4I-0000Jz-3P for guile-user@m.gmane.org; Sat, 06 Feb 2016 17:11:02 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:34069) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aSB47-0000Jj-30 for guile-user@gnu.org; Sat, 06 Feb 2016 17:10:51 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aSB43-0003CI-TB for guile-user@gnu.org; Sat, 06 Feb 2016 17:10:51 -0500 Original-Received: from vms173025pub.verizon.net ([206.46.173.25]:32917) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aSB43-0003CE-Kj for guile-user@gnu.org; Sat, 06 Feb 2016 17:10:47 -0500 Original-Received: from vz-proxy-m004.mx.aol.com ([64.236.83.15]) by vms173025.mailsrvcs.net (Oracle Communications Messaging Server 7.0.5.32.0 64bit (built Jul 16 2014)) with ESMTPA id <0O25003TMC9V1I30@vms173025.mailsrvcs.net> for guile-user@gnu.org; Sat, 06 Feb 2016 16:10:44 -0600 (CST) X-CMAE-Score: 0 X-CMAE-Analysis: v=2.1 cv=WcjxEBVX c=1 sm=1 tr=0 a=NimJJ1rWho3RDXpqkQh0Zg==:117 a=jFJIQSaiL_oA:10 a=o1OHuDzbAAAA:8 a=O7t5bKa6BUj4IVpUV4AA:9 a=QEXdDO2ut3YA:10 a=TKo-VU87RgiKA-ieZm4A:9 Original-Received: by 72.87.204.128 with SMTP id 5f4f8245; Sat, 06 Feb 2016 22:10:44 GMT In-reply-to: <61E420AD-70B6-4DEA-A7DD-EB123E22EFD0@verizon.net> X-Mailer: Apple Mail (2.2104) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 206.46.173.25 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:12371 Archived-At: --Apple-Mail=_B6FF4B6C-888A-41BE-8E74-DD1AC1B44697 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Feb 6, 2016, at 11:13 AM, Matt Wette = wrote: >=20 > 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 =E2=80=9Cregex-case=E2=80=9D. Code attached. > Comments on syntax appreciated. =E2=80=94 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) =3D> (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) =3D> (lambda (m) (rx-let m (v ...) exp ...))) ... (else else-exp ...))))) ))) --Apple-Mail=_B6FF4B6C-888A-41BE-8E74-DD1AC1B44697 Content-Disposition: attachment; filename=regex-case.scm Content-Type: application/octet-stream; name="regex-case.scm" Content-Transfer-Encoding: 7bit ;; 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 --- --Apple-Mail=_B6FF4B6C-888A-41BE-8E74-DD1AC1B44697--