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:
30
parser/tl_parser.py
Normal file
30
parser/tl_parser.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import re
|
||||
from tl.tlobject import TLObject
|
||||
|
||||
|
||||
class TLParser:
|
||||
"""Class used to parse .tl files"""
|
||||
|
||||
@staticmethod
|
||||
def parse_file(file_path):
|
||||
"""This method yields TLObjects from a given .tl file"""
|
||||
|
||||
with open(file_path, encoding='utf-8') as file:
|
||||
# Start by assuming that the next found line won't be a function (and will hence be a type)
|
||||
is_function = False
|
||||
|
||||
# Read all the lines from the .tl file
|
||||
for line in file:
|
||||
line = line.strip()
|
||||
|
||||
# Ensure that the line is not a comment
|
||||
if line and not line.startswith('//'):
|
||||
|
||||
# Check whether the line is a type change (types ⋄ functions) or not
|
||||
match = re.match('---(\w+)---', line)
|
||||
if match:
|
||||
following_types = match.group(1)
|
||||
is_function = following_types == 'functions'
|
||||
|
||||
else:
|
||||
yield TLObject.from_tl(line, is_function)
|
Reference in New Issue
Block a user