13af6ab5fSopenharmony_ci#!/usr/bin/env python3 23af6ab5fSopenharmony_ci# coding=utf-8 33af6ab5fSopenharmony_ci# 43af6ab5fSopenharmony_ci# Copyright (c) 2024 Huawei Device Co., Ltd. 53af6ab5fSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License"); 63af6ab5fSopenharmony_ci# you may not use this file except in compliance with the License. 73af6ab5fSopenharmony_ci# You may obtain a copy of the License at 83af6ab5fSopenharmony_ci# 93af6ab5fSopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0 103af6ab5fSopenharmony_ci# 113af6ab5fSopenharmony_ci# Unless required by applicable law or agreed to in writing, software 123af6ab5fSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS, 133af6ab5fSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 143af6ab5fSopenharmony_ci# See the License for the specific language governing permissions and 153af6ab5fSopenharmony_ci# limitations under the License. 163af6ab5fSopenharmony_ci 173af6ab5fSopenharmony_ci 183af6ab5fSopenharmony_cifrom typing import Tuple, List 193af6ab5fSopenharmony_cifrom log_tools import warning_log 203af6ab5fSopenharmony_cifrom text_tools import find_first_of_characters, find_scope_borders 213af6ab5fSopenharmony_ci 223af6ab5fSopenharmony_ci 233af6ab5fSopenharmony_cidef remove_comments(data: str) -> str: 243af6ab5fSopenharmony_ci """ 253af6ab5fSopenharmony_ci Returns data without c++ comments. 263af6ab5fSopenharmony_ci """ 273af6ab5fSopenharmony_ci # Remove one-line comment 283af6ab5fSopenharmony_ci double_slash_pos = data.find("//") 293af6ab5fSopenharmony_ci 303af6ab5fSopenharmony_ci while double_slash_pos != -1: 313af6ab5fSopenharmony_ci end_of_line = data.find("\n", double_slash_pos) 323af6ab5fSopenharmony_ci 333af6ab5fSopenharmony_ci if end_of_line == -1: 343af6ab5fSopenharmony_ci end_of_line = len(data) 353af6ab5fSopenharmony_ci warning_log(f"Removing single-line comment at end of file:\n'{data[double_slash_pos:end_of_line]}'") 363af6ab5fSopenharmony_ci 373af6ab5fSopenharmony_ci data = data[:double_slash_pos] + data[end_of_line:] 383af6ab5fSopenharmony_ci double_slash_pos = data.find("//") 393af6ab5fSopenharmony_ci 403af6ab5fSopenharmony_ci # Remove multi-line comment 413af6ab5fSopenharmony_ci multiline_comment_start = data.find("/*") 423af6ab5fSopenharmony_ci 433af6ab5fSopenharmony_ci while multiline_comment_start != -1: 443af6ab5fSopenharmony_ci multiline_comment_end = data.find("*/", multiline_comment_start) 453af6ab5fSopenharmony_ci 463af6ab5fSopenharmony_ci if multiline_comment_end == -1: 473af6ab5fSopenharmony_ci raise RuntimeError("Error find end of multiline-comment") 483af6ab5fSopenharmony_ci 493af6ab5fSopenharmony_ci data = data.replace(data[multiline_comment_start : multiline_comment_end + 2], "") 503af6ab5fSopenharmony_ci multiline_comment_start = data.find("/*") 513af6ab5fSopenharmony_ci 523af6ab5fSopenharmony_ci return data.strip(" \n") 533af6ab5fSopenharmony_ci 543af6ab5fSopenharmony_ci 553af6ab5fSopenharmony_cidef extract_and_remove_includes(data: str) -> Tuple[str, List[str]]: 563af6ab5fSopenharmony_ci """ 573af6ab5fSopenharmony_ci Returns data without includes and list of includes in file in format like: 583af6ab5fSopenharmony_ci [ '<ir/expression.h>', '"es2panda.h"' ] 593af6ab5fSopenharmony_ci """ 603af6ab5fSopenharmony_ci current_pos = data.find("#include") 613af6ab5fSopenharmony_ci res = [] 623af6ab5fSopenharmony_ci 633af6ab5fSopenharmony_ci while current_pos != -1: 643af6ab5fSopenharmony_ci include_start = find_first_of_characters('"<', data, current_pos) 653af6ab5fSopenharmony_ci 663af6ab5fSopenharmony_ci if data[include_start] == '"': 673af6ab5fSopenharmony_ci include_end = data.find('"', include_start + 1) 683af6ab5fSopenharmony_ci 693af6ab5fSopenharmony_ci else: 703af6ab5fSopenharmony_ci include_start, include_end = find_scope_borders(data, current_pos, "<") 713af6ab5fSopenharmony_ci 723af6ab5fSopenharmony_ci if include_start != -1 and include_end != -1: 733af6ab5fSopenharmony_ci include = data[include_start : include_end + 1] 743af6ab5fSopenharmony_ci res.append(include) 753af6ab5fSopenharmony_ci data = data[:current_pos] + data[include_end + 1 :] 763af6ab5fSopenharmony_ci 773af6ab5fSopenharmony_ci current_pos = data.find("#include", current_pos) 783af6ab5fSopenharmony_ci 793af6ab5fSopenharmony_ci else: 803af6ab5fSopenharmony_ci raise RuntimeError("Error while parsing includes") 813af6ab5fSopenharmony_ci 823af6ab5fSopenharmony_ci return data.strip(" \n"), res 83