• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetIdentifier()41     String GetIdentifier() const
42     {
43         return identifier_;
44     }
45 
GetComment()46     String GetComment() const
47     {
48         return comment_;
49     }
50 
51     void SkipCurrentLine();
52 
53     bool SkipCurrentLine(char untilChar);
54 
GetSourceFile()55     std::shared_ptr<File> GetSourceFile() const
56     {
57         return currentFile_;
58     }
59 
60     String DumpToken() const;
61 
GetTokenLineNumber()62     int GetTokenLineNumber() const
63     {
64         return tokenLineNo_;
65     }
66 
GetTokenColumnNumber()67     int GetTokenColumnNumber() const
68     {
69         return tokenColumnNo_;
70     }
71 
72     static int TokenToChar(Token token);
73 
74 private:
75     void InitializeKeywords();
76 
77     Token ReadToken(bool skipComment);
78 
79     Token ReadIdentifier(char c);
80 
81     Token ReadLineComment(char c);
82 
83     Token ReadBlockComment(char c);
84 
IsAlphabet(char c)85     static bool IsAlphabet(char c)
86     {
87         return isalpha(c);
88     }
89 
IsDecimalDigital(char c)90     static bool IsDecimalDigital(char c)
91     {
92         return isdigit(c);
93     }
94 
IsSpace(char c)95     static bool IsSpace(char c)
96     {
97         return isspace(c);
98     }
99 
100     static const char* TAG;
101     std::unordered_map<String, Token, StringHashFunc, StringEqualFunc> keywords_;
102     Token currentToken_ = Token::UNKNOWN;
103     int tokenLineNo_ = 0;
104     int tokenColumnNo_ = 0;
105     String identifier_;
106     String comment_;
107     bool havePeek_ = false;
108     std::shared_ptr<File> currentFile_;
109 };
110 } // namespace Idl
111 } // namespace OHOS
112 #endif // OHOS_IDL_LEXER_H
113