From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: Re: zregexparse
Date: Sat, 1 Apr 2017 16:21:45 -0700 [thread overview]
Message-ID: <170401162145.ZM31098@torch.brasslantern.com> (raw)
In-Reply-To: <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
prev parent reply other threads:[~2017-04-01 23:21 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-29 8:46 zregexparse Sebastian Gniazdowski
2017-03-31 5:19 ` zregexparse Bart Schaefer
2017-03-31 5:53 ` zregexparse Sebastian Gniazdowski
2017-03-31 6:58 ` zregexparse Sebastian Gniazdowski
2017-03-31 21:13 ` zregexparse Bart Schaefer
2017-04-01 10:21 ` zregexparse Sebastian Gniazdowski
2017-04-01 16:43 ` zregexparse Bart Schaefer
2017-04-01 23:21 ` Bart Schaefer [this message]
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=170401162145.ZM31098@torch.brasslantern.com \
--to=schaefer@brasslantern.com \
--cc=zsh-users@zsh.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.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
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 NNTP newsgroup(s).