zsh-users
 help / color / mirror / Atom feed
* comment block curiosity
@ 2021-01-03 17:09 Ray Andrews
  2021-01-03 17:26 ` Bart Schaefer
  0 siblings, 1 reply; 11+ messages in thread
From: Ray Andrews @ 2021-01-03 17:09 UTC (permalink / raw)
  To: Zsh Users

Emboldened by Bart's showing how fundamental syntax might be aliased, 
and knowing how dangerous that might be, yet I thought I'd play with 
that.  I've always thought we should have block comments and of course 
you can fake it very well with:

    : <<COMMENTBLOCK

    foo
    bar
    baz

    COMMENTBLOCK

... But I thought I'd try to alias that because I keep forgetting the " 
: << " syntax.  So:

    alias startcomment=': <<COMMENTBLOCK'
    alias endcomment='COMMENTBLOCK'

    echo about to start a comment:

    startcomment        # This actually works believe it or not.

    foo
    bar
    baz

    endcomment         # This is ignored, it doesn't work but no error
    either.
    COMMENTBLOCK       # This ends the comment.

    echo back to working code.


You'd think that "endcomment' would be the alias more likely to work but 
it it doesn't.  Why?  Can it be fixed?




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

* Re: comment block curiosity
  2021-01-03 17:09 comment block curiosity Ray Andrews
@ 2021-01-03 17:26 ` Bart Schaefer
  2021-01-03 17:36   ` Ray Andrews
  0 siblings, 1 reply; 11+ messages in thread
From: Bart Schaefer @ 2021-01-03 17:26 UTC (permalink / raw)
  To: Ray Andrews; +Cc: Zsh Users

On Sun, Jan 3, 2021 at 9:10 AM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
>     alias startcomment=': <<COMMENTBLOCK'
>     alias endcomment='COMMENTBLOCK'

You're overthinking this a bit, Ray.

alias startcomment=': <<\endcomment'

The backslash prevents accidental expansion of command substitutions
etc. in the block.

> [...]
>     endcomment         # This is ignored [...]
>     COMMENTBLOCK       # This ends the comment.
>
> You'd think that "endcomment' would be the alias more likely to work but
> it doesn't.  Why?  Can it be fixed?

Aliases do not expand inside here-documents.  Even if they did, the
ending delimiter must be found literally.


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

* Re: comment block curiosity
  2021-01-03 17:26 ` Bart Schaefer
@ 2021-01-03 17:36   ` Ray Andrews
  2021-01-03 17:43     ` Bart Schaefer
  0 siblings, 1 reply; 11+ messages in thread
From: Ray Andrews @ 2021-01-03 17:36 UTC (permalink / raw)
  To: zsh-users

On 2021-01-03 9:26 a.m., Bart Schaefer wrote:
> On Sun, Jan 3, 2021 at 9:10 AM Ray Andrews <rayandrews@eastlink.ca> wrote:
>>      alias startcomment=': <<COMMENTBLOCK'
>>      alias endcomment='COMMENTBLOCK'
> You're overthinking this a bit, Ray.
That happens :)
>
> Aliases do not expand inside here-documents.  Even if they did, the
> ending delimiter must be found literally.
Ah!  I hate to say 'that's clear' but it's a no brainer that while I'm 
explicitly in a comment , alias expansion is ineffective!  I should have 
known that.  Ok, just tinkering where angels fear to tread, it wasn't 
important.  Comment blocks would be cool tho. Howbout:

#{#
foo
bar
baz
#}#

>



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

* Re: comment block curiosity
  2021-01-03 17:36   ` Ray Andrews
@ 2021-01-03 17:43     ` Bart Schaefer
  2021-01-03 18:30       ` Ray Andrews
  0 siblings, 1 reply; 11+ messages in thread
From: Bart Schaefer @ 2021-01-03 17:43 UTC (permalink / raw)
  To: Ray Andrews; +Cc: Zsh Users

On Sun, Jan 3, 2021 at 9:36 AM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> Comment blocks would be cool tho. Howbout:
>
> #{#
> foo
> bar
> baz
> #}#

alias '#{#'=": <<'#}#'"

??


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

* Re: comment block curiosity
  2021-01-03 17:43     ` Bart Schaefer
