Pretty-print grammar for zones

It turns out the tree of dictionaries is not the best structure to
represent our grammar, unfortunatelly. The problem is that "zone" has
several context-dependent variants which change meaning of "zone" based
on inner field "type".

Redesigning the whole structure does not seem to be worth, so I settled
on this terrible hack.
This commit is contained in:
Petr Špaček
2022-06-24 15:05:02 +02:00
parent 435cbb61ba
commit 0392144e99
3 changed files with 19 additions and 11 deletions

View File

@@ -173,8 +173,9 @@ def domain_factory(domainname, domainlabel, todolist, grammar):
else:
separator = ""
paths = ""
subgrammar = grammar_grp[0].subgrammar
grammar_txt = (
self.isc_name
subgrammar.get("_pprint_name", self.isc_name)
+ " "
+ checkgrammar.pformat_grammar(grammar_grp[0].subgrammar, level=1)
)

View File

@@ -29,8 +29,9 @@ def read_zone():
assert len(zonegrammar) == 1
assert "zone" in zonegrammar
zone_grammars[zone_type] = zonegrammar["zone"]
zone_grammars[zone_type]["_pprint_name"] = "zone"
return {"zone": {"_mapbody": zone_grammars}}
return {"zone": {"_mapbody": zone_grammars, "_ignore_this_level": True}}
def read_main():

View File

@@ -113,19 +113,25 @@ def pformat_grammar(node, level=1):
# a nested map
out = ""
indent = level * "\t"
if "_id" in node:
out += node["_id"] + " "
out += "{\n"
if not node.get("_ignore_this_level"):
if "_id" in node:
out += node["_id"] + " "
out += "{\n"
for key in node["_mapbody"]:
out += f"{indent}{key}"
inner_grammar = pformat_grammar(node["_mapbody"][key], level=level + 1)
for key, subnode in node["_mapbody"].items():
if not subnode.get("_ignore_this_level"):
out += f"{indent}{subnode.get('_pprint_name', key)}"
inner_grammar = pformat_grammar(node["_mapbody"][key], level=level + 1)
else: # act as if we were not in a map
inner_grammar = pformat_grammar(node["_mapbody"][key], level=level)
if inner_grammar[0] != ";": # we _did_ find some arguments
out += " "
out += inner_grammar
out += indent[:-1] + "};" # unindent the closing bracket
if "_flags" in node:
out += " // " + ", ".join(node["_flags"])
if not node.get("_ignore_this_level"):
out += indent[:-1] + "};" # unindent the closing bracket
if "_flags" in node:
out += " // " + ", ".join(node["_flags"])
return out + "\n"