• 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 #include "sourceLocation.h"
17 
18 #include <lexer/token/letters.h>
19 
20 #include <cstdint>
21 
22 namespace panda::es2panda::lexer {
23 
AddCol(size_t offset)24 void OffsetEntry::AddCol(size_t offset)
25 {
26     size_t diff = offset - offset_;
27     offset_ = offset;
28 
29     if (ranges.empty()) {
30         ranges.emplace_back(Range {diff});
31         return;
32     }
33 
34     auto &range = ranges.back();
35 
36     if (diff == range.byteSize) {
37         range.cnt++;
38     } else {
39         ranges.emplace_back(Range {diff});
40     }
41 }
42 
LineIndex(const util::StringView & source)43 LineIndex::LineIndex(const util::StringView &source) noexcept
44 {
45     auto iter = util::StringView::Iterator(source);
46     entrys_.emplace_back(0);
47 
48     bool nextEntry = false;
49     while (true) {
50         switch (iter.Next()) {
51             case util::StringView::Iterator::INVALID_CP: {
52                 if (!nextEntry) {
53                     // Add the last entry if the ending character is not LEX_CHAR_LF / LEX_CHAR_PS / LEX_CHAR_LS
54                     entrys_.emplace_back(iter.Index());
55                 }
56                 return;
57             }
58             case LEX_CHAR_CR: {
59                 if (iter.HasNext() && iter.Peek() == LEX_CHAR_LF) {
60                     iter.Forward(1);
61                 }
62 
63                 [[fallthrough]];
64             }
65             case LEX_CHAR_LF:
66             case LEX_CHAR_PS:
67             case LEX_CHAR_LS: {
68                 entrys_.emplace_back(iter.Index());
69                 nextEntry = true;
70                 break;
71             }
72             default: {
73                 entrys_.back().AddCol(iter.Index());
74                 nextEntry = false;
75             }
76         }
77     }
78 }
79 
GetLocation(SourcePosition pos)80 SourceLocation LineIndex::GetLocation(SourcePosition pos) noexcept
81 {
82     size_t line = pos.line;
83 
84     size_t col = 0;
85     ASSERT(pos.line < entrys_.size());
86     const auto &entry = entrys_[pos.line];
87     size_t diff = pos.index - entry.lineStart;
88 
89     for (const auto &range : entry.ranges) {
90         if (diff < (range.cnt * range.byteSize)) {
91             col += (diff / range.byteSize) ;
92             break;
93         }
94 
95         diff -= range.cnt * range.byteSize;
96         col += range.cnt;
97     }
98 
99     return SourceLocation(line + 1, col + 1);
100 }
101 
102 }  // namespace panda::es2panda::lexer
103