[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: changequote oddity

From: Eric Blake
Subject: Re: changequote oddity
Date: Thu, 20 Jul 2017 08:51:39 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1

On 07/20/2017 04:29 AM, Jack Andrews wrote:
> new to m4, but i can't explain why the last ` in this file appears to quote
> the rest of file?
> note the last changequote()
> i got the define from stackoverflow...
> $ cat f
> define(`ticks',changequote([,])[changequote([,])```changequote(`,')]changequote(`,'))dnl

Let's figure out how m4 expanded this.  Before calling the outer define,
it macro-expands the arguments. The expansion of `ticks' is easy: the
string "ticks".  The second argument starts out by expanding
changequote([,]) (expands to nothing, but quotes have changed), then
expanding [changequote([,])```changequote(`,')] (the literal string
"changequote([,])```changequote(`,')", then expanding changequote(`,')
(expands to nothing, but restores quotes).

Now that define has its arguments collected, it expands to nothing with
the side effect that "ticks" is now defined as
"changequote([,])```changequote(`,')".  The dnl eats the newline, so
this resulted in no output.

> changequote(<,>)dnl

Now you've changed quotes to something else.  But your earlier
definition of 'ticks' is NOT compatible with the choice of quotes you
used here.  The dnl again means you have no output.

> expandticks (ticks)

'expandticks' is not the name of a macro, but it is also not followed by
a (, so it is output literally (so far, your output is "expandticks (".
Next, the parser sees 'ticks' and knows it is a macro, so it substitutes
in the definition, and expands changequote([,])```changequote(`,'); this
turns the quotes into [], outputs "```", then changes the quotes back to
`' (instead of <> that you had earlier).  Then the rest of the line is
output: ")" and newline.

> expandticks (`ticks)

Again, "expandticks (" is output literally, then ` starts a quote (since
that is the current quotation character due to your last expansion of

> '

...and it takes this ' to end the quote, resulting in the literal output

> $ m4 f
> expandticks (```)
> expandticks (ticks)

I don't know what you WANTED to have happen, but m4 did everything you
asked it to do.

Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]