A generic pattern-based Lexer/tokenizer tool.
Project description
Generic Lexer
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"'
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
generic_lexer-1.1.1.tar.gz
(7.6 kB
view details)
Built Distribution
File details
Details for the file generic_lexer-1.1.1.tar.gz
.
File metadata
- Download URL: generic_lexer-1.1.1.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | afaf5701b446a98d2c186301b0e7d39ea8ec4da16afdca03ca698f9f4b741a4b |
|
MD5 | 1d3cdfaf844b06aca235d88aa3c915ef |
|
BLAKE2b-256 | 1b711b3bcf6a314f64614ccc7a17a82e063ba82cd7924bcbce546d8b95df2c7a |
File details
Details for the file generic_lexer-1.1.1-py2.py3-none-any.whl
.
File metadata
- Download URL: generic_lexer-1.1.1-py2.py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 2, Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 10c9331e365d0c4d01529dbc699e62cd0281ef618e145f2e54e15b4e69d8bcbd |
|
MD5 | 24649decdedaa3eca66e9e7ac0c9f171 |
|
BLAKE2b-256 | 528b07a438b8b31b46f016f6473a1900927008e62057c7a6b7d87dcfb63c7714 |