Code Monkey home page Code Monkey logo

custompreprocessor's Introduction

CustomPreprocessor

CustomPreprocessor is a python script that parses and substitutes custom macros definitions, using it own style in order to differentiate from C-style macros.

How to use

The usage is simple, just call the script with python 3 in the following way:

python(3) CustomPreprocessor.py InputFile.prep -o OutputFile 

The output file is optional and, if not present, it will save as the input file minus the .prep extension.

The input file should have a .prep extension.

Syntax

The syntax is simple and very C-like:

Defining:

  • The line should start with '$', with or without a space before.
  • The '$' should be followed by "def ".
  • Then a definition name.
  • After the definition name, a '(' should be used to indicate the args.
  • Then the args (if any) should be declared, separated by ','.
  • A ')' should be used to indicate that the arguments ended.
  • Then ": " should be used to indicate the end of the definition. This should include the space.
  • After that, the replacement expression can be declared. In order to use any arguments, prepend the arg with a '!'.
  • If the line ends with '', the following line is considered too.

Usage:

  • The line should start with '$', with or without spaces before.
  • The '$' should be followed by "use ".
  • After the use keyword, the definition name that you would like to call.
  • After the definition name, a '(' should be used to indicate the args.
  • Then the args (if any) should be passed, separated by ','.
  • A ')' should be used to indicate that the arguments ended.

Example

The following example with an interface declaration in Objective-C:

$def define_interface(a, b): @interface !a : !b @end

It starts with "$def" because it's an instruction to the preprocessor.
The definition name is "simple_macro".
To use, an argument should be passed, that's 'a'.
The ": " separates the definition and the replacement.
"@interface !a : !b @end" is the replacement itself. Important to notice that the "!a" and "!b" will be replace with whatever is passed to "a" and "b".

To use it:

$use define_interface(MyClassName, NSObject)

After processed, this line will be replaced with:

@interface MyClassName : NSObject @end

Instructions can also be expanded, recursively:

$def define_view(a): $use define_interface(!a, UIView)

In order for this one to work, the previous example should be declared in a previous line.

If now I use it as following:
Instructions can also be expanded, recursively:

$use define_view(SomeClassView)

First, it will expand into:

$use define_interface(SomeClassView, UIView)

This will be evaluated for a second time, expanding as following:

@interface SomeClassView : UIView @end

Definitions can also be multi-line:

$def define_interface(a): \
class !a { \
    friend !a call(const !a& other); \
};

If used like following:

$use define_interface(Number)

Will expand to:

class Number {
    friend Number call(const Number& other);
};

For this to work, the '' should be the last thing in the line.

Keep in mind that the '$' should be at the start of a line. If there is a non space character before it, the instruction will not be evaluated.

This project includes an "example" folder, with a valid file with a ".prep" file that can be parsed.

The required extension ".prep" is to prevent the user to override files accidentally.
The script can be easily modified to remove it.

custompreprocessor's People

Contributors

pr0crustes avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.