• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #ifndef OHOS_HDI_LEXER_H
10 #define OHOS_HDI_LEXER_H
11 
12 #include <cstdlib>
13 #include <ctype.h>
14 #include <memory>
15 #include <unordered_map>
16 #include "parser/token.h"
17 #include "util/file.h"
18 #include "util/string.h"
19 
20 namespace OHOS {
21 namespace HDI {
22 class Lexer {
23 public:
24     Lexer();
25 
26     ~Lexer();
27 
28     bool OpenSourceFile(const String& filePath);
29 
30     Token GetToken(bool skipComment = true);
31 
32     Token PeekToken(bool skipComment = true);
33 
GetIdentifier()34     inline String GetIdentifier() const
35     {
36         return identifier_;
37     }
38 
GetDigit()39     inline unsigned long GetDigit() const
40     {
41         return std::atoll(digit_.string());
42     }
43 
GetComment()44     inline String GetComment() const
45     {
46         return comment_;
47     }
48 
49     void SkipCurrentLine();
50 
51     bool SkipCurrentLine(char untilChar);
52 
53     void Skip(char untilChar);
54 
55     void SkipEof();
56 
GetFilePath()57     inline String GetFilePath() const
58     {
59         if (currentFile_ == nullptr) {
60             return String("");
61         }
62 
63         return currentFile_->GetPath();
64     }
65 
66     String DumpToken() const;
67 
GetTokenLineNumber()68     inline int GetTokenLineNumber() const
69     {
70         return tokenLineNo_;
71     }
72 
GetTokenColumnNumber()73     inline int GetTokenColumnNumber() const
74     {
75         return tokenColumnNo_;
76     }
77 
78     static int TokenToChar(Token token);
79 
80 private:
81     void InitializeKeywords();
82 
83     Token ReadToken(bool skipComment);
84 
85     Token ReadIdentifier(char c);
86 
87     Token ReadDecimalDigital(char c);
88 
89     Token ReadLineComment(char c);
90 
91     Token ReadBlockComment(char c);
92 
IsAlphabet(char c)93     inline static bool IsAlphabet(char c)
94     {
95         return isalpha(c);
96     }
97 
IsDecimalDigital(char c)98     inline static bool IsDecimalDigital(char c)
99     {
100         return isdigit(c);
101     }
102 
IsSpace(char c)103     inline static bool IsSpace(char c)
104     {
105         return isspace(c);
106     }
107 
108     static const char* TAG;
109     std::unordered_map<String, Token, StringHashFunc, StringEqualFunc> keywords_;
110     std::unordered_map<char, Token> delimiters_;
111     std::unordered_map<Token, String> tokenDumps_;
112     Token currentToken_ = Token::UNKNOWN;
113     int tokenLineNo_ = 0;
114     int tokenColumnNo_ = 0;
115     String identifier_;
116     String digit_;
117     String comment_;
118     bool havePeek_ = false;
119     std::unique_ptr<File> currentFile_;
120 };
121 } // namespace HDI
122 } // namespace OHOS
123 
124 #endif // OHOS_HDI_LEXER_H