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_PARSER_H
17 #define OHOS_IDL_PARSER_H
18 
19 #include "ast/ast_interface_type.h"
20 #include "ast/ast_method.h"
21 #include "ast/ast_module.h"
22 #include "ast/ast_type.h"
23 #include "parser/lexer.h"
24 #include "parser/token.h"
25 #include "util/autoptr.h"
26 #include "util/light_refcount_base.h"
27 #include "util/options.h"
28 #include "util/string.h"
29 
30 namespace OHOS {
31 namespace Idl {
32 class Parser {
33 public:
34     explicit Parser(const Options& options);
35 
36     ~Parser() = default;
37 
38     bool Parse(const String& sourceFile);
39 
GetModule()40     AutoPtr<ASTModule> GetModule() const
41     {
42         return module_;
43     }
44 
45 private:
46     class ErrorInfo : public LightRefCountBase {
47     public:
48         String file_;
49         Token token_;
50         int lineNo_;
51         int columnNo_;
52         String message_;
53         AutoPtr<ErrorInfo> next_;
54     };
55 
56     bool ParseFile();
57 
58     bool ParseLicense();
59 
60     bool ParseInterface();
61 
62     bool ParseInterfaceMiddle(Token& token, String& interfaceFullName);
63 
64     bool ParseInterfaceEnd(Token& token, String& interfaceFullName, bool hasProperties, bool oneway, bool ret);
65 
66     bool ParseMethod(ASTInterfaceType* interface);
67 
68     bool ParseMethodProperties(bool& oneway, bool& cacheable, int& cacheTime);
69 
70     bool ParseMethodName(Token& token, ASTType* type, ASTInterfaceType* interface);
71 
72     bool ParseMethodBrackets(Token& token, ASTMethod* method, bool& ret);
73 
74     bool ParseParameter(ASTMethod* method);
75 
76     bool ParseParameterPeek(Token& token);
77 
78     bool ParseParameterInOut(Token& token, ASTParameter* parameter);
79 
80     void SetMethodAttr(ASTMethod* method, ASTType *returnType, bool oneway, bool cacheable, int cacheTime);
81 
82     AutoPtr<ASTNamespace> NameSpaceEmpty();
83 
84     AutoPtr<ASTType> ParseType();
85 
86     AutoPtr<ASTType> ParseList();
87 
88     AutoPtr<ASTType> ParseMap();
89 
90     bool ParseSequenceable();
91 
92     bool CheckIntegrity();
93 
94     bool IsValidTypeName(const String& typeName);
95 
IsPrimitiveType(Token token)96     static bool IsPrimitiveType(Token token)
97     {
98         return token >= Token::BOOLEAN && token <= Token::STRING;
99     }
100 
101     void LogError(Token token, const String& message);
102 
103     void ShowError();
104 
105     static const char* tag;
106 
107     const Options& options_;
108     AutoPtr<ASTModule> module_;
109     AutoPtr<ASTInterfaceType> parsingInterface_;
110     Lexer lexer_;
111     AutoPtr<ErrorInfo> errors_;
112 };
113 } // namespace Idl
114 } // namespace OHOS
115 #endif // OHOS_IDL_PARSER_H
116