I don't get it

This commit is contained in:
2022-12-03 03:32:47 +01:00
parent 256424f646
commit df3c6d007a
4 changed files with 40 additions and 22 deletions

View File

@ -1,8 +1,9 @@
ebnf = r""" ebnf = r"""
expr = (bar / octup / octdown / escape / rhythm / float / chord / pc / ws?)+ ziffers = (bar / octup / octdown / subdiv / escape / rhythm / float / chord / pc / ws?)+
escape = (lt (chord / pc) gt) escape = (lt (chord / pc) gt)
subdiv = (lbra ziffers rbra)
chord = pc{2,} chord = pc{2,}
pc = (neg_pc / pc_basic) pc = (neg_pc / pc_basic)

View File

@ -1,5 +1,6 @@
from parsimonious.grammar import Grammar from parsimonious.grammar import Grammar
from parsimonious.nodes import NodeVisitor from parsimonious.nodes import NodeVisitor
from rich import print
from .ebnf import ebnf from .ebnf import ebnf
__all__ = ('ZiffersVisitor', 'parse_expression',) __all__ = ('ZiffersVisitor', 'parse_expression',)
@ -7,31 +8,47 @@ __all__ = ('ZiffersVisitor', 'parse_expression',)
GRAMMAR = Grammar(ebnf) GRAMMAR = Grammar(ebnf)
class ZiffersVisitor(NodeVisitor): 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. """ Visitor for the Ziffers syntax.
key, values = visited_children """
return {key: dict(values)}
def visit_section(self, node, visited_children): def visit_ziffers(self, node, children):
""" Gets the section name. """ """
_, section, *_ = visited_children Top-level visiter. Will traverse and build something out of a complete and valid
return section.text Ziffers expression.
"""
print(f"Node: {node}, Children: {children}")
result = {'ziffers': []}
def visit_pair(self, node, visited_children): for i in children:
""" Gets each key/value pair, returns a tuple. """ if i[0] in (None, [], {}) and isinstance(i[0], dict):
key, _, value, *_ = node.children continue
return key.text, value.text try:
result['ziffers'].append(i[0])
except Exception as e:
print(f"[red]Error in ziffers:[/red] {e}")
pass
return result
def generic_visit(self, node, visited_children): def visit_pc(self, node, children):
""" The generic visit method. """ return {node.expr_name: node.text}
return visited_children or node
def visit_escape(self, node, children):
return {node.expr_name: node.text}
# def visit_subdiv(self, node, visited_children):
#  key, values = visited_children
#  ret)rn {key, dict(values)}
def generic_visit(self, node, children):
"""
This method seem to be the generic method to include in any NodeVisitor.
Probably better to keep it as is for the moment. This is appending a whole
lot of garbage to the final expression because I don't really know how to
write it properly...
"""
return children or node
def parse_expression(expression: str) -> dict: def parse_expression(expression: str) -> dict:
tree = GRAMMAR.parse(expression) tree = GRAMMAR.parse(expression)