Simple library to scan or tokenize text.
Project description
nr.parsing.core
A simple library for scanning and lexing text which makes it easy to implement new DSLs (domain specific languages).
Example (Scanning)
from nr.parsing.core import Scanner
sc = Scanner('abc + 42.0')
assert sc.getmatch(r'\w+') == 'abc'
sc.match('\s*')
assert sc.char == '+'; sc.next()
sc.match('\s*')
assert sc.getmatch(r'\d+(?:\.\d*)') == '42.0'
assert sc.char == '' # eof
Example (Lexing)
from nr.parsing.core import Scanner, Lexer, Regex, Charset
rules = [
Charset('ws', ' ', skip=True),
Charset('op', '+'),
Regex('num', r'\d+(?:\.\d*)', group=0),
Regex('id', r'\w+', group=0),
]
for tok in Lexer(Scanner('abc + 42.0'), rules):
print(tok.type, tok.value)
Copyright © 2020 Niklas Rosenstein
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
nr.parsing.core-1.1.0.tar.gz
(10.2 kB
view hashes)
Built Distribution
Close
Hashes for nr.parsing.core-1.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d0432e1f74b6edf163f1247e7d54e3aebe34d4101f7b11790011f320fe92ec8 |
|
MD5 | 6f9056101a1061c698f4257b7f2cf146 |
|
BLAKE2b-256 | a003681655fdee0808ad729ffad761eb7803829155f179d74be4a7f1037646d0 |