• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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 "util/ustring.h"
21 
22 #include <cstddef>
23 #include <cstdint>
24 #include <vector>
25 
26 namespace panda::es2panda::lexer {
27 class SourcePosition {
28 public:
29     explicit SourcePosition() noexcept = default;
SourcePosition(size_t i,size_t l)30     explicit SourcePosition(size_t i, size_t l) noexcept : index(i), line(l) {}
31     DEFAULT_COPY_SEMANTIC(SourcePosition);
32     DEFAULT_MOVE_SEMANTIC(SourcePosition);
33     ~SourcePosition() = default;
34 
35     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
36     size_t index {};
37     size_t line {};
38     // NOLINTEND(misc-non-private-member-variables-in-classes)
39 };
40 
41 class SourceRange {
42 public:
43     explicit SourceRange() noexcept = default;
SourceRange(SourcePosition s,SourcePosition e)44     SourceRange(SourcePosition s, SourcePosition e) noexcept : start(s), end(e) {}
45     DEFAULT_COPY_SEMANTIC(SourceRange);
46     DEFAULT_MOVE_SEMANTIC(SourceRange);
47     ~SourceRange() = default;
48 
49     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
50     SourcePosition start {};
51     SourcePosition end {};
52     // NOLINTEND(misc-non-private-member-variables-in-classes)
53 };
54 
55 class SourceLocation {
56 public:
57     explicit SourceLocation() noexcept = default;
SourceLocation(size_t l,size_t c)58     explicit SourceLocation(size_t l, size_t c) noexcept : line(l), col(c) {}
59     DEFAULT_COPY_SEMANTIC(SourceLocation);
60     DEFAULT_MOVE_SEMANTIC(SourceLocation);
61     ~SourceLocation() = default;
62 
63     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
64     size_t line {};
65     size_t col {};
66     // NOLINTEND(misc-non-private-member-variables-in-classes)
67 };
68 
69 class Range {
70 public:
Range(size_t bS)71     explicit Range(size_t bS) noexcept : byteSize(bS) {}
72 
73     DEFAULT_COPY_SEMANTIC(Range);
74     DEFAULT_MOVE_SEMANTIC(Range);
75     ~Range() = default;
76 
77     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
78     size_t byteSize {};
79     size_t cnt {1};
80     // NOLINTEND(misc-non-private-member-variables-in-classes)
81 };
82 
83 class OffsetEntry {
84 public:
OffsetEntry(size_t l)85     explicit OffsetEntry(size_t l) : lineStart(l), offset_(l) {};
86 
87     DEFAULT_COPY_SEMANTIC(OffsetEntry);
88     DEFAULT_MOVE_SEMANTIC(OffsetEntry);
89     ~OffsetEntry() = default;
90 
91     void AddCol(size_t offset);
92 
93     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
94     std::vector<Range> ranges {};
95     size_t lineStart {};
96     // NOLINTEND(misc-non-private-member-variables-in-classes)
97 
98 private:
99     size_t offset_ {};
100 };
101 
102 class LineIndex {
103 public:
104     explicit LineIndex(const util::StringView &source) noexcept;
105     NO_COPY_SEMANTIC(LineIndex);
106     NO_MOVE_SEMANTIC(LineIndex);
107     ~LineIndex() = default;
108 
109     SourceLocation GetLocation(SourcePosition pos) const noexcept;
110 
111 private:
112     std::vector<OffsetEntry> entries_;
113 };
114 }  // namespace panda::es2panda::lexer
115 
116 #endif
117