• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ES2PANDA_LEXER_TOKEN_SOURCE_LOCATION_H
17 #define ES2PANDA_LEXER_TOKEN_SOURCE_LOCATION_H
18 
19 #include "macros.h"
20 #include <cstddef>
21 #include <cstdint>
22 #include <vector>
23 
24 namespace ark::es2panda::parser {
25 class Program;
26 }  // namespace ark::es2panda::parser
27 
28 namespace ark::es2panda::util {
29 class StringView;
30 }  // namespace ark::es2panda::util
31 
32 namespace ark::es2panda::lexer {
33 
34 class SourceLocation;
35 class SourcePosition {
36 public:
37     explicit SourcePosition() noexcept = default;
SourcePosition(const parser::Program * prog)38     explicit SourcePosition(const parser::Program *prog) noexcept : program_(prog) {}
SourcePosition(size_t i,size_t l,const parser::Program * prog)39     explicit SourcePosition(size_t i, size_t l, const parser::Program *prog) noexcept
40         : index(i), line(l), program_(prog)
41     {
42     }
43 
44     DEFAULT_COPY_SEMANTIC(SourcePosition);
45     DEFAULT_MOVE_SEMANTIC(SourcePosition);
46     ~SourcePosition() = default;
47     SourceLocation ToLocation() const;
48     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
49     size_t index {};
50     size_t line {};
51     // NOLINTEND(misc-non-private-member-variables-in-classes)
52 
53     const parser::Program *Program() const;
54 
55 private:
56     const parser::Program *program_ {};
57 };
58 
59 class SourceRange {
60 public:
61     explicit SourceRange() noexcept = default;
SourceRange(SourcePosition s,SourcePosition e)62     SourceRange(SourcePosition s, SourcePosition e) noexcept : start(s), end(e) {}
63     DEFAULT_COPY_SEMANTIC(SourceRange);
64     DEFAULT_MOVE_SEMANTIC(SourceRange);
65     ~SourceRange() = default;
66 
67     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
68     SourcePosition start {};
69     SourcePosition end {};
70     // NOLINTEND(misc-non-private-member-variables-in-classes)
71 };
72 
73 class SourceLocation {
74 public:
75     explicit SourceLocation() noexcept = default;
SourceLocation(size_t l,size_t c,const parser::Program * prog)76     explicit SourceLocation(size_t l, size_t c, const parser::Program *prog) noexcept : line(l), col(c), program_(prog)
77     {
78     }
79     DEFAULT_COPY_SEMANTIC(SourceLocation);
80     DEFAULT_MOVE_SEMANTIC(SourceLocation);
81     ~SourceLocation() = default;
82 
83     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
84     size_t line {};
85     size_t col {};
86     // NOLINTEND(misc-non-private-member-variables-in-classes)
87 
88     const parser::Program *Program() const;
89 
90 private:
91     const parser::Program *program_ {};
92 };
93 
94 class Range {
95 public:
Range(size_t bS)96     explicit Range(size_t bS) noexcept : byteSize(bS) {}
97 
98     DEFAULT_COPY_SEMANTIC(Range);
99     DEFAULT_MOVE_SEMANTIC(Range);
100     ~Range() = default;
101 
102     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
103     size_t byteSize {};
104     size_t cnt {1};
105     // NOLINTEND(misc-non-private-member-variables-in-classes)
106 };
107 
108 class OffsetEntry {
109 public:
OffsetEntry(size_t l)110     explicit OffsetEntry(size_t l) : lineStart(l), offset_(l) {};
111 
112     DEFAULT_COPY_SEMANTIC(OffsetEntry);
113     DEFAULT_MOVE_SEMANTIC(OffsetEntry);
114     ~OffsetEntry() = default;
115 
116     void AddCol(size_t offset);
117 
118     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
119     std::vector<Range> ranges {};
120     size_t lineStart {};
121     // NOLINTEND(misc-non-private-member-variables-in-classes)
122 
123 private:
124     size_t offset_ {};
125 };
126 
127 class LineIndex {
128 public:
129     explicit LineIndex(const util::StringView &source) noexcept;
130     NO_COPY_SEMANTIC(LineIndex);
131     NO_MOVE_SEMANTIC(LineIndex);
132     ~LineIndex() = default;
133 
134     SourceLocation GetLocation(SourcePosition pos) const noexcept;
135     size_t GetOffset(SourceLocation loc) const noexcept;
136 
137 private:
138     std::vector<OffsetEntry> entries_;
139 };
140 }  // namespace ark::es2panda::lexer
141 
142 #endif
143