13af6ab5fSopenharmony_ci/**
23af6ab5fSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ci#ifndef ES2PANDA_LEXER_TOKEN_SOURCE_LOCATION_H
173af6ab5fSopenharmony_ci#define ES2PANDA_LEXER_TOKEN_SOURCE_LOCATION_H
183af6ab5fSopenharmony_ci
193af6ab5fSopenharmony_ci#include <macros.h>
203af6ab5fSopenharmony_ci#include <util/ustring.h>
213af6ab5fSopenharmony_ci
223af6ab5fSopenharmony_ci#include <cstddef>
233af6ab5fSopenharmony_ci#include <cstdint>
243af6ab5fSopenharmony_ci#include <vector>
253af6ab5fSopenharmony_ci
263af6ab5fSopenharmony_cinamespace panda::es2panda::lexer {
273af6ab5fSopenharmony_ci
283af6ab5fSopenharmony_cienum class SourceLocationFlag {
293af6ab5fSopenharmony_ci    VALID_SOURCE_LOCATION,
303af6ab5fSopenharmony_ci    INVALID_SOURCE_LOCATION
313af6ab5fSopenharmony_ci};
323af6ab5fSopenharmony_ci
333af6ab5fSopenharmony_ciclass SourcePosition {
343af6ab5fSopenharmony_cipublic:
353af6ab5fSopenharmony_ci    explicit SourcePosition() noexcept = default;
363af6ab5fSopenharmony_ci    explicit SourcePosition(size_t i, size_t l) noexcept : index(i), line(l) {}
373af6ab5fSopenharmony_ci    DEFAULT_COPY_SEMANTIC(SourcePosition);
383af6ab5fSopenharmony_ci    DEFAULT_MOVE_SEMANTIC(SourcePosition);
393af6ab5fSopenharmony_ci    ~SourcePosition() = default;
403af6ab5fSopenharmony_ci
413af6ab5fSopenharmony_ci    size_t index {};
423af6ab5fSopenharmony_ci    size_t line {};
433af6ab5fSopenharmony_ci};
443af6ab5fSopenharmony_ci
453af6ab5fSopenharmony_ciclass SourceRange {
463af6ab5fSopenharmony_cipublic:
473af6ab5fSopenharmony_ci    explicit SourceRange() noexcept = default;
483af6ab5fSopenharmony_ci    SourceRange(SourcePosition s, SourcePosition e) noexcept : start(s), end(e) {}
493af6ab5fSopenharmony_ci    DEFAULT_COPY_SEMANTIC(SourceRange);
503af6ab5fSopenharmony_ci    DEFAULT_MOVE_SEMANTIC(SourceRange);
513af6ab5fSopenharmony_ci    ~SourceRange() = default;
523af6ab5fSopenharmony_ci
533af6ab5fSopenharmony_ci    SourcePosition start {};
543af6ab5fSopenharmony_ci    SourcePosition end {};
553af6ab5fSopenharmony_ci};
563af6ab5fSopenharmony_ci
573af6ab5fSopenharmony_ciclass SourceLocation {
583af6ab5fSopenharmony_cipublic:
593af6ab5fSopenharmony_ci    explicit SourceLocation() noexcept = default;
603af6ab5fSopenharmony_ci    explicit SourceLocation(size_t l, size_t c) noexcept : line(l), col(c) {}
613af6ab5fSopenharmony_ci    DEFAULT_COPY_SEMANTIC(SourceLocation);
623af6ab5fSopenharmony_ci    DEFAULT_MOVE_SEMANTIC(SourceLocation);
633af6ab5fSopenharmony_ci    ~SourceLocation() = default;
643af6ab5fSopenharmony_ci
653af6ab5fSopenharmony_ci    size_t line {};
663af6ab5fSopenharmony_ci    size_t col {};
673af6ab5fSopenharmony_ci};
683af6ab5fSopenharmony_ci
693af6ab5fSopenharmony_ciclass Range {
703af6ab5fSopenharmony_cipublic:
713af6ab5fSopenharmony_ci    explicit Range(size_t bS) noexcept : byteSize(bS) {}
723af6ab5fSopenharmony_ci
733af6ab5fSopenharmony_ci    DEFAULT_COPY_SEMANTIC(Range);
743af6ab5fSopenharmony_ci    DEFAULT_MOVE_SEMANTIC(Range);
753af6ab5fSopenharmony_ci    ~Range() = default;
763af6ab5fSopenharmony_ci
773af6ab5fSopenharmony_ci    size_t byteSize {};
783af6ab5fSopenharmony_ci    size_t cnt {1};
793af6ab5fSopenharmony_ci};
803af6ab5fSopenharmony_ci
813af6ab5fSopenharmony_ciclass OffsetEntry {
823af6ab5fSopenharmony_cipublic:
833af6ab5fSopenharmony_ci    explicit OffsetEntry(size_t l) : lineStart(l), offset_(l) {};
843af6ab5fSopenharmony_ci
853af6ab5fSopenharmony_ci    DEFAULT_COPY_SEMANTIC(OffsetEntry);
863af6ab5fSopenharmony_ci    DEFAULT_MOVE_SEMANTIC(OffsetEntry);
873af6ab5fSopenharmony_ci    ~OffsetEntry() = default;
883af6ab5fSopenharmony_ci
893af6ab5fSopenharmony_ci    void AddCol(size_t offset);
903af6ab5fSopenharmony_ci
913af6ab5fSopenharmony_ci    std::vector<Range> ranges {};
923af6ab5fSopenharmony_ci    size_t lineStart {};
933af6ab5fSopenharmony_ci
943af6ab5fSopenharmony_ciprivate:
953af6ab5fSopenharmony_ci    size_t offset_ {};
963af6ab5fSopenharmony_ci};
973af6ab5fSopenharmony_ci
983af6ab5fSopenharmony_ciclass LineIndex {
993af6ab5fSopenharmony_cipublic:
1003af6ab5fSopenharmony_ci    explicit LineIndex(const util::StringView &source) noexcept;
1013af6ab5fSopenharmony_ci    explicit LineIndex() noexcept = default;
1023af6ab5fSopenharmony_ci    DEFAULT_COPY_SEMANTIC(LineIndex);
1033af6ab5fSopenharmony_ci    DEFAULT_MOVE_SEMANTIC(LineIndex);
1043af6ab5fSopenharmony_ci    ~LineIndex() = default;
1053af6ab5fSopenharmony_ci
1063af6ab5fSopenharmony_ci    SourceLocation GetLocation(SourcePosition pos) noexcept;
1073af6ab5fSopenharmony_ci
1083af6ab5fSopenharmony_ciprivate:
1093af6ab5fSopenharmony_ci    std::vector<OffsetEntry> entrys_;
1103af6ab5fSopenharmony_ci};
1113af6ab5fSopenharmony_ci
1123af6ab5fSopenharmony_ci}  // namespace panda::es2panda::lexer
1133af6ab5fSopenharmony_ci
1143af6ab5fSopenharmony_ci#endif
115