mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-08 21:10:29 +00:00
Added a .tl file tokenizer and parser
A TLObject class has been added, as well as a method for tokenizen .tl files. A TLObject can be created by simply calling `TLObject.from_tl(tl_line)`, this will tokenize the .tl line and return a new TLObject with all the required properties extracted. Also added another method to load .tl files and yield TLObjects, including a SourceBuilder that can be later used to generate Python code.
This commit is contained in:
38
parser/source_builder.py
Normal file
38
parser/source_builder.py
Normal file
@@ -0,0 +1,38 @@
|
||||
|
||||
class SourceBuilder:
|
||||
"""This class should be used to build .py source files"""
|
||||
|
||||
def __init__(self, indent_size=4):
|
||||
self.current_indent = 0
|
||||
self.on_new_line = False
|
||||
self.indent_size = indent_size
|
||||
|
||||
self.buffer = []
|
||||
|
||||
def indent(self):
|
||||
self.write(' ' * (self.current_indent * self.indent_size))
|
||||
|
||||
def write(self, string):
|
||||
if self.on_new_line:
|
||||
self.on_new_line = False # We're not on a new line anymore
|
||||
self.indent()
|
||||
|
||||
self.buffer += list(string)
|
||||
|
||||
def writeln(self, string=''):
|
||||
self.write(string + '\n')
|
||||
self.on_new_line = True
|
||||
|
||||
# If we're writing a block, increment indent for the next time
|
||||
if string and string[-1] == ':':
|
||||
self.current_indent += 1
|
||||
|
||||
def end_block(self):
|
||||
self.current_indent -= 1
|
||||
self.writeln()
|
||||
|
||||
def __str__(self):
|
||||
if self.buffer:
|
||||
return ''.join(self.buffer)
|
||||
else:
|
||||
return ''
|
Reference in New Issue
Block a user