• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3#
4# Copyright (c) 2024 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("#ifndef") != -1
65            or self.current_line.find("#undef") != -1
66            or self.current_line.find("#endif") != -1
67        )
68
69    def is_template(self) -> bool:
70        return self.current_line.find("template") != -1
71
72    def is_namespace(self) -> bool:
73        return self.current_line.find("namespace") != -1
74
75    def is_enum(self) -> bool:
76        return self.current_line.find("enum ") != -1
77
78    def is_struct(self) -> bool:
79        return self.current_line.find("struct ") != -1
80
81    def is_using(self) -> bool:
82        return self.current_line.find("using ") != -1
83
84    def is_define_macro(self) -> bool:
85        return self.current_line.find("#define ") != -1
86
87    def is_access_modifier(self) -> bool:
88        return self.current_line in ["private:", "public:", "protected:"]
89
90    def is_firend_class(self) -> bool:
91        return self.current_line.find("friend class ") != -1
92
93    def is_class_forward_decl(self) -> bool:
94        return self.current_line.find("class ") != -1 and self.current_line.find(";") != -1
95
96    def is_class_definition(self) -> bool:
97        return self.current_line.find("class ") != -1 and not self.is_class_forward_decl()
98
99    def is_function_or_field(self) -> bool:
100        return self.next_parenthese < self.end or self.next_semicolon < self.end or self.next_equal < self.end
101
102    def is_method_or_constructor(self) -> bool:
103        return (
104            self.is_function_or_field()
105            and self.next_parenthese < self.next_semicolon
106            and (self.next_parenthese < self.next_equal or self.current_line.find("operator==") != -1)
107        )
108
109    def is_field(self) -> bool:
110        return self.is_function_or_field()
111