zsh-users
 help / color / mirror / code / Atom feed
* How to best match $( ... ) in a string
@ 2018-06-01  8:44 ` Sebastian Gniazdowski
  2018-06-01  8:49   ` Joey Pabalinas
  2018-06-01  9:50   ` Peter Stephenson
  0 siblings, 2 replies; 5+ messages in thread
From: Sebastian Gniazdowski @ 2018-06-01  8:44 UTC (permalink / raw)
  To: Zsh Users

Hello,
the problem is possible quoting, e.g. $( echo \) ). Has anyone a
pattern that would handle some sort of quoting?

-- 
Best regards,
Sebastian Gniazdowski


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to best match $( ... ) in a string
  2018-06-01  8:44 ` How to best match $( ... ) in a string Sebastian Gniazdowski
@ 2018-06-01  8:49   ` Joey Pabalinas
  2018-06-01  9:01     ` Sebastian Gniazdowski
  2018-06-01  9:50   ` Peter Stephenson
  1 sibling, 1 reply; 5+ messages in thread
From: Joey Pabalinas @ 2018-06-01  8:49 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 349 bytes --]

On Fri, Jun 01, 2018 at 10:44:58AM +0200, Sebastian Gniazdowski wrote:
> Hello,
> the problem is possible quoting, e.g. $( echo \) ). Has anyone a
> pattern that would handle some sort of quoting?

You mean like a regex/glob to match the entire "$(...)" construct with those
possible weird \) and such inside?

-- 
Cheers,
Joey Pabalinas

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to best match $( ... ) in a string
  2018-06-01  8:49   ` Joey Pabalinas
@ 2018-06-01  9:01     ` Sebastian Gniazdowski
  0 siblings, 0 replies; 5+ messages in thread
From: Sebastian Gniazdowski @ 2018-06-01  9:01 UTC (permalink / raw)
  To: Joey Pabalinas; +Cc: Zsh Users

On 1 June 2018 at 10:49, Joey Pabalinas <joeypabalinas@gmail.com> wrote:
> On Fri, Jun 01, 2018 at 10:44:58AM +0200, Sebastian Gniazdowski wrote:
>> Hello,
>> the problem is possible quoting, e.g. $( echo \) ). Has anyone a
>> pattern that would handle some sort of quoting?
>
> You mean like a regex/glob to match the entire "$(...)" construct with those
> possible weird \) and such inside?

Yes, to not stop matching on \), etc.

-- 
Best regards,
Sebastian Gniazdowski


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to best match $( ... ) in a string
  2018-06-01  8:44 ` How to best match $( ... ) in a string Sebastian Gniazdowski
  2018-06-01  8:49   ` Joey Pabalinas
@ 2018-06-01  9:50   ` Peter Stephenson
  2018-06-01 10:56     ` Sebastian Gniazdowski
  1 sibling, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2018-06-01  9:50 UTC (permalink / raw)
  To: Zsh Users

On Fri, 1 Jun 2018 10:44:58 +0200
Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote:
> the problem is possible quoting, e.g. $( echo \) ). Has anyone a
> pattern that would handle some sort of quoting?

In general, this isn't possible.  Apart from nested unquoted
$(...), which is also valid, the real killer is

echo $(case foo in
  foo) echo This does work
  ;;
  esac)

This required us to rewrite our internal parsing completely and we're
still shaking the odd bug out of the result.

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.

pws


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to best match $( ... ) in a string
  2018-06-01  9:50   ` Peter Stephenson
@ 2018-06-01 10:56     ` Sebastian Gniazdowski
  0 siblings, 0 replies; 5+ messages in thread
From: Sebastian Gniazdowski @ 2018-06-01 10:56 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh Users

On 1 June 2018 at 11:50, Peter Stephenson <p.stephenson@samsung.com> wrote:
> On Fri, 1 Jun 2018 10:44:58 +0200
> Sebastian Gniazdowski <sgniazdowski@gmail.com> 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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-06-01 10:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20180601084641epcas5p1ca4cd48fa463123bbe980d132fd19473@epcas5p1.samsung.com>
2018-06-01  8:44 ` How to best match $( ... ) in a string Sebastian Gniazdowski
2018-06-01  8:49   ` Joey Pabalinas
2018-06-01  9:01     ` Sebastian Gniazdowski
2018-06-01  9:50   ` Peter Stephenson
2018-06-01 10:56     ` 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).