Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Discovered pattern that ideally parses any quoting. Brackets highlighting in FSH.



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