@ 2021-01-03 18:30       ` Ray Andrews
  2021-01-03 18:50         ` Bart Schaefer
  0 siblings, 1 reply; 11+ messages in thread
From: Ray Andrews @ 2021-01-03 18:30 UTC (permalink / raw)
  To: zsh-users

On 2021-01-03 9:43 a.m., Bart Schaefer wrote:
> On Sun, Jan 3, 2021 at 9:36 AM Ray Andrews <rayandrews@eastlink.ca> wrote:
>> Comment blocks would be cool tho. Howbout:
>>
>> #{#
>> foo
>> bar
>> baz
>> #}#
> alias '#{#'=": <<'#}#'"
>
> ??
>
I see what your thinking.

    alias '#{#'=": <<'#}#'"

    echo staring comment

    #{#

    foo
    bar
    baz

    #}#

    echo back to code



gives:

    $ . test1
    staring comment
    test1:9: command not found: foo
    test1:10: command not found: bar
    test1:11: command not found: baz
    back to code

... bet it can be made to work tho.


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

* Re: comment block curiosity
  2021-01-03 18:30       ` Ray Andrews
@ 2021-01-03 18:50         ` Bart Schaefer
  2021-01-03 21:17           ` Ray Andrews
  2021-01-03 21:31           ` Ray Andrews
  0 siblings, 2 replies; 11+ messages in thread
From: Bart Schaefer @ 2021-01-03 18:50 UTC (permalink / raw)
  To: Ray Andrews; +Cc: Zsh Users

On Sun, Jan 3, 2021 at 10:30 AM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
>     $ . test1
>     staring comment
>     test1:9: command not found: foo
>     test1:10: command not found: bar
>     test1:11: command not found: baz
>     back to code
>
> ... bet it can be made to work tho.

A ha ha.  Sorry.

No, it can't be made to work in a script, at least not without
breaking ordinary comments.

It works in an interactive shell with NO_INTERACTIVE_COMMENTS.


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

* Re: comment block curiosity
  2021-01-03 18:50         ` Bart Schaefer
@ 2021-01-03 21:17           ` Ray Andrews
  2021-01-03 21:31           ` Ray Andrews
  1 sibling, 0 replies; 11+ messages in thread
From: Ray Andrews @ 2021-01-03 21:17 UTC (permalink / raw)
  To: zsh-users

On 2021-01-03 10:50 a.m., Bart Schaefer wrote:
>
> It works in an interactive shell with NO_INTERACTIVE_COMMENTS.
>
Because '#" is included in my construction?  Supozin it was '[[[' and 
']]]]' or something?  But no_interactive_comments doesn't seem like much 
of a loss.



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

* Re: comment block curiosity
  2021-01-03 18:50         ` Bart Schaefer
  2021-01-03 21:17           ` Ray Andrews
@ 2021-01-03 21:31           ` Ray Andrews
  2021-01-04  6:10             ` Daniel Shahaf
  1 sibling, 1 reply; 11+ messages in thread
From: Ray Andrews @ 2021-01-03 21:31 UTC (permalink / raw)
  To: zsh-users

On 2021-01-03 10:50 a.m., Bart Schaefer wrote:

function test1 ()
{

alias START_COM=": <<'END_COM'"

echo staring comment

START_COM

foo
bar
baz

END_COM

echo back to code

}

$ . test1; test1
staring comment
back to code

Works ... dare I say ... perfectly :-)

Seems you can't use any characters that are special to the shell tho, it 
gets confused.



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

* Re: comment block curiosity
  2021-01-03 21:31           ` Ray Andrews
@ 2021-01-04  6:10             ` Daniel Shahaf
  2021-01-04 17:09               ` Ray Andrews
  0 siblings, 1 reply; 11+ messages in thread
From: Daniel Shahaf @ 2021-01-04  6:10 UTC (permalink / raw)
  To: Ray Andrews; +Cc: zsh-users

Ray Andrews wrote on Sun, Jan 03, 2021 at 13:31:35 -0800:
> Seems you can't use any characters that are special to the shell tho,

You can, actually:

% zsh -f =(print -rC1 -- 'alias -g "|&"=": <<EOC"' 'echo foo' '|&' 'echo bar' 'EOC' 'echo baz')
foo
baz
% 

