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

Re: How to best match $( ... ) in a string



On 1 June 2018 at 11:50, Peter Stephenson <p.stephenson@xxxxxxxxxxx> wrote:
> On Fri, 1 Jun 2018 10:44:58 +0200
> Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx> wrote:
>> the problem is possible quoting, e.g. $( echo \) ). Has anyone a
>> pattern that would handle some sort of quoting?
> (...)
> If you ignore that case, it's possible character by character with
> a bit of extra state for quotes, nested parentheses, etc. (as that's
> what zsh did for two decades) but you're going to need some incredibly
> sophisticated regular expression involving recursion to replace that.

Yes I should state that I'm aware it is not possible. I look for a
best possible solution. Fast-syntax-highlighting now supports
colorizing of command substitution $( ... ), it is called recursively
on it. The point is that 90% of uses will be colored good with my
current pattern:

inputs=( ${(0)${(S)__buf[1,110]//(#b)*\$\((?#)([^\\\"]\)|(#e))/${mbegin[1]};${match[1]}${match[2]%\)}${__nul}}%$__nul*}
)

The meaningful bit in above is: \$\((?#)([^\\\"]\)|(#e)). Match $(,
then look for unquoted (not \), not ")) closing ) or for end of whole
string. I utilize (S) non-greedy matching, and the (0)/$nul trick to
handle multiple substitutions in one command line.The effect is
already quite nice: http://psprint.blinkenshell.org/cmdsubst.png

So the point is, the glob can be hacky and imperfect, if it holds some
valuable solutions then syntax-highlighting user is at better
position. Any uplifts to my current pattern are welcomed.

-- 
Best regards,
Sebastian Gniazdowski



Messages sorted by: Reverse Date, Date, Thread, Author