tdlib-serializer/code_writer.py

114 lines
3.3 KiB
Python
Raw Normal View History

2020-05-01 15:39:29 +02:00
import typing
2020-05-14 22:07:53 +02:00
indent = "\t"
2020-05-01 15:39:29 +02:00
class CodeWriter:
fd: typing.TextIO
indent_depth: int
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
2020-10-16 22:12:42 +02:00
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(" */")
2020-05-01 15:39:29 +02:00
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
2020-08-20 14:02:59 +02:00
def local_assign(self, object_type: str, name: str, value: str):
self.fd.write(object_type + " " + name + " = " + value + ";")
2020-05-01 15:39:29 +02:00
2020-10-16 22:12:42 +02:00
def assign(self, name: str, value: str):
self.fd.write(name + " = " + value + ";")
2020-05-01 15:39:29 +02:00
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 + "(" + ", ".join((t + " " + n for t, n in args)) + ")"
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)