Skip to main content

PyLExt allows to add new syntax extensions into Python language.

Project description

PyLExt allows you to add the following constructions to the Python language:

  • new operators with specified priorities,
  • syntax extensions given by LR(1) grammars,
  • macros associated with new syntax rules,
  • new token types and literals.

Language extension system works in two stages: parsing text and expanding macros, i.e. transforming syntax tree from extended grammar to standard Python grammar. This procedure is applied to each statement in file separately, so it is possible to define new syntax and use it in the next statement.

Simple examples

Custom operators

The simplest syntax extension is a new operator. For example, we want to define left-associative operator /@ that applies function to each element of collection and this operator has the lowest priority. Than we should create file simple.pyg

# Import syntax extension for new operator definition
gimport pylext.macros.operator 

# Add new operator 
infixl(0) '/@' (f, data):
    return [f(x) for x in data]

# Test new operator:
from math import *
def test(n):
   print(exp /@ range(n))

Main file should be a Python file, so we create main.py:

import simple
simple.test(10)

Custom operators may be useful as a syntactic sugar for symbolic computations libraries such as SymPy or SageMath.

Function literals for binary operators

Sometimes we need to use binary operator as a function object, for example if we want to reduce array using some binary operation.

# define new literal
new_token('op_lambda', '"(" [<=>+\\-*/%~?@|!&\\^.:;]+ ")"')

defmacro op_lambda(expr, op:*op_lambda):
    op = op[1:-1]  # remove parentheses
    try:
        return `(lambda x, y: x {op} y)`
    except RuntimeError as e:  # exception will be thrown if op is not binary operator
        pass
    raise Exception(f'`{op}` is not a binary operator')

This simple macro for each binary operator op creates function literal (op) which represents lambda function lambda x, y: x op y.

After macros expansion these 2 lines will be equivalent:

reduce((^), range(100))
reduce(lambda x,y: x ^ y, range(100))

We can write test function checking that result is the same

def test():
    from functools import reduce
    result  = reduce((^), range(100))  # reduce array by XOR operation
    correct = reduce(lambda x, y: x ^ y, range(100))
    return result == correct

See more examples and the documentation at github.

Requirements

  1. C++ compiler supporting c++17:
    • Visual Studio 2019
    • gcc 8 or later
    • apple clang 11 or later
  2. CMake 3.8 or later
  3. Python >= 3.6. Recommended is Python 3.8.
  4. Package python3-dev (for Ubuntu)

Installation:

You can install pylext from PyPI using pip:

$ pip install pylext

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

pylext-0.2.1.tar.gz (66.6 kB view hashes)

Uploaded Source

Built Distributions

pylext-0.2.1-cp310-cp310-win_amd64.whl (241.0 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

pylext-0.2.1-cp310-cp310-musllinux_1_1_x86_64.whl (904.0 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

pylext-0.2.1-cp310-cp310-manylinux_2_31_x86_64.whl (362.2 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.31+ x86-64

pylext-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (390.4 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pylext-0.2.1-cp310-cp310-macosx_12_0_x86_64.whl (286.8 kB view hashes)

Uploaded CPython 3.10 macOS 12.0+ x86-64

pylext-0.2.1-cp310-cp310-macosx_10_9_x86_64.whl (286.4 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

pylext-0.2.1-cp39-cp39-win_amd64.whl (240.8 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

pylext-0.2.1-cp39-cp39-musllinux_1_1_x86_64.whl (903.9 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

pylext-0.2.1-cp39-cp39-manylinux_2_31_x86_64.whl (362.2 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.31+ x86-64

pylext-0.2.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (390.3 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pylext-0.2.1-cp39-cp39-macosx_12_0_x86_64.whl (286.7 kB view hashes)

Uploaded CPython 3.9 macOS 12.0+ x86-64

pylext-0.2.1-cp39-cp39-macosx_10_9_x86_64.whl (286.1 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

pylext-0.2.1-cp38-cp38-win_amd64.whl (241.0 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

pylext-0.2.1-cp38-cp38-musllinux_1_1_x86_64.whl (905.0 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

pylext-0.2.1-cp38-cp38-manylinux_2_31_x86_64.whl (363.1 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.31+ x86-64

pylext-0.2.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (390.9 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pylext-0.2.1-cp38-cp38-macosx_12_0_x86_64.whl (286.8 kB view hashes)

Uploaded CPython 3.8 macOS 12.0+ x86-64

pylext-0.2.1-cp38-cp38-macosx_10_9_x86_64.whl (286.3 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

pylext-0.2.1-cp37-cp37m-win_amd64.whl (239.5 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

pylext-0.2.1-cp37-cp37m-musllinux_1_1_x86_64.whl (902.7 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

pylext-0.2.1-cp37-cp37m-manylinux_2_31_x86_64.whl (361.0 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.31+ x86-64

pylext-0.2.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (389.0 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

pylext-0.2.1-cp37-cp37m-macosx_12_0_x86_64.whl (286.1 kB view hashes)

Uploaded CPython 3.7m macOS 12.0+ x86-64

pylext-0.2.1-cp37-cp37m-macosx_10_9_x86_64.whl (286.1 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

pylext-0.2.1-cp36-cp36m-win_amd64.whl (239.6 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

pylext-0.2.1-cp36-cp36m-musllinux_1_1_x86_64.whl (902.8 kB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ x86-64

pylext-0.2.1-cp36-cp36m-manylinux_2_31_x86_64.whl (361.2 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.31+ x86-64

pylext-0.2.1-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (389.2 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

pylext-0.2.1-cp36-cp36m-macosx_10_16_x86_64.whl (289.5 kB view hashes)

Uploaded CPython 3.6m macOS 10.16+ x86-64

pylext-0.2.1-cp36-cp36m-macosx_10_9_x86_64.whl (289.6 kB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

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