Skip to main content

A simple lexer based on regular expressions

Project description

Lexery

A simple lexer based on regular expressions.

Inspired by https://eli.thegreenplace.net/2013/06/25/regex-based-lexical-analysis-in-python-and-javascript

Usage

>>> import lexery
>>> import re
>>> text = 'crop \t   ( 20, 30, 40, 10 ) ;'
>>>
>>> lexer = lexery.Lexer(
...     rules=[
...         lexery.Rule(identifier='identifier', pattern=re.compile(r'[a-zA-Z_][a-zA-Z_]*')),
...         lexery.Rule(identifier='lpar', pattern=re.compile(r'\(')),
...         lexery.Rule(identifier='number', pattern=re.compile(r'[1-9][0-9]*')),
...         lexery.Rule(identifier='rpar', pattern=re.compile(r'\)')),
...         lexery.Rule(identifier='comma', pattern=re.compile(r',')),
...         lexery.Rule(identifier='semi', pattern=re.compile(r';'))
...     ],
...     skip_whitespace=True)
>>>
>>> tokens = lexer.lex(text=text)
>>> tokens
[[Token('identifier', 'crop', 0, 0), Token('lpar', '(', 9, 0), Token('number', '20', 11, 0),
Token('comma', ',', 13, 0), Token('number', '30', 15, 0), Token('comma', ',', 17, 0),
Token('number', '40', 19, 0), Token('comma', ',', 21, 0), Token('number', '10', 23, 0), Token('rpar', ')', 26, 0),
Token('semi', ';', 28, 0)]]

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

lexery-1.0.2.tar.gz (3.0 kB view details)

Uploaded Source

File details

Details for the file lexery-1.0.2.tar.gz.

File metadata

  • Download URL: lexery-1.0.2.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.5.2

File hashes

Hashes for lexery-1.0.2.tar.gz
Algorithm Hash digest
SHA256 1fa5373193e80d3c0839e46913b6f328310cef6eaf7c1c467579b24aeb18f7ae
MD5 1e40ea991ca17f127e8a5f63b5ad8694
BLAKE2b-256 b0371bb904586fd3077553abb08cbd4070cff510cc569243aeefeb564b9362dc

See more details on using hashes here.

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