Files
ziffers-python/ziffers/parser.py
Raphael Forment 0816ac65db Improvements in packaging.
The ziffers.lark file is not included in the installed package. This was
preventing users from importing the ziffers package from elsewhere.
2023-02-16 07:39:51 +01:00

54 lines
1.2 KiB
Python

""" Module for the parser """
from pathlib import Path
from lark import Lark
from .classes import Ziffers
from .mapper import ZiffersTransformer
grammar_path = Path(__file__).parent
grammar_folder = Path.joinpath(grammar_path, "spec")
grammar_file = Path.joinpath(grammar_folder, "ziffers.lark")
ziffers_parser = Lark.open(
str(grammar_file),
rel_to=__file__,
start="root",
parser="lalr",
transformer=ZiffersTransformer(),
)
def parse_expression(expr: str) -> Ziffers:
"""Parse an expression using the Ziffers parser
Args:
expr (str): Ziffers expression as a string
Returns:
Ziffers: Reutrns Ziffers iterable
"""
return ziffers_parser.parse(expr)
def zparse(expr: str, **opts) -> Ziffers:
"""Parses ziffers expression with options
Args:
expr (str): Ziffers expression as a string
opts (dict, optional): Options for parsing the Ziffers expression. Defaults to None.
Returns:
Ziffers: Returns Ziffers iterable parsed with the given options
"""
parsed = parse_expression(expr)
parsed.init_opts(opts)
return parsed
# pylint: disable=invalid-name
def z(expr: str, **opts) -> Ziffers:
"""Shortened method name for zparse"""
return zparse(expr, **opts)