A python implementation of a lexical analyzer which supports full scan, state based lexing and lookahead
Project description
pylexer
A python implementation of a lexical analyzer which supports full scan, state based lexing and lookahead
Warning This is not a GENERATOR like classical lexer is. It does not produce any python code. It's a simple plain scanner of the given input string and tokenizer into given set of tokens by matching regular expressions. Thus, at runtime you can change the token definition and use one same code for any token set
Installation
Install in your project with pip:
pip install pylexer
Usage
An example use:
from pylexer import PyLexer
config = {
'\\s' :'',
'\\d+' :'number',
'\\+' : 'plus',
'-': 'minus',
'\\*' : 'mul',
'/' : 'div',
}
#Static Scan method that returns list of tokens
tokens = PyLexer.scan(config, '2 + 3')
map(lambda x:x.get_name(), tokens)
#PyLexer Config is a dict, so you can also use it like
lexer = PyLexer()
lexer.set_input('2 + 3')
lexer.move_next()
while lexer.get_look_ahead():
print(lexer.get_look_ahead().get_name())
lexer.move_next()
Token Definition
Tokens are defined with TokenDefinition
class that holds token name and regular expression. Token name can be empty, and in that case lexer will ignore/skip such tokens
Lexer Configuration
The lexer configuration holds a list of all token definitions. With LexerDictConfig it can be easily created from an array where keys are regular expressions and values are names of tokens
Full scan
Pylexer's static scan method can be used to scan given input string and returns a list of tokens, Pylexer can also be used to walk through scanned tokens with single look ahead
License
MIT license. See LICENSE.md
for more information.
Contributors
Pylexer is inspired from PHP's Lexer(https://github.com/tmilos/lexer) and takes code heavily from doctrine API, all credits due with Milos Tomic
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 Distribution
File details
Details for the file pylexer-1.0.0.tar.gz
.
File metadata
- Download URL: pylexer-1.0.0.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/2.7.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7173e1d730cca0a03870da8bfaf7ddd1085d1aba21f25cd78cfff4e8c892beff |
|
MD5 | 50fcb8058e5548a2f0fef7896cd13224 |
|
BLAKE2b-256 | aa93785cc297351c762bcaf0e43001e3e924581216ed03c2fe28334fe8beccea |
File details
Details for the file pylexer-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: pylexer-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/2.7.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6bab41f2469361d6b9c1d96b9c6332a8fb734fe4bcf3e2c9055e18562ecd7e27 |
|
MD5 | 172a462bbe3c7ba1c288b1fe9d9a778b |
|
BLAKE2b-256 | 605d0455a5dfc1fe2438ea88277c47396320d7962761feb7d15b176467bf1ecd |