Re: [sv-bc] Possible SV macro expansion algorithm

From: Coffin, Eric <eric_coffin_at_.....>
Date: Tue Nov 20 2007 - 14:36:14 PST
I clearly made a mistake in the first sentence.  It should read "Perhaps SV macro expansion should be not be considered as a text-to-text mechanism but rather as a text-to-token transformation."

-Eric


Coffin, Eric wrote:
Perhaps SV macro expansion should be considered as a
text-to-text mechanism but rather as a text-to-token transformation.
This is a key conceptual difference that seems to be causing some
issues with various descriptions of macro processing.

For example:

`define foo( a , b ) a+b
`foo( \Q , \R )

The SV-BC community seems to be grappling with how to represent
escaped identifiers in this case, and specifically how to manipulate
white space.  Macro expansion could work over "macro-tokens"
(think of ANSI-C and its concept of a preprocessor-token), and the process could
return lexical elements as described in the LRM's Appendix A (maybe we
could call them "parser-tokens").  With such an expansion the ASCII
representation of foo's expansion is less important than the fact that
it is merely three tokens: an escaped identifier, "\Q", followed by an
operator, "+", followed by another escaped identifier, "\R".

C++ treats macro expansion as a text-to-token transformation.

*********************************************************************

Here is a rough outline of a possible way to expand macros that might give some
consistency to the various SV implementations out there.

Order of actions to expand a macro:

 - After the macro use has been identified in the SV source text,
   gather the use's actual arguments.

 - Independently expand all actual arguments, but do not substitute
   them into the macro body.  If the macro use did not specify an
   actual and a default value was specified then expand the default
   text.  Some SV implementations first expand and then substitute,
   while others do not.  Note that all arguments should be expanded
   even if they are not used within the macro body.

 - Walk through the macro body looking for identifiers matching
   formal argument names.  Replace any macro formal argument with its
   expanded actual text, unless the macro formal is adjacent to a
   tick-tick ('').  If the formal arg is next to a tick-tick, then
   literally substitute the (unexpanded) actual text for the formal arg.

 - do {
     - Perform token-pasting upon the expansion's body.  Token
       pasting should have no effect upon the `" and the `\`" macro
       operators.  Furthermore, token pasting ignores any white space,
       and will not paste comments, nor paste across comments.
     - Rescan the resulting body for any more macros to expand.
       Expand them.  Do not expand `" or `\`".
     } while the expansion body changes

 - Expand the special macro-operators, tick-quote `" and tick-slash-tick-quote `\`"

-Eric







--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean. Received on Tue Nov 20 14:36:33 2007

This archive was generated by hypermail 2.1.8 : Tue Nov 20 2007 - 14:36:43 PST