from typing import Tuple, Dict
from text_tools import (
smart_find_first_of_characters,
find_scope_borders,
find_first_of_characters,
find_first_not_restricted_character,
)
def parse_friend_class(data: str, start: int) -> Tuple[int, str]:
name_start = data.find("friend class ", start) + len("friend class ")
name_end = data.find(";", name_start)
friend_name = data[name_start:name_end].strip(" \n")
return name_end, friend_name
def parse_class(data: str, start: int = 0, namespace: str = "", parent_class_name: str = "") -> Tuple[int, Dict]:
start_of_body = smart_find_first_of_characters("{", data, start)
start_of_body, end_of_body = find_scope_borders(data, start_of_body)
class_body = data[start_of_body + 1 : end_of_body]
colon_pos = find_first_of_characters(":", data, start, start_of_body)
class_name = extract_class_name(data, data.find("class ", start) + len("class "))
from cpp_parser import CppParser
if parent_class_name != "":
parsed_class = CppParser(class_body, namespace, parent_class_name + "::" + class_name).parse()
else:
parsed_class = CppParser(class_body, namespace, class_name).parse()
if colon_pos != len(data):
extends_class = data[colon_pos + 1 : start_of_body].strip(" \n")
parsed_class["extends"] = extends_class
parsed_class["name"] = class_name
return end_of_body, parsed_class
def extract_class_name(data: str, pos: int) -> str:
name = ""
while not name or name.isupper():
start_of_name = find_first_not_restricted_character(" :{\n", data, pos)
pos = find_first_of_characters(" :{\n", data, start_of_name)
if start_of_name == len(data) or pos == len(data):
raise RuntimeError("Error while extracting class name")
name = data[start_of_name:pos]
return data[start_of_name:pos]
def parse_template_prefix(data: str, start: int) -> Tuple[int, str]:
start = find_first_not_restricted_character(" ", data, start)
if data[start : start + len("template")] != "template":
raise RuntimeError("Not a template!")
start_of_template_args, end_of_template_args = find_scope_borders(data, start, "<")
res = data[start_of_template_args + 1 : end_of_template_args]
return end_of_template_args, res