haldis/app/hlds/__init__.py

39 lines
917 B
Python
Raw Normal View History

2019-12-01 01:04:13 +01:00
#!/usr/bin/env python3
from os import path
from tatsu import parse as tatsu_parse
# TODO Use proper way to get resources, see https://stackoverflow.com/a/10935674
with open(path.join(path.dirname(__file__), "hlds.tatsu")) as fh:
GRAMMAR = fh.read()
2020-01-24 14:47:53 +01:00
def kind_comparer(compare_to):
return lambda item: item["kind"] == compare_to
2019-12-01 01:04:13 +01:00
def parse(menu):
2020-01-24 14:47:53 +01:00
parsed = tatsu_parse(GRAMMAR, menu)
return dict((
*((att["key"], att["value"]) for att in parsed["attributes"]),
("id", parsed["id"]),
("name", parsed["name"]),
("choices", filter(kind_comparer("choice_declaration"), parsed["items_"])),
("bases", filter(kind_comparer("base"), parsed["items_"])),
))
2019-12-01 01:04:13 +01:00
def main(filename):
import json
from tatsu.util import asjson
with open(filename) as fh:
ast = parse(fh.read())
print(json.dumps(asjson(ast), indent="\t"))
if __name__ == "__main__":
import sys
main(*sys.argv[1:])