1 /*
2  * Copyright (c) 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 OHOS_IDL_LEXER_H
17 #define OHOS_IDL_LEXER_H
18 
19 #include <ctype.h>
20 #include <memory>
21 #include <unordered_map>
22 
23 #include "parser/token.h"
24 #include "util/file.h"
25 #include "util/string.h"
26 
27 namespace OHOS {
28 namespace Idl {
29 class Lexer {
30 public:
31     Lexer();
32 
33     ~Lexer();
34 
35     bool OpenSourceFile(const String& filePath);
36 
37     Token GetToken(bool skipComment = true);
38 
39     Token PeekToken(bool skipComment = true);
40 
41     bool ReadTokenPeek(bool skipComment, char letter);
42 
GetIdentifier()43     String GetIdentifier() const
44     {
45         return identifier_;
46     }
47 
GetComment()48     String GetComment() const
49     {
50         return comment_;
51     }
52 
53     void SkipCurrentLine();
54 
55     bool SkipCurrentLine(char untilChar);
56 
GetSourceFile()57     std::shared_ptr<File> GetSourceFile() const
58     {
59         return currentFile_;
60     }
61 
62     String DumpToken() const;
63 
64     String DumpTokenSecond() const;
65 
GetTokenLineNumber()66     int GetTokenLineNumber() const
67     {
68         return tokenLineNo_;
69     }
70 
GetTokenColumnNumber()71     int GetTokenColumnNumber() const
72     {
73         return tokenColumnNo_;
74     }
75 
76     static int TokenToChar(Token token);
77 
78     bool ParseCacheable(int& cacheTime);
79 private:
80     void InitializeKeywords();
81 
82     Token ReadToken(bool skipComment);
83 
84     Token ReadIdentifier(char c);
85 
86     Token ReadLineComment(char c);
87 
88     Token ReadBlockComment(char c);
89 
IsAlphabet(char c)90     static bool IsAlphabet(char c)
91     {
92         return isalpha(c);
93     }
94 
IsDecimalDigital(char c)95     static bool IsDecimalDigital(char c)
96     {
97         return isdigit(c);
98     }
99 
IsSpace(char c)100     static bool IsSpace(char c)
101     {
102         return isspace(c);
103     }
104 
105     static bool strToInt(const char *str, int strLen, int& number);
106 
107     static const char* TAG;
108     std::unordered_map<String, Token, StringHashFunc, StringEqualFunc> keywords_;
109     Token currentToken_ = Token::UNKNOWN;
110     int tokenLineNo_ = 0;
111     int tokenColumnNo_ = 0;
112     String identifier_;
113     String comment_;
114     bool havePeek_ = false;
115     std::shared_ptr<File> currentFile_;
116     std::unordered_map<char, Token> token_map_ = {
117         {'<', Token::ANGLE_BRACKETS_LEFT},
118         {'>', Token::ANGLE_BRACKETS_RIGHT},
119         {'{', Token::BRACES_LEFT},
120         {'}', Token::BRACES_RIGHT},
121         {'[', Token::BRACKETS_LEFT},
122         {']', Token::BRACKETS_RIGHT},
123         {',', Token::COMMA},
124         {'(', Token::PARENTHESES_LEFT},
125         {')', Token::PARENTHESES_RIGHT},
126         {'.', Token::DOT},
127         {';', Token::SEMICOLON},
128     };
129 };
130 } // namespace Idl
131 } // namespace OHOS
132 #endif // OHOS_IDL_LEXER_H
133