Initialise
This commit is contained in:
1
ziffers/__init__.py
Normal file
1
ziffers/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from .parser import *
|
||||
BIN
ziffers/__pycache__/__init__.cpython-310.pyc
Normal file
BIN
ziffers/__pycache__/__init__.cpython-310.pyc
Normal file
Binary file not shown.
BIN
ziffers/__pycache__/ebnf.cpython-310.pyc
Normal file
BIN
ziffers/__pycache__/ebnf.cpython-310.pyc
Normal file
Binary file not shown.
BIN
ziffers/__pycache__/example.cpython-310.pyc
Normal file
BIN
ziffers/__pycache__/example.cpython-310.pyc
Normal file
Binary file not shown.
BIN
ziffers/__pycache__/parser.cpython-310.pyc
Normal file
BIN
ziffers/__pycache__/parser.cpython-310.pyc
Normal file
Binary file not shown.
29
ziffers/ebnf.py
Normal file
29
ziffers/ebnf.py
Normal file
@ -0,0 +1,29 @@
|
||||
ebnf = r"""
|
||||
expr = (number ws?)*
|
||||
number = factor additive*
|
||||
additive = ("+"/"-") factor
|
||||
factor = primary multiplicative*
|
||||
multiplicative = ("*" / "/") primary
|
||||
primary = parens / neg / number
|
||||
parens = "(" number ")"
|
||||
neg = "-" primary
|
||||
number = ((~"[0-9]"+ "."? ~"[0-9]"*) / ("." ~"[0-9]"+)) (("e"/"E") ("-"/"+") ~"[0-9]"+)?
|
||||
ws = ~"\s*"
|
||||
lpar = "("
|
||||
rpar = ")"
|
||||
lbra = "["
|
||||
rbra = "]"
|
||||
lcbra = "{"
|
||||
rcbra = "}"
|
||||
lt = "<"
|
||||
gt = ">"
|
||||
comma = ","
|
||||
octup = "^"
|
||||
octdown = "_"
|
||||
barsign = "|"
|
||||
plus = "+"
|
||||
minus = "-"
|
||||
times = "*"
|
||||
div = "/"
|
||||
emptyline = ws+
|
||||
"""
|
||||
40
ziffers/parser.py
Normal file
40
ziffers/parser.py
Normal file
@ -0,0 +1,40 @@
|
||||
from parsimonious.grammar import Grammar
|
||||
from parsimonious.nodes import NodeVisitor
|
||||
from .ebnf import ebnf
|
||||
|
||||
__all__ = ('ZiffersVisitor', 'parse_expression',)
|
||||
|
||||
GRAMMAR = Grammar(ebnf)
|
||||
|
||||
class ZiffersVisitor(NodeVisitor):
|
||||
def visit_expr(self, node, visited_children):
|
||||
""" Returns the overall output. """
|
||||
output = {}
|
||||
for child in visited_children:
|
||||
output.update(child[0])
|
||||
return output
|
||||
|
||||
def visit_entry(self, node, visited_children):
|
||||
""" Makes a dict of the section (as key) and the key/value pairs. """
|
||||
key, values = visited_children
|
||||
return {key: dict(values)}
|
||||
|
||||
def visit_section(self, node, visited_children):
|
||||
""" Gets the section name. """
|
||||
_, section, *_ = visited_children
|
||||
return section.text
|
||||
|
||||
def visit_pair(self, node, visited_children):
|
||||
""" Gets each key/value pair, returns a tuple. """
|
||||
key, _, value, *_ = node.children
|
||||
return key.text, value.text
|
||||
|
||||
def generic_visit(self, node, visited_children):
|
||||
""" The generic visit method. """
|
||||
return visited_children or node
|
||||
|
||||
def parse_expression(expression: str) -> dict:
|
||||
tree = GRAMMAR.parse(expression)
|
||||
visitor = ZiffersVisitor()
|
||||
return visitor.visit(tree)
|
||||
|
||||
Reference in New Issue
Block a user