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 HC_GEN_LEXER_H 10 #define HC_GEN_LEXER_H 11 12 #include <fstream> 13 #include <iostream> 14 #include <map> 15 #include <string> 16 17 #include "token.h" 18 19 namespace OHOS { 20 namespace Hardware { 21 class Lexer { 22 public: 23 Lexer(); 24 25 ~Lexer() = default; 26 27 bool Initialize(const std::string &sourceName); 28 29 bool Lex(Token &token); 30 31 bool SetTokenCharacter(char c, Token &token); 32 33 friend std::ostream &operator<<(std::ostream &stream, const Lexer &p); 34 35 std::shared_ptr<std::string> GetSourceName() const; 36 37 int32_t GetLineno() const; 38 39 int32_t GetLineLoc() const; 40 41 private: 42 static constexpr int BUFFER_SIZE = (1024 * 1024); 43 44 void InitToken(Token &token) const; 45 46 bool GetChar(char &c, bool skipSpace = true); 47 48 void ConsumeChar(); 49 50 char GetRawChar(); 51 52 static bool IsSpace(char c); 53 54 static bool IsNum(char c); 55 56 bool FillBuffer(); 57 58 bool ProcessComment(); 59 60 bool LexInclude(Token &token); 61 62 bool LexFromString(Token &token); 63 64 bool LexFromNumber(Token &token); 65 66 void LexHexAndBinaryNum(std::string &value, char &c, uint64_t &v); 67 68 void LexFromLiteral(Token &token); 69 70 bool PeekChar(char &c, bool skipSpace = true); 71 72 static std::map<std::string, TokenType> keyWords_; 73 74 std::ifstream src_; 75 std::shared_ptr<std::string> srcName_; 76 char buffer_[BUFFER_SIZE] {0}; 77 const char *bufferStart_ {nullptr}; 78 const char *bufferEnd_ {nullptr}; 79 int32_t lineno_; 80 int32_t lineLoc_; 81 }; 82 83 std::ostream &operator<<(std::ostream &stream, const Lexer &p); 84 } // namespace Hardware 85 } // namespace OHOS 86 87 #endif // HC_GEN_LEXER_H 88