Skip to content

pre proc

B. van Berkum edited this page Jan 27, 2019 · 2 revisions

Pre-Processor "language"

First a quick comment recap: although '#' are allowed as comment lines in many places (shell, Perl, Make, Python, YAML) they are stay-in-the ways. In some cases they are the only comment mechanism. Some languages forget about comments alltogether (JSON, markdown).

For C-like and C inspired programming languages (eg. JS, PHP) 2:

  // C++ style comment to 'comment out' a block of lines
  /* C-style comment to mark */

Still the octothorpe as comment use is wide spread. 3

"The preprocessor works before compilation and therefore before execution." 1

--traditional-cpp https://stackoverflow.com/questions/6683925/ignore-invalid-directives-in-cpp

https://gcc.gnu.org/onlinedocs/cpp/Invocation.html#Invocation

https://stackoverflow.com/questions/319328/how-to-write-a-while-loop-with-the-c-preprocessor "Take a look at the Boost preprocessor"

"you will need to use recursion in the preprocessor" [except that it cant!]

https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms Seems like more from the cpp-recursion dude.

https://stackoverflow.com/questions/1099843/using-the-c-preprocessor-for-languages-other-than-c m4. "This has traditionally been the preprocessor of choice for complex tasks; CPP is very primitive by comparison"

https://stackoverflow.com/questions/1560357/can-the-c-preprocessor-perform-integer-arithmetic people use the pre-processor strictly for text replacement, and evaluation of constant integer arithmetic happens either at compile time or run time.

As others noted however, you can do some arithmetic in #if statements.

https://stackoverflow.com/questions/396644/replacements-for-the-c-preprocessor You can use PHP as a C preprocessor. The advantages are:

Use cppinternals to build your own c preproc (in c): https://gcc.gnu.org/onlinedocs/cppinternals/

See sentinel lines for generic syntax and overlap with other use for generic, comment or otherwise quoted embedded Ids, directives, etc.

Sentinels are one way for easy static (pre) processors. Other forms require more advanced parsing, but offer the benefit of better integration ie. use field specs / values locally, and support formats without comments.

M4 can expand any tag, and takes parameters, ie. simple and variadic macro. Its ID's and concepts, like Make, are rooted in C(++) though.

There are some formats though that cannot or barely handle comments. JSON, and XML, markdown, come to mind. Many config formats and older scripting languages e.g. SQL handle comments with all kinds of line leaders.

But for those without, some preproc may be preferred to avoid things like:

[//]: # (test test test)
{
...
"comment": "test test test",
...
}

However, that would add an additional source unwrap/translit and move file content to another filename extension. Which can be bothersome. Enter parallel storage, solution with benefits but other down-sides; same preproc requirement but can be done on the fly and with regular source, however needs syncing with source.

So M4 is an option, if its expressions allow for sufficient extensible control. Otherwise a or any custom Bash script would do as well, just not as efficient.

Category: Initial
Interwiki for User-Scripts (About)
Repo Wiki
core/master wiki
- forks/bvb-dev wiki

Clone this wiki locally