Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: zregexparse
- X-seq: zsh-users 22650
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: zregexparse
- Date: Sat, 1 Apr 2017 16:21:45 -0700
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=from:message-id:date:in-reply-to:comments:references:to:subject :mime-version; bh=KINsBDD018KOzecMYys1nCVimsm6BUxaDR6HfqGmpvE=; b=LqiLGQh0nigNyn833GegbBKCVWPYDwanG9taT8mHDkDn/lmsEF0IYyHGO5P8dz84hQ Rq5sXkdXRije1XA/ybPHodi/RzCFQdUpMHMXm8tyV1qqXWHz85tdMckVWtsHxeLhY60Q FOcEfcvcnX+ov+nQLO9hWsufBDUOJHGvesrko6MKQseWie/aW6ICCBMKnJ2Eqcj8ZjxN 9TsKNKd245pl8J8j4JhoNP6XaF7yXF65x0CXc0lhTagVuFMKa7j12v2UtDGQFyY4aFfD rHtZITZTBsDBppjwln0TLC6zKzK6rDm+Om+Rpoa9QETIdWF3t1Bx7P6xP4b2rIgAWD4f /9/A==
- In-reply-to: <170401094355.ZM28604@torch.brasslantern.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <etPan.58db747f.515f007c.17199@MacMini.local> <170330221929.ZM18456@torch.brasslantern.com> <etPan.58ddeed9.12200854.17199@MacMini.local> <etPan.58ddfe2c.216231b.17199@MacMini.local> <170331141359.ZM24862@torch.brasslantern.com> <etPan.58df7f42.140e0f76.17199@MacMini.local> <170401094355.ZM28604@torch.brasslantern.com>
On Apr 1, 9:43am, Bart Schaefer wrote:
}
} This is just one of the corners of the code that has not been paid
} any attention in a very long time (it's essentially unchanged since
} 2001 or so).
Also, I just looked a little more closely at the code to find out why
Sebastian's first example with '{print $match[1]}' seemed to output
something useful, and realized that zregexparse is built on top of
the globbing pattern matcher -- it builds a state machine for the
regular expression semantics that weren't yet part of extendedglob at
the time, and then steps through the state machine calling back for
each subexpression found; but otherwise it's globbing.
That's how/why $match[1] gets set (and reset) for each callback.
Oh -- Sebastian also remarked "just that shipping own regex engine is
extreamly cool, at least for me" -- Src/pattern.c is explicity (for
licensing reasons!) derived from Henry Spencer's regular expression
package, so zsh *is* "shipping own regex engine", it merely calls it
extendedglob instead.
The equivalent (though perhaps not easily a syntactic duplication) of
zregexparse probably could now be rewritten entirely with extendedglob.
E.g. here's a fragment of it:
zrp() {
setopt localoptions extendedglob
local var1=$1 var2=$2 string=$3 pattern=$4 callback=$5
local _zrp_cb='typeset -g $var1=$mend[1] $var2=$mend[1]'
{
functions[_zrp_cb]="$_zrp_cb;$callback;return 0"
functions -M _zrp_cb
: ${string//(#b)($~pattern)/$((_zrp_cb()))}
} always {
functions +M _zrp_cb
unfunction _zrp_cb
}
}
torch% zrp p1 p2 abc 'bc|?' '{print "$p1, $p2, $mbegin[1], $mend[1], $match[1]"}'
1, 1, 1, 1, a
3, 3, 2, 3, bc
Messages sorted by:
Reverse Date,
Date,
Thread,
Author