GithubHelp home page GithubHelp logo

python-parsinghook's Introduction

Python Parsing Hook

This package adds support for easily adding parsing hooks to Python, using a special comment at or near the top of a file.

Usage

After this package has been installed, it is activated by placing the following at the top of any module you wish to use an alternate parser for:

# -*- parsing: <parsing-module> -*-

Note that this will not work for code executed directly from the command line (e.g. python3 foo.py), but will work if you execute the code as a module (e.g. python3 -m foo).

Technical Information

Parsing Hooks

Operation

When a parsing hook module is found, that module is imported, and the parse_module() function in that module is invoked. The expected return of the parse_module() function is an AST tree.

The parse_module() function MUST accept at least a file object and filename string as arguments, and MUST also accept arbitrary keyword arguments after those two arguments.

Existing Parsing Hooks

Known existing parsing hooks include:

  • parsinghook.dumpast (from this package)
  • pep505

Modifying the Wheel / Installing from Source

Note that after using python3 -m build to build the wheel, it is necessary to manually add the parsinghook.pth file to the wheel, with (for example):

cd src
zip -g ../dist/parsinghook-*-py3-none-any.whl parsinghook.pth
cd ..

If installing from source, the src/parsinghook.pth file must be placed in your site-packages directory.

Future Development

Parser Options

In the future, the parsing comment might be updated to support optional arguments to parsers, e.g.:

# -*- parsing: parsinghook.dumpast colorize=True -*-

Mutators

In the future, additional magic comments might be supported such as:

# -*- mutator: <mutation-module> -*-

Which would allow other modules to modify the AST after the initial parsing.

Thanks and Credits

This package stands on the shoulders of giants.

The parsinghook.hook.activate() function is based on André Roberge's ideas package.

python-parsinghook's People

Contributors

acaos avatar

Stargazers

cemrehancavdar avatar Juan-Pablo Scaletti 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.