No project description provided
Project description
rbnf-rts
Runtime support for generated parsers of RBNF.hs
Native Dependencies
-
The Haskell Stack Toolchain
-
The executable
rbnf-pgen
in PATH.If
~/.local/bin
is already in PATH:git clone https://github.com/thautwarm/RBNF.hs cd RBNF.hs stack install .
Usage
Check test directory:
import operator
from rbnf_rts.rbnf_prims import link, Tokens, State
from rbnf_rts.lexical import *
from rbnf_rts.rbnf_api import codegen
from rbnf_rts.token import Token
from pathlib import Path
import tempfile
import ast
from subprocess import CalledProcessError
grammar_file = "./grammar.rbnf"
py_file = "./py.py"
with tempfile.TemporaryDirectory() as tdir:
tdir = Path(tdir)
with (tdir / grammar_file).open("w") as f:
f.write("""
Mul ::= !lhs=<Mul> "*" !rhs=<Atom> -> mul(lhs, rhs);
Mul ::= !a=<Atom> -> a;
Atom ::= "(" !a=<Mul> ")" -> a;
Atom ::= !a=number -> unwrap(a);
TOP ::= BOF !a=<Mul> EOF -> a;
""")
try:
codegen((tdir/grammar_file), (tdir/py_file), k=1, inline=False, traceback=True)
except CalledProcessError as e:
print(e.stderr)
exit(0)
raise Exception
with (tdir / py_file).open('r') as f:
code = f.read()
gencode = ast.parse(code)
lexicals, run_lexer = lexer(
r(number='\d+'),
l["*"],
l(space=" "),
l['('],
l[')'],
ignores=['space']
)
def unwrap(x: Token):
return int(x.value)
scope = link(lexicals, gencode, scope=dict(unwrap=unwrap, mul=operator.mul), filename=py_file)
tokens = list(run_lexer("<current file>", "1 * 2 * (3 * 4)"))
got = scope['parse_TOP'](State(), Tokens(tokens))
print(got)
Got (True, 24)
, where True
indicates the parsing succeeded.
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 Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
rbnf_rts-0.2-py3-none-any.whl
(21.7 kB
view hashes)