from typing import Tuple, List
from log_tools import debug_log
from text_tools import find_first_of_characters, find_scope_borders
def remove_comments(data: str) -> str:
"""
Returns data without c++ comments.
"""
double_slash_pos = data.find("//")
while double_slash_pos != -1:
end_of_line = data.find("\n", double_slash_pos)
if end_of_line == -1:
end_of_line = len(data)
debug_log(f"Removing single-line comment at end of file:\n'{data[double_slash_pos:end_of_line]}'")
data = data[:double_slash_pos] + data[end_of_line:]
double_slash_pos = data.find("//")
multiline_comment_start = data.find("/*")
while multiline_comment_start != -1:
multiline_comment_end = data.find("*/", multiline_comment_start)
if multiline_comment_end == -1:
raise RuntimeError("Error find end of multiline-comment")
data = data.replace(data[multiline_comment_start : multiline_comment_end + 2], "")
multiline_comment_start = data.find("/*")
return data.strip(" \n")
def extract_and_remove_includes(data: str) -> Tuple[str, List[str]]:
"""
Returns data without includes and list of includes in file in format like:
[ '<ir/expression.h>', '"es2panda.h"' ]
"""
current_pos = data.find("#include")
res = []
while current_pos != -1:
include_start = find_first_of_characters('"<', data, current_pos)
if data[include_start] == '"':
include_end = data.find('"', include_start + 1)
else:
include_start, include_end = find_scope_borders(data, current_pos, "<")
if include_start != -1 and include_end != -1:
include = data[include_start : include_end + 1]
res.append(include)
data = data[:current_pos] + data[include_end + 1 :]
current_pos = data.find("#include", current_pos)
else:
raise RuntimeError("Error while parsing includes")
return data.strip(" \n"), res