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 28 enum class SourceLocationFlag { 29 VALID_SOURCE_LOCATION, 30 INVALID_SOURCE_LOCATION 31 }; 32 33 class SourcePosition { 34 public: 35 explicit SourcePosition() noexcept = default; SourcePosition(size_t i,size_t l)36 explicit SourcePosition(size_t i, size_t l) noexcept : index(i), line(l) {} 37 DEFAULT_COPY_SEMANTIC(SourcePosition); 38 DEFAULT_MOVE_SEMANTIC(SourcePosition); 39 ~SourcePosition() = default; 40 41 size_t index {}; 42 size_t line {}; 43 }; 44 45 class SourceRange { 46 public: 47 explicit SourceRange() noexcept = default; SourceRange(SourcePosition s,SourcePosition e)48 SourceRange(SourcePosition s, SourcePosition e) noexcept : start(s), end(e) {} 49 DEFAULT_COPY_SEMANTIC(SourceRange); 50 DEFAULT_MOVE_SEMANTIC(SourceRange); 51 ~SourceRange() = default; 52 53 SourcePosition start {}; 54 SourcePosition end {}; 55 }; 56 57 class SourceLocation { 58 public: 59 explicit SourceLocation() noexcept = default; SourceLocation(size_t l,size_t c)60 explicit SourceLocation(size_t l, size_t c) noexcept : line(l), col(c) {} 61 DEFAULT_COPY_SEMANTIC(SourceLocation); 62 DEFAULT_MOVE_SEMANTIC(SourceLocation); 63 ~SourceLocation() = default; 64 65 size_t line {}; 66 size_t col {}; 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 size_t byteSize {}; 78 size_t cnt {1}; 79 }; 80 81 class OffsetEntry { 82 public: OffsetEntry(size_t l)83 explicit OffsetEntry(size_t l) : lineStart(l), offset_(l) {}; 84 85 DEFAULT_COPY_SEMANTIC(OffsetEntry); 86 DEFAULT_MOVE_SEMANTIC(OffsetEntry); 87 ~OffsetEntry() = default; 88 89 void AddCol(size_t offset); 90 91 std::vector<Range> ranges {}; 92 size_t lineStart {}; 93 94 private: 95 size_t offset_ {}; 96 }; 97 98 class LineIndex { 99 public: 100 explicit LineIndex(const util::StringView &source) noexcept; 101 explicit LineIndex() noexcept = default; 102 DEFAULT_COPY_SEMANTIC(LineIndex); 103 DEFAULT_MOVE_SEMANTIC(LineIndex); 104 ~LineIndex() = default; 105 106 SourceLocation GetLocation(SourcePosition pos) noexcept; 107 108 private: 109 std::vector<OffsetEntry> entrys_; 110 }; 111 112 } // namespace panda::es2panda::lexer 113 114 #endif 115