• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_PARSER_H
10 #define OHOS_HDI_PARSER_H
11 
12 #include <memory>
13 #include <set>
14 #include <vector>
15 
16 #include "ast/ast.h"
17 #include "ast/ast_attribute.h"
18 #include "ast/ast_interface_type.h"
19 #include "ast/ast_method.h"
20 #include "ast/ast_type.h"
21 #include "lexer/lexer.h"
22 #include "util/autoptr.h"
23 #include "util/light_refcount_base.h"
24 #include "util/options.h"
25 
26 namespace OHOS {
27 namespace HDI {
28 using AttrSet = std::set<Token, TokenTypeCompare>;
29 
30 class Parser {
31 public:
32     Parser() = default;
33 
34     ~Parser() = default;
35 
36     bool Parse(const std::vector<std::string> &sourceFiles);
37 
38     using StrAstMap = std::unordered_map<std::string, AutoPtr<AST>>;
GetAllAst()39     inline const StrAstMap &GetAllAst() const
40     {
41         return allAsts_;
42     }
43 
44 private:
45     class Attribute : public LightRefCountBase {
46     public:
47         bool isOneWay = false;
48         bool isCallback = false;
49         bool isFull = false;
50         bool isLite = false;
51     };
52 
53     bool ParseOne(const std::string &sourceFile);
54 
55     bool Reset(const std::string &sourceFile);
56 
57     bool ParseFile();
58 
59     std::string ParseLicense();
60 
61     bool ParsePackage();
62 
63     bool ParserPackageInfo(const std::string &packageFullName);
64 
65     bool ParseImports();
66 
67     void ParseImportInfo();
68 
69     void ParseSequenceableInfo();
70 
71     bool ParseTypeDecls();
72 
73     // parse attributes of type
74     void ParseAttribute();
75 
76     AttrSet ParseAttributeInfo();
77 
78     bool AprseAttrUnit(AttrSet &attrs);
79 
80     // parse interface type
81     void ParseInterface(const AttrSet &attrs = {});
82 
83     AutoPtr<ASTInfAttr> ParseInfAttrInfo(const AttrSet &attrs);
84 
85     void ParseInterfaceBody(const AutoPtr<ASTInterfaceType> &interface);
86 
87     AutoPtr<ASTMethod> ParseMethod();
88 
89     AutoPtr<ASTMethodAttr> ParseMethodAttr();
90 
91     AutoPtr<ASTMethod> CreateGetVersionMethod();
92 
93     void ParseMethodParamList(const AutoPtr<ASTMethod> &method);
94 
95     AutoPtr<ASTParameter> ParseParam();
96 
97     AutoPtr<ASTParamAttr> ParseParamAttr();
98 
99     // parse type
100     AutoPtr<ASTType> ParseType();
101 
102     AutoPtr<ASTType> ParseBasicType();
103 
104     AutoPtr<ASTType> ParseUnsignedType();
105 
106     AutoPtr<ASTType> ParseArrayType(const AutoPtr<ASTType> &elementType);
107 
108     AutoPtr<ASTType> ParseListType();
109 
110     AutoPtr<ASTType> ParseMapType();
111 
112     AutoPtr<ASTType> ParseSmqType();
113 
114     AutoPtr<ASTType> ParseUserDefType();
115 
116     // parse declaration of enum
117     void ParseEnumDeclaration(const AttrSet &attrs = {});
118 
119     AutoPtr<ASTType> ParseEnumBaseType();
120 
121     void ParserEnumMember(const AutoPtr<ASTEnumType> &enumType);
122 
123     // parse declaration of struct
124     void ParseStructDeclaration(const AttrSet &attrs = {});
125 
126     void ParseStructMember(const AutoPtr<ASTStructType> &structType);
127 
128     // parse declaration of union
129     void ParseUnionDeclaration(const AttrSet &attrs = {});
130 
131     void ParseUnionMember(const AutoPtr<ASTUnionType> &unionType);
132 
133     bool AddUnionMember(const AutoPtr<ASTUnionType> &unionType, const AutoPtr<ASTType> &type, const std::string &name);
134 
135     AutoPtr<ASTTypeAttr> ParseUserDefTypeAttr(const AttrSet &attrs);
136 
137     // parse expression
138     AutoPtr<ASTExpr> ParseExpr();
139 
140     AutoPtr<ASTExpr> ParseAndExpr();
141 
142     AutoPtr<ASTExpr> ParseXorExpr();
143 
144     AutoPtr<ASTExpr> ParseOrExpr();
145 
146     AutoPtr<ASTExpr> ParseShiftExpr();
147 
148     AutoPtr<ASTExpr> ParseAddExpr();
149 
150     AutoPtr<ASTExpr> ParseMulExpr();
151 
152     AutoPtr<ASTExpr> ParseUnaryExpr();
153 
154     AutoPtr<ASTExpr> ParsePrimaryExpr();
155 
156     AutoPtr<ASTExpr> ParseNumExpr();
157 
158     bool CheckType(const Token &token, const AutoPtr<ASTType> &type);
159 
160     void SetAstFileType();
161 
162     bool CheckIntegrity();
163 
164     bool CheckInterfaceAst();
165 
166     bool CheckCallbackAst();
167 
168     bool CheckPackageName(const std::string &filePath, const std::string &packageName);
169 
170     bool CheckImport(const std::string &importName);
171 
IsPrimitiveType(Token token)172     inline static bool IsPrimitiveType(Token token)
173     {
174         return token.kind_ >= TokenType::BOOLEAN && token.kind_ <= TokenType::ASHMEM;
175     }
176 
177     bool AddAst(const AutoPtr<AST> &ast);
178 
179     void LogError(const std::string &message);
180 
181     void LogError(const Token &token, const std::string &message);
182 
183     void ShowError();
184 
185     Lexer lexer_;
186     std::vector<std::string> errors_;
187     AutoPtr<AST> ast_;
188     StrAstMap allAsts_;
189 };
190 } // namespace HDI
191 } // namespace OHOS
192 
193 #endif // OHOS_HDI_PARSER_H