2016-08-26 20:12:20 +03:00
|
|
|
import re
|
2016-09-08 17:55:46 +03:00
|
|
|
|
2016-09-17 21:42:34 +03:00
|
|
|
from .tl_object import TLObject
|
2016-08-26 20:12:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
class TLParser:
|
|
|
|
"""Class used to parse .tl files"""
|
|
|
|
|
|
|
|
@staticmethod
|
2017-09-04 17:29:00 +03:00
|
|
|
def parse_file(file_path, ignore_core=False):
|
2016-08-26 20:12:20 +03:00
|
|
|
"""This method yields TLObjects from a given .tl file"""
|
|
|
|
|
|
|
|
with open(file_path, encoding='utf-8') as file:
|
2017-09-04 18:10:04 +03:00
|
|
|
# Start by assuming that the next found line won't
|
|
|
|
# be a function (and will hence be a type)
|
2016-08-26 20:12:20 +03:00
|
|
|
is_function = False
|
|
|
|
|
|
|
|
# Read all the lines from the .tl file
|
|
|
|
for line in file:
|
2017-11-30 15:34:55 +03:00
|
|
|
# Strip comments from the line
|
|
|
|
comment_index = line.find('//')
|
|
|
|
if comment_index != -1:
|
|
|
|
line = line[:comment_index]
|
2016-08-26 20:12:20 +03:00
|
|
|
|
2017-11-30 15:34:55 +03:00
|
|
|
line = line.strip()
|
|
|
|
if line:
|
2017-09-04 18:10:04 +03:00
|
|
|
# Check whether the line is a type change
|
|
|
|
# (types <-> functions) or not
|
2016-08-26 20:12:20 +03:00
|
|
|
match = re.match('---(\w+)---', line)
|
|
|
|
if match:
|
|
|
|
following_types = match.group(1)
|
|
|
|
is_function = following_types == 'functions'
|
|
|
|
|
|
|
|
else:
|
2017-09-04 17:29:00 +03:00
|
|
|
try:
|
|
|
|
result = TLObject.from_tl(line, is_function)
|
|
|
|
if not ignore_core or not result.is_core_type():
|
|
|
|
yield result
|
|
|
|
except ValueError as e:
|
|
|
|
if 'vector#1cb5c415' not in str(e):
|
|
|
|
raise
|
2016-09-17 22:09:20 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def find_layer(file_path):
|
|
|
|
"""Finds the layer used on the specified scheme.tl file"""
|
|
|
|
layer_regex = re.compile(r'^//\s*LAYER\s*(\d+)$')
|
|
|
|
with open(file_path, encoding='utf-8') as file:
|
|
|
|
for line in file:
|
|
|
|
match = layer_regex.match(line)
|
|
|
|
if match:
|
|
|
|
return int(match.group(1))
|