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
- C++ compiler supporting c++17:
- Visual Studio 2019
- gcc 8 or later
- apple clang 11 or later
- CMake 3.8 or later
- Python >= 3.6. Recommended is Python 3.8.
- Package python3-dev (for Ubuntu)
Installation:
You can install pylext from PyPI using pip:
$ pip install pylext
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pylext-0.2.1-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 125b5d5d5f40c12fbc927fa16f4704e39ed877ebd07518d12852baf4b8f383b6 |
|
MD5 | eb7a5d581188754674aeb9b3ab48cafa |
|
BLAKE2b-256 | 65bc7122f18c3f96e59d7c9b2db1e6ee9220f245d8cba2fe25723181471ffc63 |
Hashes for pylext-0.2.1-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1dc60b959d3b071c52270a48526b6956f1f05ce88b289dc7ee868e0c060b10f2 |
|
MD5 | 02100a15db0a8ca9a6a67bbd932d1260 |
|
BLAKE2b-256 | 0fd62af797af73b9d1e8fe9244f5f4305a2a232cdc2ef4f0746cc514d323a954 |
Hashes for pylext-0.2.1-cp310-cp310-manylinux_2_31_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | edb5f6b48c5155209e856d9343ba0b404901c74197938ee27ce5d7f69739c8d9 |
|
MD5 | 43dbfea03ba9592df9327330c9883171 |
|
BLAKE2b-256 | 472045c2a6765268fb1ab94eb864d1b4e12f40c490d7e1016aad8f1c3a05d9ac |
Hashes for pylext-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f128d8e2c4f3523c5d7b5b044f4222c14f195fb6382641dcb76416231dce210 |
|
MD5 | eadb039d6d7d4b3786e5d0f22d1b2a84 |
|
BLAKE2b-256 | 2101964e7c263709e9d024894d3bc0eb449e743693cdfa6a402f43083f4f160c |
Hashes for pylext-0.2.1-cp310-cp310-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c943c0f3d2870db2d471c5828edd92337c3773523f4e20b3291b06ca36f999f |
|
MD5 | 7ec72ac04656f8c9ccb9f70c21b01ab5 |
|
BLAKE2b-256 | ccf55fce28b906861243a08c1c29ccef66b4f8c88d2d118fe415c3061e7dcb41 |
Hashes for pylext-0.2.1-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 202c9536ecc6ce3dcc71ea4d10214caf9396181e765577c76580b4242ddd5292 |
|
MD5 | 4da6c1971dfbaf90d3943188427f7bf3 |
|
BLAKE2b-256 | 024cf3efb50a0ef6a99076898ced65d186f7cfbd3a22c688e28bfce235926693 |
Hashes for pylext-0.2.1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | abcdb7473db1c742029ac020bbef0d75e97bf9de9b689d17154f9445e400247f |
|
MD5 | 9bae556c64c271ed232ba6089af1ace5 |
|
BLAKE2b-256 | e773d6156b4553fcaf94b3dda0022d39f6b7b12948c5db8b9a1554196f4420e3 |
Hashes for pylext-0.2.1-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d571695ff2ded5579de76e160114bf8c1b70348012c0b0d3033428bfca4aeb7 |
|
MD5 | f9f22091ae086efd437adcfe4ac2e7b5 |
|
BLAKE2b-256 | aeb41623168919193d95ac59897ed6b57813860b3995a6cd193903ab1b4e7360 |
Hashes for pylext-0.2.1-cp39-cp39-manylinux_2_31_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | acec06577fde63cba088d8fc14cbbd39634577b1d551a6a56eba355601ef471f |
|
MD5 | 53273d690e85abf4defaf5c4e52d2879 |
|
BLAKE2b-256 | b46d5341824699280e369b62977cba0d43262f79e09c6c9df515822859df73dc |
Hashes for pylext-0.2.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 150acf5c10ea3f8ed4c8e035d16c8e518815e5f9c1b76064192a2e0981f90a3a |
|
MD5 | 193c76a0fd0fcd97f6a9a4827bb3ce10 |
|
BLAKE2b-256 | 4074c9f2ad6c6870aaa871141a759e277e7af7c5bf5b1e4d8200448ca422635c |
Hashes for pylext-0.2.1-cp39-cp39-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0dbc9bd16f43311487b1732916fa3dbf37a2f7d0a9f699703dff28fcb1ed10c |
|
MD5 | d314d619c46815b7315f33faf1a09bb8 |
|
BLAKE2b-256 | 5682c23b310a70f0dc1ccd39d4367d25de1d841a06f46eadb72a7ebecfb22d28 |
Hashes for pylext-0.2.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a93dc326419effa341e87936e238996d1530ea9902dccbfde63017b73cf1403 |
|
MD5 | 1867f3876f1dc36420addb185e9d495d |
|
BLAKE2b-256 | a5da18844a6ce18b27330d0143db7ddb6264a822fb714e75fbaf8089994b813b |
Hashes for pylext-0.2.1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60f0203b00209d57aaf9618013706fe21dc1256c4594f4b681e365ae27944650 |
|
MD5 | cc618ea332b2d5e31ede9b1a66416891 |
|
BLAKE2b-256 | 9b2fba93d159c8f801502a6a7defa0a0acffea1c1299f1a7ff88d9176d8e4ef4 |
Hashes for pylext-0.2.1-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f189256d30a8fd6a49379220d1c0b68ae8d584fc7b178d78c9bffaf223a98a9 |
|
MD5 | 0ae9238a8c761b3c7d52cda99d640112 |
|
BLAKE2b-256 | 68d113bfac744c52896973a9f288883e6617dc5b24bb89834d78617c4120ffdf |
Hashes for pylext-0.2.1-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 49f765ae7ddadf77f0097d300f0cc94b3a59e1e7ee7a209217e1efbef175d7ed |
|
MD5 | 4802effeb199f953afe9440f07fd34c1 |
|
BLAKE2b-256 | f3005e1410f334f0cf03db24992116b0632e7ad927e60e6ecb65bf1c73d82c21 |
Hashes for pylext-0.2.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0a0f250d2aaaa2d3acbe48e6afc33af20a25306536f706e2721387aa4ce90c1 |
|
MD5 | 7a61a43a0f33ce002a2f0e1f7d70ee3c |
|
BLAKE2b-256 | b30fd053b48b569e97f411fba1b2be7e81e4da55901cff66587e84e530a72a01 |
Hashes for pylext-0.2.1-cp38-cp38-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 47a01b601b9a85bd37fc7399ba750aa0f76f8d5df556a7c7d47a8f2aa8b93792 |
|
MD5 | 734d08145069344da676eede480006d7 |
|
BLAKE2b-256 | 423fddbd80b61a90459bf1f89c88fab45cdf42b370a11559f9cb39b7c01dc375 |
Hashes for pylext-0.2.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | da316a70d3da640cdc056984bdc61ce4b91e64ce26b13a0b5c961cf3aa5ae24a |
|
MD5 | 27341c484c2c00c2daefc04355c1aa1d |
|
BLAKE2b-256 | 0b7788526fd90ab12c0a48b3aad867cdbb8036f5228ddc7b4192b82b501130b3 |
Hashes for pylext-0.2.1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 816207eef30a2df0ef09fa32f691a7ce5c34096b36328e129d2ea7eee4bee4b7 |
|
MD5 | d4c433b5482419b3f553131e92caf4cd |
|
BLAKE2b-256 | 854bca477ad8da81fde94bac67db8306c58f66952db859ddd32a16d95370c74f |
Hashes for pylext-0.2.1-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 762bbb50335a1c00de947f64eb88a8414eed0bcf6adcaca69d9a3c83f0041ec3 |
|
MD5 | 74407fbfa81a434b0abf35fafabd1387 |
|
BLAKE2b-256 | 1a24838278f2133b2b8f136deb0d4c6f65ef0d86fb7db9e6af9ae150d8a08331 |
Hashes for pylext-0.2.1-cp37-cp37m-manylinux_2_31_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d0382a0570498c2b0cfbe8a5e431190ded9c2060bd23b4615b2d6f306b5d379 |
|
MD5 | 4f3ffd63c4238ce0d5c4a7be84221f09 |
|
BLAKE2b-256 | 1d9ed2e8b56b85408560aa3b30660646a50cf53d5e943b8f5adcaea127ab6184 |
Hashes for pylext-0.2.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d133164173284ecaaec632aa8b27565b89a77689e75cfc1d3211242008bc7f0e |
|
MD5 | bef6c67661dd8d943fe5914b3189abd5 |
|
BLAKE2b-256 | 97009967a136ab693f94e1d6f3923cc08536e4e32c4648467681adf7bbb1af93 |
Hashes for pylext-0.2.1-cp37-cp37m-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fdc50ed3f7d7e2a500cf22d0a72f04dfb0bd334376776606c68a63b39c4eb517 |
|
MD5 | 49741e5228ee9c4d6f55ce0bb857aa4a |
|
BLAKE2b-256 | 51578290849b849f3338cb780a81460aaf91ee675506ebcf59f11046d6837ff9 |
Hashes for pylext-0.2.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4dae1c31c142f375761d41c7b63c742367bd6b0508fc869c7561ef73c61572e3 |
|
MD5 | 352cf23f60e6bda3fb29edc77120bc8f |
|
BLAKE2b-256 | 6ac4596832869513cd891b1f198b1d0de3ec537e934c9f437174e8bb127897a2 |
Hashes for pylext-0.2.1-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 90a32c07145598d12da46a88a49235b13129347fa1f00b1185e32c9bf45debb5 |
|
MD5 | 92e8808a7de3ce7d2dbb000644606a1d |
|
BLAKE2b-256 | f4bf96abda73a99ab7f5c9eba200acb8341f8dcc7cb506495051dca535219329 |
Hashes for pylext-0.2.1-cp36-cp36m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0fa352584b0cae300dba0745be44fd0690af0a24a52c4131b5b2bcea706b428c |
|
MD5 | 5032e098da8973e2b098502748492c10 |
|
BLAKE2b-256 | 4ba0b4a9469fa261517633166b11c9e113ee3d3eb9b8302cb96001cb7b15abf7 |
Hashes for pylext-0.2.1-cp36-cp36m-manylinux_2_31_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a5dae430e731a9a9f018b97ed29ef8a92b8594d8bbab38a785da0e76682cba2 |
|
MD5 | b2b099619c5f678cb8bf61684b265e7b |
|
BLAKE2b-256 | a1a2064ca2cfd068f8ffd3f8d94961991e0dc4534cad508695643e06482b1f95 |
Hashes for pylext-0.2.1-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2bdd88e56e5c4799013d40b2fbdf5875eb282a052794e70771da2dbb76b76a4f |
|
MD5 | d50079d3e090ed56b0c77482965c5290 |
|
BLAKE2b-256 | 49af83bf9c7c0b4cb7ab668e998a18288d3cc4f336dc782cf94bddf722378b77 |
Hashes for pylext-0.2.1-cp36-cp36m-macosx_10_16_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 87a84a4d4de80d1cab282caab4ebc86d269b52261c5a299e69aefa7f4000b9a3 |
|
MD5 | 94bff9bd6af2f2779afd3c3d757eaee1 |
|
BLAKE2b-256 | 6b1bd5ca91f9b951e3f973b076dd0a22a88a9437468eb82449bff5848074073d |
Hashes for pylext-0.2.1-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d6916943320fb5d709315e99476b1d832bb95ddf91b1bcee81fdbf93dfa9ad2 |
|
MD5 | d1191667cac21fc29c6f18b9bd19c8b1 |
|
BLAKE2b-256 | 5cb7ba404a738332dbb3d50cfef44ef0f73091af70acb2f874e08ea81d1dfecc |