]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Add pretty printer for JSON grammar
authorPetr Špaček <pspacek@isc.org>
Mon, 9 May 2022 16:25:18 +0000 (18:25 +0200)
committerPetr Špaček <pspacek@isc.org>
Fri, 1 Jul 2022 06:59:22 +0000 (08:59 +0200)
It produces the same format as cfg_test --grammar. The advantage is that
it allows to print any node in configuration the tree, not just whole
blocks.

doc/misc/checkgrammar.py

index 157944e0902a6c7fc8ddd4713892f860ec8c9d53..57f4c8ede5f8f24018411bda97dd9ba492eca4cb 100644 (file)
@@ -20,7 +20,6 @@ vs. '( unlimited | <duration> ) used in options.
 
 from collections import namedtuple
 from itertools import groupby
-from pprint import pformat
 import fileinput
 
 import parsegrammar
@@ -101,6 +100,35 @@ def diff_statements(whole_grammar, places):
     return out
 
 
+def pformat_grammar(node, level=1):
+    """Pretty print a given grammar node in the same way as cfg_test would"""
+    if "_grammar" in node:  # no nesting
+        assert "_id" not in node
+        assert "_mapbody" not in node
+        out = node["_grammar"] + ";"
+        if "_flags" in node:
+            out += " // " + ", ".join(node["_flags"])
+        return out + "\n"
+
+    # a nested map
+    out = ""
+    indent = level * "\t"
+    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)
+        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"])
+    return out + "\n"
+
+
 def main():
     """
     Ingest output from cfg_test --grammar and print out statements which use
@@ -117,7 +145,7 @@ def main():
                 print(
                     "- path:", ", ".join(" -> ".join(variant.path) for variant in group)
                 )
-                print(" ", pformat(group[0].subgrammar))
+                print(" ", pformat_grammar(group[0].subgrammar, level=1))
             print()