* Discovered pattern that ideally parses any quoting. Brackets highlighting in FSH. @ 2018-07-30 7:09 Sebastian Gniazdowski 2018-07-31 4:16 ` Sebastian Gniazdowski 2018-08-01 18:18 ` Bart Schaefer 0 siblings, 2 replies; 5+ messages in thread From: Sebastian Gniazdowski @ 2018-07-30 7:09 UTC (permalink / raw) To: Zsh Users 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 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Discovered pattern that ideally parses any quoting. Brackets highlighting in FSH. 2018-07-30 7:09 Discovered pattern that ideally parses any quoting. Brackets highlighting in FSH Sebastian Gniazdowski @ 2018-07-31 4:16 ` Sebastian Gniazdowski 2018-07-31 5:13 ` Sebastian Gniazdowski 2018-08-01 18:18 ` Bart Schaefer 1 sibling, 1 reply; 5+ messages in thread From: Sebastian Gniazdowski @ 2018-07-31 4:16 UTC (permalink / raw) To: Zsh Users 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 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Discovered pattern that ideally parses any quoting. Brackets highlighting in FSH. 2018-07-31 4:16 ` Sebastian Gniazdowski @ 2018-07-31 5:13 ` Sebastian Gniazdowski 0 siblings, 0 replies; 5+ messages in thread From: Sebastian Gniazdowski @ 2018-07-31 5:13 UTC (permalink / raw) To: Zsh Users There was __idx assign missing, and I fine-tuned readibility, so final version: _mybuf="A\AB'A'C" #input while [[ $_mybuf = (#b)[^ABC\"\'\\]#(([ABC\"\'])|[\\](*))(*) ]]; do [[ -n "${match[3]}" ]] && { __idx+=${mbegin[1]}+1 _mybuf="${match[3]:1}" # also skip 1 quoted char } || { __idx+=${mbegin[1]} [[ -z "$__quoting" ]] && { if [[ "${match[1]}" = [ABC] ]]; then echo "Got ${match[1]}" fi } # Toggle quoting [[ "${match[1]}" = \" && "$__quoting" != \' ]] && { [[ "$__quoting" = \" ]] && __quoting= || __quoting=\"; } [[ "${match[1]}" = \' && "$__quoting" != \" ]] && { [[ "$__quoting" = \' ]] && __quoting= || __quoting=\'; } # The last (*) in the pattern, i.e. remaining # string, to become next input to while _mybuf="${match[4]}" } done ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Discovered pattern that ideally parses any quoting. Brackets highlighting in FSH. 2018-07-30 7:09 Discovered pattern that ideally parses any quoting. Brackets highlighting in FSH Sebastian Gniazdowski 2018-07-31 4:16 ` Sebastian Gniazdowski @ 2018-08-01 18:18 ` Bart Schaefer 2018-08-02 7:32 ` Sebastian Gniazdowski 1 sibling, 1 reply; 5+ messages in thread From: Bart Schaefer @ 2018-08-01 18:18 UTC (permalink / raw) To: Sebastian Gniazdowski; +Cc: Zsh Users On Mon, Jul 30, 2018 at 12:09 AM, Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote: > > not only (z) can handle quoting, turns out regular pattern with while > loop can do it too: Yes, this is actually quite commonly done when writing ad-hoc parsers in perl via variations on while (s/^(the-leading-part)//g) { do something with $1; } Usually you need at least a counter to for example keep track of the depth of parenthesis nesting, but maybe that part is being handled outside the code snippet you demonstrate in this thread. ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Discovered pattern that ideally parses any quoting. Brackets highlighting in FSH. 2018-08-01 18:18 ` Bart Schaefer @ 2018-08-02 7:32 ` Sebastian Gniazdowski 0 siblings, 0 replies; 5+ messages in thread From: Sebastian Gniazdowski @ 2018-08-02 7:32 UTC (permalink / raw) To: Bart Schaefer; +Cc: Zsh Users On 1 August 2018 at 20:18, Bart Schaefer <schaefer@brasslantern.com> wrote: > On Mon, Jul 30, 2018 at 12:09 AM, Sebastian Gniazdowski > <sgniazdowski@gmail.com> wrote: >> >> not only (z) can handle quoting, turns out regular pattern with while >> loop can do it too: > > Yes, this is actually quite commonly done when writing ad-hoc parsers > in perl via variations on > > while (s/^(the-leading-part)//g) { > do something with $1; > } I think my code is different. It matches distinct chars (not sure if words are possible, sometimes I think yes, sometimes it seems a no go, I would have to write it) that we are interested in at the same time handling backslash, " and '. So user can query for set of chars, with their positions in buffer, without bothering with "are backslashes even or odd" and other such problems. It turns out this comes down to handling backslash, because of obvious "\\\A" problems (parity of the slash), but also \" – it is also backslash that controls whether double-quoting ends or begins. This is the [\\](*) part of the pattern and restarting with ${match[3]}. I realized that \', i.e. backslash and single quote, aren't special within single quote. So the code needs update. But it is totally possible, in the ${match[3]} block (the first one) one has to check if state==in-single-quote, and decide to skip next char accordingly. -- Sebastian Gniazdowski News: https://twitter.com/ZdharmaI IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2018-08-02 7:32 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2018-07-30 7:09 Discovered pattern that ideally parses any quoting. Brackets highlighting in FSH Sebastian Gniazdowski 2018-07-31 4:16 ` Sebastian Gniazdowski 2018-07-31 5:13 ` Sebastian Gniazdowski 2018-08-01 18:18 ` Bart Schaefer 2018-08-02 7:32 ` Sebastian Gniazdowski
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).