1 /*
2  * Copyright (c) 2021 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 FLEX_BISON_SCANNER_H
17 #define FLEX_BISON_SCANNER_H
18 
19 #include <cstring>
20 #include "script_utils.h"
21 
22 /* 重要 */
23 #if !defined(yyFlexLexerOnce)
24 #undef yyFlexLexer
25 #define yyFlexLexer script_FlexLexer // 根据prefix修改
26 #include "FlexLexer.h"
27 #endif
28 #include "parser.hpp"
29 
30 /* 替换默认的get_next_token定义 */
31 #undef YY_DECL
32 #define YY_DECL Uscript::Parser::symbol_type Uscript::Scanner::nextToken()
33 
34 namespace Uscript {
35 class ScriptInterpreter;
36 
37 class Scanner : public yyFlexLexer {
38 public:
Scanner(ScriptInterpreter * interpreter)39     explicit Scanner(ScriptInterpreter* interpreter)
40     {
41         loc = location();
42     }
~Scanner()43     ~Scanner() override {}
44     // 不需要手动实现这个函数,Flex会生成YY_DECL宏定义的代码来实现这个函数
45     virtual Uscript::Parser::symbol_type nextToken();
yywrap()46     int yywrap() override
47     {
48         return 1;
49     }
50 
51     int LexerInput(char* buf, int maxSize) override;
52 
SetPkgStream(Hpackage::PkgManager::StreamPtr pkgStream)53     void SetPkgStream(Hpackage::PkgManager::StreamPtr pkgStream)
54     {
55         pkgStream_ = pkgStream;
56     }
57 
58 private:
59     Hpackage::PkgManager::StreamPtr pkgStream_ = nullptr;
60     size_t currPos = 0;
61     location loc {};
62 };
63 } // namespace Uscript
64 #endif // FLEX_BISON_SCANNER_H
65