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 OHOS_HDI_PARSER_H 10 #define OHOS_HDI_PARSER_H 11 12 #include <vector> 13 #include <memory> 14 #include "ast/ast_module.h" 15 #include "ast/ast_interface_type.h" 16 #include "ast/ast_method.h" 17 #include "ast/ast.h" 18 #include "ast/ast_type.h" 19 #include "parser/lexer.h" 20 #include "parser/token.h" 21 #include "parser/file_detail.h" 22 #include "util/autoptr.h" 23 #include "util/light_refcount_base.h" 24 #include "util/options.h" 25 #include "util/string.h" 26 27 namespace OHOS { 28 namespace HDI { 29 class Parser { 30 public: 31 Parser(const Options& options, const AutoPtr<ASTModule>& module = nullptr); 32 33 ~Parser() = default; 34 35 bool Parse(const String& sourceFile); 36 37 bool Parse(const String& sourceFile, std::shared_ptr<FileDetail>& fileDetailPtr); 38 GetAst()39 inline AutoPtr<AST> GetAst() const 40 { 41 return ast_; 42 } 43 44 private: 45 46 class Attribute : public LightRefCountBase { 47 public: 48 bool isOneWay = false; 49 bool isCallback = false; 50 bool isFull = false; 51 bool isLite = false; 52 }; 53 54 class ErrorInfo : public LightRefCountBase { 55 public: 56 String file_; 57 Token token_; 58 int lineNo_; 59 int columnNo_; 60 String message_; 61 AutoPtr<ErrorInfo> next_; 62 }; 63 64 bool ParseIdlDetail(std::shared_ptr<FileDetail>& fileDetailPtr); 65 66 bool ParseIdlPackage(std::shared_ptr<FileDetail>& fileDetailPtr); 67 68 bool ParseIdlImport(std::shared_ptr<FileDetail>& fileDetailPtr); 69 70 bool ParseFile(); 71 72 bool ParseLicense(); 73 74 bool ParsePackageName(); 75 76 bool ParserPackageInfo(const String& packageFullName); 77 78 bool ParseImport(); 79 80 bool ParseSequenceable(); 81 82 bool ParseAttribute(); 83 84 bool ParseInterface(const AutoPtr<Attribute>& attributes = nullptr); 85 86 bool ParseInterfaceBody(const AutoPtr<ASTInterfaceType>& interface); 87 88 bool ParseMethod(const AutoPtr<ASTInterfaceType>& interface); 89 90 void SetVersionInterfaceMethod(const AutoPtr<ASTInterfaceType>& interface); 91 92 bool ParseAttributeBody(AutoPtr<Attribute>& attributes); 93 94 bool ParseAttributeParam(AutoPtr<Attribute>& attributes); 95 96 bool ParseParameterList(AutoPtr<ASTMethod>& method); 97 98 bool ParseParameter(const AutoPtr<ASTMethod>& method); 99 100 bool ParseParamAttr(const AutoPtr<ASTParameter>& parameter); 101 102 AutoPtr<ASTType> ParseType(); 103 104 AutoPtr<ASTType> ParseUnsignedType(int typeLineNo, int typeColumnNo); 105 106 AutoPtr<ASTType> ParseList(); 107 108 AutoPtr<ASTType> ParseMap(); 109 110 AutoPtr<ASTType> ParseSharedMemQueueMetaType(); 111 112 AutoPtr<ASTType> ParseArrayType(const AutoPtr<ASTType>& elementType); 113 114 AutoPtr<ASTType> ParseCustomType(); 115 116 bool ParseEnumDefine(const AutoPtr<Attribute>& attributes = nullptr); 117 118 bool ParseEnumBaseType(const AutoPtr<ASTEnumType>& type); 119 120 bool ParseEnumMember(const AutoPtr<ASTEnumType>& type); 121 122 bool ParseStructDefine(const AutoPtr<Attribute>& attributes = nullptr); 123 124 bool ParseStructMember(const AutoPtr<ASTStructType>& type); 125 126 bool ParseUnionDefine(const AutoPtr<Attribute>& attributes = nullptr); 127 128 bool ParseUnionMember(const AutoPtr<ASTUnionType>& type); 129 130 bool CheckType(int lineNo, int columnNo, const AutoPtr<ASTType>& type); 131 132 void SetAstFileType(); 133 134 bool CheckIntegrity(); 135 136 bool CheckInterfaceAst(); 137 138 bool CheckCallbackAst(); 139 140 bool IsValidTypeName(const String& typeName); 141 142 bool CheckPackageName(const String& filePath, const String& packageName); 143 IsPrimitiveType(Token token)144 inline static bool IsPrimitiveType(Token token) 145 { 146 return token >= Token::BOOLEAN && token <= Token::FILEDESCRIPTOR; 147 } 148 149 bool AddAst(); 150 151 void LogError(const String& message); 152 153 void LogError(int lineNo, int columnNo, const String& message); 154 155 void ShowError(); 156 157 static const char* TAG; 158 159 const Options& options_; 160 std::shared_ptr<Lexer> lexer_; 161 AutoPtr<ErrorInfo> errors_; 162 AutoPtr<AST> ast_; 163 AutoPtr<ASTModule> astModule_; 164 }; 165 } // namespace HDI 166 } // namespace OHOS 167 168 #endif // OHOS_HDI_PARSER_H