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.0.0.tar.gz (67.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pylext-0.2.0-cp310-cp310-win_amd64.whl (240.9 kB view details)

Uploaded CPython 3.10Windows x86-64

pylext-0.2.0-cp310-cp310-manylinux_2_31_x86_64.whl (362.2 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.31+ x86-64

pylext-0.2.0-cp310-cp310-macosx_12_0_x86_64.whl (286.8 kB view details)

Uploaded CPython 3.10macOS 12.0+ x86-64

pylext-0.2.0-cp39-cp39-win_amd64.whl (240.7 kB view details)

Uploaded CPython 3.9Windows x86-64

pylext-0.2.0-cp39-cp39-manylinux_2_31_x86_64.whl (362.1 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.31+ x86-64

pylext-0.2.0-cp39-cp39-macosx_12_0_x86_64.whl (286.6 kB view details)

Uploaded CPython 3.9macOS 12.0+ x86-64

pylext-0.2.0-cp38-cp38-win_amd64.whl (241.0 kB view details)

Uploaded CPython 3.8Windows x86-64

pylext-0.2.0-cp38-cp38-manylinux_2_31_x86_64.whl (363.1 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.31+ x86-64

pylext-0.2.0-cp38-cp38-macosx_12_0_x86_64.whl (286.8 kB view details)

Uploaded CPython 3.8macOS 12.0+ x86-64

pylext-0.2.0-cp37-cp37m-win_amd64.whl (239.5 kB view details)

Uploaded CPython 3.7mWindows x86-64

pylext-0.2.0-cp37-cp37m-manylinux_2_31_x86_64.whl (360.9 kB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.31+ x86-64

pylext-0.2.0-cp37-cp37m-macosx_12_0_x86_64.whl (286.1 kB view details)

Uploaded CPython 3.7mmacOS 12.0+ x86-64

File details

Details for the file pylext-0.2.0.0.tar.gz.

File metadata

  • Download URL: pylext-0.2.0.0.tar.gz
  • Upload date:
  • Size: 67.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for pylext-0.2.0.0.tar.gz
Algorithm Hash digest
SHA256 d2ee57aa9be434620e01b641d1817124319c9c9a61ab1136648df0ca07b847e8
MD5 208b1a18aa868950487031803d2c48f4
BLAKE2b-256 02f7918ab47b5cdfc837a5995da5dd86fd4472db4c405118072c1a0d9ebb4f01

See more details on using hashes here.

File details

Details for the file pylext-0.2.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: pylext-0.2.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 240.9 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for pylext-0.2.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a196c90dfc194299d0ad248b71789207739f6332ce146c4320237d2071090015
MD5 629a0cc7b948609c8d320f3f81f7fa8d
BLAKE2b-256 20ea74a39db1868db75d67d5b8579a912f6f3e8f1072d013af80e72eb67d8ee8

See more details on using hashes here.

File details

Details for the file pylext-0.2.0-cp310-cp310-manylinux_2_31_x86_64.whl.

File metadata

  • Download URL: pylext-0.2.0-cp310-cp310-manylinux_2_31_x86_64.whl
  • Upload date:
  • Size: 362.2 kB
  • Tags: CPython 3.10, manylinux: glibc 2.31+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for pylext-0.2.0-cp310-cp310-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 208e9a0777b66068146fbaf15812b41950fb19272daeb11ac54d7ce68c903495
MD5 a4f003ac68a9397a5536f6ba76a9304c
BLAKE2b-256 8bcb8dfd2685fadd5fd8efcddfe104ef5741d4dbb4d665b4a5f023e1a4d80477

See more details on using hashes here.

File details

Details for the file pylext-0.2.0-cp310-cp310-macosx_12_0_x86_64.whl.

File metadata

  • Download URL: pylext-0.2.0-cp310-cp310-macosx_12_0_x86_64.whl
  • Upload date:
  • Size: 286.8 kB
  • Tags: CPython 3.10, macOS 12.0+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for pylext-0.2.0-cp310-cp310-macosx_12_0_x86_64.whl
Algorithm Hash digest
SHA256 83044cbda4af9ee77fef1c1202ad4b5f3a38db08189c76f1d8cfceba5486f26f
MD5 e6ffa40d9d28e38b8ce2fd60e9f1bf50
BLAKE2b-256 eff4bb927fbb809bd33cfd196d5f95340652814c9a4c5fd1d7f97d8df997caf8

See more details on using hashes here.

File details

Details for the file pylext-0.2.0-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: pylext-0.2.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 240.7 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for pylext-0.2.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 a39f43d6294003477573dea9b2179e6f57bdceac2d38ea9173b5c20ea6367154
MD5 cc51dd096f1ba0a2af39fb3cb7cdbac9
BLAKE2b-256 3b7b7d90e166f78d65a226f03b469efa1c1cd867fca26b5d39bf0735b0344f36

See more details on using hashes here.

File details

Details for the file pylext-0.2.0-cp39-cp39-manylinux_2_31_x86_64.whl.

File metadata

  • Download URL: pylext-0.2.0-cp39-cp39-manylinux_2_31_x86_64.whl
  • Upload date:
  • Size: 362.1 kB
  • Tags: CPython 3.9, manylinux: glibc 2.31+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for pylext-0.2.0-cp39-cp39-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 1d3334b2d2aa3458a0813604364ee60b5dd8e1d6ea0ed1a9ee27434dcb920d13
MD5 a9854bfe310d68719ad98ca082747096
BLAKE2b-256 f3e4472780e40d864049597badb56bf875679c5753e2821bbd62d9d65b8d3829

See more details on using hashes here.

File details

Details for the file pylext-0.2.0-cp39-cp39-macosx_12_0_x86_64.whl.

File metadata

  • Download URL: pylext-0.2.0-cp39-cp39-macosx_12_0_x86_64.whl
  • Upload date:
  • Size: 286.6 kB
  • Tags: CPython 3.9, macOS 12.0+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for pylext-0.2.0-cp39-cp39-macosx_12_0_x86_64.whl
Algorithm Hash digest
SHA256 a2abee996a443e7ce36521c102ea19fcb85cd9ed53cb5e2654d50e66aa765abe
MD5 c6104123d4480353f3c2000225cf1b9d
BLAKE2b-256 096cfa618a905eb4911bb6abcb6b5ec76fea75b8e43b884975e80e89dff07375

See more details on using hashes here.

File details

Details for the file pylext-0.2.0-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: pylext-0.2.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 241.0 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for pylext-0.2.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 db9cb76f00c452d5e34361a19bf7cbfd9cc585e60321086fc07dbcf9b03195bc
MD5 b01b94cb2de37e78c0d488a54d859916
BLAKE2b-256 3e786abec96639bc015a86148fe1d678a2868bd139f6c3f54732c67780ffc530

See more details on using hashes here.

File details

Details for the file pylext-0.2.0-cp38-cp38-manylinux_2_31_x86_64.whl.

File metadata

  • Download URL: pylext-0.2.0-cp38-cp38-manylinux_2_31_x86_64.whl
  • Upload date:
  • Size: 363.1 kB
  • Tags: CPython 3.8, manylinux: glibc 2.31+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for pylext-0.2.0-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 70eb03fa2472aa175eb824e59566e4b9c4c83679e43f2c407a8815566bfbfb1a
MD5 269873261bf0ceb300fc2474b4e2c3e8
BLAKE2b-256 28524ba857adae1bc7c2c5e64fc690d40327ecc29338045253fdd50c8716950f

See more details on using hashes here.

File details

Details for the file pylext-0.2.0-cp38-cp38-macosx_12_0_x86_64.whl.

File metadata

  • Download URL: pylext-0.2.0-cp38-cp38-macosx_12_0_x86_64.whl
  • Upload date:
  • Size: 286.8 kB
  • Tags: CPython 3.8, macOS 12.0+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for pylext-0.2.0-cp38-cp38-macosx_12_0_x86_64.whl
Algorithm Hash digest
SHA256 9f50340d875ef9413a777ef87b94a5c4fcc05140cbd4f9f9c94fa667be9a1662
MD5 59a322bedaed69359cd1f0f01449864a
BLAKE2b-256 581d2ac222db319f04b69343c5286491844c9d194328704ee97d94511b8dc615

See more details on using hashes here.

File details

Details for the file pylext-0.2.0-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: pylext-0.2.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 239.5 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for pylext-0.2.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 c9a699aa4475b5a3dd8aad97ae90af9cbc1d2118c1368bc4bd3d066395945fc7
MD5 ed5e24bb612fc392db023bad8ed8511a
BLAKE2b-256 603f97f186784408ee2e030954d8fbf99f5084e4c5b7e06cff0e7da8e8e27fc7

See more details on using hashes here.

File details

Details for the file pylext-0.2.0-cp37-cp37m-manylinux_2_31_x86_64.whl.

File metadata

  • Download URL: pylext-0.2.0-cp37-cp37m-manylinux_2_31_x86_64.whl
  • Upload date:
  • Size: 360.9 kB
  • Tags: CPython 3.7m, manylinux: glibc 2.31+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for pylext-0.2.0-cp37-cp37m-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 e2798a20750362ad699d88a19932dbb800266111c49f82a72c2c809bc2c08ef1
MD5 0c836cba88b7966999eeca39a250dc2b
BLAKE2b-256 1143d816e7ecbd3670bd04cbd993c76b3146f4535ce14a0957135980961c76c2

See more details on using hashes here.

File details

Details for the file pylext-0.2.0-cp37-cp37m-macosx_12_0_x86_64.whl.

File metadata

  • Download URL: pylext-0.2.0-cp37-cp37m-macosx_12_0_x86_64.whl
  • Upload date:
  • Size: 286.1 kB
  • Tags: CPython 3.7m, macOS 12.0+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for pylext-0.2.0-cp37-cp37m-macosx_12_0_x86_64.whl
Algorithm Hash digest
SHA256 3d6e4701e3e1b3dce004d98902b0e7a43c131d02818ba12c1827277620fea57e
MD5 6b6cf25ad731a154ddf9a921ac6206d2
BLAKE2b-256 8ff212c31b3346883b0f50f47b753e3f31de2a0d8265995ac1b14202884f34e3

See more details on using hashes here.

Supported by

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