Skip to main content

Generic Lexer

image image image image image

A generic pattern-based Lexer/tokenizer tool.

The minimum python version is 3.6

Original Author
Eli Bendersky with this gist last modified on 2010/08
Maintainer
Leandro Benedet Garcia last modified on 2020/11
Version
1.1.0
License
The Unlicense
Documentation
The documentation can be found here

Example

If we try to execute the following code:

from generic_lexer import Lexer


rules = {
    "VARIABLE": r"(?P<var_name>[a-z_]+): (?P<var_type>[A-Z]\w+)",
    "EQUALS": r"=",
    "SPACE": r" ",
    "STRING": r"\".*\"",
}

data = "first_word: String = \"Hello\""
data = data.strip()

for curr_token in Lexer(rules, False, data):
    print(curr_token)

Will give us the following output:

VARIABLE({'var_name': 'first_word', 'var_type': 'String'}) at 0
SPACE( ) at 18
EQUALS(=) at 19
SPACE( ) at 20
STRING("Hello") at 21

As you can see differently from the original gist, we are capable of specifying multiple groups per token. You cannot use the same group twice, either per token or not because all the regex patterns are merged together to generate the tokens later on.

You may get the values of the tokens this way:

>>> from generic_lexer import Lexer
>>> rules = {
...     "VARIABLE": r"(?P<var_name>[a-z_]+): (?P<var_type>[A-Z]\w+)",
...     "EQUALS": r"=",
...     "STRING": r"\".*\"",
... }
>>> data = "first_word: String = \"Hello\""
>>> variable, equals, string = tuple(Lexer(rules, True, data))

>>> variable
VARIABLE({'var_name': 'first_word', 'var_type': 'String'}) at 0

>>> variable.val
{'var_name': 'first_word', 'var_type': 'String'}
>>> variable["var_name"]
'first_word'
>>> variable["var_type"]
'String'

>>> equals
EQUALS(=) at 19

>>> equals.val
'='

>>> string
STRING("Hello") at 21

>>> string.val
'"Hello"'

Metadata

Release files for generic-lexer 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for generic-lexer 1.1.1
File Size Uploaded
generic_lexer-1.1.1.tar.gz 7.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for generic-lexer 1.1.1
File Interpreter ABI Platform
generic_lexer-1.1.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 15.1 kB

Release files / generic_lexer-1.1.1.tar.gz

Download URL generic_lexer-1.1.1.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
afaf5701b446a98d2c186301b0e7d39ea8ec4da16afdca03ca698f9f4b741a4b
BLAKE2b-256 checksum
How to use checksums
1b711b3bcf6a314f64614ccc7a17a82e063ba82cd7924bcbce546d8b95df2c7a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

Release files / generic_lexer-1.1.1-py2.py3-none-any.whl

Download URL generic_lexer-1.1.1-py2.py3-none-any.whl
Size 7.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
10c9331e365d0c4d01529dbc699e62cd0281ef618e145f2e54e15b4e69d8bcbd
BLAKE2b-256 checksum
How to use checksums
528b07a438b8b31b46f016f6473a1900927008e62057c7a6b7d87dcfb63c7714
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page