Copyright (C) 2000-2012 |
Whole document tree
How to invoke macrosThis chapter covers macro invocation, macro arguments and how macro expansion is treated. Macro invocationMacro invocations has one of the forms name which is a macro invocation without any arguments, or name(arg1, arg2, ..., argn) which is a macro invocation with n arguments. Macros can have any number of arguments. All arguments are strings, but different macros might interpret the arguments in different ways. The opening parenthesis must follow the name directly, with no spaces in between. If it does not, the macro is called with no arguments at all. For a macro call to have no arguments, the parentheses must be left out. The macro call name() is a macro call with one argument, which is the empty string, not a call with no arguments. Preventing macro invocation
An innovation of the First of all, many builtin macros cannot meaningfully be called without arguments. For any of these macros, whenever an opening parenthesis does not immediately follow their name, the builtin macro call is not triggered. This solves the most usual cases, like for `include' or `eval'. Later in this document, the sentence "This macro is recognized only when given arguments" refers to this specific provision.
There is also a command call option (
If your version of GNU Of course, the simplest way to prevent a name to be interpreted as a call to an existing macro is to quote it. The remainder of this section studies a little more deeply how quoting affects macro invocation, and how quoting can be used to inhibit macro invocation. Even if quoting is usually done over the whole macro name, it can also be done over only a few characters of this name. It is also possible to quote the empty string, but this works only inside the name. For example: `divert' `d'ivert di`ver't div`'ert all yield the string `divert'. While in both: `'divert divert`'
the
The output of macro evaluations is always rescanned. The following
example would yield the string `de', exactly as if define(`x', `substr(ab') define(`y', `cde, 3, 2)') x`'y
Unquoted strings on either side of a quoted string are subject to
being recognized as macro names. In the following example, quoting the
empty string allows for the define(`macro', `di$1') macro(v)`'dnl Without the quotes, this would rather yield the string `divdnl' followed by an end of line. Quoting may prevent recognizing as a macro name the concatenation of a macro expansion with the surrounding characters. In this example: define(`macro', `di$1') macro(v)`ert'
the input will produce the string `divert'. If the quote was
removed, the Macro argumentsWhen a name is seen, and it has a macro definition, it will be expanded as a macro. If the name is followed by an opening parenthesis, the arguments will be collected before the macro is called. If too few arguments are supplied, the missing arguments are taken to be the empty string. If there are too many arguments, the excess arguments are ignored.
Normally Macros are expanded normally during argument collection, and whatever commas, quotes and parentheses that might show up in the resulting expanded text will serve to define the arguments as well. Thus, if foo expands to `, b, c', the macro call bar(a foo, d) is a macro call with four arguments, which are `a ', `b', `c' and `d'. To understand why the first argument contains whitespace, remember that leading unquoted whitespace is never part of an argument, but trailing whitespace always is. Quoting macro argumentsEach argument has leading unquoted whitespace removed. Within each argument, all unquoted parentheses must match. For example, if foo is a macro, foo(() (`(') `(') is a macro call, with one argument, whose value is `() (() ('. It is common practice to quote all arguments to macros, unless you are sure you want the arguments expanded. Thus, in the above example with the parentheses, the `right' way to do it is like this: foo(`() (() (') It is, however, in certain cases necessary to leave out quotes for some arguments, and there is nothing wrong in doing it. It just makes life a bit harder, if you are not careful. Macro expansionWhen the arguments, if any, to a macro call have been collected, the macro is expanded, and the expansion text is pushed back onto the input (unquoted), and reread. The expansion text from one macro call might therefore result in more macros being called, if the calls are included, completely or partially, in the first macro calls' expansion. Taking a very simple example, if foo expands to `bar', and bar expands to `Hello world', the input foo will expand first to `bar', and when this is reread and expanded, into `Hello world'. Go to the first, previous, next, last section, table of contents. |