/* * Copyright (c) 2021 Huawei Device Co., Ltd. * * HDF is dual licensed: you can use it either under the terms of * the GPL, or the BSD license, at your option. * See the LICENSE file in the root of this repository for complete details. */ #ifndef HC_GEN_LEXER_H #define HC_GEN_LEXER_H #include #include #include #include #include "token.h" namespace OHOS { namespace Hardware { class Lexer { public: Lexer(); ~Lexer() = default; bool Initialize(const std::string &sourceName); bool Lex(Token &token); bool SetTokenCharacter(char c, Token &token); friend std::ostream &operator<<(std::ostream &stream, const Lexer &p); std::shared_ptr GetSourceName() const; int32_t GetLineno() const; int32_t GetLineLoc() const; private: static constexpr int BUFFER_SIZE = (1024 * 1024); void InitToken(Token &token) const; bool GetChar(char &c, bool skipSpace = true); void ConsumeChar(); char GetRawChar(); static bool IsSpace(char c); static bool IsNum(char c); bool FillBuffer(); bool ProcessComment(); bool LexInclude(Token &token); bool LexFromString(Token &token); bool LexFromNumber(Token &token); void LexHexAndBinaryNum(std::string &value, char &c, uint64_t &v); void LexFromLiteral(Token &token); bool PeekChar(char &c, bool skipSpace = true); static std::map keyWords_; std::ifstream src_; std::shared_ptr srcName_; char buffer_[BUFFER_SIZE] {0}; const char *bufferStart_ {nullptr}; const char *bufferEnd_ {nullptr}; int32_t lineno_; int32_t lineLoc_; }; std::ostream &operator<<(std::ostream &stream, const Lexer &p); } // namespace Hardware } // namespace OHOS #endif // HC_GEN_LEXER_H