C Preprocessor is a preprocessor created with Node.js only and
running like a C preprocessor with # directives.
It was originally designed for Javascript but you can use it
with any language you want.
See changelog here.
For local installation, run the following command:
npm install c-preprocessor --save
For global installation, run the following command:
npm install -g c-preprocessor
If you have installed this package in global, you can run c-preprocessor and pass your main file and output file in arguments.
c-preprocessor mainFile.js outputFile.js
Additionally you can specify a configuration file (see below for it's format):
c-preprocessor --config configFile.js mainFile.js outputFile.js
varcompiler=require("c-preprocessor");// To compile a filecompiler.compileFile(fileName,[options,]function(err,result){if(err)returnconsole.log(err);console.log(result);});// To compile a textcompiler.compile(code,[options,]function(err,result){// ...});// Or use Compiler classvarc=newcompiler.Compiler([options]);c.on('success',/* ... */)c.on('error',/* ... */)c.compile(code);// orc.compileFile(fileName);This are the defaults options. You can modify them by passing an option object.
varoptions={// Predefined constants (ex: { "MY_CONST": "42" })constants: {},// Predefined macros (ex: { "MACRO": "(a,b) a+b" })macros: {},// End of line characternewLine: '\n',// Escape '//#' & '/*#' comments (see extra/comments)commentEscape: true,// Empty lines to add between code and included filesincludeSpaces: 0,// Limit of empty following lines (0 = no limit)emptyLinesLimit: 0,// Base path for including filesbasePath: './',// Stop the compiler when an error ocurred ?stopOnError: true,// Must constants in #enum directive be in hexadecimal ?enumInHex: true};#include"file.js"Include and parse a file.
// Define a constant#defineMY_CONST 42
// Define a macro#defineSUM(a,b) a + bCreate a constant or a macro.
#undef MY_CONSTDelete a constant or a macro.
#ifA+B==5&& defined(MY_CONST)
// Do stuff#elif"MY_CONST2"=="House"// Do other stuff#else// Do other stuff#endif#ifndefMY_CONST3// Do stuff#endifC like conditions.#if condition is evaluated in JS so you must add " between string
constants.
Note: #ifdef C and #ifndef C are faster than #if defined(C) and #if !defined(C).
#pragma onceInclude the current file once.
#error This is an errorStop the compiler and log the message given after the directive.
__TIME__// Current time__DATE__// Current date__LINE__// Current line (where this constant is used).__FILE__// Current file (where this constant is used).This constants are predefined by the compiler.
//# One line comment/*#Multi-lines comment#*/This comments will be deleted in the compiled file.
Note: options.commentEscape must be true.
// Here A=0, ..., D=3#enumA, B, C, D#endenum// With options, so Car=5, .., Truck=25#enum start=5, step=10
Car, Bike, Truck#endenumC like enumeration.
You can use this directive for creating a lot of constants.