/* * Copyright (c) 2021-2022 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 OHOS_HDI_PARSER_H #define OHOS_HDI_PARSER_H #include #include #include #include "ast/ast.h" #include "ast/ast_attribute.h" #include "ast/ast_interface_type.h" #include "ast/ast_method.h" #include "ast/ast_type.h" #include "lexer/lexer.h" #include "util/autoptr.h" #include "util/light_refcount_base.h" #include "util/options.h" namespace OHOS { namespace HDI { using AttrSet = std::set; class Parser { public: Parser() = default; ~Parser() = default; bool Parse(const std::vector &sourceFiles); using StrAstMap = std::unordered_map>; inline const StrAstMap &GetAllAst() const { return allAsts_; } private: class Attribute : public LightRefCountBase { public: bool isOneWay = false; bool isCallback = false; bool isFull = false; bool isLite = false; }; bool ParseOne(const std::string &sourceFile); bool Reset(const std::string &sourceFile); bool ParseFile(); std::string ParseLicense(); bool ParsePackage(); bool ParserPackageInfo(const std::string &packageFullName); bool ParseImports(); void ParseImportInfo(); void ParseSequenceableInfo(); bool ParseTypeDecls(); // parse attributes of type void ParseAttribute(); AttrSet ParseAttributeInfo(); bool AprseAttrUnit(AttrSet &attrs); // parse interface type void ParseInterface(const AttrSet &attrs = {}); AutoPtr ParseInfAttrInfo(const AttrSet &attrs); void ParseInterfaceBody(const AutoPtr &interface); AutoPtr ParseMethod(); AutoPtr ParseMethodAttr(); AutoPtr CreateGetVersionMethod(); void ParseMethodParamList(const AutoPtr &method); AutoPtr ParseParam(); AutoPtr ParseParamAttr(); // parse type AutoPtr ParseType(); AutoPtr ParseBasicType(); AutoPtr ParseUnsignedType(); AutoPtr ParseArrayType(const AutoPtr &elementType); AutoPtr ParseListType(); AutoPtr ParseMapType(); AutoPtr ParseSmqType(); AutoPtr ParseUserDefType(); // parse declaration of enum void ParseEnumDeclaration(const AttrSet &attrs = {}); AutoPtr ParseEnumBaseType(); void ParserEnumMember(const AutoPtr &enumType); // parse declaration of struct void ParseStructDeclaration(const AttrSet &attrs = {}); void ParseStructMember(const AutoPtr &structType); // parse declaration of union void ParseUnionDeclaration(const AttrSet &attrs = {}); void ParseUnionMember(const AutoPtr &unionType); bool AddUnionMember(const AutoPtr &unionType, const AutoPtr &type, const std::string &name); AutoPtr ParseUserDefTypeAttr(const AttrSet &attrs); // parse expression AutoPtr ParseExpr(); AutoPtr ParseAndExpr(); AutoPtr ParseXorExpr(); AutoPtr ParseOrExpr(); AutoPtr ParseShiftExpr(); AutoPtr ParseAddExpr(); AutoPtr ParseMulExpr(); AutoPtr ParseUnaryExpr(); AutoPtr ParsePrimaryExpr(); AutoPtr ParseNumExpr(); bool CheckType(const Token &token, const AutoPtr &type); void SetAstFileType(); bool CheckIntegrity(); bool CheckInterfaceAst(); bool CheckCallbackAst(); bool CheckPackageName(const std::string &filePath, const std::string &packageName); bool CheckImport(const std::string &importName); inline static bool IsPrimitiveType(Token token) { return token.kind_ >= TokenType::BOOLEAN && token.kind_ <= TokenType::ASHMEM; } bool AddAst(const AutoPtr &ast); void LogError(const std::string &message); void LogError(const Token &token, const std::string &message); void ShowError(); Lexer lexer_; std::vector errors_; AutoPtr ast_; StrAstMap allAsts_; }; } // namespace HDI } // namespace OHOS #endif // OHOS_HDI_PARSER_H