CDSoft / pp

PP - Generic preprocessor (with pandoc in mind) - macros, literate programming, diagrams, scripts...
http://cdelord.fr/pp
GNU General Public License v3.0
252 stars 21 forks source link

Question About !comment Macro #32

Closed tajmone closed 6 years ago

tajmone commented 6 years ago

Hi, I wanted to ask if the !comment macro has any impact on a macros perfomance — ie: in my macros definition I'd like to keep a comment block with description and usage instructions inside the definition itself, like this:

!define(Highlight)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!comment

USAGE:

!Highlight(LANG)(EXTRA HIGHLIGHT OPTIONS)(CODE CLASS)
~~~~~~~~~~~~~~~~
BLOCK OF SOURCECODE
~~~~~~~~~~~~~~~~
<pre class="hl"><code class="!ifdef(3)(\1 \3)(\1)">\sh

cat <<EOF | highlight -f -S \1 --no-trailing-nl --validate-input !ifdef(2)(\2) \4 EOF

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The question is wether PP would then store the defined macro with all comments stripped off (ie: as if I wrote it without the comments) or wether the comments are kept as part of the macro definition and are parsed again at each macro occurence/invocation in the doc.

I'm sure this wouldn't have a significant performance hit anyway, but I'm just curious about it.

CDSoft commented 6 years ago

Hi, The comment macro just discards its argument. It's not stored and not preprocessed. The macro just returns an empty string. I guess it's the fastest pp macro ;-)

tajmone commented 6 years ago

Thanks!

I still don't get the full picture ... when I define a macro how does PP store them in memory, as they are (raw definitions) or they are preprocessed to some extent?

What I means is: with the !define macro I'm creating a custom macro that will be kept in memory for reuse later, when processing the document. If the macro's definition body contains other custom macros, or comments, etc, are these expanded to some level before the macro being defined gets actually stored in memory?

In other words: once parsed, are macros definitions stored in PP's memory as their textual definitions (as the user wrote them) or are they converted by PP to real code and stored as such?

I'm trying to get a clearer mental picture of how macros (and nested macros) definitions work.

CDSoft commented 6 years ago

There are two kinds of macros.

  1. builtin macros are Haskell functions
  2. user defined macros are stored in an association list that maps their names to their definition. The definition is the original macro definition stored in a simple string that is preprocessed each time the macro is called.