From ca866c7e92cf23b284fe57fef6c32015cf99e0b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20=C5=A0pa=C4=8Dek?= Date: Fri, 24 Jun 2022 15:17:22 +0200 Subject: [PATCH] Sort grammar map keys while pretty printing them It would be too easy if we could just call sorted(). Thanks to zone grammar the most important key "type" gets sorted near end, so we pull it up to the top using a hack. (cherry picked from commit 5c04e3c524cc236c73e98d84d292ae4944b1cc43) --- doc/misc/checkgrammar.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/doc/misc/checkgrammar.py b/doc/misc/checkgrammar.py index 8483b2edaf..09984eceb4 100644 --- a/doc/misc/checkgrammar.py +++ b/doc/misc/checkgrammar.py @@ -102,6 +102,14 @@ def diff_statements(whole_grammar, places): def pformat_grammar(node, level=1): """Pretty print a given grammar node in the same way as cfg_test would""" + + def sortkey(item): + """Treat 'type' specially and always put it first, for zone types""" + key, _ = item + if key == "type": + return "" + return key + if "_grammar" in node: # no nesting assert "_id" not in node assert "_mapbody" not in node @@ -118,7 +126,7 @@ def pformat_grammar(node, level=1): out += node["_id"] + " " out += "{\n" - for key, subnode in node["_mapbody"].items(): + for key, subnode in sorted(node["_mapbody"].items(), key=sortkey): 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)