zsh-workers
 help / color / mirror / code / Atom feed
* Unexpected ${%F{}} evaluation
@ 2014-12-03 21:20 Marc Finet
  2014-12-03 22:55 ` Bart Schaefer
  2014-12-04  2:22 ` Mikael Magnusson
  0 siblings, 2 replies; 4+ messages in thread
From: Marc Finet @ 2014-12-03 21:20 UTC (permalink / raw)
  To: zsh-workers

Hello,

While hacking a small script I fall on the following case:

    # x="${(%):-%F{red}}XX"; echo $x
    }XX
    # x=${(%):-%F{red}}XX; echo $x
    XX

In both cases, the color was correct, i.e. (%) + %F{} did it job, but
note the extra } in the first case.
It seems that the double-quotes of the first case associated the first
'}' to the '${': its value is Outbrace in stringsubst(). The '{' after 
the %F and the second '}' remain {}, and the first { is Inbrace. 

In the second case, the two '}' are converted into Outbrace, and both
'{' are also converted into Inbrace.

For the first case, {} are not balanced according to what I expected,
(outer {} for $ and inner {} for %F), but first } for first {, i.e. ${}. 

My questions are the following:
 - is this behavior intended, i.e. break an expression inside ${} to
   the first } ? At first, I thought that it was a limitation (of ""), but
   testing the second case showed me that correct parsing could be done.
 - is it normal that %F{xx works as %F{xx} ?

Thanks,

Marc.


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

* Re: Unexpected ${%F{}} evaluation
  2014-12-03 21:20 Unexpected ${%F{}} evaluation Marc Finet
@ 2014-12-03 22:55 ` Bart Schaefer
  2014-12-04  2:22 ` Mikael Magnusson
  1 sibling, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2014-12-03 22:55 UTC (permalink / raw)
  To: Zsh hackers list

On Wed, Dec 3, 2014 at 1:20 PM, Marc Finet <m.dreadlock@gmail.com> wrote:
> Hello,
>
>     # x="${(%):-%F{red}}XX"; echo $x
>     }XX
>     # x=${(%):-%F{red}}XX; echo $x
>     XX
>
> My questions are the following:
>  - is this behavior intended, i.e. break an expression inside ${} to
>    the first } ?

Yes, I believe so.  Inside double quotes, the second open brace has no
special meaning, so the first close brace that is encountered closes
the parameter expansion.

>    At first, I thought that it was a limitation (of ""), but
>    testing the second case showed me that correct parsing could be done.

I think the second case is parsed "correctly" only by accident.  In
the second case the braces are initially parsed as a brace expansion,
but as a special case a brace expansion with only one element is
replaced by the literal string including the braces.  If you "unsetopt
braceexpand" you get the first result for both examples.

It could be argued that this is a bug in braceexpand.

>  - is it normal that %F{xx works as %F{xx} ?

In prompts a percent-expando can end at either the closing brace or
the end of the string, whichever comes first ... because it doesn't
seem useful to have the prompt report a syntax error.  %F{xx would not
work that way if there were more prompt string following it.


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

* Re: Unexpected ${%F{}} evaluation
  2014-12-03 21:20 Unexpected ${%F{}} evaluation Marc Finet
  2014-12-03 22:55 ` Bart Schaefer
@ 2014-12-04  2:22 ` Mikael Magnusson
  2014-12-06 20:03   ` Marc Finet
  1 sibling, 1 reply; 4+ messages in thread
From: Mikael Magnusson @ 2014-12-04  2:22 UTC (permalink / raw)
  To: Marc Finet; +Cc: zsh workers

On Wed, Dec 3, 2014 at 10:20 PM, Marc Finet <m.dreadlock@gmail.com> wrote:
> Hello,
>
> While hacking a small script I fall on the following case:
>
>     # x="${(%):-%F{red}}XX"; echo $x
>     }XX
>     # x=${(%):-%F{red}}XX; echo $x
>     XX
>
> In both cases, the color was correct, i.e. (%) + %F{} did it job, but
> note the extra } in the first case.
> It seems that the double-quotes of the first case associated the first
> '}' to the '${': its value is Outbrace in stringsubst(). The '{' after
> the %F and the second '}' remain {}, and the first { is Inbrace.
>
> In the second case, the two '}' are converted into Outbrace, and both
> '{' are also converted into Inbrace.
>
> For the first case, {} are not balanced according to what I expected,
> (outer {} for $ and inner {} for %F), but first } for first {, i.e. ${}.
>
> My questions are the following:
>  - is this behavior intended, i.e. break an expression inside ${} to
>    the first } ? At first, I thought that it was a limitation (of ""), but
>    testing the second case showed me that correct parsing could be done.
>  - is it normal that %F{xx works as %F{xx} ?
>
> Thanks,
>
> Marc.

To answer the unasked question, since Bart already answered,
% x="${(%):-%F{red\}X%F{green\}X%~}"; echo -E $x
and
% x=${(%):-%F\{red\}X%F\{green\}X%~}; echo -E $x
both work "as expected". (the -E is just to convince myself no stray
backslashes are left).

-- 
Mikael Magnusson


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

* Re: Unexpected ${%F{}} evaluation
  2014-12-04  2:22 ` Mikael Magnusson
@ 2014-12-06 20:03   ` Marc Finet
  0 siblings, 0 replies; 4+ messages in thread
From: Marc Finet @ 2014-12-06 20:03 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: zsh workers

Thanks Michael and Bart; I just find the \} solution unbalanced and was
"hoping" for a bug. I finally did something like this:
    x="%F{red}"; y="${(%):-$x}XX"; echo $y

Marc.


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

end of thread, other threads:[~2014-12-06 20:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-03 21:20 Unexpected ${%F{}} evaluation Marc Finet
2014-12-03 22:55 ` Bart Schaefer
2014-12-04  2:22 ` Mikael Magnusson
2014-12-06 20:03   ` Marc Finet

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).