Search code examples
c-preprocessorcode-generationterminologytemplate-enginepreprocessor

Terminology - one-time code generation directives


Is there a such thing as a preprocessor whose statements, once processed, disappear completely and get replaced by the target language syntax permanently?

I want to research it on the web but I don't know what term to search for. If I search for "code generator", "templating language", "preprocessor directives", "mixins", "annotations" I get generators whose input becomes the source of truth.

The closest thing I can think of is a macro.

What I'm trying to do

I often have to write code that is verbose and unnecessary manual labor and am looking for a smarter way to input at least the majority of it and have it automatically transformed and only source-control the output (and hand edit if necessary). For example:

  • Java code - Instead of writing getters/setters, javadoc (perhaps the transformer can be a maven plugin)
  • HTML - I just want to add URLs, and have my preprocessor automatically convert them to links, images, videos, audio etc. depending on the file extension with some regex substitution (currently I run a perl script via a cron job)

I just want to use it as my own shorthand and not enforce it in my project and make the output editable so that others have to learn a new framework or language (like Protobuf, Stringtemplate, GWT, C hash-defines, PHP, JSP etc).

There should be no direct clue that I used a template/preprocessor to generate it.


Solution

  • What you want is a "program transformation system". See https://en.wikipedia.org/wiki/Program_transformation. (This is a superset of "transpilers" [ugly term]).

    A good source-to-source transformation system will let you apply rewrite rules of the form of:

      if you see *this*, replace it by *that* if *this_condition*.
    

    You can then take your source code, and run a set of rewrite rules across that code to change it.

    The resulting code is "transformed"; the rewrite rules are not visible.