tdlib-serializer/code_writer.py
andrew (from workstation) f1d5343760 sealed classes for java17
2021-10-23 18:06:47 +02:00

152 lines
4.3 KiB
Python

import typing
indent = "\t"
class CodeWriter:
fd: typing.TextIO
indent_depth: int
indent_chr = indent
def split_args(self, args: str) -> typing.List[str]:
tokens = args.split(", ")
result = [tokens[0]]
for token in tokens[1:]:
if len(", ".join(result[-1]) + token) < 120:
result[-1] += f", {token}"
else:
result.append(token)
if len(result) > 1 and len(result[-1].split(", ")) < 3:
result[-2] += f", {result[-1]}"
del result[-1]
return result
def __init__(self, file: typing.TextIO, indent_depth: int = 0):
self.fd = file
self.indent_depth = indent_depth
def indent(self):
self.fd.write(indent * self.indent_depth)
def open_custom_block(self, *blocks: str):
self.fd.write(" ".join(blocks) + " {")
self.indent_depth += 1
def open_docs(self):
self.fd.write("/**")
def write_docs(self, text: str = ""):
if text.strip() != "":
self.fd.write(f" * {text}")
else:
self.fd.write(" *")
def close_docs(self):
self.fd.write(" */")
def class_assign(self, class_value, local_value):
self.fd.write("this." + class_value + " = " + local_value + ";")
def close_block(self, space: bool = False, newline: bool = True):
self.indent_depth -= 1
if space:
self.indent()
self.fd.write("}\n" if newline else "}")
def newline(self):
self.fd.write("\n")
def open_switch(self, data: str):
self.indent_depth += 1
self.fd.write("switch(" + data + ") {")
def switch_break(self):
self.indent_depth -= 1
self.fd.write("break;")
def open_switch_case(self, case: str):
self.indent_depth += 1
self.fd.write("case " + case + ":")
def open_switch_default(self):
self.indent_depth += 1
self.fd.write("default:")
def exception(self, exception_class: str):
self.fd.write("throw new " + exception_class + "();")
def open_if_else(self, space: bool = False):
self.indent_depth -= 1
if space:
self.indent()
self.fd.write("} else {")
self.indent_depth += 1
def local_assign(self, object_type: str, name: str, value: str):
self.fd.write(object_type + " " + name + " = " + value + ";")
def assign(self, name: str, value: str):
self.fd.write(name + " = " + value + ";")
def open_for(self, start: str, cond: str, stmt: str):
self.indent_depth += 1
self.fd.write("for (" + start + "; " + cond + "; " + stmt + ") {")
def open_if(self, cond: str):
self.indent_depth += 1
self.fd.write("if (" + cond + ") {")
def call(self, method: str, *args: str):
self.fd.write(method + "(" + ", ".join(args) + ");")
def ret(self, var: str):
self.fd.write("return " + var + ";")
def open_function(self, name: str, args: typing.List[typing.Tuple[str, str]], t: str, e: str = None):
self.indent_depth += 1
result = "public " + t + " " + name + "(" + ", ".join((t + " " + n for t, n in args)) + ")"
if e:
result += " throws " + e
result += " {"
self.fd.write(result)
def open_constructor_function(self, name: str, args: typing.List[typing.Tuple[str, str]], e: str = None):
self.indent_depth += 1
result = "public " + name + "("
result_args = ", ".join((t + " " + n for t, n in args))
if len(result_args) > 60:
result_args = result_args.split(", ")
for i, result_arg in enumerate(result_args):
if i:
result += (self.indent_depth + 1) * indent
result += result_arg
if i != len(result_args) - 1:
result += ",\n"
else:
result += result_args
result += ")"
if e:
result += " throws " + e
result += " {"
self.fd.write(result)
def declare(self, name: str, typ: str, flags: str, value: str = None):
result = flags + " " + typ + " " + name
if value:
result += " = " + value
result += ";"
self.fd.write(result)