From df3c6d007a3f572b73bb94a19a8477dccd4a5cae Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Sat, 3 Dec 2022 03:32:47 +0100 Subject: [PATCH] I don't get it --- ziffers/__pycache__/ebnf.cpython-310.pyc | Bin 811 -> 856 bytes ziffers/__pycache__/parser.cpython-310.pyc | Bin 1794 -> 2070 bytes ziffers/ebnf.py | 3 +- ziffers/parser.py | 59 +++++++++++++-------- 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/ziffers/__pycache__/ebnf.cpython-310.pyc b/ziffers/__pycache__/ebnf.cpython-310.pyc index 7a2358c828ebfde036cbfee3b72800642c76b28c..b18163ad26d5bd9c4487aaadb5818fdafe0023aa 100644 GIT binary patch delta 111 zcmZ3@c7u&KpO=@50SInw=t{mikypp)C=&w%mjVz}Wu~R278NVlDrh7n7Afc})1`AtMq1 delta 45 ycmcb?wwjGMpO=@50SF?NbS1Bt$g9IUgNcEGO92Q{D+-Dxddf`93)pzSk_i9=nheVT diff --git a/ziffers/__pycache__/parser.cpython-310.pyc b/ziffers/__pycache__/parser.cpython-310.pyc index 220598cf2f9a9937cb0fca88c20f63bf33d901f1..906f8863020697150cb3c94e7ae7eb6d3c55df6c 100644 GIT binary patch literal 2070 zcmZ`)OK;mo5Z>iWv>vVyxK7P*=wIQrr<`-^q38@LTQcG{j_G}ni+>V{Z@f;XCUQJNp98=GI zKzNP2CxpAgJT|=*z9QWOgB{YLMeTtKk|a=l}AGIA&S}2LQWZLCRTg z6vdq7G7)+hr2{5Mn#n#3SSXV$7P>lh5X2F`P<)QnB2pDzXmsLW=y%wpxy9}cLAMg= z4l8PtD%J`)usgZ=R0;mFy;3 zm*|AlLOLUz-ZVi>=`Yx6L?4qgx_f4LD~PY31_|6=iz9tzs#Q3s-a&!hly|DcyD%-> z%3KzH1hY4&y-6q6uXHI3|JNIJ9aX=FVgtnu6o}rErDkPGL89NZEP*Hfk3mZ`y#v?9} zQ9(LpOEqZ0&mWZR1-*C-S~R2t>U%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^piH*g28wY1^8&ewWfrlv^DW*|{GEm8QVirml!8qd!)}1MTT9(>#GwzLJUcpib`rZ?pTIhbT}uS;_Brl&rIm_(t1I;uY75v0moH%& zJQ-4thE${OIO@j_@y={s7Fk)F@yo8`YveY5uw%leXEXxW*k)TlNuXIKiT(mrcysHR kN^5-6_Vz6l|8ds_-*p-I9vLyHACLhZQ0Q@N#2)Sc3%OOJa{vGU diff --git a/ziffers/ebnf.py b/ziffers/ebnf.py index 7297307..f2a67a3 100644 --- a/ziffers/ebnf.py +++ b/ziffers/ebnf.py @@ -1,8 +1,9 @@ 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) + subdiv = (lbra ziffers rbra) chord = pc{2,} pc = (neg_pc / pc_basic) diff --git a/ziffers/parser.py b/ziffers/parser.py index 8ee931a..d790119 100644 --- a/ziffers/parser.py +++ b/ziffers/parser.py @@ -1,5 +1,6 @@ from parsimonious.grammar import Grammar from parsimonious.nodes import NodeVisitor +from rich import print from .ebnf import ebnf __all__ = ('ZiffersVisitor', 'parse_expression',) @@ -7,31 +8,47 @@ __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)} + """ + Visitor for the Ziffers syntax. + """ - def visit_section(self, node, visited_children): - """ Gets the section name. """ - _, section, *_ = visited_children - return section.text + def visit_ziffers(self, node, children): + """ + Top-level visiter. Will traverse and build something out of a complete and valid + Ziffers expression. + """ + print(f"Node: {node}, Children: {children}") + result = {'ziffers': []} - def visit_pair(self, node, visited_children): - """ Gets each key/value pair, returns a tuple. """ - key, _, value, *_ = node.children - return key.text, value.text + for i in children: + if i[0] in (None, [], {}) and isinstance(i[0], dict): + continue + 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): - """ The generic visit method. """ - return visited_children or node + def visit_pc(self, node, children): + return {node.expr_name: node.text} + + 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: tree = GRAMMAR.parse(expression)