IIRC, the relevant difference between «|&» and «# foo» is that the former gets
a token and the latter doesn't.  (The lexer doesn't emit a token for comments
because execution doesn't care about comments.)  You can see this in the output
of «f() { … }; which f», where «|&» is preserved but comments aren't.

tl;dr: It's an issue that's specific to comments.

> it gets confused.

Not really.  An alias was defined that would never be looked up, that's all.
That's not conceptually different from writing, say, «alias 'foo bar'=baz»
(which will never match anything).

Which, incidentally, means that «alias " $k"=$v» is a built-in, global-scope
associative array.  I'm sure that's useful for something.


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

* Re: comment block curiosity
  2021-01-04  6:10             ` Daniel Shahaf
@ 2021-01-04 17:09               ` Ray Andrews
  2021-01-04 19:28                 ` Bart Schaefer
  0 siblings, 1 reply; 11+ messages in thread
From: Ray Andrews @ 2021-01-04 17:09 UTC (permalink / raw)
  To: zsh-users

On 2021-01-03 10:10 p.m., Daniel Shahaf wrote:
> Ray Andrews wrote on Sun, Jan 03, 2021 at 13:31:35 -0800:
>> Seems you can't use any characters that are special to the shell tho,
> You can, actually:
>
> % zsh -f =(print -rC1 -- 'alias -g "|&"=": <<EOC"' 'echo foo' '|&' 'echo bar' 'EOC' 'echo baz')
> foo
> baz
> %
That is an invocation only an Adeptus dare use!
> ... The lexer doesn't emit a token for comments ...
Is there some way of seeing lexer output?  I'll bet many of my 
confusions would be cured if I could just see my code the way zsh sees it.



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

* Re: comment block curiosity
  2021-01-04 17:09               ` Ray Andrews
@ 2021-01-04 19:28                 ` Bart Schaefer
  0 siblings, 0 replies; 11+ messages in thread
From: Bart Schaefer @ 2021-01-04 19:28 UTC (permalink / raw)
  To: Ray Andrews; +Cc: Zsh Users

On Mon, Jan 4, 2021 at 9:09 AM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> On 2021-01-03 10:10 p.m., Daniel Shahaf wrote:
> >
> > % zsh -f =(print -rC1 -- 'alias -g "|&"=": <<EOC"' 'echo foo' '|&' 'echo bar' 'EOC' 'echo baz')
>
> That is an invocation only an Adeptus dare use!

A somewhat more readable variation:

% zsh -f =(<<\EOSCRIPT
alias -g "|&"=": <<EOC"
echo foo
|&
echo bar
EOC
echo baz
EOSCRIPT
)

Also, "print -C1" is the same as "print -l" if that makes it more obvious.

> > ... The lexer doesn't emit a token for comments ...
> Is there some way of seeing lexer output?

You get that from
  setopt xtrace
but redirections (including here-documents) are also omitted.
  setopt verbose
gets the contents of the here-document, but neither of those setopts
shows expanded aliases, so with the "|&" alias in place you can't see
the "<<EOC" operator itself.

You can also get a lexical breakdown by using the (z) or (Z+c+)
parameter expansion flags, but those also do not expand aliases and
don't know about here-documents so you get the contents of the
here-document parsed into tokens as well.  I suppose the latter could
be considered a bug.


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

end of thread, other threads:[~2021-01-04 19:29 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-03 17:09 comment block curiosity Ray Andrews
2021-01-03 17:26 ` Bart Schaefer
2021-01-03 17:36   ` Ray Andrews
2021-01-03 17:43     ` Bart Schaefer
2021-01-03 18:30       ` Ray Andrews
2021-01-03 18:50         ` Bart Schaefer
2021-01-03 21:17           ` Ray Andrews
2021-01-03 21:31           ` Ray Andrews
2021-01-04  6:10             ` Daniel Shahaf
2021-01-04 17:09               ` Ray Andrews
2021-01-04 19:28                 ` Bart Schaefer

zsh-users

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/zsh-users

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-users zsh-users/ http://inbox.vuxu.org/zsh-users \
		zsh-users@zsh.org
	public-inbox-index zsh-users

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


code repositories for the project(s) associated with this inbox:

	https://git.vuxu.org/mirror/zsh/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git