chicken-users
[Top][All Lists]
Advanced

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

[Chicken-users] Using irregex safely & responsibly [Was: Re: dev-snapsho


From: Peter Bex
Subject: [Chicken-users] Using irregex safely & responsibly [Was: Re: dev-snapshot 4.6.3]
Date: Thu, 7 Oct 2010 22:53:44 +0200
User-agent: Mutt/1.4.2.3i

Hello!

Since there are a few pitfalls to updating eggs to work with the new
Chicken 4.6.2, I've decided to draw up a quick list of pitfalls I noticed.

If you're just using the regex API or the regex API with a few irregex
things, usually all you need is to add (needs regex) to your egg's meta
file and you're done (but read on!).

If you are only using the irregex API and don't want to drag in another
dependency just to keep older Chickens happy, here's a way to depend
on just irregex in either old or new Chicken.  Add the following to your
.setup file:

(define regex-version
  (if (version>=? (chicken-version) "4.6.2")
      'total-irregex
      'irregex-through-regex))

and alter your compilation line to read something like this:

(compile -s -D ,regex-version my-egg.scm -j my-egg)

In your egg's file, where you would previously use this idiom:

(require-library regex)      ; or (use regex) for the lazy & sloppy
(import irregex)

you can now replace it with this block (you can delete emulation of
procedures you are sure you aren't using):

(cond-expand
 (total-irregex
  (use irregex))
 (else
  (require-library regex)
  (import (rename irregex
                  (irregex-match-start irregex-match-start-index)
                  (irregex-match-end irregex-match-end-index)))
  (define irregex-num-submatches irregex-submatches)
  (define irregex-match-num-submatches irregex-submatches)
  (define (irregex-match-valid-index? m i)
    (and (irregex-match-start-index m i) #t))
  (define (maybe-string->sre obj)
    (if (string? obj) (string->sre obj) obj))))

We need to do this because the irregex-match-{start,end} procedures have
been suffixed "-index".  But be careful!  They also have slightly changed
semantics.  If you pass an index of a submatch which did not get matched,
irrgex-match-start-index will throw an exception.  You first need to
test whether it matched.  That's what irregex-match-valid-index? is
for, which we simulate in older Chickens by simply attempting to fetch
the start index and checking whether it succeeded.
We also define irregex-[match-]num-submatches as irregex-submatches.
The old procedure accepted both match objects and irregex objects, while
the new procedures are more consistent with the rest of the API and only
accept their corresponding types.  The maybe-string->sre is just copied
from the irregex-core.scm file.

One last pitfall to take care of is the fact that the new irregex has
a few new procedures.  For example, the old Chicken's irregex simply
didn't have the chunked matching API at all.  Here's a list of things
that are unavailable in older Chickens:

irregex-extract
irregex-fold/chunked
irregex-match?
irregex-match-end-index     (but see emulation code above)
irregex-match-end-chunk
irregex-match-names
irregex-match-start-index   (but see emulation code above)
irregex-match-start-chunk
irregex-match-subchunk
irregex-match-valid-index?  (but see emulation code above)
irregex-match/chunked
irregex-num-submatches      (but see emulation code above)
irregex-opt
irregex-quote
irregex-split
make-irregex-chunker
maybe-string->sre           (but see emulation code above)

Cheers,
Peter
-- 
http://sjamaan.ath.cx
--
"The process of preparing programs for a digital computer
 is especially attractive, not only because it can be economically
 and scientifically rewarding, but also because it can be an aesthetic
 experience much like composing poetry or music."
                                                        -- Donald Knuth



reply via email to

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