1/*
2 * Copyright (c) 2024 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 OHOS_IDL_LEXER_H
17#define OHOS_IDL_LEXER_H
18
19#include <cstdlib>
20#include <cctype>
21#include <memory>
22#include <unordered_map>
23
24#include "lexer/token.h"
25#include "util/file.h"
26
27namespace OHOS {
28namespace Idl {
29class Lexer {
30public:
31    enum class ParseMode {
32        DECL_MODE,
33        EXPR_MODE,
34    };
35
36    Lexer();
37
38    ~Lexer() = default;
39
40    bool Reset(const std::string &filePath);
41
42    inline std::string GetFilePath() const
43    {
44        return (file_ != nullptr) ? file_->GetPath() : "";
45    }
46
47    Token PeekToken(bool skipComment = true);
48
49    Token GetToken(bool skipComment = true);
50
51    void SkipCurrentLine();
52
53    bool SkipCurrentLine(char untilChar);
54
55    void Skip(char untilChar);
56
57    void SkipToken(TokenType tokenType);
58
59    void SkipUntilToken(TokenType tokenType);
60
61    void SkipEof();
62
63    inline void SetParseMode(ParseMode mode)
64    {
65        mode_ = mode;
66    }
67
68    bool ReadCacheableTime(Token &token);
69
70private:
71    void ReadToken(Token &token, bool skipComment = true);
72
73    void InitCurToken(Token &token);
74
75    void ReadId(Token &token);
76
77    void ReadNum(Token &token);
78
79    void ReadBinaryNum(Token &token);
80
81    void ReadOctNum(Token &token);
82
83    void ReadHexNum(Token &token);
84
85    void ReadDecNum(Token &token);
86
87    void ReadNumSuffix(Token &token);
88
89    void ReadShiftLeftOp(Token &token);
90
91    void ReadShiftRightOp(Token &token);
92
93    void ReadPPlusOp(Token &token);
94
95    void ReadMMinusOp(Token &token);
96
97    void ReadComment(Token &token);
98
99    void ReadLineComment(Token &token);
100
101    void ReadBlockComment(Token &token);
102
103    void ReadSymbolToken(Token &token);
104
105private:
106    std::string filePath_;
107    std::unique_ptr<File> file_;
108
109    ParseMode mode_;
110    bool havePeek_;
111    Token curToken_;
112
113private:
114    using StrTokenTypeMap = std::unordered_map<std::string, TokenType>;
115    static StrTokenTypeMap keyWords_;
116    static StrTokenTypeMap symbols_;
117};
118} // namespace Idl
119} // namespace OHOS
120
121#endif // OHOS_IDL_LEXER_H