Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Discovered pattern that ideally parses any quoting. Brackets highlighting in FSH.
- X-seq: zsh-users 23547
- From: Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: Discovered pattern that ideally parses any quoting. Brackets highlighting in FSH.
- Date: Tue, 31 Jul 2018 06:16:45 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-transfer-encoding; bh=bzGjJs5BvQSf3DCg42ByUiwYPF3lzZRLAyhr9p4kHvo=; b=KzJLpbB0WbexMp1j0neD3o6UI1By+Mps4l5OBHVhCe28EjoQ71io+/t2HYahn0CVJA Wy3d1P1Ppqjj9aCZKLcAcQmpJrCV4PFf0ga/vI4iifE5NBdui1WjrPzRbc42+l4rCTW8 AQYhxbGb6GU8LWoA1QElGot/wkX7rGhzl6lF7/aT2Xe3bhr3sJuUp35bxgIkPa+ZgBxv b9F6M4CvzL+To8w0R4F4cn3zaEEmiGV33EmuiRtLMdQ9+PpdkMQShCb40zEqRrxrOEiN TkbnbdFzDrI8SHRKDi+s3zYA/oSagfBDAoDKV5EuXsAaXidfM6BjA+U43A4lzCbvO3Yh VMTQ==
- In-reply-to: <CAKc7PVAbwaZr6Pr8weACs7W7E1A6GF5y7QpwRanssYFdGnzSww@mail.gmail.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>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <CAKc7PVAbwaZr6Pr8weACs7W7E1A6GF5y7QpwRanssYFdGnzSww@mail.gmail.com>
The brackets in the pattern obfuscate a little the idea, so here's
minimal code that matches ABC instead, together with full ", '
handling.
_mybuf="A\AB'A'C";
while [[ $_mybuf = (#b)[^ABC\"\'\\]#(([ABC\"\'])|[\\](*))(*) ]]; do
[[ -n "${match[3]}" ]] && {
__idx+=${mbegin[1]}+1
_mybuf="${match[3]:1}" # also skip 1 quoted char
} || {
[[ -z "$__quoting" ]] && {
if [[ "${match[1]}" = [ABC] ]]; then
echo "Got ${match[1]}"
fi
}
[[ "${match[1]}" = \" && "$__quoting" != \' ]] && { [[
"$__quoting" = '"' ]] && __quoting="" || __quoting='"'; }
[[ "${match[1]}" = \' && "$__quoting" != \" ]] && { [[
"$__quoting" = "'" ]] && __quoting="" || __quoting="'"; }
_mybuf="${match[4]}" # The last (*) in the pattern, i.e.
remaining string, to become next input to while
}
done
output:
Got A
Got B
Got C
On 30 July 2018 at 09:09, Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx> wrote:
> Hello,
> not only (z) can handle quoting, turns out regular pattern with while
> loop can do it too:
>
> while [[ $_mybuf = (#b)[^"{}()[]\\\"'"]#((["({[]})\"'"])|[\\](*))(*) ]]; do
> [[ -n "${match[3]}" ]] && {
> __idx+=${mbegin[1]}+1
> _mybuf="${match[3]:1}" # also skip 1 quoted char
> } || {
>
> the main point is [\\](*). Parentheses are placed in ${match[3]} and
> allow to restart processing skipping e.g. a quoted backslash.
>
> The rest is matching braces – it's an example from
> Fast-Syntax-Highlighting, it now has ideal brackets highlighting – AND
> ALSO ", '. Simple handling of those two quotings allows to switch mode
> "in quoting" "outside quoting". That's all. The effect:
>
> https://raw.githubusercontent.com/zdharma/fast-syntax-highlighting/master/images/brackets.gif
>
> --
> Sebastian Gniazdowski
> News: https://twitter.com/ZdharmaI
> IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
--
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Messages sorted by:
Reverse Date,
Date,
Thread,
Author