Skip to main content

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)

Uploaded Source

Built Distribution

nr.parsing.core-1.1.0-py2.py3-none-any.whl (10.3 kB view hashes)

Uploaded Python 2 Python 3

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