Skip to main content

Bicycle Repair Man - Rewrite Python Sources

Project description

Bicycle Repair Man

Rewrite python sources, highly experimental

How?

BRM is based on tokens which can be manipulated easily. Like an ast.NodeTransformer, we offer brm.TokenTransformer. TokenTransformer can do a lot of things starting from registering a new token (via register_{TOK_NAME} functions) to finding and replacing token patterns with parser rules.

!!! This project is highly experimental and has tons of bugs !!

Examples

Simple Patterns

from brm import TokenTransformer, pattern

class OperatorTransformer(TokenTransformer):

    @pattern("number", "plus", "number")
    def plus_to_minus(self, lhs, operator, rhs):
        operator = operator._replace(string="-")
        return lhs, operator, rhs

opt = OperatorTransformer()
assert opt.transform("2 + 2") == "2 - 2"

Custom Tokens and New Syntaxes

from brm import TokenTransformer, pattern

class SquareRoot(TokenTransformer):

    def register_squareroot(self):
        return "√"

    @pattern("squareroot", "number")
    def remove_varprefix(self, sr, number):
        return self.quick_tokenize(f"int({number.string} ** 0.5)")

sqr = SquareRoot()
assert eval(sqr.transform("√9")) == 3

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

brm-0.1.0.tar.gz (5.6 kB view hashes)

Uploaded Source

Built Distribution

brm-0.1.0-py3.9.egg (12.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page