• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3#
4# Copyright (c) 2024-2025 Huawei Device Co., Ltd.
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17
18"""Provides line iterator class."""
19
20from text_tools import find_first_of_characters
21
22
23class LineIterator:  # pylint: disable=C0115
24    def __init__(self, data_raw: str, start: int = 0):
25        self.data = data_raw
26
27        self.start = start
28        self.end = find_first_of_characters("\n", self.data, start)
29
30        self.current_line = self.data[self.start : self.end].strip(" \n")
31
32        self.next_parenthese = find_first_of_characters("(", self.data, start)
33        self.next_semicolon = find_first_of_characters(";", self.data, start)
34        self.next_equal = find_first_of_characters("=", self.data, start)
35
36        self.processed_first_line = False
37
38    def next_line(self) -> bool:
39        if not self.processed_first_line:
40            self.processed_first_line = True
41            if self.current_line != "":
42                return True
43
44        self.end = find_first_of_characters("\n", self.data, self.end)
45
46        if self.end == len(self.data):
47            return False
48
49        self.start = self.end + 1
50        self.end = find_first_of_characters("\n", self.data, self.start)
51        self.current_line = self.data[self.start : self.end].strip(" \n")
52
53        self.next_parenthese = find_first_of_characters("(", self.data, self.start)
54        self.next_semicolon = find_first_of_characters(";", self.data, self.start)
55        self.next_equal = find_first_of_characters("=", self.data, self.start)
56
57        if self.current_line == "":
58            return self.next_line()
59
60        return True
61
62    def is_skip_line(self) -> bool:
63        return (
64            self.current_line.find("#if") != -1 # if, ifdef, ifndef
65            or self.current_line.find("#el") != -1 # else, elif
66            or self.current_line.find("#undef") != -1
67            or self.current_line.find("#end") != -1
68        )
69
70    def is_template(self) -> bool:
71        return self.current_line.find("template") != -1
72
73    def is_namespace(self) -> bool:
74        return self.current_line.find("namespace") != -1
75
76    def is_enum(self) -> bool:
77        return self.current_line.find("enum ") != -1
78
79    def is_struct(self) -> bool:
80        return self.current_line.find("struct ") != -1
81
82    def is_using(self) -> bool:
83        return (
84            self.current_line.find("using ") != -1
85            or self.current_line.find("namespace ") != -1
86            and self.current_line.find("=") != -1
87        )
88
89    def is_define_macro(self) -> bool:
90        return self.current_line.find("#define ") != -1
91
92    def is_access_modifier(self) -> bool:
93        return self.current_line in ["private:", "public:", "protected:"]
94
95    def is_firend_class(self) -> bool:
96        return self.current_line.find("friend class ") != -1
97
98    def is_class_forward_decl(self) -> bool:
99        return self.current_line.find("class ") != -1 and self.current_line.find(";") != -1
100
101    def is_class_definition(self) -> bool:
102        return self.current_line.find("class ") != -1 and not self.is_class_forward_decl()
103
104    def is_function_or_field(self) -> bool:
105        return self.next_parenthese < self.end or self.next_semicolon < self.end or self.next_equal < self.end
106
107    def is_method_or_constructor(self) -> bool:
108        return (
109            self.is_function_or_field()
110            and self.next_parenthese < self.next_semicolon
111            and (self.next_parenthese < self.next_equal or self.current_line.find("operator==") != -1)
112        )
113
114    def is_field(self) -> bool:
115        return self.is_function_or_field()
116