Skip to main content

PyLExt allows to add new syntax extensions into python language.

Project description

PyLExt provides add in 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 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.1.0.tar.gz (65.6 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.1.0-cp39-cp39-win_amd64.whl (232.9 kB view details)

Uploaded CPython 3.9Windows x86-64

pylext-0.1.0-cp39-cp39-macosx_10_15_x86_64.whl (277.6 kB view details)

Uploaded CPython 3.9macOS 10.15+ x86-64

pylext-0.1.0-cp38-cp38-win_amd64.whl (232.9 kB view details)

Uploaded CPython 3.8Windows x86-64

pylext-0.1.0-cp38-cp38-macosx_10_15_x86_64.whl (276.0 kB view details)

Uploaded CPython 3.8macOS 10.15+ x86-64

pylext-0.1.0-cp37-cp37m-win_amd64.whl (231.1 kB view details)

Uploaded CPython 3.7mWindows x86-64

pylext-0.1.0-cp37-cp37m-macosx_10_15_x86_64.whl (277.2 kB view details)

Uploaded CPython 3.7mmacOS 10.15+ x86-64

File details

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

File metadata

  • Download URL: pylext-0.1.0.tar.gz
  • Upload date:
  • Size: 65.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.3

File hashes

Hashes for pylext-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d4eaa0f430eeeed600578b70cb8f3940dff560cba304f87a75f434727ccba4ca
MD5 18bdb3b3d078819a13087b2076ec1e8f
BLAKE2b-256 53faac5f7aca7c7003547ac0703be2d373050ecbcaeb8337b57d7c7fac99ab4d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pylext-0.1.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 232.9 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.3

File hashes

Hashes for pylext-0.1.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 e2f865be086b89cccc082a05fd99463b5791cd6caee227ca1eb2bed544af611b
MD5 314b9540677b3bd9f61c5e4f77f02c09
BLAKE2b-256 a4f1b19ce2860e97a06848c0bc2711564611cbb7f27f30008cc47ec01e68d10a

See more details on using hashes here.

File details

Details for the file pylext-0.1.0-cp39-cp39-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: pylext-0.1.0-cp39-cp39-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 277.6 kB
  • Tags: CPython 3.9, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.3

File hashes

Hashes for pylext-0.1.0-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 aa0aeba2be0aff9b4674e822f105c4a25dce760d77ed974d46cc80e611cffb43
MD5 fa5654d8b3510578d4d860f34afae9ff
BLAKE2b-256 c4d8b31d861c7500b1a173498de5aa6effd6b827e3a542dcbb54683afd71f30d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pylext-0.1.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 232.9 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.3

File hashes

Hashes for pylext-0.1.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 98b254d5298a3ee71d1014e6d763079bd57a4f37b9eb8fadd989829949450e34
MD5 21d9614c9852b30c8e0f51e31039b5a2
BLAKE2b-256 4e70fa4022d030629b89e7a207807ca02483c245bdc1b0d13170f9c238f70e57

See more details on using hashes here.

File details

Details for the file pylext-0.1.0-cp38-cp38-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: pylext-0.1.0-cp38-cp38-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 276.0 kB
  • Tags: CPython 3.8, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.3

File hashes

Hashes for pylext-0.1.0-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 12aac93a101fa111c6f162c713ed823e449fa8fabb3869fe6d64d8c663681c0e
MD5 94fc33f4e7517ad73f1c2aa644d53e23
BLAKE2b-256 54f021bd0adcad855fd5a34999c079a8e1722a620b608bb86ef608d96520b3fc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pylext-0.1.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 231.1 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.3

File hashes

Hashes for pylext-0.1.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 ce8822b5b2428ba8c3141544836e34b4066121dc212cc0db5e6ebfbbee6d8da0
MD5 5f300e99b7e8976d1b69f15fa72f899a
BLAKE2b-256 4b1ea3622105a3dd00c68b14d84e65c7d8c92bc7c2ffa482275a13d2971398b5

See more details on using hashes here.

File details

Details for the file pylext-0.1.0-cp37-cp37m-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: pylext-0.1.0-cp37-cp37m-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 277.2 kB
  • Tags: CPython 3.7m, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.3

File hashes

Hashes for pylext-0.1.0-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 d91d62d4d8e1199b8bb24ba4035dbaee09edd97e07aeaacfdb54aeacfc5488f8
MD5 f118f7e3fb7b4cd78fe2f789cff0ab13
BLAKE2b-256 a5f05ce18afa89012d68d77e1f03a62616717071853a0bd83aae3f4b3c9a440b

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