zsh-users
 help / color / mirror / code / Atom feed
From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: Discovered pattern that ideally parses any quoting. Brackets highlighting in FSH.
Date: Tue, 31 Jul 2018 06:16:45 +0200	[thread overview]
Message-ID: <CAKc7PVBhRQE0UzjNN9ZLYRN1F_3_RzTNYAqkhS56n3yx67ty0A@mail.gmail.com> (raw)
In-Reply-To: <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@gmail.com> 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


  reply	other threads:[~2018-07-31  4:17 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-30  7:09 Sebastian Gniazdowski
2018-07-31  4:16 ` Sebastian Gniazdowski [this message]
2018-07-31  5:13   ` Sebastian Gniazdowski
2018-08-01 18:18 ` Bart Schaefer
2018-08-02  7:32   ` Sebastian Gniazdowski

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=CAKc7PVBhRQE0UzjNN9ZLYRN1F_3_RzTNYAqkhS56n3yx67ty0A@mail.gmail.com \
    --to=sgniazdowski@gmail.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).