You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
158 lines
3.5 KiB
158 lines
3.5 KiB
# -*- coding: utf-8 -*-
|
|
# This file is under GNU Affero General Public License 3.0
|
|
# see LICENSE.txt
|
|
|
|
from . import utils
|
|
|
|
|
|
def generate_yml(cid, module_name):
|
|
ret = ""
|
|
ret += _grammar_header()
|
|
ret += "module " + module_name + " {\n"
|
|
ret += _generate_functions(cid)
|
|
ret += _generate_structs(cid)
|
|
ret += _generate_enums(cid)
|
|
ret += "}"
|
|
return ret
|
|
|
|
|
|
def _grammar_header():
|
|
grammar = """
|
|
decl module @name;
|
|
decl method @name;
|
|
|
|
decl struct @name;
|
|
decl field @name;
|
|
|
|
decl enum @name;
|
|
decl item @name;
|
|
|
|
"""
|
|
return grammar
|
|
|
|
|
|
def _generate_functions(cid):
|
|
data_root = cid["functions"]
|
|
|
|
# Main
|
|
tmpl_main = ""
|
|
tmpl_main += 'method {name} {{\n'
|
|
tmpl_main += ' return type="{return_type}";\n'
|
|
tmpl_main += '{subitems}'
|
|
tmpl_main += '}};\n\n'
|
|
|
|
kind_main = "CursorKind.FUNCTION_DECL"
|
|
|
|
def format_func_main(tmpl, item, subitems_str):
|
|
return tmpl.format(
|
|
name=item["name"],
|
|
return_type=item["result_type"],
|
|
subitems=subitems_str)
|
|
|
|
# Sub
|
|
tmpl_sub = ""
|
|
tmpl_sub += ' use {name} type="{type}";'
|
|
|
|
kind_sub = "CursorKind.PARM_DECL"
|
|
|
|
def format_func_sub(tmpl, item):
|
|
return tmpl.format(
|
|
type=item["type"],
|
|
name=item["name"])
|
|
|
|
ret = _format(data_root, tmpl_main, kind_main, format_func_main, tmpl_sub, kind_sub, format_func_sub)
|
|
|
|
return ret
|
|
|
|
|
|
def _generate_structs(cid):
|
|
data_root = cid["types"]
|
|
|
|
# Main
|
|
tmpl_main = ""
|
|
tmpl_main += 'struct {name} {{\n'
|
|
tmpl_main += '{subitems}'
|
|
tmpl_main += '}};\n\n'
|
|
|
|
kind_main = "CursorKind.STRUCT_DECL"
|
|
|
|
def format_func_main(tmpl, item, subitems_str):
|
|
return tmpl.format(
|
|
name=item["name"],
|
|
subitems=subitems_str)
|
|
|
|
# Sub
|
|
tmpl_sub = ""
|
|
tmpl_sub += ' field {name} type="{type}";'
|
|
kind_sub = "CursorKind.FIELD_DECL"
|
|
|
|
def format_func_sub(tmpl, item):
|
|
return tmpl.format(
|
|
type=item["type"],
|
|
name=item["name"])
|
|
|
|
ret = _format(data_root, tmpl_main, kind_main, format_func_main, tmpl_sub, kind_sub, format_func_sub)
|
|
|
|
return ret
|
|
|
|
|
|
def _generate_enums(cid):
|
|
data_root = cid["types"]
|
|
|
|
# Main
|
|
tmpl_main = ""
|
|
tmpl_main += 'enum {name} {{\n'
|
|
tmpl_main += '{subitems}'
|
|
tmpl_main += '}};\n\n'
|
|
|
|
kind_main = "CursorKind.ENUM_DECL"
|
|
|
|
def format_func_main(tmpl, item, subitems_str):
|
|
return tmpl.format(
|
|
name=item["name"],
|
|
subitems=subitems_str)
|
|
|
|
# Sub
|
|
tmpl_sub = ""
|
|
tmpl_sub += ' item {name} value={value};'
|
|
kind_sub = "CursorKind.ENUM_CONSTANT_DECL"
|
|
|
|
def format_func_sub(tmpl, item):
|
|
return tmpl.format(
|
|
name=item["name"],
|
|
value=item["value"])
|
|
|
|
ret = _format(data_root, tmpl_main, kind_main, format_func_main, tmpl_sub, kind_sub, format_func_sub)
|
|
|
|
return ret
|
|
|
|
|
|
def _format(data, tmpl_main, kind_main, func_main, tmpl_sub, kind_sub, func_sub):
|
|
ret = ""
|
|
|
|
def filt(item):
|
|
if item["kind"] == kind_main:
|
|
return True
|
|
|
|
main_items = utils.recursive_query(data, filt)
|
|
|
|
for item in main_items:
|
|
subitems_str = _format_subitems(item, tmpl_sub, kind_sub, func_sub)
|
|
ret += func_main(tmpl_main, item, subitems_str)
|
|
|
|
return ret
|
|
|
|
|
|
def _format_subitems(data, tmpl, kind, func):
|
|
ret = ""
|
|
|
|
def filt(data):
|
|
if data["kind"] == kind:
|
|
return True
|
|
|
|
subitems = utils.recursive_query(data, filt)
|
|
|
|
for item in subitems:
|
|
ret += func(tmpl, item) + "\n"
|
|
|
|
return ret
|
|
|