From bbfbbc894b25d57fe0b3dd8ffc459b6d1dee6834 Mon Sep 17 00:00:00 2001 From: biqiboqi Date: Thu, 23 Oct 2025 00:38:45 +0300 Subject: [PATCH 01/21] feat: add parser interfaces and classes --- lib/parser/IParser.hpp | 16 ++++ lib/parser/ParserFsm.cpp | 1 + lib/parser/ParserFsm.hpp | 24 +++++ lib/parser/ast/AstVisitor.hpp | 96 +++++++++++++++++++ lib/parser/ast/IAstFactory.hpp | 93 ++++++++++++++++++ lib/parser/ast/nodes/base/AstNode.hpp | 13 +++ lib/parser/ast/nodes/base/Decl.hpp | 8 ++ lib/parser/ast/nodes/base/Expr.hpp | 8 ++ lib/parser/ast/nodes/base/Stmt.hpp | 8 ++ .../ast/nodes/class_members/CallDecl.cpp | 1 + .../ast/nodes/class_members/CallDecl.hpp | 22 +++++ .../nodes/class_members/DestructorDecl.cpp | 1 + .../nodes/class_members/DestructorDecl.hpp | 18 ++++ .../ast/nodes/class_members/FieldDecl.cpp | 1 + .../ast/nodes/class_members/FieldDecl.hpp | 22 +++++ .../ast/nodes/class_members/MethodDecl.cpp | 1 + .../ast/nodes/class_members/MethodDecl.hpp | 30 ++++++ .../nodes/class_members/StaticFieldDecl.cpp | 1 + .../nodes/class_members/StaticFieldDecl.hpp | 23 +++++ lib/parser/ast/nodes/decls/ClassDecl.cpp | 1 + lib/parser/ast/nodes/decls/ClassDecl.hpp | 21 ++++ lib/parser/ast/nodes/decls/FunctionDecl.cpp | 1 + lib/parser/ast/nodes/decls/FunctionDecl.hpp | 24 +++++ lib/parser/ast/nodes/decls/GlobalVarDecl.cpp | 1 + lib/parser/ast/nodes/decls/GlobalVarDecl.hpp | 22 +++++ lib/parser/ast/nodes/decls/InterfaceDecl.cpp | 1 + lib/parser/ast/nodes/decls/InterfaceDecl.hpp | 19 ++++ lib/parser/ast/nodes/decls/Module.cpp | 1 + lib/parser/ast/nodes/decls/Module.hpp | 21 ++++ lib/parser/ast/nodes/decls/TypeAliasDecl.cpp | 1 + lib/parser/ast/nodes/decls/TypeAliasDecl.hpp | 18 ++++ lib/parser/ast/nodes/exprs/Assign.cpp | 1 + lib/parser/ast/nodes/exprs/Assign.hpp | 20 ++++ lib/parser/ast/nodes/exprs/Binary.cpp | 1 + lib/parser/ast/nodes/exprs/Binary.hpp | 19 ++++ lib/parser/ast/nodes/exprs/Call.cpp | 1 + lib/parser/ast/nodes/exprs/Call.hpp | 18 ++++ lib/parser/ast/nodes/exprs/CastAs.cpp | 1 + lib/parser/ast/nodes/exprs/CastAs.hpp | 18 ++++ lib/parser/ast/nodes/exprs/Elvis.cpp | 1 + lib/parser/ast/nodes/exprs/Elvis.hpp | 17 ++++ lib/parser/ast/nodes/exprs/FieldAccess.cpp | 1 + lib/parser/ast/nodes/exprs/FieldAccess.hpp | 18 ++++ lib/parser/ast/nodes/exprs/IdentRef.cpp | 1 + lib/parser/ast/nodes/exprs/IdentRef.hpp | 16 ++++ lib/parser/ast/nodes/exprs/IndexAccess.cpp | 1 + lib/parser/ast/nodes/exprs/IndexAccess.hpp | 17 ++++ lib/parser/ast/nodes/exprs/NamespaceRef.cpp | 1 + lib/parser/ast/nodes/exprs/NamespaceRef.hpp | 18 ++++ lib/parser/ast/nodes/exprs/SafeCall.cpp | 1 + lib/parser/ast/nodes/exprs/SafeCall.hpp | 16 ++++ lib/parser/ast/nodes/exprs/TypeTestIs.cpp | 1 + lib/parser/ast/nodes/exprs/TypeTestIs.hpp | 18 ++++ lib/parser/ast/nodes/exprs/Unary.cpp | 1 + lib/parser/ast/nodes/exprs/Unary.hpp | 19 ++++ .../ast/nodes/exprs/literals/BoolLit.cpp | 1 + .../ast/nodes/exprs/literals/BoolLit.hpp | 14 +++ .../ast/nodes/exprs/literals/CharLit.cpp | 1 + .../ast/nodes/exprs/literals/CharLit.hpp | 14 +++ .../ast/nodes/exprs/literals/FloatLit.cpp | 1 + .../ast/nodes/exprs/literals/FloatLit.hpp | 13 +++ .../ast/nodes/exprs/literals/IntLit.cpp | 1 + .../ast/nodes/exprs/literals/IntLit.hpp | 12 +++ .../ast/nodes/exprs/literals/NullLit.cpp | 1 + .../ast/nodes/exprs/literals/NullLit.hpp | 11 +++ .../ast/nodes/exprs/literals/StringLit.cpp | 1 + .../ast/nodes/exprs/literals/StringLit.hpp | 16 ++++ .../ast/nodes/exprs/tags/IAssignOpTag.hpp | 11 +++ .../ast/nodes/exprs/tags/IBinaryOpTag.hpp | 11 +++ .../ast/nodes/exprs/tags/IUnaryOpTag.hpp | 11 +++ lib/parser/ast/nodes/exprs/tags/OpTags.hpp | 33 +++++++ lib/parser/ast/nodes/stmts/Block.cpp | 1 + lib/parser/ast/nodes/stmts/Block.hpp | 22 +++++ lib/parser/ast/nodes/stmts/Branch.cpp | 1 + lib/parser/ast/nodes/stmts/Branch.hpp | 26 +++++ lib/parser/ast/nodes/stmts/BreakStmt.cpp | 1 + lib/parser/ast/nodes/stmts/BreakStmt.hpp | 11 +++ lib/parser/ast/nodes/stmts/ContinueStmt.cpp | 1 + lib/parser/ast/nodes/stmts/ContinueStmt.hpp | 11 +++ lib/parser/ast/nodes/stmts/ExprStmt.cpp | 1 + lib/parser/ast/nodes/stmts/ExprStmt.hpp | 17 ++++ lib/parser/ast/nodes/stmts/ForStmt.cpp | 1 + lib/parser/ast/nodes/stmts/ForStmt.hpp | 20 ++++ lib/parser/ast/nodes/stmts/IfStmt.cpp | 1 + lib/parser/ast/nodes/stmts/IfStmt.hpp | 25 +++++ lib/parser/ast/nodes/stmts/ReturnStmt.cpp | 1 + lib/parser/ast/nodes/stmts/ReturnStmt.hpp | 17 ++++ lib/parser/ast/nodes/stmts/UnsafeBlock.cpp | 1 + lib/parser/ast/nodes/stmts/UnsafeBlock.hpp | 17 ++++ lib/parser/ast/nodes/stmts/VarDeclStmt.cpp | 1 + lib/parser/ast/nodes/stmts/VarDeclStmt.hpp | 22 +++++ lib/parser/ast/nodes/stmts/WhileStmt.cpp | 1 + lib/parser/ast/nodes/stmts/WhileStmt.hpp | 19 ++++ lib/parser/context/ContextParser.cpp | 1 + lib/parser/context/ContextParser.hpp | 52 ++++++++++ lib/parser/context/NodeEntry.cpp | 1 + lib/parser/context/NodeEntry.hpp | 16 ++++ lib/parser/diagnostics/Diagnostic.cpp | 1 + lib/parser/diagnostics/Diagnostic.hpp | 28 ++++++ .../diagnostics/DiagnosticCollector.cpp | 1 + .../diagnostics/DiagnosticCollector.hpp | 57 +++++++++++ lib/parser/diagnostics/FixIt.cpp | 1 + lib/parser/diagnostics/FixIt.hpp | 17 ++++ lib/parser/diagnostics/IDiagnosticSink.hpp | 24 +++++ lib/parser/diagnostics/RelatedInfo.cpp | 1 + lib/parser/diagnostics/RelatedInfo.hpp | 17 ++++ lib/parser/diagnostics/severity/ISeverity.cpp | 1 + lib/parser/diagnostics/severity/ISeverity.hpp | 13 +++ lib/parser/diagnostics/severity/Severity.cpp | 1 + lib/parser/diagnostics/severity/Severity.hpp | 14 +++ lib/parser/pratt/DefaultOperatorResolver.cpp | 1 + lib/parser/pratt/DefaultOperatorResolver.hpp | 25 +++++ lib/parser/pratt/IExpressionParser.hpp | 17 ++++ lib/parser/pratt/IOperatorResolver.hpp | 17 ++++ lib/parser/pratt/PrattExpressionParser.cpp | 1 + lib/parser/pratt/PrattExpressionParser.hpp | 35 +++++++ lib/parser/pratt/specifications/InfixSpec.cpp | 1 + lib/parser/pratt/specifications/InfixSpec.hpp | 21 ++++ .../pratt/specifications/PostfixSpec.cpp | 1 + .../pratt/specifications/PostfixSpec.hpp | 17 ++++ lib/parser/recovery/IRecoveryStrategy.hpp | 14 +++ lib/parser/recovery/SimpleRecovery.cpp | 1 + lib/parser/recovery/SimpleRecovery.hpp | 14 +++ lib/parser/states/StateBlock.cpp | 1 + lib/parser/states/StateBlock.hpp | 13 +++ lib/parser/states/StateCallDeclHdr.cpp | 1 + lib/parser/states/StateCallDeclHdr.hpp | 13 +++ lib/parser/states/StateDestructorDecl.cpp | 1 + lib/parser/states/StateDestructorDecl.hpp | 13 +++ lib/parser/states/StateExpr.cpp | 1 + lib/parser/states/StateExpr.hpp | 13 +++ lib/parser/states/StateFieldDecl.cpp | 1 + lib/parser/states/StateFieldDecl.hpp | 13 +++ lib/parser/states/StateForHead.cpp | 1 + lib/parser/states/StateForHead.hpp | 13 +++ lib/parser/states/StateGlobalVarDecl.cpp | 1 + lib/parser/states/StateGlobalVarDecl.hpp | 13 +++ lib/parser/states/StateIfHead.cpp | 1 + lib/parser/states/StateIfHead.hpp | 13 +++ lib/parser/states/StateIfTail.cpp | 1 + lib/parser/states/StateIfTail.hpp | 13 +++ lib/parser/states/StateMethodHdr.cpp | 1 + lib/parser/states/StateMethodHdr.hpp | 13 +++ lib/parser/states/StateModule.cpp | 1 + lib/parser/states/StateModule.hpp | 13 +++ lib/parser/states/StateParseType.cpp | 1 + lib/parser/states/StateParseType.hpp | 13 +++ lib/parser/states/StateReturnTail.cpp | 1 + lib/parser/states/StateReturnTail.hpp | 13 +++ lib/parser/states/StateStmt.cpp | 1 + lib/parser/states/StateStmt.hpp | 13 +++ lib/parser/states/StateSyncToBlockEnd.cpp | 1 + lib/parser/states/StateSyncToBlockEnd.hpp | 13 +++ lib/parser/states/StateSyncToStmtEnd.cpp | 1 + lib/parser/states/StateSyncToStmtEnd.hpp | 13 +++ lib/parser/states/StateTopDecl.cpp | 1 + lib/parser/states/StateTopDecl.hpp | 13 +++ lib/parser/states/StateTypeAliasDecl.cpp | 1 + lib/parser/states/StateTypeAliasDecl.hpp | 13 +++ lib/parser/states/StateUnsafeBlock.cpp | 1 + lib/parser/states/StateUnsafeBlock.hpp | 13 +++ lib/parser/states/StateVarDeclTail.cpp | 1 + lib/parser/states/StateVarDeclTail.hpp | 13 +++ lib/parser/states/StateWhileHead.cpp | 1 + lib/parser/states/StateWhileHead.hpp | 13 +++ lib/parser/states/base/IState.hpp | 14 +++ lib/parser/states/base/StateBase.cpp | 1 + lib/parser/states/base/StateBase.hpp | 14 +++ lib/parser/states/base/StateRegistry.cpp | 1 + lib/parser/states/base/StateRegistry.hpp | 45 +++++++++ lib/parser/states/class/StateClassBody.cpp | 1 + lib/parser/states/class/StateClassBody.hpp | 14 +++ lib/parser/states/class/StateClassHdr.cpp | 1 + lib/parser/states/class/StateClassHdr.hpp | 14 +++ lib/parser/states/class/StateClassMember.cpp | 1 + lib/parser/states/class/StateClassMember.hpp | 13 +++ lib/parser/states/func/StateFuncBody.cpp | 1 + lib/parser/states/func/StateFuncBody.hpp | 13 +++ lib/parser/states/func/StateFuncHdr.cpp | 1 + lib/parser/states/func/StateFuncHdr.hpp | 13 +++ lib/parser/states/func/StateFuncParams.cpp | 1 + lib/parser/states/func/StateFuncParams.hpp | 13 +++ .../states/interface/StateInterfaceBody.cpp | 1 + .../states/interface/StateInterfaceBody.hpp | 13 +++ .../states/interface/StateInterfaceDecl.cpp | 1 + .../states/interface/StateInterfaceDecl.hpp | 13 +++ .../states/interface/StateInterfaceHdr.cpp | 1 + .../states/interface/StateInterfaceHdr.hpp | 13 +++ lib/parser/tokens/SourceSpan.cpp | 1 + lib/parser/tokens/SourceSpan.hpp | 19 ++++ .../tokens/token_streams/ITokenStream.hpp | 27 ++++++ .../token_streams/VectorTokenStream.cpp | 1 + .../token_streams/VectorTokenStream.hpp | 38 ++++++++ .../tokens/token_traits/ITokenMatcher.hpp | 13 +++ .../tokens/token_traits/MatchIdentifier.cpp | 1 + .../tokens/token_traits/MatchIdentifier.hpp | 12 +++ .../tokens/token_traits/MatchLexeme.cpp | 1 + .../tokens/token_traits/MatchLexeme.hpp | 19 ++++ .../tokens/token_traits/MatchLiteral.cpp | 1 + .../tokens/token_traits/MatchLiteral.hpp | 12 +++ .../tokens/token_traits/MatchManyOf.cpp | 1 + .../tokens/token_traits/MatchManyOf.hpp | 20 ++++ lib/parser/tokens/token_traits/MatchType.cpp | 1 + lib/parser/tokens/token_traits/MatchType.hpp | 19 ++++ lib/parser/type_parser/ITypeParser.hpp | 16 ++++ lib/parser/type_parser/QNameTypeParser.cpp | 1 + lib/parser/type_parser/QNameTypeParser.hpp | 23 +++++ lib/parser/types/Param.cpp | 1 + lib/parser/types/Param.hpp | 21 ++++ lib/parser/types/TypeReference.cpp | 1 + lib/parser/types/TypeReference.hpp | 30 ++++++ 211 files changed, 2301 insertions(+) create mode 100644 lib/parser/IParser.hpp create mode 100644 lib/parser/ParserFsm.cpp create mode 100644 lib/parser/ParserFsm.hpp create mode 100644 lib/parser/ast/AstVisitor.hpp create mode 100644 lib/parser/ast/IAstFactory.hpp create mode 100644 lib/parser/ast/nodes/base/AstNode.hpp create mode 100644 lib/parser/ast/nodes/base/Decl.hpp create mode 100644 lib/parser/ast/nodes/base/Expr.hpp create mode 100644 lib/parser/ast/nodes/base/Stmt.hpp create mode 100644 lib/parser/ast/nodes/class_members/CallDecl.cpp create mode 100644 lib/parser/ast/nodes/class_members/CallDecl.hpp create mode 100644 lib/parser/ast/nodes/class_members/DestructorDecl.cpp create mode 100644 lib/parser/ast/nodes/class_members/DestructorDecl.hpp create mode 100644 lib/parser/ast/nodes/class_members/FieldDecl.cpp create mode 100644 lib/parser/ast/nodes/class_members/FieldDecl.hpp create mode 100644 lib/parser/ast/nodes/class_members/MethodDecl.cpp create mode 100644 lib/parser/ast/nodes/class_members/MethodDecl.hpp create mode 100644 lib/parser/ast/nodes/class_members/StaticFieldDecl.cpp create mode 100644 lib/parser/ast/nodes/class_members/StaticFieldDecl.hpp create mode 100644 lib/parser/ast/nodes/decls/ClassDecl.cpp create mode 100644 lib/parser/ast/nodes/decls/ClassDecl.hpp create mode 100644 lib/parser/ast/nodes/decls/FunctionDecl.cpp create mode 100644 lib/parser/ast/nodes/decls/FunctionDecl.hpp create mode 100644 lib/parser/ast/nodes/decls/GlobalVarDecl.cpp create mode 100644 lib/parser/ast/nodes/decls/GlobalVarDecl.hpp create mode 100644 lib/parser/ast/nodes/decls/InterfaceDecl.cpp create mode 100644 lib/parser/ast/nodes/decls/InterfaceDecl.hpp create mode 100644 lib/parser/ast/nodes/decls/Module.cpp create mode 100644 lib/parser/ast/nodes/decls/Module.hpp create mode 100644 lib/parser/ast/nodes/decls/TypeAliasDecl.cpp create mode 100644 lib/parser/ast/nodes/decls/TypeAliasDecl.hpp create mode 100644 lib/parser/ast/nodes/exprs/Assign.cpp create mode 100644 lib/parser/ast/nodes/exprs/Assign.hpp create mode 100644 lib/parser/ast/nodes/exprs/Binary.cpp create mode 100644 lib/parser/ast/nodes/exprs/Binary.hpp create mode 100644 lib/parser/ast/nodes/exprs/Call.cpp create mode 100644 lib/parser/ast/nodes/exprs/Call.hpp create mode 100644 lib/parser/ast/nodes/exprs/CastAs.cpp create mode 100644 lib/parser/ast/nodes/exprs/CastAs.hpp create mode 100644 lib/parser/ast/nodes/exprs/Elvis.cpp create mode 100644 lib/parser/ast/nodes/exprs/Elvis.hpp create mode 100644 lib/parser/ast/nodes/exprs/FieldAccess.cpp create mode 100644 lib/parser/ast/nodes/exprs/FieldAccess.hpp create mode 100644 lib/parser/ast/nodes/exprs/IdentRef.cpp create mode 100644 lib/parser/ast/nodes/exprs/IdentRef.hpp create mode 100644 lib/parser/ast/nodes/exprs/IndexAccess.cpp create mode 100644 lib/parser/ast/nodes/exprs/IndexAccess.hpp create mode 100644 lib/parser/ast/nodes/exprs/NamespaceRef.cpp create mode 100644 lib/parser/ast/nodes/exprs/NamespaceRef.hpp create mode 100644 lib/parser/ast/nodes/exprs/SafeCall.cpp create mode 100644 lib/parser/ast/nodes/exprs/SafeCall.hpp create mode 100644 lib/parser/ast/nodes/exprs/TypeTestIs.cpp create mode 100644 lib/parser/ast/nodes/exprs/TypeTestIs.hpp create mode 100644 lib/parser/ast/nodes/exprs/Unary.cpp create mode 100644 lib/parser/ast/nodes/exprs/Unary.hpp create mode 100644 lib/parser/ast/nodes/exprs/literals/BoolLit.cpp create mode 100644 lib/parser/ast/nodes/exprs/literals/BoolLit.hpp create mode 100644 lib/parser/ast/nodes/exprs/literals/CharLit.cpp create mode 100644 lib/parser/ast/nodes/exprs/literals/CharLit.hpp create mode 100644 lib/parser/ast/nodes/exprs/literals/FloatLit.cpp create mode 100644 lib/parser/ast/nodes/exprs/literals/FloatLit.hpp create mode 100644 lib/parser/ast/nodes/exprs/literals/IntLit.cpp create mode 100644 lib/parser/ast/nodes/exprs/literals/IntLit.hpp create mode 100644 lib/parser/ast/nodes/exprs/literals/NullLit.cpp create mode 100644 lib/parser/ast/nodes/exprs/literals/NullLit.hpp create mode 100644 lib/parser/ast/nodes/exprs/literals/StringLit.cpp create mode 100644 lib/parser/ast/nodes/exprs/literals/StringLit.hpp create mode 100644 lib/parser/ast/nodes/exprs/tags/IAssignOpTag.hpp create mode 100644 lib/parser/ast/nodes/exprs/tags/IBinaryOpTag.hpp create mode 100644 lib/parser/ast/nodes/exprs/tags/IUnaryOpTag.hpp create mode 100644 lib/parser/ast/nodes/exprs/tags/OpTags.hpp create mode 100644 lib/parser/ast/nodes/stmts/Block.cpp create mode 100644 lib/parser/ast/nodes/stmts/Block.hpp create mode 100644 lib/parser/ast/nodes/stmts/Branch.cpp create mode 100644 lib/parser/ast/nodes/stmts/Branch.hpp create mode 100644 lib/parser/ast/nodes/stmts/BreakStmt.cpp create mode 100644 lib/parser/ast/nodes/stmts/BreakStmt.hpp create mode 100644 lib/parser/ast/nodes/stmts/ContinueStmt.cpp create mode 100644 lib/parser/ast/nodes/stmts/ContinueStmt.hpp create mode 100644 lib/parser/ast/nodes/stmts/ExprStmt.cpp create mode 100644 lib/parser/ast/nodes/stmts/ExprStmt.hpp create mode 100644 lib/parser/ast/nodes/stmts/ForStmt.cpp create mode 100644 lib/parser/ast/nodes/stmts/ForStmt.hpp create mode 100644 lib/parser/ast/nodes/stmts/IfStmt.cpp create mode 100644 lib/parser/ast/nodes/stmts/IfStmt.hpp create mode 100644 lib/parser/ast/nodes/stmts/ReturnStmt.cpp create mode 100644 lib/parser/ast/nodes/stmts/ReturnStmt.hpp create mode 100644 lib/parser/ast/nodes/stmts/UnsafeBlock.cpp create mode 100644 lib/parser/ast/nodes/stmts/UnsafeBlock.hpp create mode 100644 lib/parser/ast/nodes/stmts/VarDeclStmt.cpp create mode 100644 lib/parser/ast/nodes/stmts/VarDeclStmt.hpp create mode 100644 lib/parser/ast/nodes/stmts/WhileStmt.cpp create mode 100644 lib/parser/ast/nodes/stmts/WhileStmt.hpp create mode 100644 lib/parser/context/ContextParser.cpp create mode 100644 lib/parser/context/ContextParser.hpp create mode 100644 lib/parser/context/NodeEntry.cpp create mode 100644 lib/parser/context/NodeEntry.hpp create mode 100644 lib/parser/diagnostics/Diagnostic.cpp create mode 100644 lib/parser/diagnostics/Diagnostic.hpp create mode 100644 lib/parser/diagnostics/DiagnosticCollector.cpp create mode 100644 lib/parser/diagnostics/DiagnosticCollector.hpp create mode 100644 lib/parser/diagnostics/FixIt.cpp create mode 100644 lib/parser/diagnostics/FixIt.hpp create mode 100644 lib/parser/diagnostics/IDiagnosticSink.hpp create mode 100644 lib/parser/diagnostics/RelatedInfo.cpp create mode 100644 lib/parser/diagnostics/RelatedInfo.hpp create mode 100644 lib/parser/diagnostics/severity/ISeverity.cpp create mode 100644 lib/parser/diagnostics/severity/ISeverity.hpp create mode 100644 lib/parser/diagnostics/severity/Severity.cpp create mode 100644 lib/parser/diagnostics/severity/Severity.hpp create mode 100644 lib/parser/pratt/DefaultOperatorResolver.cpp create mode 100644 lib/parser/pratt/DefaultOperatorResolver.hpp create mode 100644 lib/parser/pratt/IExpressionParser.hpp create mode 100644 lib/parser/pratt/IOperatorResolver.hpp create mode 100644 lib/parser/pratt/PrattExpressionParser.cpp create mode 100644 lib/parser/pratt/PrattExpressionParser.hpp create mode 100644 lib/parser/pratt/specifications/InfixSpec.cpp create mode 100644 lib/parser/pratt/specifications/InfixSpec.hpp create mode 100644 lib/parser/pratt/specifications/PostfixSpec.cpp create mode 100644 lib/parser/pratt/specifications/PostfixSpec.hpp create mode 100644 lib/parser/recovery/IRecoveryStrategy.hpp create mode 100644 lib/parser/recovery/SimpleRecovery.cpp create mode 100644 lib/parser/recovery/SimpleRecovery.hpp create mode 100644 lib/parser/states/StateBlock.cpp create mode 100644 lib/parser/states/StateBlock.hpp create mode 100644 lib/parser/states/StateCallDeclHdr.cpp create mode 100644 lib/parser/states/StateCallDeclHdr.hpp create mode 100644 lib/parser/states/StateDestructorDecl.cpp create mode 100644 lib/parser/states/StateDestructorDecl.hpp create mode 100644 lib/parser/states/StateExpr.cpp create mode 100644 lib/parser/states/StateExpr.hpp create mode 100644 lib/parser/states/StateFieldDecl.cpp create mode 100644 lib/parser/states/StateFieldDecl.hpp create mode 100644 lib/parser/states/StateForHead.cpp create mode 100644 lib/parser/states/StateForHead.hpp create mode 100644 lib/parser/states/StateGlobalVarDecl.cpp create mode 100644 lib/parser/states/StateGlobalVarDecl.hpp create mode 100644 lib/parser/states/StateIfHead.cpp create mode 100644 lib/parser/states/StateIfHead.hpp create mode 100644 lib/parser/states/StateIfTail.cpp create mode 100644 lib/parser/states/StateIfTail.hpp create mode 100644 lib/parser/states/StateMethodHdr.cpp create mode 100644 lib/parser/states/StateMethodHdr.hpp create mode 100644 lib/parser/states/StateModule.cpp create mode 100644 lib/parser/states/StateModule.hpp create mode 100644 lib/parser/states/StateParseType.cpp create mode 100644 lib/parser/states/StateParseType.hpp create mode 100644 lib/parser/states/StateReturnTail.cpp create mode 100644 lib/parser/states/StateReturnTail.hpp create mode 100644 lib/parser/states/StateStmt.cpp create mode 100644 lib/parser/states/StateStmt.hpp create mode 100644 lib/parser/states/StateSyncToBlockEnd.cpp create mode 100644 lib/parser/states/StateSyncToBlockEnd.hpp create mode 100644 lib/parser/states/StateSyncToStmtEnd.cpp create mode 100644 lib/parser/states/StateSyncToStmtEnd.hpp create mode 100644 lib/parser/states/StateTopDecl.cpp create mode 100644 lib/parser/states/StateTopDecl.hpp create mode 100644 lib/parser/states/StateTypeAliasDecl.cpp create mode 100644 lib/parser/states/StateTypeAliasDecl.hpp create mode 100644 lib/parser/states/StateUnsafeBlock.cpp create mode 100644 lib/parser/states/StateUnsafeBlock.hpp create mode 100644 lib/parser/states/StateVarDeclTail.cpp create mode 100644 lib/parser/states/StateVarDeclTail.hpp create mode 100644 lib/parser/states/StateWhileHead.cpp create mode 100644 lib/parser/states/StateWhileHead.hpp create mode 100644 lib/parser/states/base/IState.hpp create mode 100644 lib/parser/states/base/StateBase.cpp create mode 100644 lib/parser/states/base/StateBase.hpp create mode 100644 lib/parser/states/base/StateRegistry.cpp create mode 100644 lib/parser/states/base/StateRegistry.hpp create mode 100644 lib/parser/states/class/StateClassBody.cpp create mode 100644 lib/parser/states/class/StateClassBody.hpp create mode 100644 lib/parser/states/class/StateClassHdr.cpp create mode 100644 lib/parser/states/class/StateClassHdr.hpp create mode 100644 lib/parser/states/class/StateClassMember.cpp create mode 100644 lib/parser/states/class/StateClassMember.hpp create mode 100644 lib/parser/states/func/StateFuncBody.cpp create mode 100644 lib/parser/states/func/StateFuncBody.hpp create mode 100644 lib/parser/states/func/StateFuncHdr.cpp create mode 100644 lib/parser/states/func/StateFuncHdr.hpp create mode 100644 lib/parser/states/func/StateFuncParams.cpp create mode 100644 lib/parser/states/func/StateFuncParams.hpp create mode 100644 lib/parser/states/interface/StateInterfaceBody.cpp create mode 100644 lib/parser/states/interface/StateInterfaceBody.hpp create mode 100644 lib/parser/states/interface/StateInterfaceDecl.cpp create mode 100644 lib/parser/states/interface/StateInterfaceDecl.hpp create mode 100644 lib/parser/states/interface/StateInterfaceHdr.cpp create mode 100644 lib/parser/states/interface/StateInterfaceHdr.hpp create mode 100644 lib/parser/tokens/SourceSpan.cpp create mode 100644 lib/parser/tokens/SourceSpan.hpp create mode 100644 lib/parser/tokens/token_streams/ITokenStream.hpp create mode 100644 lib/parser/tokens/token_streams/VectorTokenStream.cpp create mode 100644 lib/parser/tokens/token_streams/VectorTokenStream.hpp create mode 100644 lib/parser/tokens/token_traits/ITokenMatcher.hpp create mode 100644 lib/parser/tokens/token_traits/MatchIdentifier.cpp create mode 100644 lib/parser/tokens/token_traits/MatchIdentifier.hpp create mode 100644 lib/parser/tokens/token_traits/MatchLexeme.cpp create mode 100644 lib/parser/tokens/token_traits/MatchLexeme.hpp create mode 100644 lib/parser/tokens/token_traits/MatchLiteral.cpp create mode 100644 lib/parser/tokens/token_traits/MatchLiteral.hpp create mode 100644 lib/parser/tokens/token_traits/MatchManyOf.cpp create mode 100644 lib/parser/tokens/token_traits/MatchManyOf.hpp create mode 100644 lib/parser/tokens/token_traits/MatchType.cpp create mode 100644 lib/parser/tokens/token_traits/MatchType.hpp create mode 100644 lib/parser/type_parser/ITypeParser.hpp create mode 100644 lib/parser/type_parser/QNameTypeParser.cpp create mode 100644 lib/parser/type_parser/QNameTypeParser.hpp create mode 100644 lib/parser/types/Param.cpp create mode 100644 lib/parser/types/Param.hpp create mode 100644 lib/parser/types/TypeReference.cpp create mode 100644 lib/parser/types/TypeReference.hpp diff --git a/lib/parser/IParser.hpp b/lib/parser/IParser.hpp new file mode 100644 index 0000000..d166499 --- /dev/null +++ b/lib/parser/IParser.hpp @@ -0,0 +1,16 @@ +#ifndef IPARSER_HPP_ +#define IPARSER_HPP_ + +#include + +#include "ast/nodes/decls/Module.hpp" +#include "diagnostics/IDiagnosticSink.hpp" +#include "tokens/token_streams/ITokenStream.hpp" + +class IParser { +public: + virtual ~IParser() = default; + virtual std::unique_ptr Parse(ITokenStream& ts, IDiagnosticSink& diags) = 0; +}; + +#endif // IPARSER_HPP_ diff --git a/lib/parser/ParserFsm.cpp b/lib/parser/ParserFsm.cpp new file mode 100644 index 0000000..2cbd8d1 --- /dev/null +++ b/lib/parser/ParserFsm.cpp @@ -0,0 +1 @@ +#include "ParserFsm.hpp" diff --git a/lib/parser/ParserFsm.hpp b/lib/parser/ParserFsm.hpp new file mode 100644 index 0000000..2b5f482 --- /dev/null +++ b/lib/parser/ParserFsm.hpp @@ -0,0 +1,24 @@ +#ifndef PARSERFSM_HPP_ +#define PARSERFSM_HPP_ + +#include + +#include "IParser.hpp" + +class ParserFsm : public IParser { +public: + ParserFsm(std::unique_ptr expr, + std::unique_ptr typep, + std::unique_ptr factory); + + ~ParserFsm() override = default; + + std::unique_ptr Parse(ITokenStream& ts, IDiagnosticSink& diags) override; + +private: + std::unique_ptr expr_parser_; + std::unique_ptr type_parser_; + std::unique_ptr factory_; +}; + +#endif // PARSERFSM_HPP_ diff --git a/lib/parser/ast/AstVisitor.hpp b/lib/parser/ast/AstVisitor.hpp new file mode 100644 index 0000000..f8b7fdb --- /dev/null +++ b/lib/parser/ast/AstVisitor.hpp @@ -0,0 +1,96 @@ +#ifndef ASTVISITOR_HPP_ +#define ASTVISITOR_HPP_ + +class Module; +class FunctionDecl; +class ClassDecl; +class InterfaceDecl; +class TypeAliasDecl; +class GlobalVarDecl; +class FieldDecl; +class StaticFieldDecl; +class MethodDecl; +class CallDecl; +class DestructorDecl; + +class Block; +class VarDeclStmt; +class ExprStmt; +class ReturnStmt; +class BreakStmt; +class ContinueStmt; +class IfStmt; +class WhileStmt; +class ForStmt; +class UnsafeBlock; + +class Binary; +class Unary; +class Assign; +class Call; +class FieldAccess; +class IndexAccess; +class NamespaceRef; +class SafeCall; +class Elvis; +class CastAs; +class TypeTestIs; +class IdentRef; +class IntLit; +class FloatLit; +class StringLit; +class CharLit; +class BoolLit; +class NullLit; + +class AstVisitor { +public: + virtual ~AstVisitor() = default; + + // Decls + virtual void Visit(Module&) = 0; + virtual void Visit(FunctionDecl&) = 0; + virtual void Visit(ClassDecl&) = 0; + virtual void Visit(InterfaceDecl&) = 0; + virtual void Visit(TypeAliasDecl&) = 0; + virtual void Visit(GlobalVarDecl&) = 0; + virtual void Visit(FieldDecl&) = 0; + virtual void Visit(StaticFieldDecl&) = 0; + virtual void Visit(MethodDecl&) = 0; + virtual void Visit(CallDecl&) = 0; + virtual void Visit(DestructorDecl&) = 0; + + // Stmts + virtual void Visit(Block&) = 0; + virtual void Visit(VarDeclStmt&) = 0; + virtual void Visit(ExprStmt&) = 0; + virtual void Visit(ReturnStmt&) = 0; + virtual void Visit(BreakStmt&) = 0; + virtual void Visit(ContinueStmt&) = 0; + virtual void Visit(IfStmt&) = 0; + virtual void Visit(WhileStmt&) = 0; + virtual void Visit(ForStmt&) = 0; + virtual void Visit(UnsafeBlock&) = 0; + + // Exprs + virtual void Visit(Binary&) = 0; + virtual void Visit(Unary&) = 0; + virtual void Visit(Assign&) = 0; + virtual void Visit(Call&) = 0; + virtual void Visit(FieldAccess&) = 0; + virtual void Visit(IndexAccess&) = 0; + virtual void Visit(NamespaceRef&) = 0; + virtual void Visit(SafeCall&) = 0; + virtual void Visit(Elvis&) = 0; + virtual void Visit(CastAs&) = 0; + virtual void Visit(TypeTestIs&) = 0; + virtual void Visit(IdentRef&) = 0; + virtual void Visit(IntLit&) = 0; + virtual void Visit(FloatLit&) = 0; + virtual void Visit(StringLit&) = 0; + virtual void Visit(CharLit&) = 0; + virtual void Visit(BoolLit&) = 0; + virtual void Visit(NullLit&) = 0; +}; + +#endif // ASTVISITOR_HPP_ diff --git a/lib/parser/ast/IAstFactory.hpp b/lib/parser/ast/IAstFactory.hpp new file mode 100644 index 0000000..e122f8b --- /dev/null +++ b/lib/parser/ast/IAstFactory.hpp @@ -0,0 +1,93 @@ +#ifndef IASTFACTORY_HPP_ +#define IASTFACTORY_HPP_ +#include + +#include "nodes/class_members/CallDecl.hpp" +#include "nodes/class_members/DestructorDecl.hpp" +#include "nodes/class_members/FieldDecl.hpp" +#include "nodes/class_members/MethodDecl.hpp" +#include "nodes/class_members/StaticFieldDecl.hpp" +#include "nodes/decls/ClassDecl.hpp" +#include "nodes/decls/FunctionDecl.hpp" +#include "nodes/decls/GlobalVarDecl.hpp" +#include "nodes/decls/InterfaceDecl.hpp" +#include "nodes/decls/Module.hpp" +#include "nodes/decls/TypeAliasDecl.hpp" +#include "nodes/exprs/Assign.hpp" +#include "nodes/exprs/Binary.hpp" +#include "nodes/exprs/Call.hpp" +#include "nodes/exprs/CastAs.hpp" +#include "nodes/exprs/Elvis.hpp" +#include "nodes/exprs/FieldAccess.hpp" +#include "nodes/exprs/IdentRef.hpp" +#include "nodes/exprs/IndexAccess.hpp" +#include "nodes/exprs/NamespaceRef.hpp" +#include "nodes/exprs/SafeCall.hpp" +#include "nodes/exprs/TypeTestIs.hpp" +#include "nodes/exprs/Unary.hpp" +#include "nodes/exprs/literals/BoolLit.hpp" +#include "nodes/exprs/literals/CharLit.hpp" +#include "nodes/exprs/literals/FloatLit.hpp" +#include "nodes/exprs/literals/IntLit.hpp" +#include "nodes/exprs/literals/NullLit.hpp" +#include "nodes/exprs/literals/StringLit.hpp" +#include "nodes/stmts/BreakStmt.hpp" +#include "nodes/stmts/ContinueStmt.hpp" +#include "nodes/stmts/ExprStmt.hpp" +#include "nodes/stmts/ForStmt.hpp" +#include "nodes/stmts/IfStmt.hpp" +#include "nodes/stmts/ReturnStmt.hpp" +#include "nodes/stmts/UnsafeBlock.hpp" +#include "nodes/stmts/VarDeclStmt.hpp" +#include "nodes/stmts/WhileStmt.hpp" + +class IAstFactory { + public: + virtual ~IAstFactory() = default; + + virtual std::unique_ptr MakeModule() = 0; + + virtual std::unique_ptr MakeFunction() = 0; + virtual std::unique_ptr MakeClass() = 0; + virtual std::unique_ptr MakeInterface() = 0; + virtual std::unique_ptr MakeTypeAlias() = 0; + virtual std::unique_ptr MakeGlobalVar() = 0; + + virtual std::unique_ptr MakeField() = 0; + virtual std::unique_ptr MakeStaticField() = 0; + virtual std::unique_ptr MakeMethod() = 0; + virtual std::unique_ptr MakeCallDecl() = 0; + virtual std::unique_ptr MakeDestructor() = 0; + + virtual std::unique_ptr MakeBlock() = 0; + virtual std::unique_ptr MakeVarDeclStmt() = 0; + virtual std::unique_ptr MakeExprStmt() = 0; + virtual std::unique_ptr MakeReturnStmt() = 0; + virtual std::unique_ptr MakeBreakStmt() = 0; + virtual std::unique_ptr MakeContinueStmt() = 0; + virtual std::unique_ptr MakeIfStmt() = 0; + virtual std::unique_ptr MakeWhileStmt() = 0; + virtual std::unique_ptr MakeForStmt() = 0; + virtual std::unique_ptr MakeUnsafeBlock() = 0; + + virtual std::unique_ptr MakeBinary(const IBinaryOpTag& op) = 0; + virtual std::unique_ptr MakeUnary(const IUnaryOpTag& op) = 0; + virtual std::unique_ptr MakeAssign(const IAssignOpTag& op) = 0; + virtual std::unique_ptr MakeCall() = 0; + virtual std::unique_ptr MakeFieldAccess() = 0; + virtual std::unique_ptr MakeIndexAccess() = 0; + virtual std::unique_ptr MakeNamespaceRef() = 0; + virtual std::unique_ptr MakeSafeCall() = 0; + virtual std::unique_ptr MakeElvis() = 0; + virtual std::unique_ptr MakeCastAs() = 0; + virtual std::unique_ptr MakeTypeTestIs() = 0; + virtual std::unique_ptr MakeIdent(std::string name) = 0; + virtual std::unique_ptr MakeInt(long long v) = 0; + virtual std::unique_ptr MakeFloat(long double v) = 0; + virtual std::unique_ptr MakeString(std::u32string v) = 0; + virtual std::unique_ptr MakeChar(char32_t v) = 0; + virtual std::unique_ptr MakeBool(bool v) = 0; + virtual std::unique_ptr MakeNull() = 0; +}; + +#endif // IASTFACTORY_HPP_ diff --git a/lib/parser/ast/nodes/base/AstNode.hpp b/lib/parser/ast/nodes/base/AstNode.hpp new file mode 100644 index 0000000..a80ba69 --- /dev/null +++ b/lib/parser/ast/nodes/base/AstNode.hpp @@ -0,0 +1,13 @@ +#ifndef ASTNODE_HPP_ +#define ASTNODE_HPP_ + +class AstVisitor; // forward + +class AstNode { +public: + virtual ~AstNode() = default; + + virtual void Accept(AstVisitor& v) = 0; +}; + +#endif // ASTNODE_HPP_ diff --git a/lib/parser/ast/nodes/base/Decl.hpp b/lib/parser/ast/nodes/base/Decl.hpp new file mode 100644 index 0000000..4e504cd --- /dev/null +++ b/lib/parser/ast/nodes/base/Decl.hpp @@ -0,0 +1,8 @@ +#ifndef DECL_HPP_ +#define DECL_HPP_ + +#include "AstNode.hpp" + +class Decl : public AstNode {}; + +#endif // DECL_HPP_ diff --git a/lib/parser/ast/nodes/base/Expr.hpp b/lib/parser/ast/nodes/base/Expr.hpp new file mode 100644 index 0000000..c448b75 --- /dev/null +++ b/lib/parser/ast/nodes/base/Expr.hpp @@ -0,0 +1,8 @@ +#ifndef EXPR_HPP_ +#define EXPR_HPP_ + +#include "AstNode.hpp" + +class Expr : public AstNode {}; + +#endif // EXPR_HPP_ diff --git a/lib/parser/ast/nodes/base/Stmt.hpp b/lib/parser/ast/nodes/base/Stmt.hpp new file mode 100644 index 0000000..1236050 --- /dev/null +++ b/lib/parser/ast/nodes/base/Stmt.hpp @@ -0,0 +1,8 @@ +#ifndef STMT_HPP_ +#define STMT_HPP_ + +#include "AstNode.hpp" + +class Stmt : public AstNode {}; + +#endif // STMT_HPP_ diff --git a/lib/parser/ast/nodes/class_members/CallDecl.cpp b/lib/parser/ast/nodes/class_members/CallDecl.cpp new file mode 100644 index 0000000..da4d1a7 --- /dev/null +++ b/lib/parser/ast/nodes/class_members/CallDecl.cpp @@ -0,0 +1 @@ +#include "CallDecl.hpp" diff --git a/lib/parser/ast/nodes/class_members/CallDecl.hpp b/lib/parser/ast/nodes/class_members/CallDecl.hpp new file mode 100644 index 0000000..765c51c --- /dev/null +++ b/lib/parser/ast/nodes/class_members/CallDecl.hpp @@ -0,0 +1,22 @@ +#ifndef CALLDECL_HPP_ +#define CALLDECL_HPP_ + +#include +#include + +#include "lib/parser/ast/nodes/base/Decl.hpp" +#include "lib/parser/types/TypeReference.hpp" + +class Param; +class CallDecl : public Decl { +public: + void Accept(AstVisitor& v) override; + +private: + bool is_public_ = true; + std::vector params_; + std::unique_ptr ret_type_; + std::unique_ptr body_; +}; + +#endif // CALLDECL_HPP_ diff --git a/lib/parser/ast/nodes/class_members/DestructorDecl.cpp b/lib/parser/ast/nodes/class_members/DestructorDecl.cpp new file mode 100644 index 0000000..1e0aaf9 --- /dev/null +++ b/lib/parser/ast/nodes/class_members/DestructorDecl.cpp @@ -0,0 +1 @@ +#include "DestructorDecl.hpp" diff --git a/lib/parser/ast/nodes/class_members/DestructorDecl.hpp b/lib/parser/ast/nodes/class_members/DestructorDecl.hpp new file mode 100644 index 0000000..f25020c --- /dev/null +++ b/lib/parser/ast/nodes/class_members/DestructorDecl.hpp @@ -0,0 +1,18 @@ +#ifndef DESTRUCTORDECL_HPP_ +#define DESTRUCTORDECL_HPP_ + +#include + +#include "lib/parser/ast/nodes/base/Decl.hpp" +#include "lib/parser/ast/nodes/stmts/Block.hpp" + +class DestructorDecl : public Decl { +public: + void Accept(AstVisitor& v) override; + +private: + bool is_public_ = true; + std::unique_ptr body_; +}; + +#endif // DESTRUCTORDECL_HPP_ diff --git a/lib/parser/ast/nodes/class_members/FieldDecl.cpp b/lib/parser/ast/nodes/class_members/FieldDecl.cpp new file mode 100644 index 0000000..a1dfd47 --- /dev/null +++ b/lib/parser/ast/nodes/class_members/FieldDecl.cpp @@ -0,0 +1 @@ +#include "FieldDecl.hpp" diff --git a/lib/parser/ast/nodes/class_members/FieldDecl.hpp b/lib/parser/ast/nodes/class_members/FieldDecl.hpp new file mode 100644 index 0000000..4c16781 --- /dev/null +++ b/lib/parser/ast/nodes/class_members/FieldDecl.hpp @@ -0,0 +1,22 @@ +#ifndef FIELDDECL_HPP_ +#define FIELDDECL_HPP_ + +#include +#include + +#include "lib/parser/ast/nodes/base/Decl.hpp" +#include "lib/parser/types/TypeReference.hpp" + +class FieldDecl : public Decl { +public: + void Accept(AstVisitor& v) override; + +private: + bool is_public_ = true; + bool is_var_ = false; + std::string name_; + TypeReference type_; + std::unique_ptr init_; // optional +}; + +#endif // FIELDDECL_HPP_ diff --git a/lib/parser/ast/nodes/class_members/MethodDecl.cpp b/lib/parser/ast/nodes/class_members/MethodDecl.cpp new file mode 100644 index 0000000..56211fb --- /dev/null +++ b/lib/parser/ast/nodes/class_members/MethodDecl.cpp @@ -0,0 +1 @@ +#include "MethodDecl.hpp" diff --git a/lib/parser/ast/nodes/class_members/MethodDecl.hpp b/lib/parser/ast/nodes/class_members/MethodDecl.hpp new file mode 100644 index 0000000..d6885d7 --- /dev/null +++ b/lib/parser/ast/nodes/class_members/MethodDecl.hpp @@ -0,0 +1,30 @@ +#ifndef METHODDECL_HPP_ +#define METHODDECL_HPP_ + +#include +#include +#include + +#include "lib/parser/ast/nodes/base/Decl.hpp" +#include "lib/parser/ast/nodes/stmts/Block.hpp" +#include "lib/parser/types/Param.hpp" +#include "lib/parser/types/TypeReference.hpp" + +class MethodDecl : public Decl { +public: + + void Accept(AstVisitor& v) override; + +private: + bool is_public_ = true; + bool is_override_ = false; + bool is_static_ = false; + bool is_pure_ = false; + + std::string name; + std::vector params; + std::unique_ptr ret_type; + std::unique_ptr body; // optional +}; + +#endif // METHODDECL_HPP_ diff --git a/lib/parser/ast/nodes/class_members/StaticFieldDecl.cpp b/lib/parser/ast/nodes/class_members/StaticFieldDecl.cpp new file mode 100644 index 0000000..bc96e1b --- /dev/null +++ b/lib/parser/ast/nodes/class_members/StaticFieldDecl.cpp @@ -0,0 +1 @@ +#include "StaticFieldDecl.hpp" diff --git a/lib/parser/ast/nodes/class_members/StaticFieldDecl.hpp b/lib/parser/ast/nodes/class_members/StaticFieldDecl.hpp new file mode 100644 index 0000000..2a38f72 --- /dev/null +++ b/lib/parser/ast/nodes/class_members/StaticFieldDecl.hpp @@ -0,0 +1,23 @@ +#ifndef STATICFIELDDECL_HPP_ +#define STATICFIELDDECL_HPP_ + +#include +#include + +#include "lib/parser/ast/nodes/base/Decl.hpp" +#include "lib/parser/ast/nodes/base/Expr.hpp" +#include "lib/parser/types/TypeReference.hpp" + +class StaticFieldDecl : public Decl { +public: + void Accept(AstVisitor& v) override; + +private: + bool is_public_ = true; + bool is_var_ = false; + std::string name_; + TypeReference type_; + std::unique_ptr init_; +}; + +#endif // STATICFIELDDECL_HPP_ diff --git a/lib/parser/ast/nodes/decls/ClassDecl.cpp b/lib/parser/ast/nodes/decls/ClassDecl.cpp new file mode 100644 index 0000000..b635f04 --- /dev/null +++ b/lib/parser/ast/nodes/decls/ClassDecl.cpp @@ -0,0 +1 @@ +#include "ClassDecl.hpp" diff --git a/lib/parser/ast/nodes/decls/ClassDecl.hpp b/lib/parser/ast/nodes/decls/ClassDecl.hpp new file mode 100644 index 0000000..1f7575d --- /dev/null +++ b/lib/parser/ast/nodes/decls/ClassDecl.hpp @@ -0,0 +1,21 @@ +#ifndef CLASSDECL_HPP_ +#define CLASSDECL_HPP_ + +#include +#include +#include + +#include "lib/parser/ast/nodes/base/Decl.hpp" +#include "lib/parser/types/TypeReference.hpp" + +class ClassDecl : public Decl { +public: + void Accept(AstVisitor& v) override; + +private: + std::string name_; + std::vector implements_; + std::vector> members_; // Field/StaticField/Method/Call/Destructor +}; + +#endif // CLASSDECL_HPP_ diff --git a/lib/parser/ast/nodes/decls/FunctionDecl.cpp b/lib/parser/ast/nodes/decls/FunctionDecl.cpp new file mode 100644 index 0000000..0b382d7 --- /dev/null +++ b/lib/parser/ast/nodes/decls/FunctionDecl.cpp @@ -0,0 +1 @@ +#include "FunctionDecl.hpp" diff --git a/lib/parser/ast/nodes/decls/FunctionDecl.hpp b/lib/parser/ast/nodes/decls/FunctionDecl.hpp new file mode 100644 index 0000000..4065d06 --- /dev/null +++ b/lib/parser/ast/nodes/decls/FunctionDecl.hpp @@ -0,0 +1,24 @@ +#ifndef FUNCTIONDECL_HPP_ +#define FUNCTIONDECL_HPP_ + +#include +#include +#include + +#include "lib/parser/ast/nodes/base/Decl.hpp" +#include "lib/parser/ast/nodes/stmts/Block.hpp" +#include "lib/parser/types/Param.hpp" + +class FunctionDecl : public Decl { +public: + void Accept(AstVisitor& v) override; + +private: + bool is_pure_ = false; + std::string name_; + std::vector params_; + std::unique_ptr return_type_; // optional + std::unique_ptr body_; // optional +}; + +#endif // FUNCTIONDECL_HPP_ diff --git a/lib/parser/ast/nodes/decls/GlobalVarDecl.cpp b/lib/parser/ast/nodes/decls/GlobalVarDecl.cpp new file mode 100644 index 0000000..ea1652d --- /dev/null +++ b/lib/parser/ast/nodes/decls/GlobalVarDecl.cpp @@ -0,0 +1 @@ +#include "GlobalVarDecl.hpp" diff --git a/lib/parser/ast/nodes/decls/GlobalVarDecl.hpp b/lib/parser/ast/nodes/decls/GlobalVarDecl.hpp new file mode 100644 index 0000000..6548077 --- /dev/null +++ b/lib/parser/ast/nodes/decls/GlobalVarDecl.hpp @@ -0,0 +1,22 @@ +#ifndef GLOBALVARDECL_HPP_ +#define GLOBALVARDECL_HPP_ + +#include +#include + +#include "lib/parser/ast/nodes/base/Decl.hpp" +#include "lib/parser/ast/nodes/base/Expr.hpp" +#include "lib/parser/types/TypeReference.hpp" + +class GlobalVarDecl : public Decl { +public: + void Accept(AstVisitor& v) override; + +private: + bool is_var_ = false; // var=true, val=false + std::string name_; + TypeReference type_; + std::unique_ptr init_; // optional +}; + +#endif // GLOBALVARDECL_HPP_ diff --git a/lib/parser/ast/nodes/decls/InterfaceDecl.cpp b/lib/parser/ast/nodes/decls/InterfaceDecl.cpp new file mode 100644 index 0000000..a6688f8 --- /dev/null +++ b/lib/parser/ast/nodes/decls/InterfaceDecl.cpp @@ -0,0 +1 @@ +#include "InterfaceDecl.hpp" diff --git a/lib/parser/ast/nodes/decls/InterfaceDecl.hpp b/lib/parser/ast/nodes/decls/InterfaceDecl.hpp new file mode 100644 index 0000000..bb5289e --- /dev/null +++ b/lib/parser/ast/nodes/decls/InterfaceDecl.hpp @@ -0,0 +1,19 @@ +#ifndef INTERFACEDECL_HPP_ +#define INTERFACEDECL_HPP_ + +#include +#include +#include + +#include "lib/parser/ast/nodes/base/Decl.hpp" + +class InterfaceDecl : public Decl { +public: + void Accept(AstVisitor& v) override; + +private: + std::string name_; + std::vector> members_; +}; + +#endif // INTERFACEDECL_HPP_ diff --git a/lib/parser/ast/nodes/decls/Module.cpp b/lib/parser/ast/nodes/decls/Module.cpp new file mode 100644 index 0000000..6f69fe8 --- /dev/null +++ b/lib/parser/ast/nodes/decls/Module.cpp @@ -0,0 +1 @@ +#include "Module.hpp" diff --git a/lib/parser/ast/nodes/decls/Module.hpp b/lib/parser/ast/nodes/decls/Module.hpp new file mode 100644 index 0000000..ef03d1e --- /dev/null +++ b/lib/parser/ast/nodes/decls/Module.hpp @@ -0,0 +1,21 @@ +#ifndef MODULE_HPP_ +#define MODULE_HPP_ + +#include +#include + +#include "lib/parser/ast/nodes/base/AstNode.hpp" +#include "lib/parser/ast/nodes/base/Decl.hpp" + +class Module : public AstNode { +public: + void Accept(AstVisitor& v) override; + + [[nodiscard]] std::vector>& GetDecls(); + [[nodiscard]] const std::vector>& GetDecls() const; + +private: + std::vector> decls_; +}; + +#endif // MODULE_HPP_ diff --git a/lib/parser/ast/nodes/decls/TypeAliasDecl.cpp b/lib/parser/ast/nodes/decls/TypeAliasDecl.cpp new file mode 100644 index 0000000..5cc1e41 --- /dev/null +++ b/lib/parser/ast/nodes/decls/TypeAliasDecl.cpp @@ -0,0 +1 @@ +#include "TypeAliasDecl.hpp" diff --git a/lib/parser/ast/nodes/decls/TypeAliasDecl.hpp b/lib/parser/ast/nodes/decls/TypeAliasDecl.hpp new file mode 100644 index 0000000..fabfa5d --- /dev/null +++ b/lib/parser/ast/nodes/decls/TypeAliasDecl.hpp @@ -0,0 +1,18 @@ +#ifndef TYPEALIASDECL_HPP_ +#define TYPEALIASDECL_HPP_ + +#include + +#include "lib/parser/ast/nodes/base/Decl.hpp" +#include "lib/parser/types/TypeReference.hpp" + +class TypeAliasDecl : public Decl { +public: + void Accept(AstVisitor& v) override; + +private: + std::string name_; + TypeReference aliased_type_; +}; + +#endif // TYPEALIASDECL_HPP_ diff --git a/lib/parser/ast/nodes/exprs/Assign.cpp b/lib/parser/ast/nodes/exprs/Assign.cpp new file mode 100644 index 0000000..8775393 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/Assign.cpp @@ -0,0 +1 @@ +#include "Assign.hpp" diff --git a/lib/parser/ast/nodes/exprs/Assign.hpp b/lib/parser/ast/nodes/exprs/Assign.hpp new file mode 100644 index 0000000..4a08d69 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/Assign.hpp @@ -0,0 +1,20 @@ +#ifndef ASSIGN_HPP_ +#define ASSIGN_HPP_ + +#include + +#include "lib/parser/ast/nodes/base/Expr.hpp" +#include "tags/IAssignOpTag.hpp" +#include "tags/OpTags.hpp" + +class Assign : public Expr { +public: + void Accept(AstVisitor& v) override; + +private: + const IAssignOpTag* kind_ = &OpTags::RefAssign(); + std::unique_ptr target_; + std::unique_ptr value_; +}; + +#endif // ASSIGN_HPP_ diff --git a/lib/parser/ast/nodes/exprs/Binary.cpp b/lib/parser/ast/nodes/exprs/Binary.cpp new file mode 100644 index 0000000..313429d --- /dev/null +++ b/lib/parser/ast/nodes/exprs/Binary.cpp @@ -0,0 +1 @@ +#include "Binary.hpp" diff --git a/lib/parser/ast/nodes/exprs/Binary.hpp b/lib/parser/ast/nodes/exprs/Binary.hpp new file mode 100644 index 0000000..3869f77 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/Binary.hpp @@ -0,0 +1,19 @@ +#ifndef BINARY_HPP_ +#define BINARY_HPP_ + +#include + +#include "lib/parser/ast/nodes/base/Expr.hpp" +#include "tags/OpTags.hpp" + +class Binary : public Expr { +public: + void Accept(AstVisitor& v) override; + +private: + const IBinaryOpTag* op_ = &OpTags::Add(); + std::unique_ptr lhs_; + std::unique_ptr rhs_; +}; + +#endif // BINARY_HPP_ diff --git a/lib/parser/ast/nodes/exprs/Call.cpp b/lib/parser/ast/nodes/exprs/Call.cpp new file mode 100644 index 0000000..e2c6e69 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/Call.cpp @@ -0,0 +1 @@ +#include "Call.hpp" diff --git a/lib/parser/ast/nodes/exprs/Call.hpp b/lib/parser/ast/nodes/exprs/Call.hpp new file mode 100644 index 0000000..1e442f1 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/Call.hpp @@ -0,0 +1,18 @@ +#ifndef CALL_HPP_ +#define CALL_HPP_ + +#include +#include + +#include "lib/parser/ast/nodes/base/Expr.hpp" + +class Call : public Expr { +public: + void Accept(AstVisitor& v) override; + +private: + std::unique_ptr callee_; + std::vector> args_; +}; + +#endif // CALL_HPP_ diff --git a/lib/parser/ast/nodes/exprs/CastAs.cpp b/lib/parser/ast/nodes/exprs/CastAs.cpp new file mode 100644 index 0000000..fd6ffb2 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/CastAs.cpp @@ -0,0 +1 @@ +#include "CastAs.hpp" diff --git a/lib/parser/ast/nodes/exprs/CastAs.hpp b/lib/parser/ast/nodes/exprs/CastAs.hpp new file mode 100644 index 0000000..3d891c6 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/CastAs.hpp @@ -0,0 +1,18 @@ +#ifndef CASTAS_HPP_ +#define CASTAS_HPP_ + +#include + +#include "lib/parser/ast/nodes/base/Expr.hpp" +#include "lib/parser/types/TypeReference.hpp" + +class CastAs : public Expr { +public: + void Accept(AstVisitor& v) override; + +private: + std::unique_ptr expr_; + TypeReference type_; +}; + +#endif // CASTAS_HPP_ diff --git a/lib/parser/ast/nodes/exprs/Elvis.cpp b/lib/parser/ast/nodes/exprs/Elvis.cpp new file mode 100644 index 0000000..87484ef --- /dev/null +++ b/lib/parser/ast/nodes/exprs/Elvis.cpp @@ -0,0 +1 @@ +#include "Elvis.hpp" diff --git a/lib/parser/ast/nodes/exprs/Elvis.hpp b/lib/parser/ast/nodes/exprs/Elvis.hpp new file mode 100644 index 0000000..563a689 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/Elvis.hpp @@ -0,0 +1,17 @@ +#ifndef ELVIS_HPP_ +#define ELVIS_HPP_ + +#include + +#include "lib/parser/ast/nodes/base/Expr.hpp" + +class Elvis : public Expr { +public: + void Accept(AstVisitor& v) override; + +private: + std::unique_ptr lhs_; + std::unique_ptr rhs_; +}; + +#endif // ELVIS_HPP_ diff --git a/lib/parser/ast/nodes/exprs/FieldAccess.cpp b/lib/parser/ast/nodes/exprs/FieldAccess.cpp new file mode 100644 index 0000000..6cc69c2 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/FieldAccess.cpp @@ -0,0 +1 @@ +#include "FieldAccess.hpp" diff --git a/lib/parser/ast/nodes/exprs/FieldAccess.hpp b/lib/parser/ast/nodes/exprs/FieldAccess.hpp new file mode 100644 index 0000000..a962576 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/FieldAccess.hpp @@ -0,0 +1,18 @@ +#ifndef FIELDACCESS_HPP_ +#define FIELDACCESS_HPP_ + +#include +#include + +#include "lib/parser/ast/nodes/base/Expr.hpp" + +class FieldAccess : public Expr { +public: + void Accept(AstVisitor& v) override; + +private: + std::unique_ptr object_; + std::string name_; +}; + +#endif // FIELDACCESS_HPP_ diff --git a/lib/parser/ast/nodes/exprs/IdentRef.cpp b/lib/parser/ast/nodes/exprs/IdentRef.cpp new file mode 100644 index 0000000..718f162 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/IdentRef.cpp @@ -0,0 +1 @@ +#include "IdentRef.hpp" diff --git a/lib/parser/ast/nodes/exprs/IdentRef.hpp b/lib/parser/ast/nodes/exprs/IdentRef.hpp new file mode 100644 index 0000000..cc6ca4d --- /dev/null +++ b/lib/parser/ast/nodes/exprs/IdentRef.hpp @@ -0,0 +1,16 @@ +#ifndef IDENTREF_HPP_ +#define IDENTREF_HPP_ + +#include + +#include "lib/parser/ast/nodes/base/Expr.hpp" + +class IdentRef : public Expr { +public: + void Accept(AstVisitor& v) override; + +private: + std::string name_; +}; + +#endif // IDENTREF_HPP_ diff --git a/lib/parser/ast/nodes/exprs/IndexAccess.cpp b/lib/parser/ast/nodes/exprs/IndexAccess.cpp new file mode 100644 index 0000000..23b647c --- /dev/null +++ b/lib/parser/ast/nodes/exprs/IndexAccess.cpp @@ -0,0 +1 @@ +#include "IndexAccess.hpp" diff --git a/lib/parser/ast/nodes/exprs/IndexAccess.hpp b/lib/parser/ast/nodes/exprs/IndexAccess.hpp new file mode 100644 index 0000000..1c56553 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/IndexAccess.hpp @@ -0,0 +1,17 @@ +#ifndef INDEXACCESS_HPP_ +#define INDEXACCESS_HPP_ + +#include + +#include "lib/parser/ast/nodes/base/Expr.hpp" + +class IndexAccess : public Expr { +public: + void Accept(AstVisitor& v) override; + +private: + std::unique_ptr object_; + std::unique_ptr index_; +}; + +#endif // INDEXACCESS_HPP_ diff --git a/lib/parser/ast/nodes/exprs/NamespaceRef.cpp b/lib/parser/ast/nodes/exprs/NamespaceRef.cpp new file mode 100644 index 0000000..2460147 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/NamespaceRef.cpp @@ -0,0 +1 @@ +#include "NamespaceRef.hpp" diff --git a/lib/parser/ast/nodes/exprs/NamespaceRef.hpp b/lib/parser/ast/nodes/exprs/NamespaceRef.hpp new file mode 100644 index 0000000..e9c090d --- /dev/null +++ b/lib/parser/ast/nodes/exprs/NamespaceRef.hpp @@ -0,0 +1,18 @@ +#ifndef NAMESPACEREF_HPP_ +#define NAMESPACEREF_HPP_ + +#include +#include + +#include "lib/parser/ast/nodes/base/Expr.hpp" + +class NamespaceRef : public Expr { +public: + void Accept(AstVisitor& v) override; + +private: + std::unique_ptr namespace_; + std::string name_; +}; + +#endif // NAMESPACEREF_HPP_ diff --git a/lib/parser/ast/nodes/exprs/SafeCall.cpp b/lib/parser/ast/nodes/exprs/SafeCall.cpp new file mode 100644 index 0000000..f486092 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/SafeCall.cpp @@ -0,0 +1 @@ +#include "SafeCall.hpp" diff --git a/lib/parser/ast/nodes/exprs/SafeCall.hpp b/lib/parser/ast/nodes/exprs/SafeCall.hpp new file mode 100644 index 0000000..cbd5981 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/SafeCall.hpp @@ -0,0 +1,16 @@ +#ifndef SAFECALL_HPP_ +#define SAFECALL_HPP_ + +#include + +#include "lib/parser/ast/nodes/base/Expr.hpp" + +class SafeCall : public Expr { +public: + void Accept(AstVisitor& v) override; + +private: + std::unique_ptr base; // TODO: idk how to implement it now... +}; + +#endif // SAFECALL_HPP_ diff --git a/lib/parser/ast/nodes/exprs/TypeTestIs.cpp b/lib/parser/ast/nodes/exprs/TypeTestIs.cpp new file mode 100644 index 0000000..c2c5635 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/TypeTestIs.cpp @@ -0,0 +1 @@ +#include "TypeTestIs.hpp" diff --git a/lib/parser/ast/nodes/exprs/TypeTestIs.hpp b/lib/parser/ast/nodes/exprs/TypeTestIs.hpp new file mode 100644 index 0000000..32313e3 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/TypeTestIs.hpp @@ -0,0 +1,18 @@ +#ifndef TYPETESTIS_HPP_ +#define TYPETESTIS_HPP_ + +#include + +#include "lib/parser/ast/nodes/base/Expr.hpp" +#include "lib/parser/types/TypeReference.hpp" + +class TypeTestIs : public Expr { +public: + void Accept(AstVisitor& v) override; + +private: + std::unique_ptr expr; + TypeReference type; +}; + +#endif // TYPETESTIS_HPP_ diff --git a/lib/parser/ast/nodes/exprs/Unary.cpp b/lib/parser/ast/nodes/exprs/Unary.cpp new file mode 100644 index 0000000..b3ee440 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/Unary.cpp @@ -0,0 +1 @@ +#include "Unary.hpp" diff --git a/lib/parser/ast/nodes/exprs/Unary.hpp b/lib/parser/ast/nodes/exprs/Unary.hpp new file mode 100644 index 0000000..164cb6e --- /dev/null +++ b/lib/parser/ast/nodes/exprs/Unary.hpp @@ -0,0 +1,19 @@ +#ifndef UNARY_HPP_ +#define UNARY_HPP_ + +#include + +#include "lib/parser/ast/nodes/base/Expr.hpp" +#include "tags/IUnaryOpTag.hpp" +#include "tags/OpTags.hpp" + +class Unary : public Expr { +public: + void Accept(AstVisitor& v) override; + +private: + const IUnaryOpTag* op_ = &OpTags::Neg(); + std::unique_ptr operand_; +}; + +#endif // UNARY_HPP_ diff --git a/lib/parser/ast/nodes/exprs/literals/BoolLit.cpp b/lib/parser/ast/nodes/exprs/literals/BoolLit.cpp new file mode 100644 index 0000000..ae07945 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/literals/BoolLit.cpp @@ -0,0 +1 @@ +#include "BoolLit.hpp" diff --git a/lib/parser/ast/nodes/exprs/literals/BoolLit.hpp b/lib/parser/ast/nodes/exprs/literals/BoolLit.hpp new file mode 100644 index 0000000..baffb12 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/literals/BoolLit.hpp @@ -0,0 +1,14 @@ +#ifndef BOOLLIT_HPP_ +#define BOOLLIT_HPP_ + +#include "lib/parser/ast/nodes/base/Expr.hpp" + +class BoolLit : public Expr { +public: + void Accept(AstVisitor& v) override; + +private: + bool value = false; +}; + +#endif // BOOLLIT_HPP_ diff --git a/lib/parser/ast/nodes/exprs/literals/CharLit.cpp b/lib/parser/ast/nodes/exprs/literals/CharLit.cpp new file mode 100644 index 0000000..621fe20 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/literals/CharLit.cpp @@ -0,0 +1 @@ +#include "CharLit.hpp" diff --git a/lib/parser/ast/nodes/exprs/literals/CharLit.hpp b/lib/parser/ast/nodes/exprs/literals/CharLit.hpp new file mode 100644 index 0000000..2bf310a --- /dev/null +++ b/lib/parser/ast/nodes/exprs/literals/CharLit.hpp @@ -0,0 +1,14 @@ +#ifndef CHARLIT_HPP_ +#define CHARLIT_HPP_ + +#include "lib/parser/ast/nodes/base/Expr.hpp" + +class CharLit : public Expr { +public: + void Accept(AstVisitor& v) override; + +private: + char32_t value_ = U'\0'; +}; + +#endif // CHARLIT_HPP_ diff --git a/lib/parser/ast/nodes/exprs/literals/FloatLit.cpp b/lib/parser/ast/nodes/exprs/literals/FloatLit.cpp new file mode 100644 index 0000000..2bebaaa --- /dev/null +++ b/lib/parser/ast/nodes/exprs/literals/FloatLit.cpp @@ -0,0 +1 @@ +#include "../FloatLit.hpp" diff --git a/lib/parser/ast/nodes/exprs/literals/FloatLit.hpp b/lib/parser/ast/nodes/exprs/literals/FloatLit.hpp new file mode 100644 index 0000000..37dde1b --- /dev/null +++ b/lib/parser/ast/nodes/exprs/literals/FloatLit.hpp @@ -0,0 +1,13 @@ +#ifndef FLOATLIT_HPP_ +#define FLOATLIT_HPP_ +#include "lib/parser/ast/nodes/base/Expr.hpp" + +class FloatLit : public Expr { +public: + void Accept(AstVisitor& v) override; + +private: + long double value = 0.0L; +}; + +#endif // FLOATLIT_HPP_ diff --git a/lib/parser/ast/nodes/exprs/literals/IntLit.cpp b/lib/parser/ast/nodes/exprs/literals/IntLit.cpp new file mode 100644 index 0000000..9a4f39e --- /dev/null +++ b/lib/parser/ast/nodes/exprs/literals/IntLit.cpp @@ -0,0 +1 @@ +#include "../IntLit.hpp" diff --git a/lib/parser/ast/nodes/exprs/literals/IntLit.hpp b/lib/parser/ast/nodes/exprs/literals/IntLit.hpp new file mode 100644 index 0000000..f41454c --- /dev/null +++ b/lib/parser/ast/nodes/exprs/literals/IntLit.hpp @@ -0,0 +1,12 @@ +#ifndef INTLIT_HPP_ +#define INTLIT_HPP_ +#include "lib/parser/ast/nodes/base/Expr.hpp" + +class IntLit : public Expr { +public: + void Accept(AstVisitor& v) override; + +private: + long long value = 0; +}; +#endif // INTLIT_HPP_ diff --git a/lib/parser/ast/nodes/exprs/literals/NullLit.cpp b/lib/parser/ast/nodes/exprs/literals/NullLit.cpp new file mode 100644 index 0000000..dc7ba0d --- /dev/null +++ b/lib/parser/ast/nodes/exprs/literals/NullLit.cpp @@ -0,0 +1 @@ +#include "NullLit.hpp" diff --git a/lib/parser/ast/nodes/exprs/literals/NullLit.hpp b/lib/parser/ast/nodes/exprs/literals/NullLit.hpp new file mode 100644 index 0000000..80275bf --- /dev/null +++ b/lib/parser/ast/nodes/exprs/literals/NullLit.hpp @@ -0,0 +1,11 @@ +#ifndef NULLLIT_HPP_ +#define NULLLIT_HPP_ + +#include "lib/parser/ast/nodes/base/Expr.hpp" + +class NullLit : public Expr { +public: + void Accept(AstVisitor& v) override; +}; + +#endif // NULLLIT_HPP_ diff --git a/lib/parser/ast/nodes/exprs/literals/StringLit.cpp b/lib/parser/ast/nodes/exprs/literals/StringLit.cpp new file mode 100644 index 0000000..212dbf1 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/literals/StringLit.cpp @@ -0,0 +1 @@ +#include "../StringLit.hpp" diff --git a/lib/parser/ast/nodes/exprs/literals/StringLit.hpp b/lib/parser/ast/nodes/exprs/literals/StringLit.hpp new file mode 100644 index 0000000..139ea0b --- /dev/null +++ b/lib/parser/ast/nodes/exprs/literals/StringLit.hpp @@ -0,0 +1,16 @@ +#ifndef STRINGLIT_HPP_ +#define STRINGLIT_HPP_ + +#include + +#include "lib/parser/ast/nodes/base/Expr.hpp" + +class StringLit : public Expr { +public: + void Accept(AstVisitor& v) override; + +private: + std::u32string value_; +}; + +#endif // STRINGLIT_HPP_ diff --git a/lib/parser/ast/nodes/exprs/tags/IAssignOpTag.hpp b/lib/parser/ast/nodes/exprs/tags/IAssignOpTag.hpp new file mode 100644 index 0000000..d003fb0 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/tags/IAssignOpTag.hpp @@ -0,0 +1,11 @@ +#ifndef IASSIGNOPTAG_HPP_ +#define IASSIGNOPTAG_HPP_ + +class IAssignOpTag { +public: + virtual ~IAssignOpTag() = default; + + virtual const char* Name() const = 0; +}; + +#endif // IASSIGNOPTAG_HPP_ diff --git a/lib/parser/ast/nodes/exprs/tags/IBinaryOpTag.hpp b/lib/parser/ast/nodes/exprs/tags/IBinaryOpTag.hpp new file mode 100644 index 0000000..9d456c3 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/tags/IBinaryOpTag.hpp @@ -0,0 +1,11 @@ +#ifndef IBINARYOPTAG_HPP_ +#define IBINARYOPTAG_HPP_ + +class IBinaryOpTag { +public: + virtual ~IBinaryOpTag() = default; + + virtual const char* Name() const = 0; +}; + +#endif // IBINARYOPTAG_HPP_ diff --git a/lib/parser/ast/nodes/exprs/tags/IUnaryOpTag.hpp b/lib/parser/ast/nodes/exprs/tags/IUnaryOpTag.hpp new file mode 100644 index 0000000..c4bd51f --- /dev/null +++ b/lib/parser/ast/nodes/exprs/tags/IUnaryOpTag.hpp @@ -0,0 +1,11 @@ +#ifndef IUNARYOPTAG_HPP_ +#define IUNARYOPTAG_HPP_ + +class IUnaryOpTag { +public: + virtual ~IUnaryOpTag() = default; + + virtual const char* Name() const = 0; +}; + +#endif // IUNARYOPTAG_HPP_ diff --git a/lib/parser/ast/nodes/exprs/tags/OpTags.hpp b/lib/parser/ast/nodes/exprs/tags/OpTags.hpp new file mode 100644 index 0000000..75559f3 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/tags/OpTags.hpp @@ -0,0 +1,33 @@ +#ifndef OPTAGS_HPP_ +#define OPTAGS_HPP_ +#include "IAssignOpTag.hpp" +#include "IBinaryOpTag.hpp" +#include "IUnaryOpTag.hpp" + +namespace OpTags { +static const IBinaryOpTag& Add(); +static const IBinaryOpTag& Sub(); +static const IBinaryOpTag& Mul(); +static const IBinaryOpTag& Div(); +static const IBinaryOpTag& Mod(); +static const IBinaryOpTag& Lt(); +static const IBinaryOpTag& Le(); +static const IBinaryOpTag& Gt(); +static const IBinaryOpTag& Ge(); +static const IBinaryOpTag& Eq(); +static const IBinaryOpTag& Ne(); +static const IBinaryOpTag& And(); +static const IBinaryOpTag& Or(); +static const IBinaryOpTag& Xor(); + +// unary +static const IUnaryOpTag& Neg(); +static const IUnaryOpTag& Plus(); +static const IUnaryOpTag& Not(); + +// assign +static const IAssignOpTag& RefAssign(); // = +static const IAssignOpTag& CopyAssign(); // := +} // namespace OpTags + +#endif // OPTAGS_HPP_ diff --git a/lib/parser/ast/nodes/stmts/Block.cpp b/lib/parser/ast/nodes/stmts/Block.cpp new file mode 100644 index 0000000..71b09e5 --- /dev/null +++ b/lib/parser/ast/nodes/stmts/Block.cpp @@ -0,0 +1 @@ +#include "Block.hpp" diff --git a/lib/parser/ast/nodes/stmts/Block.hpp b/lib/parser/ast/nodes/stmts/Block.hpp new file mode 100644 index 0000000..ef1712d --- /dev/null +++ b/lib/parser/ast/nodes/stmts/Block.hpp @@ -0,0 +1,22 @@ +#ifndef BLOCK_HPP_ +#define BLOCK_HPP_ + +#include +#include + +#include "lib/parser/ast/nodes/base/AstNode.hpp" +#include "lib/parser/ast/nodes/base/Stmt.hpp" + +class Block : public Stmt { +public: + void Accept(AstVisitor& v) override; + + std::vector>& GetStatements(); + const std::vector>& GetStatements() const; + void Append(std::unique_ptr s); + +private: + std::vector> stmts_; +}; + +#endif // BLOCK_HPP_ diff --git a/lib/parser/ast/nodes/stmts/Branch.cpp b/lib/parser/ast/nodes/stmts/Branch.cpp new file mode 100644 index 0000000..60d4ee0 --- /dev/null +++ b/lib/parser/ast/nodes/stmts/Branch.cpp @@ -0,0 +1 @@ +#include "Branch.hpp" diff --git a/lib/parser/ast/nodes/stmts/Branch.hpp b/lib/parser/ast/nodes/stmts/Branch.hpp new file mode 100644 index 0000000..3008abd --- /dev/null +++ b/lib/parser/ast/nodes/stmts/Branch.hpp @@ -0,0 +1,26 @@ +#ifndef BRANCH_HPP_ +#define BRANCH_HPP_ + +#include + +#include "Block.hpp" +#include "lib/parser/ast/nodes/base/Expr.hpp" + +class Branch { +public: + Branch(std::unique_ptr expr, std::unique_ptr block); + + [[nodiscard]] std::unique_ptr GetBlock(); + + void AddBlock(std::unique_ptr block); + + [[nodiscard]] std::unique_ptr GetExpr(); + + void AddExpr(std::unique_ptr expr); + +private: + std::unique_ptr condition_; + std::unique_ptr then_block_; +}; + +#endif // BRANCH_HPP_ diff --git a/lib/parser/ast/nodes/stmts/BreakStmt.cpp b/lib/parser/ast/nodes/stmts/BreakStmt.cpp new file mode 100644 index 0000000..8e979c6 --- /dev/null +++ b/lib/parser/ast/nodes/stmts/BreakStmt.cpp @@ -0,0 +1 @@ +#include "BreakStmt.hpp" diff --git a/lib/parser/ast/nodes/stmts/BreakStmt.hpp b/lib/parser/ast/nodes/stmts/BreakStmt.hpp new file mode 100644 index 0000000..1abc187 --- /dev/null +++ b/lib/parser/ast/nodes/stmts/BreakStmt.hpp @@ -0,0 +1,11 @@ +#ifndef BREAKSTMT_HPP_ +#define BREAKSTMT_HPP_ + +#include "lib/parser/ast/nodes/base/Stmt.hpp" + +class BreakStmt : public Stmt { +public: + void Accept(AstVisitor& v) override; +}; + +#endif // BREAKSTMT_HPP_ diff --git a/lib/parser/ast/nodes/stmts/ContinueStmt.cpp b/lib/parser/ast/nodes/stmts/ContinueStmt.cpp new file mode 100644 index 0000000..5de3431 --- /dev/null +++ b/lib/parser/ast/nodes/stmts/ContinueStmt.cpp @@ -0,0 +1 @@ +#include "ContinueStmt.hpp" diff --git a/lib/parser/ast/nodes/stmts/ContinueStmt.hpp b/lib/parser/ast/nodes/stmts/ContinueStmt.hpp new file mode 100644 index 0000000..2554d86 --- /dev/null +++ b/lib/parser/ast/nodes/stmts/ContinueStmt.hpp @@ -0,0 +1,11 @@ +#ifndef CONTINUESTMT_HPP_ +#define CONTINUESTMT_HPP_ + +#include "lib/parser/ast/nodes/base/Stmt.hpp" + +class ContinueStmt : public Stmt { +public: + void Accept(AstVisitor& v) override; +}; + +#endif // CONTINUESTMT_HPP_ diff --git a/lib/parser/ast/nodes/stmts/ExprStmt.cpp b/lib/parser/ast/nodes/stmts/ExprStmt.cpp new file mode 100644 index 0000000..b80efca --- /dev/null +++ b/lib/parser/ast/nodes/stmts/ExprStmt.cpp @@ -0,0 +1 @@ +#include "ExprStmt.hpp" diff --git a/lib/parser/ast/nodes/stmts/ExprStmt.hpp b/lib/parser/ast/nodes/stmts/ExprStmt.hpp new file mode 100644 index 0000000..4932301 --- /dev/null +++ b/lib/parser/ast/nodes/stmts/ExprStmt.hpp @@ -0,0 +1,17 @@ +#ifndef EXPRSTMT_HPP_ +#define EXPRSTMT_HPP_ + +#include + +#include "lib/parser/ast/nodes/base/Expr.hpp" +#include "lib/parser/ast/nodes/base/Stmt.hpp" + +class ExprStmt : public Stmt { +public: + void Accept(AstVisitor& v) override; + +private: + std::unique_ptr expr_; +}; + +#endif // EXPRSTMT_HPP_ diff --git a/lib/parser/ast/nodes/stmts/ForStmt.cpp b/lib/parser/ast/nodes/stmts/ForStmt.cpp new file mode 100644 index 0000000..fbce6f8 --- /dev/null +++ b/lib/parser/ast/nodes/stmts/ForStmt.cpp @@ -0,0 +1 @@ +#include "ForStmt.hpp" diff --git a/lib/parser/ast/nodes/stmts/ForStmt.hpp b/lib/parser/ast/nodes/stmts/ForStmt.hpp new file mode 100644 index 0000000..396930a --- /dev/null +++ b/lib/parser/ast/nodes/stmts/ForStmt.hpp @@ -0,0 +1,20 @@ +#ifndef FORSTMT_HPP_ +#define FORSTMT_HPP_ + +#include +#include + +#include "Block.hpp" +#include "lib/parser/ast/nodes/base/Expr.hpp" + +class ForStmt : public Stmt { +public: + void Accept(AstVisitor& v) override; + +private: + std::string iter_name_; + std::unique_ptr iter_expr_; + std::unique_ptr body_; +}; + +#endif // FORSTMT_HPP_ diff --git a/lib/parser/ast/nodes/stmts/IfStmt.cpp b/lib/parser/ast/nodes/stmts/IfStmt.cpp new file mode 100644 index 0000000..9d7e8da --- /dev/null +++ b/lib/parser/ast/nodes/stmts/IfStmt.cpp @@ -0,0 +1 @@ +#include "IfStmt.hpp" diff --git a/lib/parser/ast/nodes/stmts/IfStmt.hpp b/lib/parser/ast/nodes/stmts/IfStmt.hpp new file mode 100644 index 0000000..39e4266 --- /dev/null +++ b/lib/parser/ast/nodes/stmts/IfStmt.hpp @@ -0,0 +1,25 @@ +#ifndef IFSTMT_HPP_ +#define IFSTMT_HPP_ + +#include +#include + +#include "Block.hpp" +#include "lib/parser/ast/nodes/base/Expr.hpp" +#include "lib/parser/ast/nodes/base/Stmt.hpp" + +class IfStmt : public Stmt { +public: + struct Branch { + std::unique_ptr cond; + std::unique_ptr then_block; + }; + + void Accept(AstVisitor& v) override; + +private: + std::vector branches_; // if + else-if* + std::unique_ptr else_block_; +}; + +#endif // IFSTMT_HPP_ diff --git a/lib/parser/ast/nodes/stmts/ReturnStmt.cpp b/lib/parser/ast/nodes/stmts/ReturnStmt.cpp new file mode 100644 index 0000000..619814e --- /dev/null +++ b/lib/parser/ast/nodes/stmts/ReturnStmt.cpp @@ -0,0 +1 @@ +#include "ReturnStmt.hpp" diff --git a/lib/parser/ast/nodes/stmts/ReturnStmt.hpp b/lib/parser/ast/nodes/stmts/ReturnStmt.hpp new file mode 100644 index 0000000..da615c4 --- /dev/null +++ b/lib/parser/ast/nodes/stmts/ReturnStmt.hpp @@ -0,0 +1,17 @@ +#ifndef RETURNSTMT_HPP_ +#define RETURNSTMT_HPP_ + +#include + +#include "lib/parser/ast/nodes/base/Stmt.hpp" + +class Expr; +class ReturnStmt : public Stmt { +public: + void Accept(AstVisitor& v) override; + +private: + std::unique_ptr value_; // optional +}; + +#endif // RETURNSTMT_HPP_ diff --git a/lib/parser/ast/nodes/stmts/UnsafeBlock.cpp b/lib/parser/ast/nodes/stmts/UnsafeBlock.cpp new file mode 100644 index 0000000..3c8c05e --- /dev/null +++ b/lib/parser/ast/nodes/stmts/UnsafeBlock.cpp @@ -0,0 +1 @@ +#include "UnsafeBlock.hpp" diff --git a/lib/parser/ast/nodes/stmts/UnsafeBlock.hpp b/lib/parser/ast/nodes/stmts/UnsafeBlock.hpp new file mode 100644 index 0000000..002f3dc --- /dev/null +++ b/lib/parser/ast/nodes/stmts/UnsafeBlock.hpp @@ -0,0 +1,17 @@ +#ifndef UNSAFEBLOCK_HPP_ +#define UNSAFEBLOCK_HPP_ + +#include + +#include "Block.hpp" +#include "lib/parser/ast/nodes/base/Stmt.hpp" + +class UnsafeBlock : public Stmt { +public: + void Accept(AstVisitor& v) override; + +private: + std::unique_ptr body_; +}; + +#endif // UNSAFEBLOCK_HPP_ diff --git a/lib/parser/ast/nodes/stmts/VarDeclStmt.cpp b/lib/parser/ast/nodes/stmts/VarDeclStmt.cpp new file mode 100644 index 0000000..e285238 --- /dev/null +++ b/lib/parser/ast/nodes/stmts/VarDeclStmt.cpp @@ -0,0 +1 @@ +#include "VarDeclStmt.hpp" diff --git a/lib/parser/ast/nodes/stmts/VarDeclStmt.hpp b/lib/parser/ast/nodes/stmts/VarDeclStmt.hpp new file mode 100644 index 0000000..5f88bae --- /dev/null +++ b/lib/parser/ast/nodes/stmts/VarDeclStmt.hpp @@ -0,0 +1,22 @@ +#ifndef VARDECLSTMT_HPP_ +#define VARDECLSTMT_HPP_ + +#include +#include + +#include "lib/parser/ast/nodes/base/Expr.hpp" +#include "lib/parser/ast/nodes/base/Stmt.hpp" +#include "lib/parser/types/TypeReference.hpp" + +class VarDeclStmt : public Stmt { +public: + void Accept(AstVisitor& v) override; + +private: + bool is_var_ = false; + std::string name_; + TypeReference type_; + std::unique_ptr init_; +}; + +#endif // VARDECLSTMT_HPP_ diff --git a/lib/parser/ast/nodes/stmts/WhileStmt.cpp b/lib/parser/ast/nodes/stmts/WhileStmt.cpp new file mode 100644 index 0000000..66d2995 --- /dev/null +++ b/lib/parser/ast/nodes/stmts/WhileStmt.cpp @@ -0,0 +1 @@ +#include "WhileStmt.hpp" diff --git a/lib/parser/ast/nodes/stmts/WhileStmt.hpp b/lib/parser/ast/nodes/stmts/WhileStmt.hpp new file mode 100644 index 0000000..5594de3 --- /dev/null +++ b/lib/parser/ast/nodes/stmts/WhileStmt.hpp @@ -0,0 +1,19 @@ +#ifndef WHILESTMT_HPP_ +#define WHILESTMT_HPP_ + +#include + +#include "Block.hpp" +#include "lib/parser/ast/nodes/base/Expr.hpp" +#include "lib/parser/ast/nodes/base/Stmt.hpp" + +class WhileStmt : public Stmt { +public: + void Accept(AstVisitor& v) override; + +private: + std::unique_ptr cond_; + std::unique_ptr body_; +}; + +#endif // WHILESTMT_HPP_ diff --git a/lib/parser/context/ContextParser.cpp b/lib/parser/context/ContextParser.cpp new file mode 100644 index 0000000..ee3ec25 --- /dev/null +++ b/lib/parser/context/ContextParser.cpp @@ -0,0 +1 @@ +#include "ContextParser.hpp" diff --git a/lib/parser/context/ContextParser.hpp b/lib/parser/context/ContextParser.hpp new file mode 100644 index 0000000..69b9dab --- /dev/null +++ b/lib/parser/context/ContextParser.hpp @@ -0,0 +1,52 @@ +#ifndef CONTEXTPARSER_HPP_ +#define CONTEXTPARSER_HPP_ + +#include + +#include "../states/IState.hpp" +#include "NodeEntry.hpp" +#include "lib/parser/diagnostics/IDiagnosticSink.hpp" +#include "lib/parser/states/base/IState.hpp" + +class IExpressionParser; // forward +class ITypeParser; // forward + +class ContextParser { +public: + std::vector& StateStack(); + std::vector& NodeStack(); + + void SetDiagnostics(IDiagnosticSink* d); + [[nodiscard]] IDiagnosticSink* Diags() const; + + void SetExpr(IExpressionParser* parser); + [[nodiscard]] IExpressionParser* Expr() const; + + void SetTypeParser(ITypeParser* parser); + [[nodiscard]] ITypeParser* TypeParser() const; + + void PushState(const IState& state); + void PopState(); + const IState& CurrentState() const; + + template + T* TopNodeAs() { + if (node_stack_.empty()) { + return nullptr; + } + + return dynamic_cast(node_stack_.back()); + } + + void PushNode(std::unique_ptr node); + std::unique_ptr PopNode(); + +private: + std::vector state_stack_; + std::vector node_stack_; + IDiagnosticSink* diags_ = nullptr; + IExpressionParser* expr_ = nullptr; + ITypeParser* typep_ = nullptr; +}; + +#endif // CONTEXTPARSER_HPP_ diff --git a/lib/parser/context/NodeEntry.cpp b/lib/parser/context/NodeEntry.cpp new file mode 100644 index 0000000..1261a37 --- /dev/null +++ b/lib/parser/context/NodeEntry.cpp @@ -0,0 +1 @@ +#include "NodeEntry.hpp" diff --git a/lib/parser/context/NodeEntry.hpp b/lib/parser/context/NodeEntry.hpp new file mode 100644 index 0000000..b59c332 --- /dev/null +++ b/lib/parser/context/NodeEntry.hpp @@ -0,0 +1,16 @@ +#ifndef NODEENTRY_HPP_ +#define NODEENTRY_HPP_ + +#include + +#include "lib/parser/ast/nodes/base/AstNode.hpp" + +class NodeEntry { +public: + std::unique_ptr GetNode; + +private: + std::unique_ptr node_; +}; + +#endif // NODEENTRY_HPP_ diff --git a/lib/parser/diagnostics/Diagnostic.cpp b/lib/parser/diagnostics/Diagnostic.cpp new file mode 100644 index 0000000..2f0f694 --- /dev/null +++ b/lib/parser/diagnostics/Diagnostic.cpp @@ -0,0 +1 @@ +#include "Diagnostic.hpp" diff --git a/lib/parser/diagnostics/Diagnostic.hpp b/lib/parser/diagnostics/Diagnostic.hpp new file mode 100644 index 0000000..7893ed2 --- /dev/null +++ b/lib/parser/diagnostics/Diagnostic.hpp @@ -0,0 +1,28 @@ +#ifndef DIAGNOSTIC_HPP_ +#define DIAGNOSTIC_HPP_ + +#include +#include +#include + +#include "FixIt.hpp" +#include "RelatedInfo.hpp" +#include "lib/parser/tokens/SourceSpan.hpp" +#include "severity/ISeverity.hpp" +#include "severity/Severity.hpp" + +class Diagnostic { + const ISeverity* severity = &Severity::Error(); + std::string code; + std::string message; + std::string category; + + std::optional where; + std::vector notes; + std::vector fixes; + + bool is_fatal = false; + bool is_suppressed = false; +}; + +#endif // DIAGNOSTIC_HPP_ diff --git a/lib/parser/diagnostics/DiagnosticCollector.cpp b/lib/parser/diagnostics/DiagnosticCollector.cpp new file mode 100644 index 0000000..a50f3f6 --- /dev/null +++ b/lib/parser/diagnostics/DiagnosticCollector.cpp @@ -0,0 +1 @@ +#include "DiagnosticCollector.hpp" diff --git a/lib/parser/diagnostics/DiagnosticCollector.hpp b/lib/parser/diagnostics/DiagnosticCollector.hpp new file mode 100644 index 0000000..f77a074 --- /dev/null +++ b/lib/parser/diagnostics/DiagnosticCollector.hpp @@ -0,0 +1,57 @@ +#ifndef DIAGNOSTICCOLLECTOR_HPP_ +#define DIAGNOSTICCOLLECTOR_HPP_ + +#include +#include +#include + +#include "IDiagnosticSink.hpp" + +class DiagnosticCollector : public IDiagnosticSink { +public: + using Predicate = std::function; + + ~DiagnosticCollector() override = default; + + void Report(Diagnostic d) override; + bool HasErrors() const override; + std::size_t Count() const override; + std::size_t ErrorCount() const override; + std::size_t WarningCount() const override; + + void Note(std::string_view code, std::string_view msg, std::optional where = std::nullopt) override; + void Warn(std::string_view code, std::string_view msg, std::optional where = std::nullopt) override; + void Error(std::string_view code, std::string_view msg, std::optional where = std::nullopt) override; + + const std::vector& All() const; + void Clear(); + + void SuppressCode(std::string code); + void SuppressCategory(std::string category); + void SetGlobalFilter(Predicate p); + void EnableDeduplication(bool on); + void SetCapacity(std::size_t max_total); + void SetErrorLimit(std::size_t max_errors); + void SetWarningLimit(std::size_t max_warnings); + + bool IsSuppressed(const Diagnostic& d) const; + +private: + bool ShouldKeep(const Diagnostic& d) const; + bool IsDuplicate(const Diagnostic& d) const; + + std::vector diags_; + std::unordered_set suppressed_codes_; + std::unordered_set suppressed_categories_; + Predicate global_filter_{}; + + bool dedup_ = true; + std::size_t capacity_ = 0; // 0 == without limits + std::size_t error_limit_ = 0; // 0 == without limits + std::size_t warning_limit_ = 0; // 0 == without limits + + std::size_t errors_ = 0; + std::size_t warnings_ = 0; +}; + +#endif // DIAGNOSTICCOLLECTOR_HPP_ diff --git a/lib/parser/diagnostics/FixIt.cpp b/lib/parser/diagnostics/FixIt.cpp new file mode 100644 index 0000000..9b04ed9 --- /dev/null +++ b/lib/parser/diagnostics/FixIt.cpp @@ -0,0 +1 @@ +#include "FixIt.hpp" diff --git a/lib/parser/diagnostics/FixIt.hpp b/lib/parser/diagnostics/FixIt.hpp new file mode 100644 index 0000000..ae6b2b6 --- /dev/null +++ b/lib/parser/diagnostics/FixIt.hpp @@ -0,0 +1,17 @@ +#ifndef FIXIT_HPP_ +#define FIXIT_HPP_ + +#include + +#include "lib/parser/tokens/SourceSpan.hpp" + +class FixIt { +public: + FixIt(SourceSpan source_span, std::string replacement); + // TODO: make methods +private: + SourceSpan where_; + std::string replacement_; +}; + +#endif // FIXIT_HPP_ diff --git a/lib/parser/diagnostics/IDiagnosticSink.hpp b/lib/parser/diagnostics/IDiagnosticSink.hpp new file mode 100644 index 0000000..ed54085 --- /dev/null +++ b/lib/parser/diagnostics/IDiagnosticSink.hpp @@ -0,0 +1,24 @@ +#ifndef IDIAGNOSTICSINK_HPP_ +#define IDIAGNOSTICSINK_HPP_ + +#include + +#include "Diagnostic.hpp" + +class IDiagnosticSink { +public: + virtual ~IDiagnosticSink() = default; + + virtual void Report(Diagnostic d) = 0; + + virtual bool HasErrors() const = 0; + virtual std::size_t Count() const = 0; + virtual std::size_t ErrorCount() const = 0; + virtual std::size_t WarningCount() const = 0; + + virtual void Note(std::string_view code, std::string_view msg, std::optional where = std::nullopt) = 0; + virtual void Warn(std::string_view code, std::string_view msg, std::optional where = std::nullopt) = 0; + virtual void Error(std::string_view code, std::string_view msg, std::optional where = std::nullopt) = 0; +}; + +#endif // IDIAGNOSTICSINK_HPP_ diff --git a/lib/parser/diagnostics/RelatedInfo.cpp b/lib/parser/diagnostics/RelatedInfo.cpp new file mode 100644 index 0000000..6d462e0 --- /dev/null +++ b/lib/parser/diagnostics/RelatedInfo.cpp @@ -0,0 +1 @@ +#include "RelatedInfo.hpp" diff --git a/lib/parser/diagnostics/RelatedInfo.hpp b/lib/parser/diagnostics/RelatedInfo.hpp new file mode 100644 index 0000000..71acd7d --- /dev/null +++ b/lib/parser/diagnostics/RelatedInfo.hpp @@ -0,0 +1,17 @@ +#ifndef RELATEDINFO_HPP_ +#define RELATEDINFO_HPP_ + +#include +#include + +#include "lib/parser/tokens/SourceSpan.hpp" + +class RelatedInfo { +public: + // TODO: make methods +private: + std::string message_; + std::optional where_; +}; + +#endif // RELATEDINFO_HPP_ diff --git a/lib/parser/diagnostics/severity/ISeverity.cpp b/lib/parser/diagnostics/severity/ISeverity.cpp new file mode 100644 index 0000000..d69e1d5 --- /dev/null +++ b/lib/parser/diagnostics/severity/ISeverity.cpp @@ -0,0 +1 @@ +#include "ISeverity.hpp" diff --git a/lib/parser/diagnostics/severity/ISeverity.hpp b/lib/parser/diagnostics/severity/ISeverity.hpp new file mode 100644 index 0000000..5381598 --- /dev/null +++ b/lib/parser/diagnostics/severity/ISeverity.hpp @@ -0,0 +1,13 @@ +#ifndef ISEVERITY_HPP_ +#define ISEVERITY_HPP_ + +#include + +class ISeverity { +public: + virtual ~ISeverity() = default; + virtual std::string_view Name() const = 0; + virtual int Level() const = 0; +}; + +#endif // ISEVERITY_HPP_ diff --git a/lib/parser/diagnostics/severity/Severity.cpp b/lib/parser/diagnostics/severity/Severity.cpp new file mode 100644 index 0000000..631496f --- /dev/null +++ b/lib/parser/diagnostics/severity/Severity.cpp @@ -0,0 +1 @@ +#include "Severity.hpp" diff --git a/lib/parser/diagnostics/severity/Severity.hpp b/lib/parser/diagnostics/severity/Severity.hpp new file mode 100644 index 0000000..64b2780 --- /dev/null +++ b/lib/parser/diagnostics/severity/Severity.hpp @@ -0,0 +1,14 @@ +#ifndef SEVERITY_HPP_ +#define SEVERITY_HPP_ +#include "ISeverity.hpp" + +class Severity { +public: + static const ISeverity& Note(); // Level=0 + static const ISeverity& Warning(); // Level=1 + static const ISeverity& Error(); // Level=2 + + static const ISeverity& Custom(std::string_view name, int level); +}; + +#endif // SEVERITY_HPP_ diff --git a/lib/parser/pratt/DefaultOperatorResolver.cpp b/lib/parser/pratt/DefaultOperatorResolver.cpp new file mode 100644 index 0000000..0b8d708 --- /dev/null +++ b/lib/parser/pratt/DefaultOperatorResolver.cpp @@ -0,0 +1 @@ +#include "DefaultOperatorResolver.hpp" diff --git a/lib/parser/pratt/DefaultOperatorResolver.hpp b/lib/parser/pratt/DefaultOperatorResolver.hpp new file mode 100644 index 0000000..8350b7d --- /dev/null +++ b/lib/parser/pratt/DefaultOperatorResolver.hpp @@ -0,0 +1,25 @@ +#ifndef DEFAULTOPERATORRESOLVER_HPP_ +#define DEFAULTOPERATORRESOLVER_HPP_ + +#include + +#include "IOperatorResolver.hpp" +#include "lib/parser/ast/nodes/exprs/tags/IUnaryOpTag.hpp" +#include "specifications/InfixSpec.hpp" +#include "specifications/PostfixSpec.hpp" + +class DefaultOperatorResolver : public IOperatorResolver { +public: + ~DefaultOperatorResolver() override = default; + + const InfixSpec* FindInfix(const Token& t) const override; + const PostfixSpec* FindPostfix(const Token& t) const override; + const IUnaryOpTag* FindPrefix(const Token& t) const override; + bool IsContinuation(const Token& t) const override; + +private: + std::vector infix_; + std::vector postfix_; +}; + +#endif // DEFAULTOPERATORRESOLVER_HPP_ diff --git a/lib/parser/pratt/IExpressionParser.hpp b/lib/parser/pratt/IExpressionParser.hpp new file mode 100644 index 0000000..4aa1134 --- /dev/null +++ b/lib/parser/pratt/IExpressionParser.hpp @@ -0,0 +1,17 @@ +#ifndef IEXPRESSIONPARSER_HPP_ +#define IEXPRESSIONPARSER_HPP_ + +#include + +#include "lib/parser/ast/nodes/base/Expr.hpp" +#include "lib/parser/diagnostics/IDiagnosticSink.hpp" +#include "lib/parser/tokens/token_streams/ITokenStream.hpp" + +class IExpressionParser { +public: + virtual ~IExpressionParser() = default; + + virtual std::unique_ptr Parse(ITokenStream& ts, IDiagnosticSink& diags) = 0; +}; + +#endif // IEXPRESSIONPARSER_HPP_ diff --git a/lib/parser/pratt/IOperatorResolver.hpp b/lib/parser/pratt/IOperatorResolver.hpp new file mode 100644 index 0000000..76b2625 --- /dev/null +++ b/lib/parser/pratt/IOperatorResolver.hpp @@ -0,0 +1,17 @@ +#ifndef IOPERATORRESOLVER_HPP_ +#define IOPERATORRESOLVER_HPP_ + +#include "lib/parser/ast/nodes/exprs/tags/IUnaryOpTag.hpp" +#include "lib/parser/pratt/specifications/InfixSpec.hpp" +#include "lib/parser/pratt/specifications/PostfixSpec.hpp" + +class IOperatorResolver { +public: + virtual ~IOperatorResolver() = default; + virtual const InfixSpec* FindInfix(const Token& t) const = 0; + virtual const PostfixSpec* FindPostfix(const Token& t) const = 0; + virtual const IUnaryOpTag* FindPrefix(const Token& t) const = 0; + virtual bool IsContinuation(const Token& t) const = 0; +}; + +#endif // IOPERATORRESOLVER_HPP_ diff --git a/lib/parser/pratt/PrattExpressionParser.cpp b/lib/parser/pratt/PrattExpressionParser.cpp new file mode 100644 index 0000000..4ea1b64 --- /dev/null +++ b/lib/parser/pratt/PrattExpressionParser.cpp @@ -0,0 +1 @@ +#include "PrattExpressionParser.hpp" diff --git a/lib/parser/pratt/PrattExpressionParser.hpp b/lib/parser/pratt/PrattExpressionParser.hpp new file mode 100644 index 0000000..a2a465c --- /dev/null +++ b/lib/parser/pratt/PrattExpressionParser.hpp @@ -0,0 +1,35 @@ +#ifndef PRATTEXPRESSIONPARSER_HPP_ +#define PRATTEXPRESSIONPARSER_HPP_ + +#include +#include + +#include "IExpressionParser.hpp" +#include "IOperatorResolver.hpp" +#include "lib/parser/ast/IAstFactory.hpp" +#include "lib/parser/ast/nodes/base/Expr.hpp" +#include "lib/parser/diagnostics/IDiagnosticSink.hpp" +#include "lib/parser/tokens/token_streams/ITokenStream.hpp" +#include "specifications/InfixSpec.hpp" + +class PrattExpressionParser : public IExpressionParser { +public: + explicit PrattExpressionParser(std::unique_ptr r, IAstFactory* factory); + ~PrattExpressionParser() override = default; + + std::unique_ptr Parse(ITokenStream& ts, IDiagnosticSink& diags) override; + + std::unique_ptr ParseExpr(ITokenStream& ts, IDiagnosticSink& diags, int min_bp); + std::unique_ptr ParsePrefix(ITokenStream& ts, IDiagnosticSink& diags); + std::unique_ptr ParsePostfix(ITokenStream& ts, IDiagnosticSink& diags, std::unique_ptr base); + + std::vector> ParseArgList(ITokenStream& ts, IDiagnosticSink& diags, char closing); + +private: + std::unique_ptr MakeInfix(const InfixSpec& spec, std::unique_ptr lhs, std::unique_ptr rhs); + + std::unique_ptr r_; + IAstFactory* factory_; +}; + +#endif // PRATTEXPRESSIONPARSER_HPP_ diff --git a/lib/parser/pratt/specifications/InfixSpec.cpp b/lib/parser/pratt/specifications/InfixSpec.cpp new file mode 100644 index 0000000..aa3ca96 --- /dev/null +++ b/lib/parser/pratt/specifications/InfixSpec.cpp @@ -0,0 +1 @@ +#include "InfixSpec.hpp" diff --git a/lib/parser/pratt/specifications/InfixSpec.hpp b/lib/parser/pratt/specifications/InfixSpec.hpp new file mode 100644 index 0000000..0c6fca5 --- /dev/null +++ b/lib/parser/pratt/specifications/InfixSpec.hpp @@ -0,0 +1,21 @@ +#ifndef INFIXSPEC_HPP_ +#define INFIXSPEC_HPP_ + +#include + +#include "lib/lexer/tokens/Token.hpp" +#include "lib/parser/ast/nodes/exprs/tags/IBinaryOpTag.hpp" + +class InfixSpec { +public: + // TODO: implement getters and setters +private: + std::function match_; + int lbp = 0; + int rbp = 0; + bool right_associative = false; + const IBinaryOpTag* tag_ = nullptr; + bool is_elvis = false; +}; + +#endif // INFIXSPEC_HPP_ diff --git a/lib/parser/pratt/specifications/PostfixSpec.cpp b/lib/parser/pratt/specifications/PostfixSpec.cpp new file mode 100644 index 0000000..e0e3efd --- /dev/null +++ b/lib/parser/pratt/specifications/PostfixSpec.cpp @@ -0,0 +1 @@ +#include "PostfixSpec.hpp" diff --git a/lib/parser/pratt/specifications/PostfixSpec.hpp b/lib/parser/pratt/specifications/PostfixSpec.hpp new file mode 100644 index 0000000..d6cfaec --- /dev/null +++ b/lib/parser/pratt/specifications/PostfixSpec.hpp @@ -0,0 +1,17 @@ +#ifndef POSTFIXSPEC_HPP_ +#define POSTFIXSPEC_HPP_ + +#include + +#include "lib/lexer/tokens/Token.hpp" + +class PostfixSpec { +public: + // TODO: implements getters and setters +private: + std::function match_; + int bp = 0; + bool keyword = false; +}; + +#endif // POSTFIXSPEC_HPP_ diff --git a/lib/parser/recovery/IRecoveryStrategy.hpp b/lib/parser/recovery/IRecoveryStrategy.hpp new file mode 100644 index 0000000..ad75813 --- /dev/null +++ b/lib/parser/recovery/IRecoveryStrategy.hpp @@ -0,0 +1,14 @@ +#ifndef IRECOVERYSTRATEGY_HPP_ +#define IRECOVERYSTRATEGY_HPP_ + +#include "lib/parser/tokens/token_streams/ITokenStream.hpp" + +class IRecoveryStrategy { +public: + virtual ~IRecoveryStrategy() { + } + virtual void SyncToStmtEnd(ITokenStream& ts) = 0; + virtual void SyncToBlockEnd(ITokenStream& ts) = 0; +}; + +#endif // IRECOVERYSTRATEGY_HPP_ diff --git a/lib/parser/recovery/SimpleRecovery.cpp b/lib/parser/recovery/SimpleRecovery.cpp new file mode 100644 index 0000000..a5ad7a5 --- /dev/null +++ b/lib/parser/recovery/SimpleRecovery.cpp @@ -0,0 +1 @@ +#include "SimpleRecovery.hpp" diff --git a/lib/parser/recovery/SimpleRecovery.hpp b/lib/parser/recovery/SimpleRecovery.hpp new file mode 100644 index 0000000..3cf9688 --- /dev/null +++ b/lib/parser/recovery/SimpleRecovery.hpp @@ -0,0 +1,14 @@ +#ifndef SIMPLERECOVERY_HPP_ +#define SIMPLERECOVERY_HPP_ + +#include "IRecoveryStrategy.hpp" + +class SimpleRecovery : IRecoveryStrategy { +public: + ~SimpleRecovery() override = default; + + void SyncToStmtEnd(ITokenStream& ts) override; + void SyncToBlockEnd(ITokenStream& ts) override; +}; + +#endif // SIMPLERECOVERY_HPP_ diff --git a/lib/parser/states/StateBlock.cpp b/lib/parser/states/StateBlock.cpp new file mode 100644 index 0000000..279fa35 --- /dev/null +++ b/lib/parser/states/StateBlock.cpp @@ -0,0 +1 @@ +#include "StateBlock.hpp" diff --git a/lib/parser/states/StateBlock.hpp b/lib/parser/states/StateBlock.hpp new file mode 100644 index 0000000..4f5de4a --- /dev/null +++ b/lib/parser/states/StateBlock.hpp @@ -0,0 +1,13 @@ +#ifndef STATEBLOCK_HPP_ +#define STATEBLOCK_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateBlock : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEBLOCK_HPP_ diff --git a/lib/parser/states/StateCallDeclHdr.cpp b/lib/parser/states/StateCallDeclHdr.cpp new file mode 100644 index 0000000..7d1ae4a --- /dev/null +++ b/lib/parser/states/StateCallDeclHdr.cpp @@ -0,0 +1 @@ +#include "StateCallDeclHdr.hpp" diff --git a/lib/parser/states/StateCallDeclHdr.hpp b/lib/parser/states/StateCallDeclHdr.hpp new file mode 100644 index 0000000..7ffbdcd --- /dev/null +++ b/lib/parser/states/StateCallDeclHdr.hpp @@ -0,0 +1,13 @@ +#ifndef STATECALLDECLHDR_HPP_ +#define STATECALLDECLHDR_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateCallDeclHdr : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATECALLDECLHDR_HPP_ diff --git a/lib/parser/states/StateDestructorDecl.cpp b/lib/parser/states/StateDestructorDecl.cpp new file mode 100644 index 0000000..0972e30 --- /dev/null +++ b/lib/parser/states/StateDestructorDecl.cpp @@ -0,0 +1 @@ +#include "StateDestructorDecl.hpp" diff --git a/lib/parser/states/StateDestructorDecl.hpp b/lib/parser/states/StateDestructorDecl.hpp new file mode 100644 index 0000000..a5e98cd --- /dev/null +++ b/lib/parser/states/StateDestructorDecl.hpp @@ -0,0 +1,13 @@ +#ifndef STATEDESTRUCTORDECL_HPP_ +#define STATEDESTRUCTORDECL_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateDestructorDecl : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEDESTRUCTORDECL_HPP_ diff --git a/lib/parser/states/StateExpr.cpp b/lib/parser/states/StateExpr.cpp new file mode 100644 index 0000000..811a990 --- /dev/null +++ b/lib/parser/states/StateExpr.cpp @@ -0,0 +1 @@ +#include "StateExpr.hpp" diff --git a/lib/parser/states/StateExpr.hpp b/lib/parser/states/StateExpr.hpp new file mode 100644 index 0000000..31c2f3f --- /dev/null +++ b/lib/parser/states/StateExpr.hpp @@ -0,0 +1,13 @@ +#ifndef STATEEXPR_HPP_ +#define STATEEXPR_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateExpr : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEEXPR_HPP_ diff --git a/lib/parser/states/StateFieldDecl.cpp b/lib/parser/states/StateFieldDecl.cpp new file mode 100644 index 0000000..b0c7a3f --- /dev/null +++ b/lib/parser/states/StateFieldDecl.cpp @@ -0,0 +1 @@ +#include "StateFieldDecl.hpp" diff --git a/lib/parser/states/StateFieldDecl.hpp b/lib/parser/states/StateFieldDecl.hpp new file mode 100644 index 0000000..38c9c22 --- /dev/null +++ b/lib/parser/states/StateFieldDecl.hpp @@ -0,0 +1,13 @@ +#ifndef STATEFIELDDECL_HPP_ +#define STATEFIELDDECL_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateFieldDecl : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEFIELDDECL_HPP_ diff --git a/lib/parser/states/StateForHead.cpp b/lib/parser/states/StateForHead.cpp new file mode 100644 index 0000000..8548873 --- /dev/null +++ b/lib/parser/states/StateForHead.cpp @@ -0,0 +1 @@ +#include "StateForHead.hpp" diff --git a/lib/parser/states/StateForHead.hpp b/lib/parser/states/StateForHead.hpp new file mode 100644 index 0000000..64126b0 --- /dev/null +++ b/lib/parser/states/StateForHead.hpp @@ -0,0 +1,13 @@ +#ifndef STATEFORHEAD_HPP_ +#define STATEFORHEAD_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateForHead : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEFORHEAD_HPP_ diff --git a/lib/parser/states/StateGlobalVarDecl.cpp b/lib/parser/states/StateGlobalVarDecl.cpp new file mode 100644 index 0000000..53f53e3 --- /dev/null +++ b/lib/parser/states/StateGlobalVarDecl.cpp @@ -0,0 +1 @@ +#include "StateGlobalVarDecl.hpp" diff --git a/lib/parser/states/StateGlobalVarDecl.hpp b/lib/parser/states/StateGlobalVarDecl.hpp new file mode 100644 index 0000000..301359f --- /dev/null +++ b/lib/parser/states/StateGlobalVarDecl.hpp @@ -0,0 +1,13 @@ +#ifndef STATEGLOBALVARDECL_HPP_ +#define STATEGLOBALVARDECL_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateGlobalVarDecl : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEGLOBALVARDECL_HPP_ diff --git a/lib/parser/states/StateIfHead.cpp b/lib/parser/states/StateIfHead.cpp new file mode 100644 index 0000000..c365675 --- /dev/null +++ b/lib/parser/states/StateIfHead.cpp @@ -0,0 +1 @@ +#include "StateIfHead.hpp" diff --git a/lib/parser/states/StateIfHead.hpp b/lib/parser/states/StateIfHead.hpp new file mode 100644 index 0000000..0739c60 --- /dev/null +++ b/lib/parser/states/StateIfHead.hpp @@ -0,0 +1,13 @@ +#ifndef STATEIFHEAD_HPP_ +#define STATEIFHEAD_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateIfHead : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEIFHEAD_HPP_ diff --git a/lib/parser/states/StateIfTail.cpp b/lib/parser/states/StateIfTail.cpp new file mode 100644 index 0000000..b849664 --- /dev/null +++ b/lib/parser/states/StateIfTail.cpp @@ -0,0 +1 @@ +#include "StateIfTail.hpp" diff --git a/lib/parser/states/StateIfTail.hpp b/lib/parser/states/StateIfTail.hpp new file mode 100644 index 0000000..d75a5be --- /dev/null +++ b/lib/parser/states/StateIfTail.hpp @@ -0,0 +1,13 @@ +#ifndef STATEIFTAIL_HPP_ +#define STATEIFTAIL_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateIfTail : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEIFTAIL_HPP_ diff --git a/lib/parser/states/StateMethodHdr.cpp b/lib/parser/states/StateMethodHdr.cpp new file mode 100644 index 0000000..414d0d8 --- /dev/null +++ b/lib/parser/states/StateMethodHdr.cpp @@ -0,0 +1 @@ +#include "StateMethodHdr.hpp" diff --git a/lib/parser/states/StateMethodHdr.hpp b/lib/parser/states/StateMethodHdr.hpp new file mode 100644 index 0000000..3f12f60 --- /dev/null +++ b/lib/parser/states/StateMethodHdr.hpp @@ -0,0 +1,13 @@ +#ifndef STATEMETHODHDR_HPP_ +#define STATEMETHODHDR_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateMethodHdr : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEMETHODHDR_HPP_ diff --git a/lib/parser/states/StateModule.cpp b/lib/parser/states/StateModule.cpp new file mode 100644 index 0000000..25c44a5 --- /dev/null +++ b/lib/parser/states/StateModule.cpp @@ -0,0 +1 @@ +#include "StateModule.hpp" diff --git a/lib/parser/states/StateModule.hpp b/lib/parser/states/StateModule.hpp new file mode 100644 index 0000000..bba7700 --- /dev/null +++ b/lib/parser/states/StateModule.hpp @@ -0,0 +1,13 @@ +#ifndef STATEMODULE_HPP_ +#define STATEMODULE_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateModule : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEMODULE_HPP_ diff --git a/lib/parser/states/StateParseType.cpp b/lib/parser/states/StateParseType.cpp new file mode 100644 index 0000000..2e91214 --- /dev/null +++ b/lib/parser/states/StateParseType.cpp @@ -0,0 +1 @@ +#include "StateParseType.hpp" diff --git a/lib/parser/states/StateParseType.hpp b/lib/parser/states/StateParseType.hpp new file mode 100644 index 0000000..f4cdfd9 --- /dev/null +++ b/lib/parser/states/StateParseType.hpp @@ -0,0 +1,13 @@ +#ifndef STATEPARSETYPE_HPP_ +#define STATEPARSETYPE_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateParseType : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEPARSETYPE_HPP_ diff --git a/lib/parser/states/StateReturnTail.cpp b/lib/parser/states/StateReturnTail.cpp new file mode 100644 index 0000000..228c916 --- /dev/null +++ b/lib/parser/states/StateReturnTail.cpp @@ -0,0 +1 @@ +#include "StateReturnTail.hpp" diff --git a/lib/parser/states/StateReturnTail.hpp b/lib/parser/states/StateReturnTail.hpp new file mode 100644 index 0000000..59cd220 --- /dev/null +++ b/lib/parser/states/StateReturnTail.hpp @@ -0,0 +1,13 @@ +#ifndef STATERETURNTAIL_HPP_ +#define STATERETURNTAIL_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateReturnTail : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATERETURNTAIL_HPP_ diff --git a/lib/parser/states/StateStmt.cpp b/lib/parser/states/StateStmt.cpp new file mode 100644 index 0000000..df33c70 --- /dev/null +++ b/lib/parser/states/StateStmt.cpp @@ -0,0 +1 @@ +#include "StateStmt.hpp" diff --git a/lib/parser/states/StateStmt.hpp b/lib/parser/states/StateStmt.hpp new file mode 100644 index 0000000..c892121 --- /dev/null +++ b/lib/parser/states/StateStmt.hpp @@ -0,0 +1,13 @@ +#ifndef STATESTMT_HPP_ +#define STATESTMT_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateStmt : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATESTMT_HPP_ diff --git a/lib/parser/states/StateSyncToBlockEnd.cpp b/lib/parser/states/StateSyncToBlockEnd.cpp new file mode 100644 index 0000000..aaddd56 --- /dev/null +++ b/lib/parser/states/StateSyncToBlockEnd.cpp @@ -0,0 +1 @@ +#include "StateSyncToBlockEnd.hpp" diff --git a/lib/parser/states/StateSyncToBlockEnd.hpp b/lib/parser/states/StateSyncToBlockEnd.hpp new file mode 100644 index 0000000..ba54686 --- /dev/null +++ b/lib/parser/states/StateSyncToBlockEnd.hpp @@ -0,0 +1,13 @@ +#ifndef STATESYNCTOBLOCKEND_HPP_ +#define STATESYNCTOBLOCKEND_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateSyncToBlockEnd : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATESYNCTOBLOCKEND_HPP_ diff --git a/lib/parser/states/StateSyncToStmtEnd.cpp b/lib/parser/states/StateSyncToStmtEnd.cpp new file mode 100644 index 0000000..caee1ca --- /dev/null +++ b/lib/parser/states/StateSyncToStmtEnd.cpp @@ -0,0 +1 @@ +#include "StateSyncToStmtEnd.hpp" diff --git a/lib/parser/states/StateSyncToStmtEnd.hpp b/lib/parser/states/StateSyncToStmtEnd.hpp new file mode 100644 index 0000000..6348f4e --- /dev/null +++ b/lib/parser/states/StateSyncToStmtEnd.hpp @@ -0,0 +1,13 @@ +#ifndef STATESYNCTOSTMTEND_HPP_ +#define STATESYNCTOSTMTEND_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateSyncToStmtEnd : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATESYNCTOSTMTEND_HPP_ diff --git a/lib/parser/states/StateTopDecl.cpp b/lib/parser/states/StateTopDecl.cpp new file mode 100644 index 0000000..e7c08da --- /dev/null +++ b/lib/parser/states/StateTopDecl.cpp @@ -0,0 +1 @@ +#include "StateTopDecl.hpp" diff --git a/lib/parser/states/StateTopDecl.hpp b/lib/parser/states/StateTopDecl.hpp new file mode 100644 index 0000000..df36014 --- /dev/null +++ b/lib/parser/states/StateTopDecl.hpp @@ -0,0 +1,13 @@ +#ifndef STATETOPDECL_HPP_ +#define STATETOPDECL_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateTopDecl : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATETOPDECL_HPP_ diff --git a/lib/parser/states/StateTypeAliasDecl.cpp b/lib/parser/states/StateTypeAliasDecl.cpp new file mode 100644 index 0000000..9510c96 --- /dev/null +++ b/lib/parser/states/StateTypeAliasDecl.cpp @@ -0,0 +1 @@ +#include "StateTypeAliasDecl.hpp" diff --git a/lib/parser/states/StateTypeAliasDecl.hpp b/lib/parser/states/StateTypeAliasDecl.hpp new file mode 100644 index 0000000..21ceaad --- /dev/null +++ b/lib/parser/states/StateTypeAliasDecl.hpp @@ -0,0 +1,13 @@ +#ifndef STATETYPEALIASDECL_HPP_ +#define STATETYPEALIASDECL_HPP_ + +#include "lib/parser/states/base/StateBase.hpp" +#include "lib/parser/tokens/token_streams/ITokenStream.hpp" + +class StateTypeAliasDecl : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATETYPEALIASDECL_HPP_ diff --git a/lib/parser/states/StateUnsafeBlock.cpp b/lib/parser/states/StateUnsafeBlock.cpp new file mode 100644 index 0000000..118a37a --- /dev/null +++ b/lib/parser/states/StateUnsafeBlock.cpp @@ -0,0 +1 @@ +#include "StateUnsafeBlock.hpp" diff --git a/lib/parser/states/StateUnsafeBlock.hpp b/lib/parser/states/StateUnsafeBlock.hpp new file mode 100644 index 0000000..4d9f68c --- /dev/null +++ b/lib/parser/states/StateUnsafeBlock.hpp @@ -0,0 +1,13 @@ +#ifndef STATEUNSAFEBLOCK_HPP_ +#define STATEUNSAFEBLOCK_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateUnsafeBlock : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEUNSAFEBLOCK_HPP_ diff --git a/lib/parser/states/StateVarDeclTail.cpp b/lib/parser/states/StateVarDeclTail.cpp new file mode 100644 index 0000000..68f85c5 --- /dev/null +++ b/lib/parser/states/StateVarDeclTail.cpp @@ -0,0 +1 @@ +#include "StateVarDeclTail.hpp" diff --git a/lib/parser/states/StateVarDeclTail.hpp b/lib/parser/states/StateVarDeclTail.hpp new file mode 100644 index 0000000..1984b08 --- /dev/null +++ b/lib/parser/states/StateVarDeclTail.hpp @@ -0,0 +1,13 @@ +#ifndef STATEVARDECLTAIL_HPP_ +#define STATEVARDECLTAIL_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateVarDeclTail : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEVARDECLTAIL_HPP_ diff --git a/lib/parser/states/StateWhileHead.cpp b/lib/parser/states/StateWhileHead.cpp new file mode 100644 index 0000000..bdc5f9b --- /dev/null +++ b/lib/parser/states/StateWhileHead.cpp @@ -0,0 +1 @@ +#include "StateWhileHead.hpp" diff --git a/lib/parser/states/StateWhileHead.hpp b/lib/parser/states/StateWhileHead.hpp new file mode 100644 index 0000000..5515efe --- /dev/null +++ b/lib/parser/states/StateWhileHead.hpp @@ -0,0 +1,13 @@ +#ifndef STATEWHILEHEAD_HPP_ +#define STATEWHILEHEAD_HPP_ + +#include "base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" + +class StateWhileHead : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEWHILEHEAD_HPP_ diff --git a/lib/parser/states/base/IState.hpp b/lib/parser/states/base/IState.hpp new file mode 100644 index 0000000..ee3d794 --- /dev/null +++ b/lib/parser/states/base/IState.hpp @@ -0,0 +1,14 @@ +#ifndef ISTATE_HPP_ +#define ISTATE_HPP_ + +#include "lib/parser/context/ContextParser.hpp" +#include "lib/parser/tokens/token_streams/ITokenStream.hpp" + +class IState { +public: + virtual ~IState() = default; + virtual const char* Name() const = 0; + virtual bool Step(class ContextParser& ctx, ITokenStream& ts) = 0; +}; + +#endif // ISTATE_HPP_ diff --git a/lib/parser/states/base/StateBase.cpp b/lib/parser/states/base/StateBase.cpp new file mode 100644 index 0000000..fc5ccc2 --- /dev/null +++ b/lib/parser/states/base/StateBase.cpp @@ -0,0 +1 @@ +#include "StateBase.hpp" diff --git a/lib/parser/states/base/StateBase.hpp b/lib/parser/states/base/StateBase.hpp new file mode 100644 index 0000000..71ac357 --- /dev/null +++ b/lib/parser/states/base/StateBase.hpp @@ -0,0 +1,14 @@ +#ifndef STATEBASE_HPP_ +#define STATEBASE_HPP_ + +#include "IState.hpp" +#include "lib/parser/tokens/SourceSpan.hpp" + +class StateBase : IState { +public: + ~StateBase() override = default; + static SourceSpan SpanFrom(const Token& token); + static SourceSpan Union(const SourceSpan& lhs, const SourceSpan& rhs); +}; + +#endif // STATEBASE_HPP_ diff --git a/lib/parser/states/base/StateRegistry.cpp b/lib/parser/states/base/StateRegistry.cpp new file mode 100644 index 0000000..aa6279d --- /dev/null +++ b/lib/parser/states/base/StateRegistry.cpp @@ -0,0 +1 @@ +#include "StateRegistry.hpp" diff --git a/lib/parser/states/base/StateRegistry.hpp b/lib/parser/states/base/StateRegistry.hpp new file mode 100644 index 0000000..4deb850 --- /dev/null +++ b/lib/parser/states/base/StateRegistry.hpp @@ -0,0 +1,45 @@ +#ifndef STATEREGISTRY_HPP_ +#define STATEREGISTRY_HPP_ + +#include "IState.hpp" + +class StateRegistry { + static const IState& Module(); + static const IState& TopDecl(); + + static const IState& FuncHdr(); + static const IState& FuncParams(); + static const IState& FuncBody(); + + static const IState& ClassHdr(); + static const IState& ClassBody(); + static const IState& ClassMember(); + static const IState& InterfaceHdr(); + static const IState& InterfaceBody(); + static const IState& SignatureDecl(); + + static const IState& TypeAliasDecl(); + static const IState& GlobalVarDecl(); + + static const IState& DestructorDecl(); + static const IState& CallDeclHdr(); + static const IState& MethodHdr(); + static const IState& FieldDecl(); + + static const IState& Block(); + static const IState& Stmt(); + static const IState& IfHead(); + static const IState& IfTail(); + static const IState& WhileHead(); + static const IState& ForHead(); + static const IState& ReturnTail(); + static const IState& VarDeclTail(); + static const IState& UnsafeBlock(); + + static const IState& ParseType(); + static const IState& Expr(); + static const IState& SyncToStmtEnd(); + static const IState& SyncToBlockEnd(); +}; + +#endif // STATEREGISTRY_HPP_ diff --git a/lib/parser/states/class/StateClassBody.cpp b/lib/parser/states/class/StateClassBody.cpp new file mode 100644 index 0000000..818ced4 --- /dev/null +++ b/lib/parser/states/class/StateClassBody.cpp @@ -0,0 +1 @@ +#include "StateClassBody.hpp" diff --git a/lib/parser/states/class/StateClassBody.hpp b/lib/parser/states/class/StateClassBody.hpp new file mode 100644 index 0000000..3f5e2dc --- /dev/null +++ b/lib/parser/states/class/StateClassBody.hpp @@ -0,0 +1,14 @@ +#ifndef STATECLASSBODY_HPP_ +#define STATECLASSBODY_HPP_ + +#include "../base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" +#include "lib/parser/tokens/token_streams/ITokenStream.hpp" + +class StateClassBody : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATECLASSBODY_HPP_ diff --git a/lib/parser/states/class/StateClassHdr.cpp b/lib/parser/states/class/StateClassHdr.cpp new file mode 100644 index 0000000..62189e1 --- /dev/null +++ b/lib/parser/states/class/StateClassHdr.cpp @@ -0,0 +1 @@ +#include "StateClassHdr.hpp" diff --git a/lib/parser/states/class/StateClassHdr.hpp b/lib/parser/states/class/StateClassHdr.hpp new file mode 100644 index 0000000..c99c9cd --- /dev/null +++ b/lib/parser/states/class/StateClassHdr.hpp @@ -0,0 +1,14 @@ +#ifndef STATECLASSHDR_HPP_ +#define STATECLASSHDR_HPP_ + +#include "../base/StateBase.hpp" +#include "lib/parser/context/ContextParser.hpp" +#include "lib/parser/tokens/token_streams/ITokenStream.hpp" + +class StateClassHdr : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATECLASSHDR_HPP_ diff --git a/lib/parser/states/class/StateClassMember.cpp b/lib/parser/states/class/StateClassMember.cpp new file mode 100644 index 0000000..f299633 --- /dev/null +++ b/lib/parser/states/class/StateClassMember.cpp @@ -0,0 +1 @@ +#include "StateClassMember.hpp" diff --git a/lib/parser/states/class/StateClassMember.hpp b/lib/parser/states/class/StateClassMember.hpp new file mode 100644 index 0000000..81facb8 --- /dev/null +++ b/lib/parser/states/class/StateClassMember.hpp @@ -0,0 +1,13 @@ +#ifndef STATECLASSMEMBER_HPP_ +#define STATECLASSMEMBER_HPP_ + +#include "lib/parser/states/base/StateBase.hpp" +#include "lib/parser/tokens/token_streams/ITokenStream.hpp" + +class StateClassHdr : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATECLASSMEMBER_HPP_ diff --git a/lib/parser/states/func/StateFuncBody.cpp b/lib/parser/states/func/StateFuncBody.cpp new file mode 100644 index 0000000..8babe7f --- /dev/null +++ b/lib/parser/states/func/StateFuncBody.cpp @@ -0,0 +1 @@ +#include "StateFuncBody.hpp" diff --git a/lib/parser/states/func/StateFuncBody.hpp b/lib/parser/states/func/StateFuncBody.hpp new file mode 100644 index 0000000..b1982f8 --- /dev/null +++ b/lib/parser/states/func/StateFuncBody.hpp @@ -0,0 +1,13 @@ +#ifndef STATEFUNCBODY_HPP_ +#define STATEFUNCBODY_HPP_ + +#include "lib/parser/states/base/StateBase.hpp" +#include "lib/parser/tokens/token_streams/ITokenStream.hpp" + +class StateFuncBody : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEFUNCBODY_HPP_ diff --git a/lib/parser/states/func/StateFuncHdr.cpp b/lib/parser/states/func/StateFuncHdr.cpp new file mode 100644 index 0000000..e11bddf --- /dev/null +++ b/lib/parser/states/func/StateFuncHdr.cpp @@ -0,0 +1 @@ +#include "StateFuncHdr.hpp" diff --git a/lib/parser/states/func/StateFuncHdr.hpp b/lib/parser/states/func/StateFuncHdr.hpp new file mode 100644 index 0000000..ec6aadb --- /dev/null +++ b/lib/parser/states/func/StateFuncHdr.hpp @@ -0,0 +1,13 @@ +#ifndef STATEFUNCHDR_HPP_ +#define STATEFUNCHDR_HPP_ + +#include "lib/parser/states/base/StateBase.hpp" +#include "lib/parser/tokens/token_streams/ITokenStream.hpp" + +class StateFuncHdr : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEFUNCHDR_HPP_ diff --git a/lib/parser/states/func/StateFuncParams.cpp b/lib/parser/states/func/StateFuncParams.cpp new file mode 100644 index 0000000..6932a35 --- /dev/null +++ b/lib/parser/states/func/StateFuncParams.cpp @@ -0,0 +1 @@ +#include "StateFuncParams.hpp" diff --git a/lib/parser/states/func/StateFuncParams.hpp b/lib/parser/states/func/StateFuncParams.hpp new file mode 100644 index 0000000..4e26a82 --- /dev/null +++ b/lib/parser/states/func/StateFuncParams.hpp @@ -0,0 +1,13 @@ +#ifndef STATEFUNCPARAMS_HPP_ +#define STATEFUNCPARAMS_HPP_ + +#include "lib/parser/states/base/StateBase.hpp" +#include "lib/parser/tokens/token_streams/ITokenStream.hpp" + +class StateFuncParams : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEFUNCPARAMS_HPP_ diff --git a/lib/parser/states/interface/StateInterfaceBody.cpp b/lib/parser/states/interface/StateInterfaceBody.cpp new file mode 100644 index 0000000..f79c31a --- /dev/null +++ b/lib/parser/states/interface/StateInterfaceBody.cpp @@ -0,0 +1 @@ +#include "StateInterfaceBody.hpp" diff --git a/lib/parser/states/interface/StateInterfaceBody.hpp b/lib/parser/states/interface/StateInterfaceBody.hpp new file mode 100644 index 0000000..9ce5073 --- /dev/null +++ b/lib/parser/states/interface/StateInterfaceBody.hpp @@ -0,0 +1,13 @@ +#ifndef STATEINTERFACEBODY_HPP_ +#define STATEINTERFACEBODY_HPP_ + +#include "lib/parser/states/base/StateBase.hpp" +#include "lib/parser/tokens/token_streams/ITokenStream.hpp" + +class StateInterfaceBody : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEINTERFACEBODY_HPP_ diff --git a/lib/parser/states/interface/StateInterfaceDecl.cpp b/lib/parser/states/interface/StateInterfaceDecl.cpp new file mode 100644 index 0000000..c8d556c --- /dev/null +++ b/lib/parser/states/interface/StateInterfaceDecl.cpp @@ -0,0 +1 @@ +#include "StateInterfaceDecl.hpp" diff --git a/lib/parser/states/interface/StateInterfaceDecl.hpp b/lib/parser/states/interface/StateInterfaceDecl.hpp new file mode 100644 index 0000000..ecea6da --- /dev/null +++ b/lib/parser/states/interface/StateInterfaceDecl.hpp @@ -0,0 +1,13 @@ +#ifndef STATEINTERFACEDECL_HPP_ +#define STATEINTERFACEDECL_HPP_ + +#include "lib/parser/states/base/StateBase.hpp" +#include "lib/parser/tokens/token_streams/ITokenStream.hpp" + +class StateInterfaceDecl : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEINTERFACEDECL_HPP_ diff --git a/lib/parser/states/interface/StateInterfaceHdr.cpp b/lib/parser/states/interface/StateInterfaceHdr.cpp new file mode 100644 index 0000000..74e2c1d --- /dev/null +++ b/lib/parser/states/interface/StateInterfaceHdr.cpp @@ -0,0 +1 @@ +#include "StateInterfaceHdr.hpp" diff --git a/lib/parser/states/interface/StateInterfaceHdr.hpp b/lib/parser/states/interface/StateInterfaceHdr.hpp new file mode 100644 index 0000000..2993ec7 --- /dev/null +++ b/lib/parser/states/interface/StateInterfaceHdr.hpp @@ -0,0 +1,13 @@ +#ifndef STATEINTERFACEHDR_HPP_ +#define STATEINTERFACEHDR_HPP_ + +#include "lib/parser/states/base/StateBase.hpp" +#include "lib/parser/tokens/token_streams/ITokenStream.hpp" + +class StateInterfaceHdr : public StateBase { +public: + const char* Name() const override; + bool Step(ContextParser& ctx, ITokenStream& ts) override; +}; + +#endif // STATEINTERFACEHDR_HPP_ diff --git a/lib/parser/tokens/SourceSpan.cpp b/lib/parser/tokens/SourceSpan.cpp new file mode 100644 index 0000000..aaa1a2a --- /dev/null +++ b/lib/parser/tokens/SourceSpan.cpp @@ -0,0 +1 @@ +#include "SourceSpan.hpp" diff --git a/lib/parser/tokens/SourceSpan.hpp b/lib/parser/tokens/SourceSpan.hpp new file mode 100644 index 0000000..92476e5 --- /dev/null +++ b/lib/parser/tokens/SourceSpan.hpp @@ -0,0 +1,19 @@ +#ifndef SOURCESPAN_HPP_ +#define SOURCESPAN_HPP_ + +#include "lib/lexer/tokens/TokenPosition.hpp" + +class SourceSpan { +public: + SourceSpan(TokenPosition start, TokenPosition end); + + [[nodiscard]] TokenPosition GetStart() const; + + [[nodiscard]] TokenPosition GetEnd() const; + +private: + TokenPosition begin_; + TokenPosition end_; +}; + +#endif // SOURCESPAN_HPP_ diff --git a/lib/parser/tokens/token_streams/ITokenStream.hpp b/lib/parser/tokens/token_streams/ITokenStream.hpp new file mode 100644 index 0000000..3d76df7 --- /dev/null +++ b/lib/parser/tokens/token_streams/ITokenStream.hpp @@ -0,0 +1,27 @@ +#ifndef ITOKENSTREAM_HPP_ +#define ITOKENSTREAM_HPP_ + +#include + +#include "lib/lexer/tokens/Token.hpp" + +class ITokenStream { +public: + virtual ~ITokenStream() = default; + + virtual const Token& Peek(std::size_t k = 0) = 0; + + virtual TokenPtr Consume() = 0; + + virtual std::size_t Position() const = 0; + + virtual void Rewind(std::size_t n) = 0; + + virtual bool IsEof() const = 0; + + virtual const Token* LastConsumed() const = 0; + + virtual const Token* TryPeek(std::size_t k = 0) = 0; +}; + +#endif // ITOKENSTREAM_HPP_ diff --git a/lib/parser/tokens/token_streams/VectorTokenStream.cpp b/lib/parser/tokens/token_streams/VectorTokenStream.cpp new file mode 100644 index 0000000..41e54fc --- /dev/null +++ b/lib/parser/tokens/token_streams/VectorTokenStream.cpp @@ -0,0 +1 @@ +#include "VectorTokenStream.hpp" diff --git a/lib/parser/tokens/token_streams/VectorTokenStream.hpp b/lib/parser/tokens/token_streams/VectorTokenStream.hpp new file mode 100644 index 0000000..fd15c3d --- /dev/null +++ b/lib/parser/tokens/token_streams/VectorTokenStream.hpp @@ -0,0 +1,38 @@ +#ifndef VECTORTOKENSTREAM_HPP_ +#define VECTORTOKENSTREAM_HPP_ + +#include +#include + +#include "ITokenStream.hpp" +#include "lib/lexer/tokens/Token.hpp" + +class VectorTokenStream : public ITokenStream { +public: + explicit VectorTokenStream(std::vector tokens); + + const Token& Peek(std::size_t k = 0) override; + + TokenPtr Consume() override; + + std::size_t Position() const override; + + void Rewind(std::size_t n) override; + + bool IsEof() const override; + + const Token* LastConsumed() const override; + + const Token* TryPeek(std::size_t k = 0) override; + + std::size_t Size() const { + return tokens_.size(); + } + +private: + std::vector tokens_; + std::size_t index_ = 0; + const Token* last_ = nullptr; +}; + +#endif // VECTORTOKENSTREAM_HPP_ diff --git a/lib/parser/tokens/token_traits/ITokenMatcher.hpp b/lib/parser/tokens/token_traits/ITokenMatcher.hpp new file mode 100644 index 0000000..c8e24d5 --- /dev/null +++ b/lib/parser/tokens/token_traits/ITokenMatcher.hpp @@ -0,0 +1,13 @@ +#ifndef ITOKENMATCHER_HPP_ +#define ITOKENMATCHER_HPP_ + +#include "lib/lexer/tokens/Token.hpp" + +class ITokenMatcher { +public: + virtual ~ITokenMatcher() = default; + + virtual bool Match(const Token& t) const = 0; +}; + +#endif // ITOKENMATCHER_HPP_ diff --git a/lib/parser/tokens/token_traits/MatchIdentifier.cpp b/lib/parser/tokens/token_traits/MatchIdentifier.cpp new file mode 100644 index 0000000..6fe57af --- /dev/null +++ b/lib/parser/tokens/token_traits/MatchIdentifier.cpp @@ -0,0 +1 @@ +#include "MatchIdentifier.hpp" diff --git a/lib/parser/tokens/token_traits/MatchIdentifier.hpp b/lib/parser/tokens/token_traits/MatchIdentifier.hpp new file mode 100644 index 0000000..fea595f --- /dev/null +++ b/lib/parser/tokens/token_traits/MatchIdentifier.hpp @@ -0,0 +1,12 @@ +#ifndef MATCHIDENTIFIER_HPP_ +#define MATCHIDENTIFIER_HPP_ + +#include "ITokenMatcher.hpp" +#include "lib/lexer/tokens/Token.hpp" + +class MatchIdentifier : public ITokenMatcher { +public: + bool Match(const Token& tok) const override; +}; + +#endif // MATCHIDENTIFIER_HPP_ diff --git a/lib/parser/tokens/token_traits/MatchLexeme.cpp b/lib/parser/tokens/token_traits/MatchLexeme.cpp new file mode 100644 index 0000000..d7a6c5d --- /dev/null +++ b/lib/parser/tokens/token_traits/MatchLexeme.cpp @@ -0,0 +1 @@ +#include "MatchLexeme.hpp" diff --git a/lib/parser/tokens/token_traits/MatchLexeme.hpp b/lib/parser/tokens/token_traits/MatchLexeme.hpp new file mode 100644 index 0000000..42b71bb --- /dev/null +++ b/lib/parser/tokens/token_traits/MatchLexeme.hpp @@ -0,0 +1,19 @@ +#ifndef MATCHLEXEME_HPP_ +#define MATCHLEXEME_HPP_ + +#include + +#include "ITokenMatcher.hpp" +#include "lib/lexer/tokens/Token.hpp" + +class MatchLexeme : public ITokenMatcher { +public: + explicit MatchLexeme(std::string_view lexeme); + + bool Match(const Token& tok) const override; + +private: + std::string_view lexeme_; +}; + +#endif // MATCHLEXEME_HPP_ diff --git a/lib/parser/tokens/token_traits/MatchLiteral.cpp b/lib/parser/tokens/token_traits/MatchLiteral.cpp new file mode 100644 index 0000000..bd21b50 --- /dev/null +++ b/lib/parser/tokens/token_traits/MatchLiteral.cpp @@ -0,0 +1 @@ +#include "MatchLiteral.hpp" diff --git a/lib/parser/tokens/token_traits/MatchLiteral.hpp b/lib/parser/tokens/token_traits/MatchLiteral.hpp new file mode 100644 index 0000000..74b031a --- /dev/null +++ b/lib/parser/tokens/token_traits/MatchLiteral.hpp @@ -0,0 +1,12 @@ +#ifndef MATCHLITERAL_HPP_ +#define MATCHLITERAL_HPP_ + +#include "ITokenMatcher.hpp" +#include "lib/lexer/tokens/Token.hpp" + +class MatchLiteral : public ITokenMatcher { +public: + bool Match(const Token& tok) const override; +}; + +#endif // MATCHLITERAL_HPP_ diff --git a/lib/parser/tokens/token_traits/MatchManyOf.cpp b/lib/parser/tokens/token_traits/MatchManyOf.cpp new file mode 100644 index 0000000..56a0342 --- /dev/null +++ b/lib/parser/tokens/token_traits/MatchManyOf.cpp @@ -0,0 +1 @@ +#include "MatchManyOf.hpp" diff --git a/lib/parser/tokens/token_traits/MatchManyOf.hpp b/lib/parser/tokens/token_traits/MatchManyOf.hpp new file mode 100644 index 0000000..52df6e0 --- /dev/null +++ b/lib/parser/tokens/token_traits/MatchManyOf.hpp @@ -0,0 +1,20 @@ +#ifndef MATCHMANYOF_HPP_ +#define MATCHMANYOF_HPP_ + +#include +#include + +#include "ITokenMatcher.hpp" +#include "lib/lexer/tokens/Token.hpp" + +class MatchAnyOf : public ITokenMatcher { +public: + explicit MatchAnyOf(std::vector> ms); + + bool Match(const Token& tok) const override; + +private: + std::vector> ms_; +}; + +#endif // MATCHMANYOF_HPP_ diff --git a/lib/parser/tokens/token_traits/MatchType.cpp b/lib/parser/tokens/token_traits/MatchType.cpp new file mode 100644 index 0000000..691d217 --- /dev/null +++ b/lib/parser/tokens/token_traits/MatchType.cpp @@ -0,0 +1 @@ +#include "MatchType.hpp" diff --git a/lib/parser/tokens/token_traits/MatchType.hpp b/lib/parser/tokens/token_traits/MatchType.hpp new file mode 100644 index 0000000..736191e --- /dev/null +++ b/lib/parser/tokens/token_traits/MatchType.hpp @@ -0,0 +1,19 @@ +#ifndef MATCHTYPE_HPP_ +#define MATCHTYPE_HPP_ + +#include + +#include "ITokenMatcher.hpp" +#include "lib/lexer/tokens/Token.hpp" + +class MatchType : public ITokenMatcher { +public: + explicit MatchType(std::string_view type_name); + + bool Match(const Token& tok) const override; + +private: + std::string_view type_; +}; + +#endif // MATCHTYPE_HPP_ diff --git a/lib/parser/type_parser/ITypeParser.hpp b/lib/parser/type_parser/ITypeParser.hpp new file mode 100644 index 0000000..1b2802b --- /dev/null +++ b/lib/parser/type_parser/ITypeParser.hpp @@ -0,0 +1,16 @@ +#ifndef ITYPEPARSER_HPP_ +#define ITYPEPARSER_HPP_ + +#include + +#include "lib/parser/diagnostics/IDiagnosticSink.hpp" +#include "lib/parser/tokens/token_streams/ITokenStream.hpp" +#include "lib/parser/types/TypeReference.hpp" + +class ITypeParser { +public: + virtual ~ITypeParser() = default; + virtual std::unique_ptr ParseType(ITokenStream& ts, IDiagnosticSink& diags) = 0; +}; + +#endif // ITYPEPARSER_HPP_ diff --git a/lib/parser/type_parser/QNameTypeParser.cpp b/lib/parser/type_parser/QNameTypeParser.cpp new file mode 100644 index 0000000..b29a329 --- /dev/null +++ b/lib/parser/type_parser/QNameTypeParser.cpp @@ -0,0 +1 @@ +#include "QNameTypeParser.hpp" diff --git a/lib/parser/type_parser/QNameTypeParser.hpp b/lib/parser/type_parser/QNameTypeParser.hpp new file mode 100644 index 0000000..cd5c1ff --- /dev/null +++ b/lib/parser/type_parser/QNameTypeParser.hpp @@ -0,0 +1,23 @@ +#ifndef QNAMETYPEPARSER_HPP_ +#define QNAMETYPEPARSER_HPP_ + +#include + +#include "ITypeParser.hpp" +#include "lib/parser/ast/IAstFactory.hpp" +#include "lib/parser/diagnostics/IDiagnosticSink.hpp" +#include "lib/parser/tokens/token_streams/ITokenStream.hpp" +#include "lib/parser/types/TypeReference.hpp" + +class QNameTypeParser : public ITypeParser { +public: + explicit QNameTypeParser(IAstFactory* factory); + ~QNameTypeParser() override = default; + + std::unique_ptr ParseType(ITokenStream& ts, IDiagnosticSink& diags) override; + +private: + IAstFactory* factory_; +}; + +#endif // QNAMETYPEPARSER_HPP_ diff --git a/lib/parser/types/Param.cpp b/lib/parser/types/Param.cpp new file mode 100644 index 0000000..9fca7ad --- /dev/null +++ b/lib/parser/types/Param.cpp @@ -0,0 +1 @@ +#include "Param.hpp" diff --git a/lib/parser/types/Param.hpp b/lib/parser/types/Param.hpp new file mode 100644 index 0000000..a9ddee1 --- /dev/null +++ b/lib/parser/types/Param.hpp @@ -0,0 +1,21 @@ +#ifndef PARAM_HPP_ +#define PARAM_HPP_ + +#include + +#include "TypeReference.hpp" + +class Param { +public: + Param(std::string name, TypeReference typeReference); + + [[nodiscard]] std::string getName(); + + [[nodiscard]] TypeReference getType(); + +private: + std::string name; + TypeReference reference; +}; + +#endif // PARAM_HPP_ diff --git a/lib/parser/types/TypeReference.cpp b/lib/parser/types/TypeReference.cpp new file mode 100644 index 0000000..13a7e1b --- /dev/null +++ b/lib/parser/types/TypeReference.cpp @@ -0,0 +1 @@ +#include "TypeReference.hpp" diff --git a/lib/parser/types/TypeReference.hpp b/lib/parser/types/TypeReference.hpp new file mode 100644 index 0000000..3ce572f --- /dev/null +++ b/lib/parser/types/TypeReference.hpp @@ -0,0 +1,30 @@ +#ifndef TYPEREFERENCE_HPP_ +#define TYPEREFERENCE_HPP_ + +#include +#include + +class TypeReference { +public: + TypeReference(); + + TypeReference(std::string&& name); + + TypeReference(const TypeReference& ref); + + ~TypeReference(); + + TypeReference& operator=(const TypeReference& ref); + + TypeReference& operator=(std::string&& name); + + [[nodiscard]] std::string GetName() const; + + [[nodiscard]] std::vector GetQualifiedName() const; + +private: + std::vector qname; + bool is_nullable = false; +}; + +#endif // TYPEREFERENCE_HPP_ From 50ae1f117538ed1cd60bbf71fef787bbdd20d888 Mon Sep 17 00:00:00 2001 From: biqiboqi Date: Thu, 23 Oct 2025 12:56:46 +0300 Subject: [PATCH 02/21] fix: clang issues fixed --- lib/parser/ParserFsm.hpp | 9 ++- lib/parser/ast/AstVisitor.hpp | 79 +++++++++---------- lib/parser/ast/IAstFactory.hpp | 3 +- .../ast/nodes/class_members/CallDecl.hpp | 5 +- .../ast/nodes/exprs/literals/FloatLit.cpp | 2 +- .../ast/nodes/exprs/literals/FloatLit.hpp | 1 + .../ast/nodes/exprs/literals/IntLit.cpp | 2 +- .../ast/nodes/exprs/literals/IntLit.hpp | 1 + .../ast/nodes/exprs/literals/StringLit.cpp | 2 +- lib/parser/ast/nodes/exprs/tags/OpTags.hpp | 2 + lib/parser/ast/nodes/stmts/ReturnStmt.hpp | 2 +- lib/parser/context/ContextParser.hpp | 1 - lib/parser/diagnostics/RelatedInfo.hpp | 3 +- lib/parser/diagnostics/severity/Severity.hpp | 1 + lib/parser/pratt/specifications/InfixSpec.hpp | 9 ++- .../pratt/specifications/PostfixSpec.hpp | 1 + lib/parser/states/base/IState.hpp | 2 +- lib/parser/states/class/StateClassBody.hpp | 3 +- lib/parser/states/class/StateClassHdr.hpp | 3 +- lib/parser/types/TypeReference.hpp | 2 +- lib/preprocessor/CMakeLists.txt | 12 +-- 21 files changed, 76 insertions(+), 69 deletions(-) diff --git a/lib/parser/ParserFsm.hpp b/lib/parser/ParserFsm.hpp index 2b5f482..cb6a4c4 100644 --- a/lib/parser/ParserFsm.hpp +++ b/lib/parser/ParserFsm.hpp @@ -4,12 +4,15 @@ #include #include "IParser.hpp" +#include "ast/IAstFactory.hpp" +#include "pratt/IExpressionParser.hpp" +#include "type_parser/ITypeParser.hpp" class ParserFsm : public IParser { public: - ParserFsm(std::unique_ptr expr, - std::unique_ptr typep, - std::unique_ptr factory); + ParserFsm(std::unique_ptr expr, + std::unique_ptr typep, + std::unique_ptr factory); ~ParserFsm() override = default; diff --git a/lib/parser/ast/AstVisitor.hpp b/lib/parser/ast/AstVisitor.hpp index f8b7fdb..c071c4f 100644 --- a/lib/parser/ast/AstVisitor.hpp +++ b/lib/parser/ast/AstVisitor.hpp @@ -1,47 +1,44 @@ #ifndef ASTVISITOR_HPP_ #define ASTVISITOR_HPP_ -class Module; -class FunctionDecl; -class ClassDecl; -class InterfaceDecl; -class TypeAliasDecl; -class GlobalVarDecl; -class FieldDecl; -class StaticFieldDecl; -class MethodDecl; -class CallDecl; -class DestructorDecl; - -class Block; -class VarDeclStmt; -class ExprStmt; -class ReturnStmt; -class BreakStmt; -class ContinueStmt; -class IfStmt; -class WhileStmt; -class ForStmt; -class UnsafeBlock; - -class Binary; -class Unary; -class Assign; -class Call; -class FieldAccess; -class IndexAccess; -class NamespaceRef; -class SafeCall; -class Elvis; -class CastAs; -class TypeTestIs; -class IdentRef; -class IntLit; -class FloatLit; -class StringLit; -class CharLit; -class BoolLit; -class NullLit; +#include "nodes/class_members/CallDecl.hpp" +#include "nodes/class_members/DestructorDecl.hpp" +#include "nodes/class_members/FieldDecl.hpp" +#include "nodes/class_members/MethodDecl.hpp" +#include "nodes/class_members/StaticFieldDecl.hpp" +#include "nodes/decls/ClassDecl.hpp" +#include "nodes/decls/FunctionDecl.hpp" +#include "nodes/decls/GlobalVarDecl.hpp" +#include "nodes/decls/InterfaceDecl.hpp" +#include "nodes/decls/Module.hpp" +#include "nodes/decls/TypeAliasDecl.hpp" +#include "nodes/exprs/Assign.hpp" +#include "nodes/exprs/Binary.hpp" +#include "nodes/exprs/Call.hpp" +#include "nodes/exprs/CastAs.hpp" +#include "nodes/exprs/Elvis.hpp" +#include "nodes/exprs/FieldAccess.hpp" +#include "nodes/exprs/IdentRef.hpp" +#include "nodes/exprs/IndexAccess.hpp" +#include "nodes/exprs/NamespaceRef.hpp" +#include "nodes/exprs/SafeCall.hpp" +#include "nodes/exprs/TypeTestIs.hpp" +#include "nodes/exprs/Unary.hpp" +#include "nodes/exprs/literals/BoolLit.hpp" +#include "nodes/exprs/literals/CharLit.hpp" +#include "nodes/exprs/literals/FloatLit.hpp" +#include "nodes/exprs/literals/IntLit.hpp" +#include "nodes/exprs/literals/NullLit.hpp" +#include "nodes/exprs/literals/StringLit.hpp" +#include "nodes/stmts/BreakStmt.hpp" +#include "nodes/stmts/ContinueStmt.hpp" +#include "nodes/stmts/ExprStmt.hpp" +#include "nodes/stmts/ForStmt.hpp" +#include "nodes/stmts/IfStmt.hpp" +#include "nodes/stmts/ReturnStmt.hpp" +#include "nodes/stmts/UnsafeBlock.hpp" +#include "nodes/stmts/VarDeclStmt.hpp" +#include "nodes/stmts/WhileStmt.hpp" class AstVisitor { public: diff --git a/lib/parser/ast/IAstFactory.hpp b/lib/parser/ast/IAstFactory.hpp index e122f8b..4db64a0 100644 --- a/lib/parser/ast/IAstFactory.hpp +++ b/lib/parser/ast/IAstFactory.hpp @@ -1,5 +1,6 @@ #ifndef IASTFACTORY_HPP_ #define IASTFACTORY_HPP_ + #include #include "nodes/class_members/CallDecl.hpp" @@ -42,7 +43,7 @@ #include "nodes/stmts/WhileStmt.hpp" class IAstFactory { - public: +public: virtual ~IAstFactory() = default; virtual std::unique_ptr MakeModule() = 0; diff --git a/lib/parser/ast/nodes/class_members/CallDecl.hpp b/lib/parser/ast/nodes/class_members/CallDecl.hpp index 765c51c..75f5e38 100644 --- a/lib/parser/ast/nodes/class_members/CallDecl.hpp +++ b/lib/parser/ast/nodes/class_members/CallDecl.hpp @@ -5,9 +5,10 @@ #include #include "lib/parser/ast/nodes/base/Decl.hpp" +#include "lib/parser/ast/nodes/stmts/Block.hpp" +#include "lib/parser/types/Param.hpp" #include "lib/parser/types/TypeReference.hpp" -class Param; class CallDecl : public Decl { public: void Accept(AstVisitor& v) override; @@ -16,7 +17,7 @@ class CallDecl : public Decl { bool is_public_ = true; std::vector params_; std::unique_ptr ret_type_; - std::unique_ptr body_; + std::unique_ptr body_; }; #endif // CALLDECL_HPP_ diff --git a/lib/parser/ast/nodes/exprs/literals/FloatLit.cpp b/lib/parser/ast/nodes/exprs/literals/FloatLit.cpp index 2bebaaa..98631a4 100644 --- a/lib/parser/ast/nodes/exprs/literals/FloatLit.cpp +++ b/lib/parser/ast/nodes/exprs/literals/FloatLit.cpp @@ -1 +1 @@ -#include "../FloatLit.hpp" +#include "FloatLit.hpp" diff --git a/lib/parser/ast/nodes/exprs/literals/FloatLit.hpp b/lib/parser/ast/nodes/exprs/literals/FloatLit.hpp index 37dde1b..abaea73 100644 --- a/lib/parser/ast/nodes/exprs/literals/FloatLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/FloatLit.hpp @@ -1,5 +1,6 @@ #ifndef FLOATLIT_HPP_ #define FLOATLIT_HPP_ + #include "lib/parser/ast/nodes/base/Expr.hpp" class FloatLit : public Expr { diff --git a/lib/parser/ast/nodes/exprs/literals/IntLit.cpp b/lib/parser/ast/nodes/exprs/literals/IntLit.cpp index 9a4f39e..ad568ba 100644 --- a/lib/parser/ast/nodes/exprs/literals/IntLit.cpp +++ b/lib/parser/ast/nodes/exprs/literals/IntLit.cpp @@ -1 +1 @@ -#include "../IntLit.hpp" +#include "IntLit.hpp" diff --git a/lib/parser/ast/nodes/exprs/literals/IntLit.hpp b/lib/parser/ast/nodes/exprs/literals/IntLit.hpp index f41454c..1c7c814 100644 --- a/lib/parser/ast/nodes/exprs/literals/IntLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/IntLit.hpp @@ -1,5 +1,6 @@ #ifndef INTLIT_HPP_ #define INTLIT_HPP_ + #include "lib/parser/ast/nodes/base/Expr.hpp" class IntLit : public Expr { diff --git a/lib/parser/ast/nodes/exprs/literals/StringLit.cpp b/lib/parser/ast/nodes/exprs/literals/StringLit.cpp index 212dbf1..5e45622 100644 --- a/lib/parser/ast/nodes/exprs/literals/StringLit.cpp +++ b/lib/parser/ast/nodes/exprs/literals/StringLit.cpp @@ -1 +1 @@ -#include "../StringLit.hpp" +#include "StringLit.hpp" diff --git a/lib/parser/ast/nodes/exprs/tags/OpTags.hpp b/lib/parser/ast/nodes/exprs/tags/OpTags.hpp index 75559f3..e41e2a2 100644 --- a/lib/parser/ast/nodes/exprs/tags/OpTags.hpp +++ b/lib/parser/ast/nodes/exprs/tags/OpTags.hpp @@ -1,10 +1,12 @@ #ifndef OPTAGS_HPP_ #define OPTAGS_HPP_ + #include "IAssignOpTag.hpp" #include "IBinaryOpTag.hpp" #include "IUnaryOpTag.hpp" namespace OpTags { +// binary static const IBinaryOpTag& Add(); static const IBinaryOpTag& Sub(); static const IBinaryOpTag& Mul(); diff --git a/lib/parser/ast/nodes/stmts/ReturnStmt.hpp b/lib/parser/ast/nodes/stmts/ReturnStmt.hpp index da615c4..5ff8473 100644 --- a/lib/parser/ast/nodes/stmts/ReturnStmt.hpp +++ b/lib/parser/ast/nodes/stmts/ReturnStmt.hpp @@ -3,9 +3,9 @@ #include +#include "lib/parser/ast/nodes/base/Expr.hpp" #include "lib/parser/ast/nodes/base/Stmt.hpp" -class Expr; class ReturnStmt : public Stmt { public: void Accept(AstVisitor& v) override; diff --git a/lib/parser/context/ContextParser.hpp b/lib/parser/context/ContextParser.hpp index 69b9dab..4fffc86 100644 --- a/lib/parser/context/ContextParser.hpp +++ b/lib/parser/context/ContextParser.hpp @@ -3,7 +3,6 @@ #include -#include "../states/IState.hpp" #include "NodeEntry.hpp" #include "lib/parser/diagnostics/IDiagnosticSink.hpp" #include "lib/parser/states/base/IState.hpp" diff --git a/lib/parser/diagnostics/RelatedInfo.hpp b/lib/parser/diagnostics/RelatedInfo.hpp index 71acd7d..8abee5f 100644 --- a/lib/parser/diagnostics/RelatedInfo.hpp +++ b/lib/parser/diagnostics/RelatedInfo.hpp @@ -8,7 +8,8 @@ class RelatedInfo { public: - // TODO: make methods + RelatedInfo(std::string&& message, SourceSpan&& span); + private: std::string message_; std::optional where_; diff --git a/lib/parser/diagnostics/severity/Severity.hpp b/lib/parser/diagnostics/severity/Severity.hpp index 64b2780..70aada2 100644 --- a/lib/parser/diagnostics/severity/Severity.hpp +++ b/lib/parser/diagnostics/severity/Severity.hpp @@ -1,5 +1,6 @@ #ifndef SEVERITY_HPP_ #define SEVERITY_HPP_ + #include "ISeverity.hpp" class Severity { diff --git a/lib/parser/pratt/specifications/InfixSpec.hpp b/lib/parser/pratt/specifications/InfixSpec.hpp index 0c6fca5..f667581 100644 --- a/lib/parser/pratt/specifications/InfixSpec.hpp +++ b/lib/parser/pratt/specifications/InfixSpec.hpp @@ -8,14 +8,15 @@ class InfixSpec { public: + InfixSpec(int lbp, int rbp, bool right_associative, IBinaryOpTag* tag, bool is_elvis); // TODO: implement getters and setters private: std::function match_; - int lbp = 0; - int rbp = 0; - bool right_associative = false; + int lbp_ = 0; + int rbp_ = 0; + bool right_associative_ = false; const IBinaryOpTag* tag_ = nullptr; - bool is_elvis = false; + bool is_elvis_ = false; }; #endif // INFIXSPEC_HPP_ diff --git a/lib/parser/pratt/specifications/PostfixSpec.hpp b/lib/parser/pratt/specifications/PostfixSpec.hpp index d6cfaec..922db19 100644 --- a/lib/parser/pratt/specifications/PostfixSpec.hpp +++ b/lib/parser/pratt/specifications/PostfixSpec.hpp @@ -7,6 +7,7 @@ class PostfixSpec { public: + PostfixSpec(std::function match, int bp, bool keyword); // TODO: implements getters and setters private: std::function match_; diff --git a/lib/parser/states/base/IState.hpp b/lib/parser/states/base/IState.hpp index ee3d794..58211e5 100644 --- a/lib/parser/states/base/IState.hpp +++ b/lib/parser/states/base/IState.hpp @@ -8,7 +8,7 @@ class IState { public: virtual ~IState() = default; virtual const char* Name() const = 0; - virtual bool Step(class ContextParser& ctx, ITokenStream& ts) = 0; + virtual bool Step(ContextParser& ctx, ITokenStream& ts) = 0; }; #endif // ISTATE_HPP_ diff --git a/lib/parser/states/class/StateClassBody.hpp b/lib/parser/states/class/StateClassBody.hpp index 3f5e2dc..b6b9937 100644 --- a/lib/parser/states/class/StateClassBody.hpp +++ b/lib/parser/states/class/StateClassBody.hpp @@ -1,8 +1,7 @@ #ifndef STATECLASSBODY_HPP_ #define STATECLASSBODY_HPP_ -#include "../base/StateBase.hpp" -#include "lib/parser/context/ContextParser.hpp" +#include "lib/parser/states/base/StateBase.hpp" #include "lib/parser/tokens/token_streams/ITokenStream.hpp" class StateClassBody : public StateBase { diff --git a/lib/parser/states/class/StateClassHdr.hpp b/lib/parser/states/class/StateClassHdr.hpp index c99c9cd..0d152cc 100644 --- a/lib/parser/states/class/StateClassHdr.hpp +++ b/lib/parser/states/class/StateClassHdr.hpp @@ -1,8 +1,7 @@ #ifndef STATECLASSHDR_HPP_ #define STATECLASSHDR_HPP_ -#include "../base/StateBase.hpp" -#include "lib/parser/context/ContextParser.hpp" +#include "lib/parser/states/base/StateBase.hpp" #include "lib/parser/tokens/token_streams/ITokenStream.hpp" class StateClassHdr : public StateBase { diff --git a/lib/parser/types/TypeReference.hpp b/lib/parser/types/TypeReference.hpp index 3ce572f..f99d7c9 100644 --- a/lib/parser/types/TypeReference.hpp +++ b/lib/parser/types/TypeReference.hpp @@ -8,7 +8,7 @@ class TypeReference { public: TypeReference(); - TypeReference(std::string&& name); + explicit TypeReference(std::string&& name); TypeReference(const TypeReference& ref); diff --git a/lib/preprocessor/CMakeLists.txt b/lib/preprocessor/CMakeLists.txt index 07eaffa..329f125 100644 --- a/lib/preprocessor/CMakeLists.txt +++ b/lib/preprocessor/CMakeLists.txt @@ -1,14 +1,14 @@ add_library(preprocessor STATIC - Preprocessor.cpp - token_processor_factory.cpp + Preprocessor.cpp + token_processor_factory.cpp ) target_include_directories(preprocessor - PUBLIC - ${CMAKE_SOURCE_DIR} + PUBLIC + ${CMAKE_SOURCE_DIR} ) target_include_directories(preprocessor - PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR} + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} ) From 4ac3bd2895b642dff139783f0175df207f0986e5 Mon Sep 17 00:00:00 2001 From: biqiboqi Date: Thu, 23 Oct 2025 13:03:32 +0300 Subject: [PATCH 03/21] refactor: SPACES --- lib/parser/ast/nodes/class_members/MethodDecl.hpp | 3 +-- lib/parser/ast/nodes/decls/FunctionDecl.hpp | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/parser/ast/nodes/class_members/MethodDecl.hpp b/lib/parser/ast/nodes/class_members/MethodDecl.hpp index d6885d7..b6ac3f9 100644 --- a/lib/parser/ast/nodes/class_members/MethodDecl.hpp +++ b/lib/parser/ast/nodes/class_members/MethodDecl.hpp @@ -12,7 +12,6 @@ class MethodDecl : public Decl { public: - void Accept(AstVisitor& v) override; private: @@ -24,7 +23,7 @@ class MethodDecl : public Decl { std::string name; std::vector params; std::unique_ptr ret_type; - std::unique_ptr body; // optional + std::unique_ptr body; // optional }; #endif // METHODDECL_HPP_ diff --git a/lib/parser/ast/nodes/decls/FunctionDecl.hpp b/lib/parser/ast/nodes/decls/FunctionDecl.hpp index 4065d06..e7d75f5 100644 --- a/lib/parser/ast/nodes/decls/FunctionDecl.hpp +++ b/lib/parser/ast/nodes/decls/FunctionDecl.hpp @@ -18,7 +18,7 @@ class FunctionDecl : public Decl { std::string name_; std::vector params_; std::unique_ptr return_type_; // optional - std::unique_ptr body_; // optional + std::unique_ptr body_; // optional }; #endif // FUNCTIONDECL_HPP_ From dd4dd26b6dbbaf449b90f8de58ce97ddd225b48c Mon Sep 17 00:00:00 2001 From: biqiboqi Date: Thu, 23 Oct 2025 14:20:28 +0300 Subject: [PATCH 04/21] refactor: IState forward declaration fix --- lib/parser/states/base/IState.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/parser/states/base/IState.hpp b/lib/parser/states/base/IState.hpp index 58211e5..66a6814 100644 --- a/lib/parser/states/base/IState.hpp +++ b/lib/parser/states/base/IState.hpp @@ -1,9 +1,10 @@ #ifndef ISTATE_HPP_ #define ISTATE_HPP_ -#include "lib/parser/context/ContextParser.hpp" #include "lib/parser/tokens/token_streams/ITokenStream.hpp" +class ContextParser; // forward + class IState { public: virtual ~IState() = default; From 771ef204a67b8064da05567fd3549050dd878af7 Mon Sep 17 00:00:00 2001 From: biqiboqi Date: Sun, 2 Nov 2025 23:15:14 +0300 Subject: [PATCH 05/21] feat: afterreview fixes --- lib/lexer/tokens/TokenPosition.cpp | 19 ++++ lib/lexer/tokens/TokenPosition.hpp | 8 +- lib/parser/ast/AstVisitor.hpp | 2 +- lib/parser/ast/IAstFactory.hpp | 3 +- lib/parser/ast/nodes/decls/InterfaceDecl.hpp | 9 +- .../ast/nodes/decls/InterfaceMethod.cpp | 1 + .../ast/nodes/decls/InterfaceMethod.hpp | 35 ++++++++ lib/parser/ast/nodes/decls/Module.cpp | 1 - lib/parser/ast/nodes/decls/Module.hpp | 21 ----- lib/parser/ast/nodes/exprs/SafeCall.hpp | 26 +++++- .../ast/nodes/exprs/literals/CharLit.hpp | 2 +- .../ast/nodes/exprs/literals/FloatLit.hpp | 2 +- .../ast/nodes/exprs/literals/IntLit.hpp | 3 +- .../ast/nodes/exprs/literals/StringLit.hpp | 2 +- .../ast/nodes/exprs/tags/IAssignOpTag.hpp | 2 +- .../ast/nodes/exprs/tags/IBinaryOpTag.hpp | 2 +- .../ast/nodes/exprs/tags/IUnaryOpTag.hpp | 2 +- lib/parser/ast/nodes/exprs/tags/OpTags.hpp | 38 ++++---- lib/parser/ast/nodes/stmts/Branch.hpp | 27 +++--- lib/parser/ast/nodes/stmts/IfStmt.hpp | 21 +++-- lib/parser/ast/nodes/stmts/ReturnStmt.hpp | 9 +- lib/parser/context/ContextParser.hpp | 24 ++--- lib/parser/diagnostics/Diagnostic.hpp | 58 +++++++++--- .../diagnostics/DiagnosticCollector.hpp | 20 +++-- lib/parser/diagnostics/severity/Severity.hpp | 10 ++- lib/parser/pratt/DefaultOperatorResolver.hpp | 8 +- lib/parser/pratt/IOperatorResolver.hpp | 17 ++-- lib/parser/pratt/PrattExpressionParser.hpp | 7 +- lib/parser/states/StateBlock.hpp | 4 +- lib/parser/states/StateCallDeclHdr.hpp | 4 +- lib/parser/states/StateDestructorDecl.hpp | 4 +- lib/parser/states/StateExpr.hpp | 4 +- lib/parser/states/StateFieldDecl.hpp | 4 +- lib/parser/states/StateForHead.hpp | 4 +- lib/parser/states/StateGlobalVarDecl.hpp | 4 +- lib/parser/states/StateIfHead.hpp | 4 +- lib/parser/states/StateIfTail.hpp | 4 +- lib/parser/states/StateMethodHdr.hpp | 4 +- lib/parser/states/StateModule.hpp | 4 +- lib/parser/states/StateParseType.hpp | 4 +- lib/parser/states/StateReturnTail.hpp | 4 +- lib/parser/states/StateStmt.hpp | 4 +- lib/parser/states/StateSyncToBlockEnd.hpp | 4 +- lib/parser/states/StateSyncToStmtEnd.hpp | 4 +- lib/parser/states/StateTopDecl.hpp | 4 +- lib/parser/states/StateTypeAliasDecl.hpp | 4 +- lib/parser/states/StateUnsafeBlock.hpp | 4 +- lib/parser/states/StateVarDeclTail.hpp | 4 +- lib/parser/states/StateWhileHead.hpp | 4 +- lib/parser/states/base/IState.hpp | 9 +- lib/parser/states/base/StateBase.hpp | 1 + lib/parser/states/base/StateError.cpp | 1 + lib/parser/states/base/StateError.hpp | 16 ++++ lib/parser/states/class/StateClassBody.hpp | 4 +- lib/parser/states/class/StateClassHdr.hpp | 4 +- lib/parser/states/class/StateClassMember.hpp | 4 +- lib/parser/states/func/StateFuncBody.hpp | 4 +- lib/parser/states/func/StateFuncHdr.hpp | 4 +- lib/parser/states/func/StateFuncParams.hpp | 4 +- .../states/interface/StateInterfaceBody.hpp | 4 +- .../states/interface/StateInterfaceDecl.hpp | 4 +- .../states/interface/StateInterfaceHdr.hpp | 4 +- lib/parser/tokens/SourceId.cpp | 1 + lib/parser/tokens/SourceId.hpp | 26 ++++++ lib/parser/tokens/SourceSpan.hpp | 20 +++-- .../tokens/token_streams/ITokenStream.hpp | 2 +- .../token_streams/VectorTokenStream.hpp | 15 ++-- lib/parser/type_parser/QNameTypeParser.hpp | 14 ++- lib/parser/types/Nullable.cpp | 1 + lib/parser/types/Nullable.hpp | 22 +++++ lib/parser/types/Param.hpp | 8 +- lib/parser/types/ResolvedTypeHandle.cpp | 1 + lib/parser/types/ResolvedTypeHandle.hpp | 29 ++++++ lib/parser/types/TypeReference.hpp | 90 +++++++++++++++++-- 74 files changed, 549 insertions(+), 206 deletions(-) create mode 100644 lib/parser/ast/nodes/decls/InterfaceMethod.cpp create mode 100644 lib/parser/ast/nodes/decls/InterfaceMethod.hpp delete mode 100644 lib/parser/ast/nodes/decls/Module.cpp delete mode 100644 lib/parser/ast/nodes/decls/Module.hpp create mode 100644 lib/parser/states/base/StateError.cpp create mode 100644 lib/parser/states/base/StateError.hpp create mode 100644 lib/parser/tokens/SourceId.cpp create mode 100644 lib/parser/tokens/SourceId.hpp create mode 100644 lib/parser/types/Nullable.cpp create mode 100644 lib/parser/types/Nullable.hpp create mode 100644 lib/parser/types/ResolvedTypeHandle.cpp create mode 100644 lib/parser/types/ResolvedTypeHandle.hpp diff --git a/lib/lexer/tokens/TokenPosition.cpp b/lib/lexer/tokens/TokenPosition.cpp index 2c9dcec..0f077c1 100644 --- a/lib/lexer/tokens/TokenPosition.cpp +++ b/lib/lexer/tokens/TokenPosition.cpp @@ -1,5 +1,8 @@ #include "TokenPosition.hpp" +TokenPosition::TokenPosition() : line_(0), column_(0) { +} + TokenPosition::TokenPosition(int32_t line, int32_t column) : line_(line), column_(column) { } @@ -10,3 +13,19 @@ int32_t TokenPosition::GetLine() const noexcept { int32_t TokenPosition::GetColumn() const noexcept { return column_; } + +bool TokenPosition::IsValid() const noexcept { + return (line_ > 0) && (column_ > 0); +} + +TokenPosition TokenPosition::Invalid() noexcept { + return {-1, -1}; +} + +bool TokenPosition::operator<(const TokenPosition& rhs) const noexcept { + return (line_ < rhs.line_) || (line_ == rhs.line_ && column_ < rhs.column_); +} + +bool TokenPosition::operator==(const TokenPosition& rhs) const noexcept { + return line_ == rhs.line_ && column_ == rhs.column_; +} diff --git a/lib/lexer/tokens/TokenPosition.hpp b/lib/lexer/tokens/TokenPosition.hpp index 2b2be92..8a16337 100644 --- a/lib/lexer/tokens/TokenPosition.hpp +++ b/lib/lexer/tokens/TokenPosition.hpp @@ -5,12 +5,18 @@ class TokenPosition { public: + TokenPosition(); TokenPosition(int32_t line, int32_t column); [[nodiscard]] int32_t GetLine() const noexcept; - [[nodiscard]] int32_t GetColumn() const noexcept; + [[nodiscard]] bool IsValid() const noexcept; + [[nodiscard]] static TokenPosition Invalid() noexcept; + + [[nodiscard]] bool operator<(const TokenPosition& rhs) const noexcept; + [[nodiscard]] bool operator==(const TokenPosition& rhs) const noexcept; + private: int32_t line_; int32_t column_; diff --git a/lib/parser/ast/AstVisitor.hpp b/lib/parser/ast/AstVisitor.hpp index c071c4f..4bbb7eb 100644 --- a/lib/parser/ast/AstVisitor.hpp +++ b/lib/parser/ast/AstVisitor.hpp @@ -45,9 +45,9 @@ class AstVisitor { virtual ~AstVisitor() = default; // Decls - virtual void Visit(Module&) = 0; virtual void Visit(FunctionDecl&) = 0; virtual void Visit(ClassDecl&) = 0; + virtual void visit(InterfaceMethod&) = 0; virtual void Visit(InterfaceDecl&) = 0; virtual void Visit(TypeAliasDecl&) = 0; virtual void Visit(GlobalVarDecl&) = 0; diff --git a/lib/parser/ast/IAstFactory.hpp b/lib/parser/ast/IAstFactory.hpp index 4db64a0..f4f96c8 100644 --- a/lib/parser/ast/IAstFactory.hpp +++ b/lib/parser/ast/IAstFactory.hpp @@ -46,11 +46,10 @@ class IAstFactory { public: virtual ~IAstFactory() = default; - virtual std::unique_ptr MakeModule() = 0; - virtual std::unique_ptr MakeFunction() = 0; virtual std::unique_ptr MakeClass() = 0; virtual std::unique_ptr MakeInterface() = 0; + virtual std::unique_ptr MakeInterfaceMethod() = 0; virtual std::unique_ptr MakeTypeAlias() = 0; virtual std::unique_ptr MakeGlobalVar() = 0; diff --git a/lib/parser/ast/nodes/decls/InterfaceDecl.hpp b/lib/parser/ast/nodes/decls/InterfaceDecl.hpp index bb5289e..45b95c6 100644 --- a/lib/parser/ast/nodes/decls/InterfaceDecl.hpp +++ b/lib/parser/ast/nodes/decls/InterfaceDecl.hpp @@ -5,15 +5,22 @@ #include #include +#include "InterfaceMethod.hpp" #include "lib/parser/ast/nodes/base/Decl.hpp" class InterfaceDecl : public Decl { public: void Accept(AstVisitor& v) override; + const std::string& Name() const noexcept; + void SetName(std::string n); + + const std::vector>& Members() const noexcept; + std::vector>& MutableMembers() noexcept; + private: std::string name_; - std::vector> members_; + std::vector> methods_; }; #endif // INTERFACEDECL_HPP_ diff --git a/lib/parser/ast/nodes/decls/InterfaceMethod.cpp b/lib/parser/ast/nodes/decls/InterfaceMethod.cpp new file mode 100644 index 0000000..d0e5786 --- /dev/null +++ b/lib/parser/ast/nodes/decls/InterfaceMethod.cpp @@ -0,0 +1 @@ +#include "InterfaceMethod.hpp" diff --git a/lib/parser/ast/nodes/decls/InterfaceMethod.hpp b/lib/parser/ast/nodes/decls/InterfaceMethod.hpp new file mode 100644 index 0000000..5bbfdf6 --- /dev/null +++ b/lib/parser/ast/nodes/decls/InterfaceMethod.hpp @@ -0,0 +1,35 @@ +#ifndef INTERFACEMETHOD_HPP_ +#define INTERFACEMETHOD_HPP_ + +#include +#include + +#include "lib/parser/ast/nodes/base/AstNode.hpp" +#include "lib/parser/ast/nodes/base/Decl.hpp" +#include "lib/parser/types/TypeReference.hpp" + +class InterfaceMethod : public Decl { +public: + struct Param { + std::string name; + TypeReference type; + }; + + void Accept(AstVisitor& v) override; + + const std::string& Name() const noexcept; + void SetName(std::string n); + + const std::vector& Params() const noexcept; + std::vector& MutableParams() noexcept; + + const TypeReference* ReturnType() const noexcept; + void SetReturnType(std::unique_ptr t); + +private: + std::string name_; + std::vector params_; + std::unique_ptr ret_type_; +}; + +#endif // INTERFACEMETHOD_HPP_ diff --git a/lib/parser/ast/nodes/decls/Module.cpp b/lib/parser/ast/nodes/decls/Module.cpp deleted file mode 100644 index 6f69fe8..0000000 --- a/lib/parser/ast/nodes/decls/Module.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "Module.hpp" diff --git a/lib/parser/ast/nodes/decls/Module.hpp b/lib/parser/ast/nodes/decls/Module.hpp deleted file mode 100644 index ef03d1e..0000000 --- a/lib/parser/ast/nodes/decls/Module.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef MODULE_HPP_ -#define MODULE_HPP_ - -#include -#include - -#include "lib/parser/ast/nodes/base/AstNode.hpp" -#include "lib/parser/ast/nodes/base/Decl.hpp" - -class Module : public AstNode { -public: - void Accept(AstVisitor& v) override; - - [[nodiscard]] std::vector>& GetDecls(); - [[nodiscard]] const std::vector>& GetDecls() const; - -private: - std::vector> decls_; -}; - -#endif // MODULE_HPP_ diff --git a/lib/parser/ast/nodes/exprs/SafeCall.hpp b/lib/parser/ast/nodes/exprs/SafeCall.hpp index cbd5981..4e9728f 100644 --- a/lib/parser/ast/nodes/exprs/SafeCall.hpp +++ b/lib/parser/ast/nodes/exprs/SafeCall.hpp @@ -9,8 +9,32 @@ class SafeCall : public Expr { public: void Accept(AstVisitor& v) override; + [[nodiscard]] const Expr* Object() const noexcept; + [[nodiscard]] Expr* MutableObject() noexcept; + void SetObject(std::unique_ptr obj); + std::unique_ptr ReplaceObject(std::unique_ptr obj); + + [[nodiscard]] const std::string& Method() const noexcept; + void SetMethod(std::string m); + + [[nodiscard]] const std::vector>& Args() const noexcept; + [[nodiscard]] std::vector>& MutableArgs() noexcept; + void AddArg(std::unique_ptr e); + + void ClearArgs(); + + [[nodiscard]] bool IsNullPropagating() const noexcept; + + void SetInferredType(TypeReference t); + [[nodiscard]] const TypeReference* InferredType() const noexcept; + private: - std::unique_ptr base; // TODO: idk how to implement it now... + std::unique_ptr object_; + std::string method_; + std::vector> args_; + + TypeReference inferred_type_; + bool inferred_type_set_ = false; }; #endif // SAFECALL_HPP_ diff --git a/lib/parser/ast/nodes/exprs/literals/CharLit.hpp b/lib/parser/ast/nodes/exprs/literals/CharLit.hpp index 2bf310a..54aa309 100644 --- a/lib/parser/ast/nodes/exprs/literals/CharLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/CharLit.hpp @@ -8,7 +8,7 @@ class CharLit : public Expr { void Accept(AstVisitor& v) override; private: - char32_t value_ = U'\0'; + char value_ = '\0'; }; #endif // CHARLIT_HPP_ diff --git a/lib/parser/ast/nodes/exprs/literals/FloatLit.hpp b/lib/parser/ast/nodes/exprs/literals/FloatLit.hpp index abaea73..f9dd921 100644 --- a/lib/parser/ast/nodes/exprs/literals/FloatLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/FloatLit.hpp @@ -8,7 +8,7 @@ class FloatLit : public Expr { void Accept(AstVisitor& v) override; private: - long double value = 0.0L; + double value = 0.0L; }; #endif // FLOATLIT_HPP_ diff --git a/lib/parser/ast/nodes/exprs/literals/IntLit.hpp b/lib/parser/ast/nodes/exprs/literals/IntLit.hpp index 1c7c814..4b51822 100644 --- a/lib/parser/ast/nodes/exprs/literals/IntLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/IntLit.hpp @@ -8,6 +8,7 @@ class IntLit : public Expr { void Accept(AstVisitor& v) override; private: - long long value = 0; + int64_t value = 0; }; + #endif // INTLIT_HPP_ diff --git a/lib/parser/ast/nodes/exprs/literals/StringLit.hpp b/lib/parser/ast/nodes/exprs/literals/StringLit.hpp index 139ea0b..ddc91d3 100644 --- a/lib/parser/ast/nodes/exprs/literals/StringLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/StringLit.hpp @@ -10,7 +10,7 @@ class StringLit : public Expr { void Accept(AstVisitor& v) override; private: - std::u32string value_; + std::string value_; }; #endif // STRINGLIT_HPP_ diff --git a/lib/parser/ast/nodes/exprs/tags/IAssignOpTag.hpp b/lib/parser/ast/nodes/exprs/tags/IAssignOpTag.hpp index d003fb0..cad7082 100644 --- a/lib/parser/ast/nodes/exprs/tags/IAssignOpTag.hpp +++ b/lib/parser/ast/nodes/exprs/tags/IAssignOpTag.hpp @@ -5,7 +5,7 @@ class IAssignOpTag { public: virtual ~IAssignOpTag() = default; - virtual const char* Name() const = 0; + virtual std::string_view Name() const = 0; }; #endif // IASSIGNOPTAG_HPP_ diff --git a/lib/parser/ast/nodes/exprs/tags/IBinaryOpTag.hpp b/lib/parser/ast/nodes/exprs/tags/IBinaryOpTag.hpp index 9d456c3..b82f5a1 100644 --- a/lib/parser/ast/nodes/exprs/tags/IBinaryOpTag.hpp +++ b/lib/parser/ast/nodes/exprs/tags/IBinaryOpTag.hpp @@ -5,7 +5,7 @@ class IBinaryOpTag { public: virtual ~IBinaryOpTag() = default; - virtual const char* Name() const = 0; + virtual std::string_view Name() const = 0; }; #endif // IBINARYOPTAG_HPP_ diff --git a/lib/parser/ast/nodes/exprs/tags/IUnaryOpTag.hpp b/lib/parser/ast/nodes/exprs/tags/IUnaryOpTag.hpp index c4bd51f..3eb72e8 100644 --- a/lib/parser/ast/nodes/exprs/tags/IUnaryOpTag.hpp +++ b/lib/parser/ast/nodes/exprs/tags/IUnaryOpTag.hpp @@ -5,7 +5,7 @@ class IUnaryOpTag { public: virtual ~IUnaryOpTag() = default; - virtual const char* Name() const = 0; + virtual std::string_view Name() const = 0; }; #endif // IUNARYOPTAG_HPP_ diff --git a/lib/parser/ast/nodes/exprs/tags/OpTags.hpp b/lib/parser/ast/nodes/exprs/tags/OpTags.hpp index e41e2a2..e80a0ce 100644 --- a/lib/parser/ast/nodes/exprs/tags/OpTags.hpp +++ b/lib/parser/ast/nodes/exprs/tags/OpTags.hpp @@ -7,29 +7,29 @@ namespace OpTags { // binary -static const IBinaryOpTag& Add(); -static const IBinaryOpTag& Sub(); -static const IBinaryOpTag& Mul(); -static const IBinaryOpTag& Div(); -static const IBinaryOpTag& Mod(); -static const IBinaryOpTag& Lt(); -static const IBinaryOpTag& Le(); -static const IBinaryOpTag& Gt(); -static const IBinaryOpTag& Ge(); -static const IBinaryOpTag& Eq(); -static const IBinaryOpTag& Ne(); -static const IBinaryOpTag& And(); -static const IBinaryOpTag& Or(); -static const IBinaryOpTag& Xor(); +const IBinaryOpTag& Add(); +const IBinaryOpTag& Sub(); +const IBinaryOpTag& Mul(); +const IBinaryOpTag& Div(); +const IBinaryOpTag& Mod(); +const IBinaryOpTag& Lt(); +const IBinaryOpTag& Le(); +const IBinaryOpTag& Gt(); +const IBinaryOpTag& Ge(); +const IBinaryOpTag& Eq(); +const IBinaryOpTag& Ne(); +const IBinaryOpTag& And(); +const IBinaryOpTag& Or(); +const IBinaryOpTag& Xor(); // unary -static const IUnaryOpTag& Neg(); -static const IUnaryOpTag& Plus(); -static const IUnaryOpTag& Not(); +const IUnaryOpTag& Neg(); +const IUnaryOpTag& Plus(); +const IUnaryOpTag& Not(); // assign -static const IAssignOpTag& RefAssign(); // = -static const IAssignOpTag& CopyAssign(); // := +const IAssignOpTag& RefAssign(); // = +const IAssignOpTag& CopyAssign(); // := } // namespace OpTags #endif // OPTAGS_HPP_ diff --git a/lib/parser/ast/nodes/stmts/Branch.hpp b/lib/parser/ast/nodes/stmts/Branch.hpp index 3008abd..3569658 100644 --- a/lib/parser/ast/nodes/stmts/Branch.hpp +++ b/lib/parser/ast/nodes/stmts/Branch.hpp @@ -6,17 +6,24 @@ #include "Block.hpp" #include "lib/parser/ast/nodes/base/Expr.hpp" -class Branch { +class Branch Branch { public: - Branch(std::unique_ptr expr, std::unique_ptr block); - - [[nodiscard]] std::unique_ptr GetBlock(); - - void AddBlock(std::unique_ptr block); - - [[nodiscard]] std::unique_ptr GetExpr(); - - void AddExpr(std::unique_ptr expr); + Branch(std::unique_ptr condition, std::unique_ptr then_block); + Branch(const Branch&) = delete; + Branch& operator=(const Branch&) = delete; + Branch(Branch&&) noexcept = default; + Branch& operator=(Branch&&) noexcept = default; + ~Branch() = default; + + const Expr* Condition() const noexcept; + Expr* MutableCondition() noexcept; + void SetCondition(std::unique_ptr e); + std::unique_ptr ReleaseCondition(); + + const Block* Then() const noexcept; + Block* MutableThen() noexcept; + void SetThen(std::unique_ptr b); + std::unique_ptr ReleaseThen(); private: std::unique_ptr condition_; diff --git a/lib/parser/ast/nodes/stmts/IfStmt.hpp b/lib/parser/ast/nodes/stmts/IfStmt.hpp index 39e4266..b78ffc9 100644 --- a/lib/parser/ast/nodes/stmts/IfStmt.hpp +++ b/lib/parser/ast/nodes/stmts/IfStmt.hpp @@ -5,20 +5,29 @@ #include #include "Block.hpp" +#include "Branch.hpp" #include "lib/parser/ast/nodes/base/Expr.hpp" #include "lib/parser/ast/nodes/base/Stmt.hpp" class IfStmt : public Stmt { public: - struct Branch { - std::unique_ptr cond; - std::unique_ptr then_block; - }; - void Accept(AstVisitor& v) override; + const std::vector& Branches() const noexcept; + std::vector& MutableBranches() noexcept; + void AddBranch(Branch b); + void EmplaceBranch(std::unique_ptr cond, std::unique_ptr then_blk); + + const Block* ElseBlock() const noexcept; + Block* MutableElseBlock() noexcept; + void SetElseBlock(std::unique_ptr b); + std::unique_ptr ReleaseElseBlock(); + + bool HasElse() const noexcept; + bool Empty() const noexcept; + private: - std::vector branches_; // if + else-if* + std::vector branches_; std::unique_ptr else_block_; }; diff --git a/lib/parser/ast/nodes/stmts/ReturnStmt.hpp b/lib/parser/ast/nodes/stmts/ReturnStmt.hpp index 5ff8473..a2ba0bb 100644 --- a/lib/parser/ast/nodes/stmts/ReturnStmt.hpp +++ b/lib/parser/ast/nodes/stmts/ReturnStmt.hpp @@ -10,8 +10,15 @@ class ReturnStmt : public Stmt { public: void Accept(AstVisitor& v) override; + bool HasValue() const noexcept; + const Expr* Value() const noexcept; + Expr* MutableValue() noexcept; + void SetValue(std::unique_ptr e); + void ResetValue(); + std::unique_ptr ReleaseValue(); + private: - std::unique_ptr value_; // optional + std::optional> value_; }; #endif // RETURNSTMT_HPP_ diff --git a/lib/parser/context/ContextParser.hpp b/lib/parser/context/ContextParser.hpp index 4fffc86..ccffc6f 100644 --- a/lib/parser/context/ContextParser.hpp +++ b/lib/parser/context/ContextParser.hpp @@ -1,14 +1,20 @@ #ifndef CONTEXTPARSER_HPP_ #define CONTEXTPARSER_HPP_ +#include +#include #include #include "NodeEntry.hpp" #include "lib/parser/diagnostics/IDiagnosticSink.hpp" #include "lib/parser/states/base/IState.hpp" -class IExpressionParser; // forward -class ITypeParser; // forward +class AstNode; +class IExpressionParser; +class ITypeParser; + +template +concept AstNodeDerived = std::is_base_of_v; class ContextParser { public: @@ -26,19 +32,17 @@ class ContextParser { void PushState(const IState& state); void PopState(); - const IState& CurrentState() const; + [[nodiscard]] const IState* CurrentState() const; - template - T* TopNodeAs() { - if (node_stack_.empty()) { + template + [[nodiscard]] T* TopNodeAs() { + if (node_stack_.empty()) return nullptr; - } - - return dynamic_cast(node_stack_.back()); + return dynamic_cast(node_stack_.back().GetNode); } void PushNode(std::unique_ptr node); - std::unique_ptr PopNode(); + [[nodiscard]] std::unique_ptr PopNode(); private: std::vector state_stack_; diff --git a/lib/parser/diagnostics/Diagnostic.hpp b/lib/parser/diagnostics/Diagnostic.hpp index 7893ed2..8f33b4f 100644 --- a/lib/parser/diagnostics/Diagnostic.hpp +++ b/lib/parser/diagnostics/Diagnostic.hpp @@ -1,6 +1,7 @@ #ifndef DIAGNOSTIC_HPP_ #define DIAGNOSTIC_HPP_ +#include #include #include #include @@ -9,20 +10,57 @@ #include "RelatedInfo.hpp" #include "lib/parser/tokens/SourceSpan.hpp" #include "severity/ISeverity.hpp" -#include "severity/Severity.hpp" class Diagnostic { - const ISeverity* severity = &Severity::Error(); - std::string code; - std::string message; - std::string category; +public: + Diagnostic(); + Diagnostic(std::shared_ptr sev, std::string code, std::string message); + Diagnostic(const Diagnostic& other); + Diagnostic(Diagnostic&& other) noexcept; + Diagnostic& operator=(const Diagnostic& other); + Diagnostic& operator=(Diagnostic&& other) noexcept; + ~Diagnostic(); - std::optional where; - std::vector notes; - std::vector fixes; + void SetSeverity(std::shared_ptr sev); + [[nodiscard]] const std::shared_ptr& GetSeverity() const noexcept; - bool is_fatal = false; - bool is_suppressed = false; + void SetCode(std::string c); + [[nodiscard]] const std::string& GetCode() const noexcept; + + void SetMessage(std::string m); + [[nodiscard]] const std::string& GetMessage() const noexcept; + + void SetCategory(std::string cat); + [[nodiscard]] const std::string& GetCategory() const noexcept; + + void SetWhere(SourceSpan sp); + void ResetWhere(); + [[nodiscard]] const std::optional& GetWhere() const noexcept; + + void AddNote(RelatedInfo note); + void ClearNotes(); + [[nodiscard]] const std::vector& GetNotes() const noexcept; + + void AddFix(FixIt fix); + void ClearFixes(); + [[nodiscard]] const std::vector& GetFixes() const noexcept; + + void SetFatal(bool on); + [[nodiscard]] bool IsFatal() const noexcept; + + void SetSuppressed(bool on); + [[nodiscard]] bool IsSuppressed() const noexcept; + +private: + std::shared_ptr severity_; + std::string code_; + std::string message_; + std::string category_; + std::optional where_; + std::vector notes_; + std::vector fixes_; + bool is_fatal_ = false; + bool is_suppressed_ = false; }; #endif // DIAGNOSTIC_HPP_ diff --git a/lib/parser/diagnostics/DiagnosticCollector.hpp b/lib/parser/diagnostics/DiagnosticCollector.hpp index f77a074..83e7232 100644 --- a/lib/parser/diagnostics/DiagnosticCollector.hpp +++ b/lib/parser/diagnostics/DiagnosticCollector.hpp @@ -3,7 +3,11 @@ #include #include +#include +#include +#include #include +#include #include "IDiagnosticSink.hpp" @@ -29,10 +33,12 @@ class DiagnosticCollector : public IDiagnosticSink { void SuppressCode(std::string code); void SuppressCategory(std::string category); void SetGlobalFilter(Predicate p); + void ResetGlobalFilter(); void EnableDeduplication(bool on); - void SetCapacity(std::size_t max_total); - void SetErrorLimit(std::size_t max_errors); - void SetWarningLimit(std::size_t max_warnings); + + void SetCapacity(std::optional max_total); + void SetErrorLimit(std::optional max_errors); + void SetWarningLimit(std::optional max_warnings); bool IsSuppressed(const Diagnostic& d) const; @@ -43,12 +49,12 @@ class DiagnosticCollector : public IDiagnosticSink { std::vector diags_; std::unordered_set suppressed_codes_; std::unordered_set suppressed_categories_; - Predicate global_filter_{}; + std::optional global_filter_; bool dedup_ = true; - std::size_t capacity_ = 0; // 0 == without limits - std::size_t error_limit_ = 0; // 0 == without limits - std::size_t warning_limit_ = 0; // 0 == without limits + std::optional capacity_; + std::optional error_limit_; + std::optional warning_limit_; std::size_t errors_ = 0; std::size_t warnings_ = 0; diff --git a/lib/parser/diagnostics/severity/Severity.hpp b/lib/parser/diagnostics/severity/Severity.hpp index 70aada2..aa95556 100644 --- a/lib/parser/diagnostics/severity/Severity.hpp +++ b/lib/parser/diagnostics/severity/Severity.hpp @@ -1,15 +1,17 @@ #ifndef SEVERITY_HPP_ #define SEVERITY_HPP_ +#include +#include #include "ISeverity.hpp" class Severity { public: - static const ISeverity& Note(); // Level=0 - static const ISeverity& Warning(); // Level=1 - static const ISeverity& Error(); // Level=2 + static const std::shared_ptr& Note(); + static const std::shared_ptr& Warning(); + static const std::shared_ptr& Error(); - static const ISeverity& Custom(std::string_view name, int level); + static std::shared_ptr Custom(std::string_view name, int level); }; #endif // SEVERITY_HPP_ diff --git a/lib/parser/pratt/DefaultOperatorResolver.hpp b/lib/parser/pratt/DefaultOperatorResolver.hpp index 8350b7d..f46cfc2 100644 --- a/lib/parser/pratt/DefaultOperatorResolver.hpp +++ b/lib/parser/pratt/DefaultOperatorResolver.hpp @@ -1,6 +1,8 @@ #ifndef DEFAULTOPERATORRESOLVER_HPP_ #define DEFAULTOPERATORRESOLVER_HPP_ +#include +#include #include #include "IOperatorResolver.hpp" @@ -12,9 +14,9 @@ class DefaultOperatorResolver : public IOperatorResolver { public: ~DefaultOperatorResolver() override = default; - const InfixSpec* FindInfix(const Token& t) const override; - const PostfixSpec* FindPostfix(const Token& t) const override; - const IUnaryOpTag* FindPrefix(const Token& t) const override; + std::optional> FindInfix(const Token& t) const override; + std::optional> FindPostfix(const Token& t) const override; + std::optional> FindPrefix(const Token& t) const override; bool IsContinuation(const Token& t) const override; private: diff --git a/lib/parser/pratt/IOperatorResolver.hpp b/lib/parser/pratt/IOperatorResolver.hpp index 76b2625..a210cb2 100644 --- a/lib/parser/pratt/IOperatorResolver.hpp +++ b/lib/parser/pratt/IOperatorResolver.hpp @@ -1,16 +1,21 @@ #ifndef IOPERATORRESOLVER_HPP_ #define IOPERATORRESOLVER_HPP_ -#include "lib/parser/ast/nodes/exprs/tags/IUnaryOpTag.hpp" -#include "lib/parser/pratt/specifications/InfixSpec.hpp" -#include "lib/parser/pratt/specifications/PostfixSpec.hpp" +#include +#include + +class Token; +class IUnaryOpTag; +class InfixSpec; +class PostfixSpec; class IOperatorResolver { public: virtual ~IOperatorResolver() = default; - virtual const InfixSpec* FindInfix(const Token& t) const = 0; - virtual const PostfixSpec* FindPostfix(const Token& t) const = 0; - virtual const IUnaryOpTag* FindPrefix(const Token& t) const = 0; + + virtual std::optional> FindInfix(const Token& t) const = 0; + virtual std::optional> FindPostfix(const Token& t) const = 0; + virtual std::optional> FindPrefix(const Token& t) const = 0; virtual bool IsContinuation(const Token& t) const = 0; }; diff --git a/lib/parser/pratt/PrattExpressionParser.hpp b/lib/parser/pratt/PrattExpressionParser.hpp index a2a465c..b183b6c 100644 --- a/lib/parser/pratt/PrattExpressionParser.hpp +++ b/lib/parser/pratt/PrattExpressionParser.hpp @@ -10,11 +10,10 @@ #include "lib/parser/ast/nodes/base/Expr.hpp" #include "lib/parser/diagnostics/IDiagnosticSink.hpp" #include "lib/parser/tokens/token_streams/ITokenStream.hpp" -#include "specifications/InfixSpec.hpp" class PrattExpressionParser : public IExpressionParser { public: - explicit PrattExpressionParser(std::unique_ptr r, IAstFactory* factory); + explicit PrattExpressionParser(std::unique_ptr resolver, std::shared_ptr factory); ~PrattExpressionParser() override = default; std::unique_ptr Parse(ITokenStream& ts, IDiagnosticSink& diags) override; @@ -28,8 +27,8 @@ class PrattExpressionParser : public IExpressionParser { private: std::unique_ptr MakeInfix(const InfixSpec& spec, std::unique_ptr lhs, std::unique_ptr rhs); - std::unique_ptr r_; - IAstFactory* factory_; + std::unique_ptr resolver_; + std::shared_ptr factory_; }; #endif // PRATTEXPRESSIONPARSER_HPP_ diff --git a/lib/parser/states/StateBlock.hpp b/lib/parser/states/StateBlock.hpp index 4f5de4a..8cde812 100644 --- a/lib/parser/states/StateBlock.hpp +++ b/lib/parser/states/StateBlock.hpp @@ -6,8 +6,8 @@ class StateBlock : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEBLOCK_HPP_ diff --git a/lib/parser/states/StateCallDeclHdr.hpp b/lib/parser/states/StateCallDeclHdr.hpp index 7ffbdcd..3229de2 100644 --- a/lib/parser/states/StateCallDeclHdr.hpp +++ b/lib/parser/states/StateCallDeclHdr.hpp @@ -6,8 +6,8 @@ class StateCallDeclHdr : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATECALLDECLHDR_HPP_ diff --git a/lib/parser/states/StateDestructorDecl.hpp b/lib/parser/states/StateDestructorDecl.hpp index a5e98cd..0335977 100644 --- a/lib/parser/states/StateDestructorDecl.hpp +++ b/lib/parser/states/StateDestructorDecl.hpp @@ -6,8 +6,8 @@ class StateDestructorDecl : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEDESTRUCTORDECL_HPP_ diff --git a/lib/parser/states/StateExpr.hpp b/lib/parser/states/StateExpr.hpp index 31c2f3f..01e6d98 100644 --- a/lib/parser/states/StateExpr.hpp +++ b/lib/parser/states/StateExpr.hpp @@ -6,8 +6,8 @@ class StateExpr : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEEXPR_HPP_ diff --git a/lib/parser/states/StateFieldDecl.hpp b/lib/parser/states/StateFieldDecl.hpp index 38c9c22..52eba65 100644 --- a/lib/parser/states/StateFieldDecl.hpp +++ b/lib/parser/states/StateFieldDecl.hpp @@ -6,8 +6,8 @@ class StateFieldDecl : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEFIELDDECL_HPP_ diff --git a/lib/parser/states/StateForHead.hpp b/lib/parser/states/StateForHead.hpp index 64126b0..8e23608 100644 --- a/lib/parser/states/StateForHead.hpp +++ b/lib/parser/states/StateForHead.hpp @@ -6,8 +6,8 @@ class StateForHead : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEFORHEAD_HPP_ diff --git a/lib/parser/states/StateGlobalVarDecl.hpp b/lib/parser/states/StateGlobalVarDecl.hpp index 301359f..dfb15c1 100644 --- a/lib/parser/states/StateGlobalVarDecl.hpp +++ b/lib/parser/states/StateGlobalVarDecl.hpp @@ -6,8 +6,8 @@ class StateGlobalVarDecl : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEGLOBALVARDECL_HPP_ diff --git a/lib/parser/states/StateIfHead.hpp b/lib/parser/states/StateIfHead.hpp index 0739c60..3edd58d 100644 --- a/lib/parser/states/StateIfHead.hpp +++ b/lib/parser/states/StateIfHead.hpp @@ -6,8 +6,8 @@ class StateIfHead : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEIFHEAD_HPP_ diff --git a/lib/parser/states/StateIfTail.hpp b/lib/parser/states/StateIfTail.hpp index d75a5be..93a3e6b 100644 --- a/lib/parser/states/StateIfTail.hpp +++ b/lib/parser/states/StateIfTail.hpp @@ -6,8 +6,8 @@ class StateIfTail : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEIFTAIL_HPP_ diff --git a/lib/parser/states/StateMethodHdr.hpp b/lib/parser/states/StateMethodHdr.hpp index 3f12f60..e2b19a3 100644 --- a/lib/parser/states/StateMethodHdr.hpp +++ b/lib/parser/states/StateMethodHdr.hpp @@ -6,8 +6,8 @@ class StateMethodHdr : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEMETHODHDR_HPP_ diff --git a/lib/parser/states/StateModule.hpp b/lib/parser/states/StateModule.hpp index bba7700..0293bc2 100644 --- a/lib/parser/states/StateModule.hpp +++ b/lib/parser/states/StateModule.hpp @@ -6,8 +6,8 @@ class StateModule : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEMODULE_HPP_ diff --git a/lib/parser/states/StateParseType.hpp b/lib/parser/states/StateParseType.hpp index f4cdfd9..bfb6223 100644 --- a/lib/parser/states/StateParseType.hpp +++ b/lib/parser/states/StateParseType.hpp @@ -6,8 +6,8 @@ class StateParseType : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEPARSETYPE_HPP_ diff --git a/lib/parser/states/StateReturnTail.hpp b/lib/parser/states/StateReturnTail.hpp index 59cd220..942a58d 100644 --- a/lib/parser/states/StateReturnTail.hpp +++ b/lib/parser/states/StateReturnTail.hpp @@ -6,8 +6,8 @@ class StateReturnTail : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATERETURNTAIL_HPP_ diff --git a/lib/parser/states/StateStmt.hpp b/lib/parser/states/StateStmt.hpp index c892121..7e4970b 100644 --- a/lib/parser/states/StateStmt.hpp +++ b/lib/parser/states/StateStmt.hpp @@ -6,8 +6,8 @@ class StateStmt : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATESTMT_HPP_ diff --git a/lib/parser/states/StateSyncToBlockEnd.hpp b/lib/parser/states/StateSyncToBlockEnd.hpp index ba54686..c9736f0 100644 --- a/lib/parser/states/StateSyncToBlockEnd.hpp +++ b/lib/parser/states/StateSyncToBlockEnd.hpp @@ -6,8 +6,8 @@ class StateSyncToBlockEnd : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATESYNCTOBLOCKEND_HPP_ diff --git a/lib/parser/states/StateSyncToStmtEnd.hpp b/lib/parser/states/StateSyncToStmtEnd.hpp index 6348f4e..f585bfc 100644 --- a/lib/parser/states/StateSyncToStmtEnd.hpp +++ b/lib/parser/states/StateSyncToStmtEnd.hpp @@ -6,8 +6,8 @@ class StateSyncToStmtEnd : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATESYNCTOSTMTEND_HPP_ diff --git a/lib/parser/states/StateTopDecl.hpp b/lib/parser/states/StateTopDecl.hpp index df36014..28ed858 100644 --- a/lib/parser/states/StateTopDecl.hpp +++ b/lib/parser/states/StateTopDecl.hpp @@ -6,8 +6,8 @@ class StateTopDecl : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATETOPDECL_HPP_ diff --git a/lib/parser/states/StateTypeAliasDecl.hpp b/lib/parser/states/StateTypeAliasDecl.hpp index 21ceaad..c6cc2bf 100644 --- a/lib/parser/states/StateTypeAliasDecl.hpp +++ b/lib/parser/states/StateTypeAliasDecl.hpp @@ -6,8 +6,8 @@ class StateTypeAliasDecl : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATETYPEALIASDECL_HPP_ diff --git a/lib/parser/states/StateUnsafeBlock.hpp b/lib/parser/states/StateUnsafeBlock.hpp index 4d9f68c..469c8c0 100644 --- a/lib/parser/states/StateUnsafeBlock.hpp +++ b/lib/parser/states/StateUnsafeBlock.hpp @@ -6,8 +6,8 @@ class StateUnsafeBlock : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEUNSAFEBLOCK_HPP_ diff --git a/lib/parser/states/StateVarDeclTail.hpp b/lib/parser/states/StateVarDeclTail.hpp index 1984b08..51f4c80 100644 --- a/lib/parser/states/StateVarDeclTail.hpp +++ b/lib/parser/states/StateVarDeclTail.hpp @@ -6,8 +6,8 @@ class StateVarDeclTail : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEVARDECLTAIL_HPP_ diff --git a/lib/parser/states/StateWhileHead.hpp b/lib/parser/states/StateWhileHead.hpp index 5515efe..94deceb 100644 --- a/lib/parser/states/StateWhileHead.hpp +++ b/lib/parser/states/StateWhileHead.hpp @@ -6,8 +6,8 @@ class StateWhileHead : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEWHILEHEAD_HPP_ diff --git a/lib/parser/states/base/IState.hpp b/lib/parser/states/base/IState.hpp index 66a6814..5f1e559 100644 --- a/lib/parser/states/base/IState.hpp +++ b/lib/parser/states/base/IState.hpp @@ -1,15 +1,20 @@ #ifndef ISTATE_HPP_ #define ISTATE_HPP_ +#include + +#include "StateError.hpp" #include "lib/parser/tokens/token_streams/ITokenStream.hpp" class ContextParser; // forward class IState { public: + using StepResult = std::expected; + virtual ~IState() = default; - virtual const char* Name() const = 0; - virtual bool Step(ContextParser& ctx, ITokenStream& ts) = 0; + virtual std::string_view Name() const = 0; + virtual StepResult TryStep(ContextParser& ctx, ITokenStream& ts) = 0; }; #endif // ISTATE_HPP_ diff --git a/lib/parser/states/base/StateBase.hpp b/lib/parser/states/base/StateBase.hpp index 71ac357..3d98892 100644 --- a/lib/parser/states/base/StateBase.hpp +++ b/lib/parser/states/base/StateBase.hpp @@ -6,6 +6,7 @@ class StateBase : IState { public: + using StepResult = StepResult; ~StateBase() override = default; static SourceSpan SpanFrom(const Token& token); static SourceSpan Union(const SourceSpan& lhs, const SourceSpan& rhs); diff --git a/lib/parser/states/base/StateError.cpp b/lib/parser/states/base/StateError.cpp new file mode 100644 index 0000000..384ace5 --- /dev/null +++ b/lib/parser/states/base/StateError.cpp @@ -0,0 +1 @@ +#include "StateError.hpp" diff --git a/lib/parser/states/base/StateError.hpp b/lib/parser/states/base/StateError.hpp new file mode 100644 index 0000000..997bbbc --- /dev/null +++ b/lib/parser/states/base/StateError.hpp @@ -0,0 +1,16 @@ +#ifndef STATEERROR_HPP_ +#define STATEERROR_HPP_ + +#include + +class StateError { +public: + StateError(); + explicit StateError(std::string message); + const std::string& Message() const noexcept; + +private: + std::string message_; +}; + +#endif // STATEERROR_HPP_ diff --git a/lib/parser/states/class/StateClassBody.hpp b/lib/parser/states/class/StateClassBody.hpp index b6b9937..b7faa38 100644 --- a/lib/parser/states/class/StateClassBody.hpp +++ b/lib/parser/states/class/StateClassBody.hpp @@ -6,8 +6,8 @@ class StateClassBody : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATECLASSBODY_HPP_ diff --git a/lib/parser/states/class/StateClassHdr.hpp b/lib/parser/states/class/StateClassHdr.hpp index 0d152cc..eb00f41 100644 --- a/lib/parser/states/class/StateClassHdr.hpp +++ b/lib/parser/states/class/StateClassHdr.hpp @@ -6,8 +6,8 @@ class StateClassHdr : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATECLASSHDR_HPP_ diff --git a/lib/parser/states/class/StateClassMember.hpp b/lib/parser/states/class/StateClassMember.hpp index 81facb8..ae02ff6 100644 --- a/lib/parser/states/class/StateClassMember.hpp +++ b/lib/parser/states/class/StateClassMember.hpp @@ -6,8 +6,8 @@ class StateClassHdr : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATECLASSMEMBER_HPP_ diff --git a/lib/parser/states/func/StateFuncBody.hpp b/lib/parser/states/func/StateFuncBody.hpp index b1982f8..0c8f0ab 100644 --- a/lib/parser/states/func/StateFuncBody.hpp +++ b/lib/parser/states/func/StateFuncBody.hpp @@ -6,8 +6,8 @@ class StateFuncBody : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEFUNCBODY_HPP_ diff --git a/lib/parser/states/func/StateFuncHdr.hpp b/lib/parser/states/func/StateFuncHdr.hpp index ec6aadb..1ce85f8 100644 --- a/lib/parser/states/func/StateFuncHdr.hpp +++ b/lib/parser/states/func/StateFuncHdr.hpp @@ -6,8 +6,8 @@ class StateFuncHdr : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEFUNCHDR_HPP_ diff --git a/lib/parser/states/func/StateFuncParams.hpp b/lib/parser/states/func/StateFuncParams.hpp index 4e26a82..869b5f6 100644 --- a/lib/parser/states/func/StateFuncParams.hpp +++ b/lib/parser/states/func/StateFuncParams.hpp @@ -6,8 +6,8 @@ class StateFuncParams : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEFUNCPARAMS_HPP_ diff --git a/lib/parser/states/interface/StateInterfaceBody.hpp b/lib/parser/states/interface/StateInterfaceBody.hpp index 9ce5073..10accc9 100644 --- a/lib/parser/states/interface/StateInterfaceBody.hpp +++ b/lib/parser/states/interface/StateInterfaceBody.hpp @@ -6,8 +6,8 @@ class StateInterfaceBody : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEINTERFACEBODY_HPP_ diff --git a/lib/parser/states/interface/StateInterfaceDecl.hpp b/lib/parser/states/interface/StateInterfaceDecl.hpp index ecea6da..09c0b64 100644 --- a/lib/parser/states/interface/StateInterfaceDecl.hpp +++ b/lib/parser/states/interface/StateInterfaceDecl.hpp @@ -6,8 +6,8 @@ class StateInterfaceDecl : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEINTERFACEDECL_HPP_ diff --git a/lib/parser/states/interface/StateInterfaceHdr.hpp b/lib/parser/states/interface/StateInterfaceHdr.hpp index 2993ec7..bc6aba7 100644 --- a/lib/parser/states/interface/StateInterfaceHdr.hpp +++ b/lib/parser/states/interface/StateInterfaceHdr.hpp @@ -6,8 +6,8 @@ class StateInterfaceHdr : public StateBase { public: - const char* Name() const override; - bool Step(ContextParser& ctx, ITokenStream& ts) override; + std::string_view Name() const override; + StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; #endif // STATEINTERFACEHDR_HPP_ diff --git a/lib/parser/tokens/SourceId.cpp b/lib/parser/tokens/SourceId.cpp new file mode 100644 index 0000000..202a610 --- /dev/null +++ b/lib/parser/tokens/SourceId.cpp @@ -0,0 +1 @@ +#include "SourceId.hpp" diff --git a/lib/parser/tokens/SourceId.hpp b/lib/parser/tokens/SourceId.hpp new file mode 100644 index 0000000..0c56078 --- /dev/null +++ b/lib/parser/tokens/SourceId.hpp @@ -0,0 +1,26 @@ +#ifndef SOURCEID_HPP_ +#define SOURCEID_HPP_ + +#include +#include +#include + +class SourceId { +public: + SourceId() = default; + explicit SourceId(std::string path) : path_(std::move(path)) { + } + + [[nodiscard]] std::string_view Path() const noexcept; + + [[nodiscard]] std::string Basename() const; + + [[nodiscard]] bool IsValid() const noexcept; + [[nodiscard]] bool operator==(const SourceId& other) const noexcept; + [[nodiscard]] bool operator!=(const SourceId& other) const noexcept; + +private: + std::string path_; +}; + +#endif // SOURCEID_HPP_ diff --git a/lib/parser/tokens/SourceSpan.hpp b/lib/parser/tokens/SourceSpan.hpp index 92476e5..3594de1 100644 --- a/lib/parser/tokens/SourceSpan.hpp +++ b/lib/parser/tokens/SourceSpan.hpp @@ -1,19 +1,29 @@ #ifndef SOURCESPAN_HPP_ #define SOURCESPAN_HPP_ +#include "SourceId.hpp" + #include "lib/lexer/tokens/TokenPosition.hpp" class SourceSpan { public: - SourceSpan(TokenPosition start, TokenPosition end); + SourceSpan() = default; + SourceSpan(SourceId id, TokenPosition start, TokenPosition end); + + [[nodiscard]] const SourceId& GetSourceId() const noexcept; + [[nodiscard]] TokenPosition GetStart() const noexcept; + [[nodiscard]] TokenPosition GetEnd() const noexcept; - [[nodiscard]] TokenPosition GetStart() const; + [[nodiscard]] bool IsValid() const noexcept; + void Normalize() noexcept; + [[nodiscard]] static SourceSpan SinglePoint(SourceId id, TokenPosition p); - [[nodiscard]] TokenPosition GetEnd() const; + [[nodiscard]] static SourceSpan Union(const SourceSpan& a, const SourceSpan& b); private: - TokenPosition begin_; - TokenPosition end_; + SourceId id_; + TokenPosition begin_{}; + TokenPosition end_{}; }; #endif // SOURCESPAN_HPP_ diff --git a/lib/parser/tokens/token_streams/ITokenStream.hpp b/lib/parser/tokens/token_streams/ITokenStream.hpp index 3d76df7..87f0a46 100644 --- a/lib/parser/tokens/token_streams/ITokenStream.hpp +++ b/lib/parser/tokens/token_streams/ITokenStream.hpp @@ -13,7 +13,7 @@ class ITokenStream { virtual TokenPtr Consume() = 0; - virtual std::size_t Position() const = 0; + virtual size_t Position() const = 0; virtual void Rewind(std::size_t n) = 0; diff --git a/lib/parser/tokens/token_streams/VectorTokenStream.hpp b/lib/parser/tokens/token_streams/VectorTokenStream.hpp index fd15c3d..d92fe89 100644 --- a/lib/parser/tokens/token_streams/VectorTokenStream.hpp +++ b/lib/parser/tokens/token_streams/VectorTokenStream.hpp @@ -2,6 +2,7 @@ #define VECTORTOKENSTREAM_HPP_ #include +#include #include #include "ITokenStream.hpp" @@ -11,27 +12,25 @@ class VectorTokenStream : public ITokenStream { public: explicit VectorTokenStream(std::vector tokens); - const Token& Peek(std::size_t k = 0) override; + const Token& Peek(size_t k = 0) override; TokenPtr Consume() override; - std::size_t Position() const override; + size_t Position() const override; - void Rewind(std::size_t n) override; + void Rewind(size_t n) override; bool IsEof() const override; const Token* LastConsumed() const override; - const Token* TryPeek(std::size_t k = 0) override; + const Token* TryPeek(size_t k = 0) override; - std::size_t Size() const { - return tokens_.size(); - } + size_t Size() const; private: std::vector tokens_; - std::size_t index_ = 0; + size_t index_ = 0; const Token* last_ = nullptr; }; diff --git a/lib/parser/type_parser/QNameTypeParser.hpp b/lib/parser/type_parser/QNameTypeParser.hpp index cd5c1ff..b5068b1 100644 --- a/lib/parser/type_parser/QNameTypeParser.hpp +++ b/lib/parser/type_parser/QNameTypeParser.hpp @@ -9,15 +9,21 @@ #include "lib/parser/tokens/token_streams/ITokenStream.hpp" #include "lib/parser/types/TypeReference.hpp" -class QNameTypeParser : public ITypeParser { +class QNameTypeParser final : public ITypeParser { public: - explicit QNameTypeParser(IAstFactory* factory); + explicit QNameTypeParser(IAstFactory& factory) noexcept; ~QNameTypeParser() override = default; - std::unique_ptr ParseType(ITokenStream& ts, IDiagnosticSink& diags) override; + [[nodiscard]] std::unique_ptr ParseType(ITokenStream& ts, IDiagnosticSink& diags) override; + + QNameTypeParser(const QNameTypeParser&) = delete; + QNameTypeParser& operator=(const QNameTypeParser&) = delete; + + QNameTypeParser(QNameTypeParser&&) noexcept = default; + QNameTypeParser& operator=(QNameTypeParser&&) noexcept = delete; private: - IAstFactory* factory_; + std::shared_ptr factory_; }; #endif // QNAMETYPEPARSER_HPP_ diff --git a/lib/parser/types/Nullable.cpp b/lib/parser/types/Nullable.cpp new file mode 100644 index 0000000..576c114 --- /dev/null +++ b/lib/parser/types/Nullable.cpp @@ -0,0 +1 @@ +#include "Nullable.hpp" diff --git a/lib/parser/types/Nullable.hpp b/lib/parser/types/Nullable.hpp new file mode 100644 index 0000000..6e629a5 --- /dev/null +++ b/lib/parser/types/Nullable.hpp @@ -0,0 +1,22 @@ +#ifndef NULLABLE_HPP_ +#define NULLABLE_HPP_ + +class Nullable { +public: + Nullable() noexcept = default; + + explicit Nullable(bool on) noexcept; + + bool IsOn() const noexcept; + + void Set(bool on) noexcept; + + void Enable() noexcept; + + void Disable() noexcept; + +private: + bool on_ = false; +}; + +#endif // NULLABLE_HPP_ diff --git a/lib/parser/types/Param.hpp b/lib/parser/types/Param.hpp index a9ddee1..a104f82 100644 --- a/lib/parser/types/Param.hpp +++ b/lib/parser/types/Param.hpp @@ -9,13 +9,13 @@ class Param { public: Param(std::string name, TypeReference typeReference); - [[nodiscard]] std::string getName(); + [[nodiscard]] std::string GetName(); - [[nodiscard]] TypeReference getType(); + [[nodiscard]] TypeReference GetType(); private: - std::string name; - TypeReference reference; + std::string name_; + TypeReference reference_; }; #endif // PARAM_HPP_ diff --git a/lib/parser/types/ResolvedTypeHandle.cpp b/lib/parser/types/ResolvedTypeHandle.cpp new file mode 100644 index 0000000..f8cc3af --- /dev/null +++ b/lib/parser/types/ResolvedTypeHandle.cpp @@ -0,0 +1 @@ +#include "ResolvedTypeHandle.hpp" diff --git a/lib/parser/types/ResolvedTypeHandle.hpp b/lib/parser/types/ResolvedTypeHandle.hpp new file mode 100644 index 0000000..f68747f --- /dev/null +++ b/lib/parser/types/ResolvedTypeHandle.hpp @@ -0,0 +1,29 @@ +#ifndef RESOLVEDTYPEHANDLE_HPP_ +#define RESOLVEDTYPEHANDLE_HPP_ + +#include + +class ResolvedTypeHandle { +public: + ResolvedTypeHandle() = default; + ResolvedTypeHandle(const void* decl, std::string mangled, bool is_interface); + + ResolvedTypeHandle(const ResolvedTypeHandle& other) = default; + ResolvedTypeHandle(ResolvedTypeHandle&& other) noexcept = default; + ~ResolvedTypeHandle() = default; + + ResolvedTypeHandle& operator=(const ResolvedTypeHandle& other) = default; + ResolvedTypeHandle& operator=(ResolvedTypeHandle&& other) noexcept = default; + + const void* Decl() const noexcept; + std::string_view Mangled() const noexcept; + bool IsInterface() const noexcept; + bool IsValid() const noexcept; + +private: + const void* decl_ = nullptr; + std::string mangled_; + bool is_interface_ = false; +}; + +#endif // RESOLVEDTYPEHANDLE_HPP_ diff --git a/lib/parser/types/TypeReference.hpp b/lib/parser/types/TypeReference.hpp index f99d7c9..fccc013 100644 --- a/lib/parser/types/TypeReference.hpp +++ b/lib/parser/types/TypeReference.hpp @@ -1,30 +1,106 @@ #ifndef TYPEREFERENCE_HPP_ #define TYPEREFERENCE_HPP_ +#include #include #include +#include "Nullable.hpp" +#include "ResolvedTypeHandle.hpp" + class TypeReference { public: TypeReference(); - - explicit TypeReference(std::string&& name); + explicit TypeReference(std::string name); + explicit TypeReference(std::vector qname); TypeReference(const TypeReference& ref); + TypeReference(TypeReference&& ref) noexcept; ~TypeReference(); TypeReference& operator=(const TypeReference& ref); + TypeReference& operator=(TypeReference&& ref) noexcept; + + TypeReference& operator=(std::string name); + + bool StructurallyEquals(const TypeReference& other) const noexcept; + + std::string StableKey() const; + + std::string ToStringHuman() const { + return StableKey(); + } + + const std::vector& QualifiedName() const noexcept { + return qname_; + } + + std::string_view SimpleName() const noexcept; - TypeReference& operator=(std::string&& name); + void SetQualifiedName(std::vector qname); - [[nodiscard]] std::string GetName() const; + void SetSimpleName(std::string name); - [[nodiscard]] std::vector GetQualifiedName() const; + void PushQualifier(std::string qualifier); + bool PopFrontQualifier(); + bool PopBackQualifier(); + + const std::vector& TypeArguments() const noexcept { + return type_args_; + } + std::vector& MutableTypeArguments() noexcept { + return type_args_; + } + + void ClearTypeArguments(); + void AddTypeArgument(TypeReference arg); + std::size_t Arity() const noexcept { + return type_args_.size(); + } + + const Nullable& Nullability() const noexcept { + return nullable_; + } + bool IsNullable() const noexcept { + return nullable_.IsOn(); + } + void SetNullable(bool on) noexcept { + nullable_.Set(on); + } + void MakeNullable() noexcept { + nullable_.Enable(); + } + void MakeNonNullable() noexcept { + nullable_.Disable(); + } + + TypeReference WithoutNullable() const; + + bool IsResolved() const noexcept { + return static_cast(resolved_); + } + const ResolvedTypeHandle* Resolved() const noexcept { + return resolved_ ? resolved_.get() : nullptr; + } + + void SetResolvedHandle(const void* decl, std::string mangled, bool is_interface); + + void ResetResolvedHandle() noexcept { + resolved_.reset(); + } private: - std::vector qname; - bool is_nullable = false; + static std::string JoinQualified(const std::vector& parts, std::string_view sep); + static std::string ArgsToString(const std::vector& args); + + void InvalidateResolution() noexcept { + resolved_.reset(); + } + std::vector qname_; + std::vector type_args_; + Nullable nullable_; + std::unique_ptr resolved_; }; #endif // TYPEREFERENCE_HPP_ From 60038968bf57ac7131bdfe3b170b23761bb0030d Mon Sep 17 00:00:00 2001 From: biqiboqi Date: Mon, 3 Nov 2025 00:18:43 +0300 Subject: [PATCH 06/21] fix: fix branch class --- lib/parser/ast/nodes/stmts/Branch.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/parser/ast/nodes/stmts/Branch.hpp b/lib/parser/ast/nodes/stmts/Branch.hpp index 3569658..301168a 100644 --- a/lib/parser/ast/nodes/stmts/Branch.hpp +++ b/lib/parser/ast/nodes/stmts/Branch.hpp @@ -6,7 +6,7 @@ #include "Block.hpp" #include "lib/parser/ast/nodes/base/Expr.hpp" -class Branch Branch { +class Branch { public: Branch(std::unique_ptr condition, std::unique_ptr then_block); Branch(const Branch&) = delete; From cd624eb7a986ee266176b5217a856d93484f696f Mon Sep 17 00:00:00 2001 From: biqiboqi Date: Mon, 3 Nov 2025 00:40:28 +0300 Subject: [PATCH 07/21] fix: fix branch class and some lil mistakes --- lib/parser/ast/IAstFactory.hpp | 1 - lib/parser/ast/nodes/decls/Module.cpp | 1 + lib/parser/ast/nodes/decls/Module.hpp | 33 +++++++++++++++++++ lib/parser/ast/nodes/exprs/SafeCall.hpp | 1 + .../ast/nodes/exprs/literals/IntLit.hpp | 1 + .../ast/nodes/exprs/tags/IAssignOpTag.hpp | 2 ++ .../ast/nodes/exprs/tags/IBinaryOpTag.hpp | 2 ++ .../ast/nodes/exprs/tags/IUnaryOpTag.hpp | 2 ++ tests/unit_tests.cpp | 2 ++ 9 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 lib/parser/ast/nodes/decls/Module.cpp create mode 100644 lib/parser/ast/nodes/decls/Module.hpp diff --git a/lib/parser/ast/IAstFactory.hpp b/lib/parser/ast/IAstFactory.hpp index f4f96c8..1d8afdf 100644 --- a/lib/parser/ast/IAstFactory.hpp +++ b/lib/parser/ast/IAstFactory.hpp @@ -12,7 +12,6 @@ #include "nodes/decls/FunctionDecl.hpp" #include "nodes/decls/GlobalVarDecl.hpp" #include "nodes/decls/InterfaceDecl.hpp" -#include "nodes/decls/Module.hpp" #include "nodes/decls/TypeAliasDecl.hpp" #include "nodes/exprs/Assign.hpp" #include "nodes/exprs/Binary.hpp" diff --git a/lib/parser/ast/nodes/decls/Module.cpp b/lib/parser/ast/nodes/decls/Module.cpp new file mode 100644 index 0000000..6f69fe8 --- /dev/null +++ b/lib/parser/ast/nodes/decls/Module.cpp @@ -0,0 +1 @@ +#include "Module.hpp" diff --git a/lib/parser/ast/nodes/decls/Module.hpp b/lib/parser/ast/nodes/decls/Module.hpp new file mode 100644 index 0000000..1d204ea --- /dev/null +++ b/lib/parser/ast/nodes/decls/Module.hpp @@ -0,0 +1,33 @@ +#ifndef MODULE_HPP_ +#define MODULE_HPP_ + +#include +#include +#include + +#include "lib/parser/ast/nodes/base/AstNode.hpp" +#include "lib/parser/ast/nodes/base/Decl.hpp" +#include "lib/parser/tokens/SourceId.hpp" + +class Module : public AstNode { +public: + void Accept(AstVisitor& v) override; + + const std::string& Name() const noexcept; + void SetName(std::string n); + + const SourceId& Source() const noexcept; + void SetSource(SourceId id); + + const std::vector>& Decls() const noexcept; + std::vector>& MutableDecls() noexcept; + void AddDecl(std::unique_ptr d); + std::unique_ptr ReleaseDecl(std::size_t index); + +private: + std::string name_; + SourceId source_; + std::vector> decls_; +}; + +#endif // MODULE_HPP_ diff --git a/lib/parser/ast/nodes/exprs/SafeCall.hpp b/lib/parser/ast/nodes/exprs/SafeCall.hpp index 4e9728f..41c78c5 100644 --- a/lib/parser/ast/nodes/exprs/SafeCall.hpp +++ b/lib/parser/ast/nodes/exprs/SafeCall.hpp @@ -2,6 +2,7 @@ #define SAFECALL_HPP_ #include +#include #include "lib/parser/ast/nodes/base/Expr.hpp" diff --git a/lib/parser/ast/nodes/exprs/literals/IntLit.hpp b/lib/parser/ast/nodes/exprs/literals/IntLit.hpp index 4b51822..43e25f1 100644 --- a/lib/parser/ast/nodes/exprs/literals/IntLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/IntLit.hpp @@ -2,6 +2,7 @@ #define INTLIT_HPP_ #include "lib/parser/ast/nodes/base/Expr.hpp" +#include class IntLit : public Expr { public: diff --git a/lib/parser/ast/nodes/exprs/tags/IAssignOpTag.hpp b/lib/parser/ast/nodes/exprs/tags/IAssignOpTag.hpp index cad7082..8da40ad 100644 --- a/lib/parser/ast/nodes/exprs/tags/IAssignOpTag.hpp +++ b/lib/parser/ast/nodes/exprs/tags/IAssignOpTag.hpp @@ -1,6 +1,8 @@ #ifndef IASSIGNOPTAG_HPP_ #define IASSIGNOPTAG_HPP_ +#include + class IAssignOpTag { public: virtual ~IAssignOpTag() = default; diff --git a/lib/parser/ast/nodes/exprs/tags/IBinaryOpTag.hpp b/lib/parser/ast/nodes/exprs/tags/IBinaryOpTag.hpp index b82f5a1..22760ad 100644 --- a/lib/parser/ast/nodes/exprs/tags/IBinaryOpTag.hpp +++ b/lib/parser/ast/nodes/exprs/tags/IBinaryOpTag.hpp @@ -1,6 +1,8 @@ #ifndef IBINARYOPTAG_HPP_ #define IBINARYOPTAG_HPP_ +#include + class IBinaryOpTag { public: virtual ~IBinaryOpTag() = default; diff --git a/lib/parser/ast/nodes/exprs/tags/IUnaryOpTag.hpp b/lib/parser/ast/nodes/exprs/tags/IUnaryOpTag.hpp index 3eb72e8..50dc98c 100644 --- a/lib/parser/ast/nodes/exprs/tags/IUnaryOpTag.hpp +++ b/lib/parser/ast/nodes/exprs/tags/IUnaryOpTag.hpp @@ -1,6 +1,8 @@ #ifndef IUNARYOPTAG_HPP_ #define IUNARYOPTAG_HPP_ +#include + class IUnaryOpTag { public: virtual ~IUnaryOpTag() = default; diff --git a/tests/unit_tests.cpp b/tests/unit_tests.cpp index 43ffadd..fe98950 100644 --- a/tests/unit_tests.cpp +++ b/tests/unit_tests.cpp @@ -1,5 +1,7 @@ +#include #include TEST(LexerUnitTestSuite, BasicTest1) { + ASSERT_EQ(1, 1); // TODO: Implement test } From 1b4c52af7f4bfa08655beab1731ce242786aad7f Mon Sep 17 00:00:00 2001 From: biqiboqi Date: Mon, 3 Nov 2025 00:45:47 +0300 Subject: [PATCH 08/21] fix: I HATE !$@$!%#@$!@ CI/CD WITH !*@#^&!(@&#^ CODESTYLE --- lib/parser/ast/nodes/exprs/literals/IntLit.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/parser/ast/nodes/exprs/literals/IntLit.hpp b/lib/parser/ast/nodes/exprs/literals/IntLit.hpp index 43e25f1..ea3e2c1 100644 --- a/lib/parser/ast/nodes/exprs/literals/IntLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/IntLit.hpp @@ -1,8 +1,8 @@ #ifndef INTLIT_HPP_ #define INTLIT_HPP_ -#include "lib/parser/ast/nodes/base/Expr.hpp" #include +#include "lib/parser/ast/nodes/base/Expr.hpp" class IntLit : public Expr { public: From 1094bf4300d2b8b6e50df1d293d1c87ae3861787 Mon Sep 17 00:00:00 2001 From: biqiboqi Date: Mon, 3 Nov 2025 01:04:30 +0300 Subject: [PATCH 09/21] fix: fixed SafeCall --- lib/parser/ast/nodes/exprs/SafeCall.hpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/parser/ast/nodes/exprs/SafeCall.hpp b/lib/parser/ast/nodes/exprs/SafeCall.hpp index 41c78c5..3a7679b 100644 --- a/lib/parser/ast/nodes/exprs/SafeCall.hpp +++ b/lib/parser/ast/nodes/exprs/SafeCall.hpp @@ -2,16 +2,21 @@ #define SAFECALL_HPP_ #include +#include +#include #include #include "lib/parser/ast/nodes/base/Expr.hpp" +#include "lib/parser/types/TypeReference.hpp" + +class AstVisitor; class SafeCall : public Expr { public: void Accept(AstVisitor& v) override; - [[nodiscard]] const Expr* Object() const noexcept; - [[nodiscard]] Expr* MutableObject() noexcept; + [[nodiscard]] const Expr& Object() const noexcept; + [[nodiscard]] Expr& MutableObject() noexcept; void SetObject(std::unique_ptr obj); std::unique_ptr ReplaceObject(std::unique_ptr obj); @@ -21,21 +26,19 @@ class SafeCall : public Expr { [[nodiscard]] const std::vector>& Args() const noexcept; [[nodiscard]] std::vector>& MutableArgs() noexcept; void AddArg(std::unique_ptr e); - void ClearArgs(); [[nodiscard]] bool IsNullPropagating() const noexcept; void SetInferredType(TypeReference t); - [[nodiscard]] const TypeReference* InferredType() const noexcept; + [[nodiscard]] const std::optional& InferredType() const noexcept; private: std::unique_ptr object_; std::string method_; std::vector> args_; - TypeReference inferred_type_; - bool inferred_type_set_ = false; + std::optional inferred_type_; }; #endif // SAFECALL_HPP_ From 0d66cefd78e9b0216890e6306c545cd2060f8a1f Mon Sep 17 00:00:00 2001 From: biqiboqi Date: Sun, 9 Nov 2025 22:11:27 +0300 Subject: [PATCH 10/21] feat: add ast nodes implementation --- lib/parser/ast/AstVisitor.hpp | 3 +- lib/parser/ast/nodes/base/AstNode.hpp | 2 +- .../ast/nodes/class_members/CallDecl.cpp | 62 ++++++++++++- .../ast/nodes/class_members/CallDecl.hpp | 19 +++- .../nodes/class_members/DestructorDecl.cpp | 34 ++++++- .../nodes/class_members/DestructorDecl.hpp | 10 +- .../ast/nodes/class_members/FieldDecl.cpp | 62 ++++++++++++- .../ast/nodes/class_members/FieldDecl.hpp | 23 ++++- .../ast/nodes/class_members/MethodDecl.cpp | 93 ++++++++++++++++++- .../ast/nodes/class_members/MethodDecl.hpp | 31 ++++++- .../nodes/class_members/StaticFieldDecl.cpp | 62 ++++++++++++- .../nodes/class_members/StaticFieldDecl.hpp | 20 +++- lib/parser/ast/nodes/decls/ClassDecl.cpp | 48 ++++++++++ lib/parser/ast/nodes/decls/ClassDecl.hpp | 14 ++- lib/parser/ast/nodes/decls/FunctionDecl.cpp | 65 +++++++++++++ lib/parser/ast/nodes/decls/FunctionDecl.hpp | 24 ++++- lib/parser/ast/nodes/decls/GlobalVarDecl.cpp | 50 ++++++++++ lib/parser/ast/nodes/decls/GlobalVarDecl.hpp | 17 +++- lib/parser/ast/nodes/decls/InterfaceDecl.cpp | 36 +++++++ lib/parser/ast/nodes/decls/InterfaceDecl.hpp | 7 +- .../ast/nodes/decls/InterfaceMethod.cpp | 34 ++++++- .../ast/nodes/decls/InterfaceMethod.hpp | 6 +- lib/parser/ast/nodes/decls/Module.cpp | 44 +++++++++ lib/parser/ast/nodes/decls/Module.hpp | 6 +- lib/parser/ast/nodes/decls/TypeAliasDecl.cpp | 30 +++++- lib/parser/ast/nodes/decls/TypeAliasDecl.hpp | 9 +- lib/parser/ast/nodes/exprs/Assign.cpp | 54 ++++++++++- lib/parser/ast/nodes/exprs/Assign.hpp | 15 ++- lib/parser/ast/nodes/exprs/Binary.cpp | 54 ++++++++++- lib/parser/ast/nodes/exprs/Binary.hpp | 16 +++- lib/parser/ast/nodes/exprs/Call.cpp | 43 ++++++++- lib/parser/ast/nodes/exprs/Call.hpp | 12 ++- lib/parser/ast/nodes/exprs/CastAs.cpp | 40 +++++++- lib/parser/ast/nodes/exprs/CastAs.hpp | 11 ++- lib/parser/ast/nodes/exprs/Elvis.cpp | 46 ++++++++- lib/parser/ast/nodes/exprs/Elvis.hpp | 12 ++- lib/parser/ast/nodes/exprs/FieldAccess.cpp | 36 ++++++- lib/parser/ast/nodes/exprs/FieldAccess.hpp | 10 +- lib/parser/ast/nodes/exprs/IdentRef.cpp | 18 +++- lib/parser/ast/nodes/exprs/IdentRef.hpp | 5 +- lib/parser/ast/nodes/exprs/IndexAccess.cpp | 46 ++++++++- lib/parser/ast/nodes/exprs/IndexAccess.hpp | 12 ++- lib/parser/ast/nodes/exprs/NamespaceRef.cpp | 36 ++++++- lib/parser/ast/nodes/exprs/NamespaceRef.hpp | 10 +- lib/parser/ast/nodes/exprs/SafeCall.cpp | 64 ++++++++++++- lib/parser/ast/nodes/exprs/SafeCall.hpp | 25 +++-- lib/parser/ast/nodes/exprs/TypeTestIs.cpp | 40 +++++++- lib/parser/ast/nodes/exprs/TypeTestIs.hpp | 11 ++- lib/parser/ast/nodes/exprs/Unary.cpp | 36 ++++++- lib/parser/ast/nodes/exprs/Unary.hpp | 10 +- .../ast/nodes/exprs/literals/BoolLit.cpp | 16 +++- .../ast/nodes/exprs/literals/BoolLit.hpp | 5 +- .../ast/nodes/exprs/literals/CharLit.cpp | 16 +++- .../ast/nodes/exprs/literals/CharLit.hpp | 5 +- .../ast/nodes/exprs/literals/FloatLit.cpp | 16 +++- .../ast/nodes/exprs/literals/FloatLit.hpp | 7 +- .../ast/nodes/exprs/literals/IntLit.cpp | 16 +++- .../ast/nodes/exprs/literals/IntLit.hpp | 5 +- .../ast/nodes/exprs/literals/NullLit.cpp | 8 +- .../ast/nodes/exprs/literals/NullLit.hpp | 2 +- .../ast/nodes/exprs/literals/StringLit.cpp | 18 +++- .../ast/nodes/exprs/literals/StringLit.hpp | 5 +- lib/parser/ast/nodes/stmts/Block.cpp | 48 +++++++++- lib/parser/ast/nodes/stmts/Block.hpp | 10 +- lib/parser/ast/nodes/stmts/Branch.cpp | 40 +++++++- lib/parser/ast/nodes/stmts/Branch.hpp | 4 +- lib/parser/ast/nodes/stmts/BreakStmt.cpp | 8 +- lib/parser/ast/nodes/stmts/BreakStmt.hpp | 2 +- lib/parser/ast/nodes/stmts/ContinueStmt.cpp | 8 +- lib/parser/ast/nodes/stmts/ContinueStmt.hpp | 2 +- lib/parser/ast/nodes/stmts/ExprStmt.cpp | 26 +++++- lib/parser/ast/nodes/stmts/ExprStmt.hpp | 7 +- lib/parser/ast/nodes/stmts/ForStmt.cpp | 50 +++++++++- lib/parser/ast/nodes/stmts/ForStmt.hpp | 15 ++- lib/parser/ast/nodes/stmts/IfStmt.cpp | 50 +++++++++- lib/parser/ast/nodes/stmts/IfStmt.hpp | 6 +- lib/parser/ast/nodes/stmts/ReturnStmt.cpp | 40 +++++++- lib/parser/ast/nodes/stmts/ReturnStmt.hpp | 5 +- lib/parser/ast/nodes/stmts/UnsafeBlock.cpp | 26 +++++- lib/parser/ast/nodes/stmts/UnsafeBlock.hpp | 7 +- lib/parser/ast/nodes/stmts/VarDeclStmt.cpp | 54 ++++++++++- lib/parser/ast/nodes/stmts/VarDeclStmt.hpp | 17 +++- lib/parser/ast/nodes/stmts/WhileStmt.cpp | 42 ++++++++- lib/parser/ast/nodes/stmts/WhileStmt.hpp | 12 ++- 84 files changed, 1968 insertions(+), 103 deletions(-) diff --git a/lib/parser/ast/AstVisitor.hpp b/lib/parser/ast/AstVisitor.hpp index 4bbb7eb..9bd93ca 100644 --- a/lib/parser/ast/AstVisitor.hpp +++ b/lib/parser/ast/AstVisitor.hpp @@ -45,9 +45,10 @@ class AstVisitor { virtual ~AstVisitor() = default; // Decls + virtual void Visit(Module&) = 0; virtual void Visit(FunctionDecl&) = 0; virtual void Visit(ClassDecl&) = 0; - virtual void visit(InterfaceMethod&) = 0; + virtual void Visit(InterfaceMethod&) = 0; virtual void Visit(InterfaceDecl&) = 0; virtual void Visit(TypeAliasDecl&) = 0; virtual void Visit(GlobalVarDecl&) = 0; diff --git a/lib/parser/ast/nodes/base/AstNode.hpp b/lib/parser/ast/nodes/base/AstNode.hpp index a80ba69..1db12fd 100644 --- a/lib/parser/ast/nodes/base/AstNode.hpp +++ b/lib/parser/ast/nodes/base/AstNode.hpp @@ -7,7 +7,7 @@ class AstNode { public: virtual ~AstNode() = default; - virtual void Accept(AstVisitor& v) = 0; + virtual void Accept(AstVisitor& visitor) = 0; }; #endif // ASTNODE_HPP_ diff --git a/lib/parser/ast/nodes/class_members/CallDecl.cpp b/lib/parser/ast/nodes/class_members/CallDecl.cpp index da4d1a7..d2fe8bb 100644 --- a/lib/parser/ast/nodes/class_members/CallDecl.cpp +++ b/lib/parser/ast/nodes/class_members/CallDecl.cpp @@ -1 +1,61 @@ -#include "CallDecl.hpp" +#include "lib/parser/ast/nodes/class_members/CallDecl.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void CallDecl::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +bool CallDecl::IsPublic() const noexcept { + return is_public_; +} + +void CallDecl::SetPublic(bool v) noexcept { + is_public_ = v; +} + +const std::vector& CallDecl::Params() const noexcept { + return params_; +} + +std::vector& CallDecl::MutableParams() noexcept { + return params_; +} + +void CallDecl::AddParam(Param param) { + params_.emplace_back(std::move(param)); +} + +const TypeReference* CallDecl::ReturnType() const noexcept { + return ret_type_.get(); +} + +TypeReference* CallDecl::MutableReturnType() noexcept { + return ret_type_.get(); +} + +void CallDecl::SetReturnType(std::unique_ptr type) { + ret_type_ = std::move(type); +} + +std::unique_ptr CallDecl::ReleaseReturnType() { + return std::move(ret_type_); +} + +const Block* CallDecl::Body() const noexcept { + return body_.get(); +} + +Block* CallDecl::MutableBody() noexcept { + return body_.get(); +} + +void CallDecl::SetBody(std::unique_ptr block) { + body_ = std::move(block); +} + +std::unique_ptr CallDecl::ReleaseBody() { + return std::move(body_); +} diff --git a/lib/parser/ast/nodes/class_members/CallDecl.hpp b/lib/parser/ast/nodes/class_members/CallDecl.hpp index 75f5e38..eab872e 100644 --- a/lib/parser/ast/nodes/class_members/CallDecl.hpp +++ b/lib/parser/ast/nodes/class_members/CallDecl.hpp @@ -11,7 +11,24 @@ class CallDecl : public Decl { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + bool IsPublic() const noexcept; + void SetPublic(bool v) noexcept; + + const std::vector& Params() const noexcept; + std::vector& MutableParams() noexcept; + void AddParam(Param param); + + const TypeReference* ReturnType() const noexcept; + TypeReference* MutableReturnType() noexcept; + void SetReturnType(std::unique_ptr type); + std::unique_ptr ReleaseReturnType(); + + const Block* Body() const noexcept; + Block* MutableBody() noexcept; + void SetBody(std::unique_ptr block); + std::unique_ptr ReleaseBody(); private: bool is_public_ = true; diff --git a/lib/parser/ast/nodes/class_members/DestructorDecl.cpp b/lib/parser/ast/nodes/class_members/DestructorDecl.cpp index 1e0aaf9..d5b1f2c 100644 --- a/lib/parser/ast/nodes/class_members/DestructorDecl.cpp +++ b/lib/parser/ast/nodes/class_members/DestructorDecl.cpp @@ -1 +1,33 @@ -#include "DestructorDecl.hpp" +#include "lib/parser/ast/nodes/class_members/DestructorDecl.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void DestructorDecl::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +bool DestructorDecl::IsPublic() const noexcept { + return is_public_; +} + +void DestructorDecl::SetPublic(bool value) noexcept { + is_public_ = value; +} + +const Block* DestructorDecl::Body() const noexcept { + return body_.get(); +} + +Block* DestructorDecl::MutableBody() noexcept { + return body_.get(); +} + +void DestructorDecl::SetBody(std::unique_ptr block) { + body_ = std::move(block); +} + +std::unique_ptr DestructorDecl::ReleaseBody() { + return std::move(body_); +} diff --git a/lib/parser/ast/nodes/class_members/DestructorDecl.hpp b/lib/parser/ast/nodes/class_members/DestructorDecl.hpp index f25020c..7246a44 100644 --- a/lib/parser/ast/nodes/class_members/DestructorDecl.hpp +++ b/lib/parser/ast/nodes/class_members/DestructorDecl.hpp @@ -8,7 +8,15 @@ class DestructorDecl : public Decl { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + bool IsPublic() const noexcept; + void SetPublic(bool value) noexcept; + + const Block* Body() const noexcept; + Block* MutableBody() noexcept; + void SetBody(std::unique_ptr block); + std::unique_ptr ReleaseBody(); private: bool is_public_ = true; diff --git a/lib/parser/ast/nodes/class_members/FieldDecl.cpp b/lib/parser/ast/nodes/class_members/FieldDecl.cpp index a1dfd47..3c6e105 100644 --- a/lib/parser/ast/nodes/class_members/FieldDecl.cpp +++ b/lib/parser/ast/nodes/class_members/FieldDecl.cpp @@ -1 +1,61 @@ -#include "FieldDecl.hpp" +#include "lib/parser/ast/nodes/class_members/FieldDecl.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void FieldDecl::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +bool FieldDecl::IsPublic() const noexcept { + return is_public_; +} + +void FieldDecl::SetPublic(bool value) noexcept { + is_public_ = value; +} + +bool FieldDecl::IsVar() const noexcept { + return is_var_; +} + +void FieldDecl::SetVar(bool var) noexcept { + is_var_ = var; +} + +const std::string& FieldDecl::Name() const noexcept { + return name_; +} + +void FieldDecl::SetName(std::string name) { + name_ = std::move(name); +} + +const TypeReference& FieldDecl::Type() const noexcept { + return type_; +} + +TypeReference& FieldDecl::MutableType() noexcept { + return type_; +} + +void FieldDecl::SetType(TypeReference type) { + type_ = std::move(type); +} + +const Expr* FieldDecl::Init() const noexcept { + return init_.get(); +} + +Expr* FieldDecl::MutableInit() noexcept { + return init_.get(); +} + +void FieldDecl::SetInit(std::unique_ptr expr) { + init_ = std::move(expr); +} + +std::unique_ptr FieldDecl::ReleaseInit() { + return std::move(init_); +} diff --git a/lib/parser/ast/nodes/class_members/FieldDecl.hpp b/lib/parser/ast/nodes/class_members/FieldDecl.hpp index 4c16781..ce7e5e6 100644 --- a/lib/parser/ast/nodes/class_members/FieldDecl.hpp +++ b/lib/parser/ast/nodes/class_members/FieldDecl.hpp @@ -5,18 +5,37 @@ #include #include "lib/parser/ast/nodes/base/Decl.hpp" +#include "lib/parser/ast/nodes/base/Expr.hpp" #include "lib/parser/types/TypeReference.hpp" class FieldDecl : public Decl { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + bool IsPublic() const noexcept; + void SetPublic(bool value) noexcept; + + bool IsVar() const noexcept; + void SetVar(bool var) noexcept; + + const std::string& Name() const noexcept; + void SetName(std::string name); + + const TypeReference& Type() const noexcept; + TypeReference& MutableType() noexcept; + void SetType(TypeReference type); + + const Expr* Init() const noexcept; + Expr* MutableInit() noexcept; + void SetInit(std::unique_ptr expr); + std::unique_ptr ReleaseInit(); private: bool is_public_ = true; bool is_var_ = false; std::string name_; TypeReference type_; - std::unique_ptr init_; // optional + std::unique_ptr init_; // optional }; #endif // FIELDDECL_HPP_ diff --git a/lib/parser/ast/nodes/class_members/MethodDecl.cpp b/lib/parser/ast/nodes/class_members/MethodDecl.cpp index 56211fb..cd64a85 100644 --- a/lib/parser/ast/nodes/class_members/MethodDecl.cpp +++ b/lib/parser/ast/nodes/class_members/MethodDecl.cpp @@ -1 +1,92 @@ -#include "MethodDecl.hpp" +#include "lib/parser/ast/nodes/class_members/MethodDecl.hpp" +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void MethodDecl::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +bool MethodDecl::IsPublic() const noexcept { + return is_public_; +} + +void MethodDecl::SetPublic(bool is_public) noexcept { + is_public_ = is_public; +} + +bool MethodDecl::IsOverride() const noexcept { + return is_override_; +} + +void MethodDecl::SetOverride(bool is_override) noexcept { + is_override_ = is_override; +} + +bool MethodDecl::IsStatic() const noexcept { + return is_static_; +} + +void MethodDecl::SetStatic(bool is_static) noexcept { + is_static_ = is_static; +} + +bool MethodDecl::IsPure() const noexcept { + return is_pure_; +} + +void MethodDecl::SetPure(bool is_pure) noexcept { + is_pure_ = is_pure; +} + +const std::string& MethodDecl::Name() const noexcept { + return name; +} + +void MethodDecl::SetName(std::string new_name) { + name = std::move(new_name); +} + +const std::vector& MethodDecl::Params() const noexcept { + return params; +} + +std::vector& MethodDecl::MutableParams() noexcept { + return params; +} + +void MethodDecl::AddParam(Param param) { + params.emplace_back(std::move(param)); +} + +const TypeReference* MethodDecl::ReturnType() const noexcept { + return ret_type.get(); +} + +TypeReference* MethodDecl::MutableReturnType() noexcept { + return ret_type.get(); +} + +void MethodDecl::SetReturnType(std::unique_ptr type) { + ret_type = std::move(type); +} + +std::unique_ptr MethodDecl::ReleaseReturnType() { + return std::move(ret_type); +} + +const Block* MethodDecl::Body() const noexcept { + return body.get(); +} + +Block* MethodDecl::MutableBody() noexcept { + return body.get(); +} + +void MethodDecl::SetBody(std::unique_ptr block) { + body = std::move(block); +} + +std::unique_ptr MethodDecl::ReleaseBody() { + return std::move(body); +} diff --git a/lib/parser/ast/nodes/class_members/MethodDecl.hpp b/lib/parser/ast/nodes/class_members/MethodDecl.hpp index b6ac3f9..48cf063 100644 --- a/lib/parser/ast/nodes/class_members/MethodDecl.hpp +++ b/lib/parser/ast/nodes/class_members/MethodDecl.hpp @@ -12,7 +12,36 @@ class MethodDecl : public Decl { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + bool IsPublic() const noexcept; + void SetPublic(bool is_public) noexcept; + + bool IsOverride() const noexcept; + void SetOverride(bool is_override) noexcept; + + bool IsStatic() const noexcept; + void SetStatic(bool is_static) noexcept; + + bool IsPure() const noexcept; + void SetPure(bool is_pure) noexcept; + + const std::string& Name() const noexcept; + void SetName(std::string new_name); + + const std::vector& Params() const noexcept; + std::vector& MutableParams() noexcept; + void AddParam(Param param); + + const TypeReference* ReturnType() const noexcept; + TypeReference* MutableReturnType() noexcept; + void SetReturnType(std::unique_ptr type); + std::unique_ptr ReleaseReturnType(); + + const Block* Body() const noexcept; + Block* MutableBody() noexcept; + void SetBody(std::unique_ptr block); + std::unique_ptr ReleaseBody(); private: bool is_public_ = true; diff --git a/lib/parser/ast/nodes/class_members/StaticFieldDecl.cpp b/lib/parser/ast/nodes/class_members/StaticFieldDecl.cpp index bc96e1b..dd666db 100644 --- a/lib/parser/ast/nodes/class_members/StaticFieldDecl.cpp +++ b/lib/parser/ast/nodes/class_members/StaticFieldDecl.cpp @@ -1 +1,61 @@ -#include "StaticFieldDecl.hpp" +#include "lib/parser/ast/nodes/class_members/StaticFieldDecl.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void StaticFieldDecl::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +bool StaticFieldDecl::IsPublic() const noexcept { + return is_public_; +} + +void StaticFieldDecl::SetPublic(bool is_public) noexcept { + is_public_ = is_public; +} + +bool StaticFieldDecl::IsVar() const noexcept { + return is_var_; +} + +void StaticFieldDecl::SetVar(bool is_var) noexcept { + is_var_ = is_var; +} + +const std::string& StaticFieldDecl::Name() const noexcept { + return name_; +} + +void StaticFieldDecl::SetName(std::string new_name) { + name_ = std::move(new_name); +} + +const TypeReference& StaticFieldDecl::Type() const noexcept { + return type_; +} + +TypeReference& StaticFieldDecl::MutableType() noexcept { + return type_; +} + +void StaticFieldDecl::SetType(TypeReference type) { + type_ = std::move(type); +} + +const Expr* StaticFieldDecl::Init() const noexcept { + return init_.get(); +} + +Expr* StaticFieldDecl::MutableInit() noexcept { + return init_.get(); +} + +void StaticFieldDecl::SetInit(std::unique_ptr expr) { + init_ = std::move(expr); +} + +std::unique_ptr StaticFieldDecl::ReleaseInit() { + return std::move(init_); +} diff --git a/lib/parser/ast/nodes/class_members/StaticFieldDecl.hpp b/lib/parser/ast/nodes/class_members/StaticFieldDecl.hpp index 2a38f72..0075ea1 100644 --- a/lib/parser/ast/nodes/class_members/StaticFieldDecl.hpp +++ b/lib/parser/ast/nodes/class_members/StaticFieldDecl.hpp @@ -10,7 +10,25 @@ class StaticFieldDecl : public Decl { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + bool IsPublic() const noexcept; + void SetPublic(bool is_public) noexcept; + + bool IsVar() const noexcept; + void SetVar(bool is_var) noexcept; + + const std::string& Name() const noexcept; + void SetName(std::string new_name); + + const TypeReference& Type() const noexcept; + TypeReference& MutableType() noexcept; + void SetType(TypeReference type); + + const Expr* Init() const noexcept; + Expr* MutableInit() noexcept; + void SetInit(std::unique_ptr expr); + std::unique_ptr ReleaseInit(); private: bool is_public_ = true; diff --git a/lib/parser/ast/nodes/decls/ClassDecl.cpp b/lib/parser/ast/nodes/decls/ClassDecl.cpp index b635f04..f4011e3 100644 --- a/lib/parser/ast/nodes/decls/ClassDecl.cpp +++ b/lib/parser/ast/nodes/decls/ClassDecl.cpp @@ -1 +1,49 @@ #include "ClassDecl.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +void ClassDecl::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const std::string& ClassDecl::Name() const noexcept { + return name_; +} + +void ClassDecl::SetName(std::string name) { + name_ = std::move(name); +} + +const std::vector& ClassDecl::Implements() const noexcept { + return implements_; +} + +std::vector& ClassDecl::MutableImplements() noexcept { + return implements_; +} + +void ClassDecl::AddImplements(TypeReference type) { + implements_.emplace_back(std::move(type)); +} + +const std::vector>& ClassDecl::Members() const noexcept { + return members_; +} + +std::vector>& ClassDecl::MutableMembers() noexcept { + return members_; +} + +void ClassDecl::AddMember(std::unique_ptr decl) { + members_.emplace_back(std::move(decl)); +} + +std::unique_ptr ClassDecl::ReleaseMember(std::size_t index) { + if (index >= members_.size()) { + return nullptr; + } + + auto old = std::move(members_[index]); + members_.erase(members_.begin() + static_cast(index)); + return old; +} diff --git a/lib/parser/ast/nodes/decls/ClassDecl.hpp b/lib/parser/ast/nodes/decls/ClassDecl.hpp index 1f7575d..a74c7ef 100644 --- a/lib/parser/ast/nodes/decls/ClassDecl.hpp +++ b/lib/parser/ast/nodes/decls/ClassDecl.hpp @@ -10,7 +10,19 @@ class ClassDecl : public Decl { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + const std::string& Name() const noexcept; + void SetName(std::string name); + + const std::vector& Implements() const noexcept; + std::vector& MutableImplements() noexcept; + void AddImplements(TypeReference type); + + const std::vector>& Members() const noexcept; + std::vector>& MutableMembers() noexcept; + void AddMember(std::unique_ptr decl); + std::unique_ptr ReleaseMember(std::size_t index); private: std::string name_; diff --git a/lib/parser/ast/nodes/decls/FunctionDecl.cpp b/lib/parser/ast/nodes/decls/FunctionDecl.cpp index 0b382d7..484b281 100644 --- a/lib/parser/ast/nodes/decls/FunctionDecl.cpp +++ b/lib/parser/ast/nodes/decls/FunctionDecl.cpp @@ -1 +1,66 @@ #include "FunctionDecl.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +void FunctionDecl::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +bool FunctionDecl::IsPure() const noexcept { + return is_pure_; +} + +void FunctionDecl::SetPure(bool is_pure) noexcept { + is_pure_ = is_pure; +} + +const std::string& FunctionDecl::Name() const noexcept { + return name_; +} + +void FunctionDecl::SetName(std::string new_name) { + name_ = std::move(new_name); +} + +const std::vector& FunctionDecl::Params() const noexcept { + return params_; +} + +std::vector& FunctionDecl::MutableParams() noexcept { + return params_; +} + +void FunctionDecl::AddParam(Param param) { + params_.emplace_back(std::move(param)); +} + +const TypeReference* FunctionDecl::ReturnType() const noexcept { + return return_type_.get(); +} +TypeReference* FunctionDecl::MutableReturnType() noexcept { + return return_type_.get(); +} + +void FunctionDecl::SetReturnType(std::unique_ptr type) { + return_type_ = std::move(type); +} + +std::unique_ptr FunctionDecl::ReleaseReturnType() { + return std::move(return_type_); +} + +const Block* FunctionDecl::Body() const noexcept { + return body_.get(); +} + +Block* FunctionDecl::MutableBody() noexcept { + return body_.get(); +} + +void FunctionDecl::SetBody(std::unique_ptr block) { + body_ = std::move(block); +} + +std::unique_ptr FunctionDecl::ReleaseBody() { + return std::move(body_); +} diff --git a/lib/parser/ast/nodes/decls/FunctionDecl.hpp b/lib/parser/ast/nodes/decls/FunctionDecl.hpp index e7d75f5..b8c0e4a 100644 --- a/lib/parser/ast/nodes/decls/FunctionDecl.hpp +++ b/lib/parser/ast/nodes/decls/FunctionDecl.hpp @@ -9,9 +9,31 @@ #include "lib/parser/ast/nodes/stmts/Block.hpp" #include "lib/parser/types/Param.hpp" +class TypeReference; + class FunctionDecl : public Decl { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + bool IsPure() const noexcept; + void SetPure(bool is_pure) noexcept; + + const std::string& Name() const noexcept; + void SetName(std::string new_name); + + const std::vector& Params() const noexcept; + std::vector& MutableParams() noexcept; + void AddParam(Param param); + + const TypeReference* ReturnType() const noexcept; + TypeReference* MutableReturnType() noexcept; + void SetReturnType(std::unique_ptr type); + std::unique_ptr ReleaseReturnType(); + + const Block* Body() const noexcept; + Block* MutableBody() noexcept; + void SetBody(std::unique_ptr block); + std::unique_ptr ReleaseBody(); private: bool is_pure_ = false; diff --git a/lib/parser/ast/nodes/decls/GlobalVarDecl.cpp b/lib/parser/ast/nodes/decls/GlobalVarDecl.cpp index ea1652d..17ecb7a 100644 --- a/lib/parser/ast/nodes/decls/GlobalVarDecl.cpp +++ b/lib/parser/ast/nodes/decls/GlobalVarDecl.cpp @@ -1 +1,51 @@ #include "GlobalVarDecl.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +void GlobalVarDecl::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +bool GlobalVarDecl::IsVar() const noexcept { + return is_var_; +} + +void GlobalVarDecl::SetVar(bool is_var) noexcept { + is_var_ = is_var; +} + +const std::string& GlobalVarDecl::Name() const noexcept { + return name_; +} + +void GlobalVarDecl::SetName(std::string new_name) { + name_ = std::move(new_name); +} + +const TypeReference& GlobalVarDecl::Type() const noexcept { + return type_; +} + +TypeReference& GlobalVarDecl::MutableType() noexcept { + return type_; +} + +void GlobalVarDecl::SetType(TypeReference type) { + type_ = std::move(type); +} + +const Expr* GlobalVarDecl::Init() const noexcept { + return init_.get(); +} + +Expr* GlobalVarDecl::MutableInit() noexcept { + return init_.get(); +} + +void GlobalVarDecl::SetInit(std::unique_ptr expr) { + init_ = std::move(expr); +} + +std::unique_ptr GlobalVarDecl::ReleaseInit() { + return std::move(init_); +} diff --git a/lib/parser/ast/nodes/decls/GlobalVarDecl.hpp b/lib/parser/ast/nodes/decls/GlobalVarDecl.hpp index 6548077..a7605c7 100644 --- a/lib/parser/ast/nodes/decls/GlobalVarDecl.hpp +++ b/lib/parser/ast/nodes/decls/GlobalVarDecl.hpp @@ -10,7 +10,22 @@ class GlobalVarDecl : public Decl { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + bool IsVar() const noexcept; + void SetVar(bool is_var) noexcept; + + const std::string& Name() const noexcept; + void SetName(std::string new_name); + + const TypeReference& Type() const noexcept; + TypeReference& MutableType() noexcept; + void SetType(TypeReference type); + + const Expr* Init() const noexcept; + Expr* MutableInit() noexcept; + void SetInit(std::unique_ptr expr); + std::unique_ptr ReleaseInit(); private: bool is_var_ = false; // var=true, val=false diff --git a/lib/parser/ast/nodes/decls/InterfaceDecl.cpp b/lib/parser/ast/nodes/decls/InterfaceDecl.cpp index a6688f8..00ec1a0 100644 --- a/lib/parser/ast/nodes/decls/InterfaceDecl.cpp +++ b/lib/parser/ast/nodes/decls/InterfaceDecl.cpp @@ -1 +1,37 @@ #include "InterfaceDecl.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +void InterfaceDecl::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const std::string& InterfaceDecl::Name() const noexcept { + return name_; +} + +void InterfaceDecl::SetName(std::string new_name) { + name_ = std::move(new_name); +} + +const std::vector>& InterfaceDecl::Members() const noexcept { + return methods_; +} + +std::vector>& InterfaceDecl::MutableMembers() noexcept { + return methods_; +} + +void InterfaceDecl::AddMember(std::unique_ptr method) { + methods_.emplace_back(std::move(method)); +} + +std::unique_ptr InterfaceDecl::ReleaseMember(std::size_t index) { + if (index >= methods_.size()) { + return nullptr; + } + + auto old = std::move(methods_[index]); + methods_.erase(methods_.begin() + static_cast(index)); + return old; +} diff --git a/lib/parser/ast/nodes/decls/InterfaceDecl.hpp b/lib/parser/ast/nodes/decls/InterfaceDecl.hpp index 45b95c6..706c7cc 100644 --- a/lib/parser/ast/nodes/decls/InterfaceDecl.hpp +++ b/lib/parser/ast/nodes/decls/InterfaceDecl.hpp @@ -10,14 +10,17 @@ class InterfaceDecl : public Decl { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; const std::string& Name() const noexcept; - void SetName(std::string n); + void SetName(std::string new_name); const std::vector>& Members() const noexcept; std::vector>& MutableMembers() noexcept; + void AddMember(std::unique_ptr method); + std::unique_ptr ReleaseMember(std::size_t index); + private: std::string name_; std::vector> methods_; diff --git a/lib/parser/ast/nodes/decls/InterfaceMethod.cpp b/lib/parser/ast/nodes/decls/InterfaceMethod.cpp index d0e5786..07787ec 100644 --- a/lib/parser/ast/nodes/decls/InterfaceMethod.cpp +++ b/lib/parser/ast/nodes/decls/InterfaceMethod.cpp @@ -1 +1,33 @@ -#include "InterfaceMethod.hpp" +#include "lib/parser/ast/nodes/decls/InterfaceMethod.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void InterfaceMethod::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const std::string& InterfaceMethod::Name() const noexcept { + return name_; +} + +void InterfaceMethod::SetName(std::string new_name) { + name_ = std::move(new_name); +} + +const std::vector& InterfaceMethod::Params() const noexcept { + return params_; +} + +std::vector& InterfaceMethod::MutableParams() noexcept { + return params_; +} + +const TypeReference* InterfaceMethod::ReturnType() const noexcept { + return ret_type_.get(); +} + +void InterfaceMethod::SetReturnType(std::unique_ptr type) { + ret_type_ = std::move(type); +} diff --git a/lib/parser/ast/nodes/decls/InterfaceMethod.hpp b/lib/parser/ast/nodes/decls/InterfaceMethod.hpp index 5bbfdf6..6406b92 100644 --- a/lib/parser/ast/nodes/decls/InterfaceMethod.hpp +++ b/lib/parser/ast/nodes/decls/InterfaceMethod.hpp @@ -15,16 +15,16 @@ class InterfaceMethod : public Decl { TypeReference type; }; - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; const std::string& Name() const noexcept; - void SetName(std::string n); + void SetName(std::string new_name); const std::vector& Params() const noexcept; std::vector& MutableParams() noexcept; const TypeReference* ReturnType() const noexcept; - void SetReturnType(std::unique_ptr t); + void SetReturnType(std::unique_ptr type); private: std::string name_; diff --git a/lib/parser/ast/nodes/decls/Module.cpp b/lib/parser/ast/nodes/decls/Module.cpp index 6f69fe8..39e766a 100644 --- a/lib/parser/ast/nodes/decls/Module.cpp +++ b/lib/parser/ast/nodes/decls/Module.cpp @@ -1 +1,45 @@ #include "Module.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +void Module::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const std::string& Module::Name() const noexcept { + return name_; +} + +void Module::SetName(std::string new_name) { + name_ = std::move(new_name); +} + +const SourceId& Module::Source() const noexcept { + return source_; +} + +void Module::SetSource(SourceId id) { + source_ = id; +} + +const std::vector>& Module::Decls() const noexcept { + return decls_; +} + +std::vector>& Module::MutableDecls() noexcept { + return decls_; +} + +void Module::AddDecl(std::unique_ptr decl) { + decls_.emplace_back(std::move(decl)); +} + +std::unique_ptr Module::ReleaseDecl(std::size_t index) { + if (index >= decls_.size()) { + return nullptr; + } + + auto old = std::move(decls_[index]); + decls_.erase(decls_.begin() + index); + return std::move(old); +} diff --git a/lib/parser/ast/nodes/decls/Module.hpp b/lib/parser/ast/nodes/decls/Module.hpp index 1d204ea..4537df7 100644 --- a/lib/parser/ast/nodes/decls/Module.hpp +++ b/lib/parser/ast/nodes/decls/Module.hpp @@ -11,17 +11,17 @@ class Module : public AstNode { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; const std::string& Name() const noexcept; - void SetName(std::string n); + void SetName(std::string new_name); const SourceId& Source() const noexcept; void SetSource(SourceId id); const std::vector>& Decls() const noexcept; std::vector>& MutableDecls() noexcept; - void AddDecl(std::unique_ptr d); + void AddDecl(std::unique_ptr decl); std::unique_ptr ReleaseDecl(std::size_t index); private: diff --git a/lib/parser/ast/nodes/decls/TypeAliasDecl.cpp b/lib/parser/ast/nodes/decls/TypeAliasDecl.cpp index 5cc1e41..6c14fc9 100644 --- a/lib/parser/ast/nodes/decls/TypeAliasDecl.cpp +++ b/lib/parser/ast/nodes/decls/TypeAliasDecl.cpp @@ -1 +1,29 @@ -#include "TypeAliasDecl.hpp" +#include "lib/parser/ast/nodes/decls/TypeAliasDecl.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void TypeAliasDecl::Accept(AstVisitor& visitor) { + v.Visit(*this); +} + +const std::string& TypeAliasDecl::Name() const noexcept { + return name_; +} + +void TypeAliasDecl::SetName(std::string new_name) { + name_ = std::move(new_name); +} + +const TypeReference& TypeAliasDecl::AliasedType() const noexcept { + return aliased_type_; +} + +TypeReference& TypeAliasDecl::MutableAliasedType() noexcept { + return aliased_type_; +} + +void TypeAliasDecl::SetAliasedType(TypeReference type) { + aliased_type_ = std::move(type); +} diff --git a/lib/parser/ast/nodes/decls/TypeAliasDecl.hpp b/lib/parser/ast/nodes/decls/TypeAliasDecl.hpp index fabfa5d..c0c8d40 100644 --- a/lib/parser/ast/nodes/decls/TypeAliasDecl.hpp +++ b/lib/parser/ast/nodes/decls/TypeAliasDecl.hpp @@ -8,7 +8,14 @@ class TypeAliasDecl : public Decl { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + const std::string& Name() const noexcept; + void SetName(std::string new_name); + + const TypeReference& AliasedType() const noexcept; + TypeReference& MutableAliasedType() noexcept; + void SetAliasedType(TypeReference type); private: std::string name_; diff --git a/lib/parser/ast/nodes/exprs/Assign.cpp b/lib/parser/ast/nodes/exprs/Assign.cpp index 8775393..01f19ad 100644 --- a/lib/parser/ast/nodes/exprs/Assign.cpp +++ b/lib/parser/ast/nodes/exprs/Assign.cpp @@ -1 +1,53 @@ -#include "Assign.hpp" +#include "lib/parser/ast/nodes/exprs/Assign.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void Assign::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const IAssignOpTag& Assign::Kind() const noexcept { + return *kind_; +} + +void Assign::SetKind(const IAssignOpTag& new_kind) noexcept { + kind_ = &new_kind; +} + +const Expr& Assign::Target() const noexcept { + return *target_; +} + +Expr& Assign::MutableTarget() noexcept { + return *target_; +} + +void Assign::SetTarget(std::unique_ptr new_target) { + target_ = std::move(new_target); +} + +std::unique_ptr Assign::ReplaceTarget(std::unique_ptr new_target) { + auto old_target = std::move(target_); + target_ = std::move(new_target); + return old_target; +} + +const Expr& Assign::Value() const noexcept { + return *value_; +} + +Expr& Assign::MutableValue() noexcept { + return *value_; +} + +void Assign::SetValue(std::unique_ptr new_value) { + value_ = std::move(new_value); +} + +std::unique_ptr Assign::ReplaceValue(std::unique_ptr new_value) { + auto old_value = std::move(value_); + value_ = std::move(new_value); + return old_value; +} diff --git a/lib/parser/ast/nodes/exprs/Assign.hpp b/lib/parser/ast/nodes/exprs/Assign.hpp index 4a08d69..adebde9 100644 --- a/lib/parser/ast/nodes/exprs/Assign.hpp +++ b/lib/parser/ast/nodes/exprs/Assign.hpp @@ -9,7 +9,20 @@ class Assign : public Expr { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + const IAssignOpTag& Kind() const noexcept; + void SetKind(const IAssignOpTag& new_kind) noexcept; + + const Expr& Target() const noexcept; + Expr& MutableTarget() noexcept; + void SetTarget(std::unique_ptr new_target); + std::unique_ptr ReplaceTarget(std::unique_ptr new_target); + + const Expr& Value() const noexcept; + Expr& MutableValue() noexcept; + void SetValue(std::unique_ptr new_value); + std::unique_ptr ReplaceValue(std::unique_ptr new_value); private: const IAssignOpTag* kind_ = &OpTags::RefAssign(); diff --git a/lib/parser/ast/nodes/exprs/Binary.cpp b/lib/parser/ast/nodes/exprs/Binary.cpp index 313429d..9d42d2f 100644 --- a/lib/parser/ast/nodes/exprs/Binary.cpp +++ b/lib/parser/ast/nodes/exprs/Binary.cpp @@ -1 +1,53 @@ -#include "Binary.hpp" +#include "lib/parser/ast/nodes/exprs/Binary.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void Binary::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const IBinaryOpTag& Binary::Op() const noexcept { + return *op_; +} + +void Binary::SetOp(const IBinaryOpTag& new_op) noexcept { + op_ = &new_op; +} + +const Expr& Binary::Lhs() const noexcept { + return *lhs_; +} + +Expr& Binary::MutableLhs() noexcept { + return *lhs_; +} + +void Binary::SetLhs(std::unique_ptr new_lhs) { + lhs_ = std::move(new_lhs); +} + +std::unique_ptr Binary::ReplaceLhs(std::unique_ptr new_lhs) { + auto old_lhs = std::move(lhs_); + lhs_ = std::move(new_lhs); + return old_lhs; +} + +const Expr& Binary::Rhs() const noexcept { + return *rhs_; +} + +Expr& Binary::MutableRhs() noexcept { + return *rhs_; +} + +void Binary::SetRhs(std::unique_ptr new_rhs) { + rhs_ = std::move(new_rhs); +} + +std::unique_ptr Binary::ReplaceRhs(std::unique_ptr new_rhs) { + auto old_rhs = std::move(rhs_); + rhs_ = std::move(new_rhs); + return old_rhs; +} diff --git a/lib/parser/ast/nodes/exprs/Binary.hpp b/lib/parser/ast/nodes/exprs/Binary.hpp index 3869f77..53cc9f7 100644 --- a/lib/parser/ast/nodes/exprs/Binary.hpp +++ b/lib/parser/ast/nodes/exprs/Binary.hpp @@ -4,11 +4,25 @@ #include #include "lib/parser/ast/nodes/base/Expr.hpp" +#include "tags/IBinaryOpTag.hpp" #include "tags/OpTags.hpp" class Binary : public Expr { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + const IBinaryOpTag& Op() const noexcept; + void SetOp(const IBinaryOpTag& new_op) noexcept; + + const Expr& Lhs() const noexcept; + Expr& MutableLhs() noexcept; + void SetLhs(std::unique_ptr new_lhs); + std::unique_ptr ReplaceLhs(std::unique_ptr new_lhs); + + const Expr& Rhs() const noexcept; + Expr& MutableRhs() noexcept; + void SetRhs(std::unique_ptr new_rhs); + std::unique_ptr ReplaceRhs(std::unique_ptr new_rhs); private: const IBinaryOpTag* op_ = &OpTags::Add(); diff --git a/lib/parser/ast/nodes/exprs/Call.cpp b/lib/parser/ast/nodes/exprs/Call.cpp index e2c6e69..cb88ab1 100644 --- a/lib/parser/ast/nodes/exprs/Call.cpp +++ b/lib/parser/ast/nodes/exprs/Call.cpp @@ -1 +1,42 @@ -#include "Call.hpp" +#include "lib/parser/ast/AstVisitor.hpp" +#include "lib/parser/ast/nodes/exprs/Call.hpp" + +#include + +void Call::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const Expr& Call::Callee() const noexcept { + return *callee_; +} + +Expr& Call::MutableCallee() noexcept { + return *callee_; +} + +void Call::SetCallee(std::unique_ptr new_callee) { + callee_ = std::move(new_callee); +} + +std::unique_ptr Call::ReplaceCallee(std::unique_ptr new_callee) { + auto old_callee = std::move(callee_); + callee_ = std::move(new_callee); + return old_callee; +} + +const std::vector>& Call::Args() const noexcept { + return args_; +} + +std::vector>& Call::MutableArgs() noexcept { + return args_; +} + +void Call::AddArg(std::unique_ptr new_arg) { + args_.emplace_back(std::move(new_arg)); +} + +void Call::ClearArgs() { + args_.clear(); +} diff --git a/lib/parser/ast/nodes/exprs/Call.hpp b/lib/parser/ast/nodes/exprs/Call.hpp index 1e442f1..b2725f8 100644 --- a/lib/parser/ast/nodes/exprs/Call.hpp +++ b/lib/parser/ast/nodes/exprs/Call.hpp @@ -8,7 +8,17 @@ class Call : public Expr { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + const Expr& Callee() const noexcept; + Expr& MutableCallee() noexcept; + void SetCallee(std::unique_ptr new_callee); + std::unique_ptr ReplaceCallee(std::unique_ptr new_callee); + + const std::vector>& Args() const noexcept; + std::vector>& MutableArgs() noexcept; + void AddArg(std::unique_ptr new_arg); + void ClearArgs(); private: std::unique_ptr callee_; diff --git a/lib/parser/ast/nodes/exprs/CastAs.cpp b/lib/parser/ast/nodes/exprs/CastAs.cpp index fd6ffb2..abf545f 100644 --- a/lib/parser/ast/nodes/exprs/CastAs.cpp +++ b/lib/parser/ast/nodes/exprs/CastAs.cpp @@ -1 +1,39 @@ -#include "CastAs.hpp" +#include "lib/parser/ast/nodes/exprs/CastAs.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void CastAs::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const Expr& CastAs::Expression() const noexcept { + return *expr_; +} + +Expr& CastAs::MutableExpression() noexcept { + return *expr_; +} + +void CastAs::SetExpression(std::unique_ptr new_expression) { + expr_ = std::move(new_expression); +} + +std::unique_ptr CastAs::ReplaceExpression(std::unique_ptr new_expression) { + auto old_expr = std::move(expr_); + expr_ = std::move(new_expression); + return old_expr; +} + +const TypeReference& CastAs::Type() const noexcept { + return type_; +} + +TypeReference& CastAs::MutableType() noexcept { + return type_; +} + +void CastAs::SetType(TypeReference new_type) { + type_ = std::move(new_type); +} diff --git a/lib/parser/ast/nodes/exprs/CastAs.hpp b/lib/parser/ast/nodes/exprs/CastAs.hpp index 3d891c6..ac1c2e9 100644 --- a/lib/parser/ast/nodes/exprs/CastAs.hpp +++ b/lib/parser/ast/nodes/exprs/CastAs.hpp @@ -8,7 +8,16 @@ class CastAs : public Expr { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + const Expr& Expression() const noexcept; + Expr& MutableExpression() noexcept; + void SetExpression(std::unique_ptr new_expression); + std::unique_ptr ReplaceExpression(std::unique_ptr new_expression); + + const TypeReference& Type() const noexcept; + TypeReference& MutableType() noexcept; + void SetType(TypeReference new_type); private: std::unique_ptr expr_; diff --git a/lib/parser/ast/nodes/exprs/Elvis.cpp b/lib/parser/ast/nodes/exprs/Elvis.cpp index 87484ef..304a02a 100644 --- a/lib/parser/ast/nodes/exprs/Elvis.cpp +++ b/lib/parser/ast/nodes/exprs/Elvis.cpp @@ -1 +1,45 @@ -#include "Elvis.hpp" +#include "lib/parser/ast/nodes/exprs/Elvis.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void Elvis::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const Expr& Elvis::Lhs() const noexcept { + return *lhs_; +} + +Expr& Elvis::MutableLhs() noexcept { + return *lhs_; +} + +void Elvis::SetLhs(std::unique_ptr new_lhs) { + lhs_ = std::move(new_lhs); +} + +std::unique_ptr Elvis::ReplaceLhs(std::unique_ptr new_lhs) { + auto old_lhs = std::move(lhs_); + lhs_ = std::move(new_lhs); + return old_lhs; +} + +const Expr& Elvis::Rhs() const noexcept { + return *rhs_; +} + +Expr& Elvis::MutableRhs() noexcept { + return *rhs_; +} + +void Elvis::SetRhs(std::unique_ptr new_rhs) { + rhs_ = std::move(new_rhs); +} + +std::unique_ptr Elvis::ReplaceRhs(std::unique_ptr new_rhs) { + auto old_rhs = std::move(rhs_); + rhs_ = std::move(new_rhs); + return old_rhs; +} diff --git a/lib/parser/ast/nodes/exprs/Elvis.hpp b/lib/parser/ast/nodes/exprs/Elvis.hpp index 563a689..cdba57d 100644 --- a/lib/parser/ast/nodes/exprs/Elvis.hpp +++ b/lib/parser/ast/nodes/exprs/Elvis.hpp @@ -7,7 +7,17 @@ class Elvis : public Expr { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + const Expr& Lhs() const noexcept; + Expr& MutableLhs() noexcept; + void SetLhs(std::unique_ptr new_lhs); + std::unique_ptr ReplaceLhs(std::unique_ptr new_lhs); + + const Expr& Rhs() const noexcept; + Expr& MutableRhs() noexcept; + void SetRhs(std::unique_ptr new_rhs); + std::unique_ptr ReplaceRhs(std::unique_ptr new_rhs); private: std::unique_ptr lhs_; diff --git a/lib/parser/ast/nodes/exprs/FieldAccess.cpp b/lib/parser/ast/nodes/exprs/FieldAccess.cpp index 6cc69c2..cd553d6 100644 --- a/lib/parser/ast/nodes/exprs/FieldAccess.cpp +++ b/lib/parser/ast/nodes/exprs/FieldAccess.cpp @@ -1 +1,35 @@ -#include "FieldAccess.hpp" +#include "lib/parser/ast/nodes/exprs/FieldAccess.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void FieldAccess::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const Expr& FieldAccess::Object() const noexcept { + return *object_; +} + +Expr& FieldAccess::MutableObject() noexcept { + return *object_; +} + +void FieldAccess::SetObject(std::unique_ptr new_object) { + object_ = std::move(new_object); +} + +std::unique_ptr FieldAccess::ReplaceObject(std::unique_ptr new_object) { + auto old_object = std::move(object_); + object_ = std::move(new_object); + return old_object; +} + +const std::string& FieldAccess::Name() const noexcept { + return name_; +} + +void FieldAccess::SetName(std::string new_name) { + name_ = std::move(new_name); +} diff --git a/lib/parser/ast/nodes/exprs/FieldAccess.hpp b/lib/parser/ast/nodes/exprs/FieldAccess.hpp index a962576..2a9a123 100644 --- a/lib/parser/ast/nodes/exprs/FieldAccess.hpp +++ b/lib/parser/ast/nodes/exprs/FieldAccess.hpp @@ -8,7 +8,15 @@ class FieldAccess : public Expr { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + const Expr& Object() const noexcept; + Expr& MutableObject() noexcept; + void SetObject(std::unique_ptr new_object); + std::unique_ptr ReplaceObject(std::unique_ptr new_object); + + const std::string& Name() const noexcept; + void SetName(std::string new_name); private: std::unique_ptr object_; diff --git a/lib/parser/ast/nodes/exprs/IdentRef.cpp b/lib/parser/ast/nodes/exprs/IdentRef.cpp index 718f162..711acbb 100644 --- a/lib/parser/ast/nodes/exprs/IdentRef.cpp +++ b/lib/parser/ast/nodes/exprs/IdentRef.cpp @@ -1 +1,17 @@ -#include "IdentRef.hpp" +#include "lib/parser/ast/nodes/exprs/IdentRef.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void IdentRef::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const std::string& IdentRef::Name() const noexcept { + return name_; +} + +void IdentRef::SetName(std::string new_name) { + name_ = std::move(new_name); +} diff --git a/lib/parser/ast/nodes/exprs/IdentRef.hpp b/lib/parser/ast/nodes/exprs/IdentRef.hpp index cc6ca4d..1494077 100644 --- a/lib/parser/ast/nodes/exprs/IdentRef.hpp +++ b/lib/parser/ast/nodes/exprs/IdentRef.hpp @@ -7,7 +7,10 @@ class IdentRef : public Expr { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + const std::string& Name() const noexcept; + void SetName(std::string new_name); private: std::string name_; diff --git a/lib/parser/ast/nodes/exprs/IndexAccess.cpp b/lib/parser/ast/nodes/exprs/IndexAccess.cpp index 23b647c..ccd0b61 100644 --- a/lib/parser/ast/nodes/exprs/IndexAccess.cpp +++ b/lib/parser/ast/nodes/exprs/IndexAccess.cpp @@ -1 +1,45 @@ -#include "IndexAccess.hpp" +#include "lib/parser/ast/nodes/exprs/IndexAccess.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void IndexAccess::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const Expr& IndexAccess::Object() const noexcept { + return *object_; +} + +Expr& IndexAccess::MutableObject() noexcept { + return *object_; +} + +void IndexAccess::SetObject(std::unique_ptr new_object) { + object_ = std::move(new_object); +} + +std::unique_ptr IndexAccess::ReplaceObject(std::unique_ptr new_object) { + auto old_object = std::move(object_); + object_ = std::move(new_object); + return old_object; +} + +const Expr& IndexAccess::IndexExpr() const noexcept { + return *index_; +} + +Expr& IndexAccess::MutableIndexExpr() noexcept { + return *index_; +} + +void IndexAccess::SetIndexExpr(std::unique_ptr new_index) { + index_ = std::move(new_index); +} + +std::unique_ptr IndexAccess::ReplaceIndexExpr(std::unique_ptr new_index) { + auto old_index = std::move(index_); + index_ = std::move(new_index); + return old_index; +} diff --git a/lib/parser/ast/nodes/exprs/IndexAccess.hpp b/lib/parser/ast/nodes/exprs/IndexAccess.hpp index 1c56553..7c68dfe 100644 --- a/lib/parser/ast/nodes/exprs/IndexAccess.hpp +++ b/lib/parser/ast/nodes/exprs/IndexAccess.hpp @@ -7,7 +7,17 @@ class IndexAccess : public Expr { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + const Expr& Object() const noexcept; + Expr& MutableObject() noexcept; + void SetObject(std::unique_ptr new_object); + std::unique_ptr ReplaceObject(std::unique_ptr new_object); + + const Expr& IndexExpr() const noexcept; + Expr& MutableIndexExpr() noexcept; + void SetIndexExpr(std::unique_ptr new_index); + std::unique_ptr ReplaceIndexExpr(std::unique_ptr new_index); private: std::unique_ptr object_; diff --git a/lib/parser/ast/nodes/exprs/NamespaceRef.cpp b/lib/parser/ast/nodes/exprs/NamespaceRef.cpp index 2460147..c07a10d 100644 --- a/lib/parser/ast/nodes/exprs/NamespaceRef.cpp +++ b/lib/parser/ast/nodes/exprs/NamespaceRef.cpp @@ -1 +1,35 @@ -#include "NamespaceRef.hpp" +#include "lib/parser/ast/nodes/exprs/NamespaceRef.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void NamespaceRef::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const Expr& NamespaceRef::NamespaceExpr() const noexcept { + return *namespace_; +} + +Expr& NamespaceRef::MutableNamespaceExpr() noexcept { + return *namespace_; +} + +void NamespaceRef::SetNamespaceExpr(std::unique_ptr new_namespace_expr) { + namespace_ = std::move(new_namespace_expr); +} + +std::unique_ptr NamespaceRef::ReplaceNamespaceExpr(std::unique_ptr new_namespace_expr) { + auto old_ns = std::move(namespace_); + namespace_ = std::move(new_namespace_expr); + return old_ns; +} + +const std::string& NamespaceRef::Name() const noexcept { + return name_; +} + +void NamespaceRef::SetName(std::string new_name) { + name_ = std::move(new_name); +} diff --git a/lib/parser/ast/nodes/exprs/NamespaceRef.hpp b/lib/parser/ast/nodes/exprs/NamespaceRef.hpp index e9c090d..bfb11ce 100644 --- a/lib/parser/ast/nodes/exprs/NamespaceRef.hpp +++ b/lib/parser/ast/nodes/exprs/NamespaceRef.hpp @@ -8,7 +8,15 @@ class NamespaceRef : public Expr { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + const Expr& NamespaceExpr() const noexcept; + Expr& MutableNamespaceExpr() noexcept; + void SetNamespaceExpr(std::unique_ptr new_namespace_expr); + std::unique_ptr ReplaceNamespaceExpr(std::unique_ptr new_namespace_expr); + + const std::string& Name() const noexcept; + void SetName(std::string new_name); private: std::unique_ptr namespace_; diff --git a/lib/parser/ast/nodes/exprs/SafeCall.cpp b/lib/parser/ast/nodes/exprs/SafeCall.cpp index f486092..ba32860 100644 --- a/lib/parser/ast/nodes/exprs/SafeCall.cpp +++ b/lib/parser/ast/nodes/exprs/SafeCall.cpp @@ -1 +1,63 @@ -#include "SafeCall.hpp" +#include "lib/parser/ast/nodes/exprs/SafeCall.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void SafeCall::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const Expr& SafeCall::Object() const noexcept { + return *object_; +} + +Expr& SafeCall::MutableObject() noexcept { + return *object_; +} + +void SafeCall::SetObject(std::unique_ptr object_expr) { + object_ = std::move(object_expr); +} + +std::unique_ptr SafeCall::ReplaceObject(std::unique_ptr new_object) { + auto old_object = std::move(object_); + object_ = std::move(new_object); + return old_object; +} + +const std::string& SafeCall::Method() const noexcept { + return method_; +} + +void SafeCall::SetMethod(std::string method_name) { + method_ = std::move(method_name); +} + +const std::vector>& SafeCall::Args() const noexcept { + return args_; +} + +std::vector>& SafeCall::MutableArgs() noexcept { + return args_; +} + +void SafeCall::AddArg(std::unique_ptr argument_expr) { + args_.emplace_back(std::move(argument_expr)); +} + +void SafeCall::ClearArgs() { + args_.clear(); +} + +bool SafeCall::IsNullPropagating() const noexcept { + return true; +} + +void SafeCall::SetInferredType(TypeReference inferred) { + inferred_type_ = std::move(inferred); +} + +const std::optional& SafeCall::InferredType() const noexcept { + return inferred_type_; +} diff --git a/lib/parser/ast/nodes/exprs/SafeCall.hpp b/lib/parser/ast/nodes/exprs/SafeCall.hpp index 3a7679b..37f435e 100644 --- a/lib/parser/ast/nodes/exprs/SafeCall.hpp +++ b/lib/parser/ast/nodes/exprs/SafeCall.hpp @@ -15,29 +15,28 @@ class SafeCall : public Expr { public: void Accept(AstVisitor& v) override; - [[nodiscard]] const Expr& Object() const noexcept; - [[nodiscard]] Expr& MutableObject() noexcept; - void SetObject(std::unique_ptr obj); - std::unique_ptr ReplaceObject(std::unique_ptr obj); + const Expr& Object() const noexcept; + Expr& MutableObject() noexcept; + void SetObject(std::unique_ptr object_expr); + std::unique_ptr ReplaceObject(std::unique_ptr new_object); - [[nodiscard]] const std::string& Method() const noexcept; - void SetMethod(std::string m); + const std::string& Method() const noexcept; + void SetMethod(std::string method_name); - [[nodiscard]] const std::vector>& Args() const noexcept; - [[nodiscard]] std::vector>& MutableArgs() noexcept; - void AddArg(std::unique_ptr e); + const std::vector>& Args() const noexcept; + std::vector>& MutableArgs() noexcept; + void AddArg(std::unique_ptr argument_expr); void ClearArgs(); - [[nodiscard]] bool IsNullPropagating() const noexcept; + bool IsNullPropagating() const noexcept; - void SetInferredType(TypeReference t); - [[nodiscard]] const std::optional& InferredType() const noexcept; + void SetInferredType(TypeReference inferred); + const std::optional& InferredType() const noexcept; private: std::unique_ptr object_; std::string method_; std::vector> args_; - std::optional inferred_type_; }; diff --git a/lib/parser/ast/nodes/exprs/TypeTestIs.cpp b/lib/parser/ast/nodes/exprs/TypeTestIs.cpp index c2c5635..63b9721 100644 --- a/lib/parser/ast/nodes/exprs/TypeTestIs.cpp +++ b/lib/parser/ast/nodes/exprs/TypeTestIs.cpp @@ -1 +1,39 @@ -#include "TypeTestIs.hpp" +#include "lib/parser/ast/nodes/exprs/TypeTestIs.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void TypeTestIs::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const Expr& TypeTestIs::Expression() const noexcept { + return *expr; +} + +Expr& TypeTestIs::MutableExpression() noexcept { + return *expr; +} + +void TypeTestIs::SetExpression(std::unique_ptr new_expression) { + expr = std::move(new_expression); +} + +std::unique_ptr TypeTestIs::ReplaceExpression(std::unique_ptr new_expression) { + auto old_expr = std::move(expr); + expr = std::move(new_expression); + return old_expr; +} + +const TypeReference& TypeTestIs::Type() const noexcept { + return type; +} + +TypeReference& TypeTestIs::MutableType() noexcept { + return type; +} + +void TypeTestIs::SetType(TypeReference new_type) { + type = std::move(new_type); +} diff --git a/lib/parser/ast/nodes/exprs/TypeTestIs.hpp b/lib/parser/ast/nodes/exprs/TypeTestIs.hpp index 32313e3..e0840e9 100644 --- a/lib/parser/ast/nodes/exprs/TypeTestIs.hpp +++ b/lib/parser/ast/nodes/exprs/TypeTestIs.hpp @@ -8,7 +8,16 @@ class TypeTestIs : public Expr { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + const Expr& Expression() const noexcept; + Expr& MutableExpression() noexcept; + void SetExpression(std::unique_ptr new_expression); + std::unique_ptr ReplaceExpression(std::unique_ptr new_expression); + + const TypeReference& Type() const noexcept; + TypeReference& MutableType() noexcept; + void SetType(TypeReference new_type); private: std::unique_ptr expr; diff --git a/lib/parser/ast/nodes/exprs/Unary.cpp b/lib/parser/ast/nodes/exprs/Unary.cpp index b3ee440..4f2384a 100644 --- a/lib/parser/ast/nodes/exprs/Unary.cpp +++ b/lib/parser/ast/nodes/exprs/Unary.cpp @@ -1 +1,35 @@ -#include "Unary.hpp" +#include "lib/parser/ast/nodes/exprs/Unary.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void Unary::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const IUnaryOpTag& Unary::Op() const noexcept { + return *op_; +} + +void Unary::SetOp(const IUnaryOpTag& new_op) noexcept { + op_ = &new_op; +} + +const Expr& Unary::Operand() const noexcept { + return *operand_; +} + +Expr& Unary::MutableOperand() noexcept { + return *operand_; +} + +void Unary::SetOperand(std::unique_ptr new_operand) { + operand_ = std::move(new_operand); +} + +std::unique_ptr Unary::ReplaceOperand(std::unique_ptr new_operand) { + auto old_operand = std::move(operand_); + operand_ = std::move(new_operand); + return old_operand; +} diff --git a/lib/parser/ast/nodes/exprs/Unary.hpp b/lib/parser/ast/nodes/exprs/Unary.hpp index 164cb6e..b746563 100644 --- a/lib/parser/ast/nodes/exprs/Unary.hpp +++ b/lib/parser/ast/nodes/exprs/Unary.hpp @@ -9,7 +9,15 @@ class Unary : public Expr { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + const IUnaryOpTag& Op() const noexcept; + void SetOp(const IUnaryOpTag& new_op) noexcept; + + const Expr& Operand() const noexcept; + Expr& MutableOperand() noexcept; + void SetOperand(std::unique_ptr new_operand); + std::unique_ptr ReplaceOperand(std::unique_ptr new_operand); private: const IUnaryOpTag* op_ = &OpTags::Neg(); diff --git a/lib/parser/ast/nodes/exprs/literals/BoolLit.cpp b/lib/parser/ast/nodes/exprs/literals/BoolLit.cpp index ae07945..424027a 100644 --- a/lib/parser/ast/nodes/exprs/literals/BoolLit.cpp +++ b/lib/parser/ast/nodes/exprs/literals/BoolLit.cpp @@ -1 +1,15 @@ -#include "BoolLit.hpp" +#include "lib/parser/ast/nodes/exprs/literals/BoolLit.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +void BoolLit::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +bool BoolLit::Value() const noexcept { + return value; +} + +void BoolLit::SetValue(bool new_value) noexcept { + value = new_value; +} diff --git a/lib/parser/ast/nodes/exprs/literals/BoolLit.hpp b/lib/parser/ast/nodes/exprs/literals/BoolLit.hpp index baffb12..7cdfbdb 100644 --- a/lib/parser/ast/nodes/exprs/literals/BoolLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/BoolLit.hpp @@ -5,7 +5,10 @@ class BoolLit : public Expr { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + bool Value() const noexcept; + void SetValue(bool new_value) noexcept; private: bool value = false; diff --git a/lib/parser/ast/nodes/exprs/literals/CharLit.cpp b/lib/parser/ast/nodes/exprs/literals/CharLit.cpp index 621fe20..437ff5c 100644 --- a/lib/parser/ast/nodes/exprs/literals/CharLit.cpp +++ b/lib/parser/ast/nodes/exprs/literals/CharLit.cpp @@ -1 +1,15 @@ -#include "CharLit.hpp" +#include "lib/parser/ast/nodes/exprs/literals/CharLit.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +void CharLit::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +char CharLit::Value() const noexcept { + return value_; +} + +void CharLit::SetValue(char new_value) noexcept { + value_ = new_value; +} diff --git a/lib/parser/ast/nodes/exprs/literals/CharLit.hpp b/lib/parser/ast/nodes/exprs/literals/CharLit.hpp index 54aa309..68fd980 100644 --- a/lib/parser/ast/nodes/exprs/literals/CharLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/CharLit.hpp @@ -5,7 +5,10 @@ class CharLit : public Expr { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + char Value() const noexcept; + void SetValue(char new_value) noexcept; private: char value_ = '\0'; diff --git a/lib/parser/ast/nodes/exprs/literals/FloatLit.cpp b/lib/parser/ast/nodes/exprs/literals/FloatLit.cpp index 98631a4..062e7ca 100644 --- a/lib/parser/ast/nodes/exprs/literals/FloatLit.cpp +++ b/lib/parser/ast/nodes/exprs/literals/FloatLit.cpp @@ -1 +1,15 @@ -#include "FloatLit.hpp" +#include "lib/parser/ast/nodes/exprs/literals/FloatLit.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +void FloatLit::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +double FloatLit::Value() const noexcept { + return value; +} + +void FloatLit::SetValue(double new_value) noexcept { + value = new_value; +} diff --git a/lib/parser/ast/nodes/exprs/literals/FloatLit.hpp b/lib/parser/ast/nodes/exprs/literals/FloatLit.hpp index f9dd921..77af064 100644 --- a/lib/parser/ast/nodes/exprs/literals/FloatLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/FloatLit.hpp @@ -5,10 +5,13 @@ class FloatLit : public Expr { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + double Value() const noexcept; + void SetValue(double new_value) noexcept; private: - double value = 0.0L; + double value = 0.0; }; #endif // FLOATLIT_HPP_ diff --git a/lib/parser/ast/nodes/exprs/literals/IntLit.cpp b/lib/parser/ast/nodes/exprs/literals/IntLit.cpp index ad568ba..9d6d2b7 100644 --- a/lib/parser/ast/nodes/exprs/literals/IntLit.cpp +++ b/lib/parser/ast/nodes/exprs/literals/IntLit.cpp @@ -1 +1,15 @@ -#include "IntLit.hpp" +#include "lib/parser/ast/nodes/exprs/literals/IntLit.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +void IntLit::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +int64_t IntLit::Value() const noexcept { + return value; +} + +void IntLit::SetValue(int64_t new_value) noexcept { + value = new_value; +} diff --git a/lib/parser/ast/nodes/exprs/literals/IntLit.hpp b/lib/parser/ast/nodes/exprs/literals/IntLit.hpp index ea3e2c1..9fc1977 100644 --- a/lib/parser/ast/nodes/exprs/literals/IntLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/IntLit.hpp @@ -6,7 +6,10 @@ class IntLit : public Expr { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + int64_t Value() const noexcept; + void SetValue(int64_t new_value) noexcept; private: int64_t value = 0; diff --git a/lib/parser/ast/nodes/exprs/literals/NullLit.cpp b/lib/parser/ast/nodes/exprs/literals/NullLit.cpp index dc7ba0d..5cef978 100644 --- a/lib/parser/ast/nodes/exprs/literals/NullLit.cpp +++ b/lib/parser/ast/nodes/exprs/literals/NullLit.cpp @@ -1 +1,7 @@ -#include "NullLit.hpp" +#include "lib/parser/ast/nodes/exprs/literals/NullLit.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +void NullLit::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} diff --git a/lib/parser/ast/nodes/exprs/literals/NullLit.hpp b/lib/parser/ast/nodes/exprs/literals/NullLit.hpp index 80275bf..8966ceb 100644 --- a/lib/parser/ast/nodes/exprs/literals/NullLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/NullLit.hpp @@ -5,7 +5,7 @@ class NullLit : public Expr { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; }; #endif // NULLLIT_HPP_ diff --git a/lib/parser/ast/nodes/exprs/literals/StringLit.cpp b/lib/parser/ast/nodes/exprs/literals/StringLit.cpp index 5e45622..c8cb2d7 100644 --- a/lib/parser/ast/nodes/exprs/literals/StringLit.cpp +++ b/lib/parser/ast/nodes/exprs/literals/StringLit.cpp @@ -1 +1,17 @@ -#include "StringLit.hpp" +#include "lib/parser/ast/nodes/exprs/literals/StringLit.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void StringLit::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const std::string& StringLit::Value() const noexcept { + return value_; +} + +void StringLit::SetValue(std::string new_value) { + value_ = std::move(new_value); +} diff --git a/lib/parser/ast/nodes/exprs/literals/StringLit.hpp b/lib/parser/ast/nodes/exprs/literals/StringLit.hpp index ddc91d3..9374e85 100644 --- a/lib/parser/ast/nodes/exprs/literals/StringLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/StringLit.hpp @@ -7,7 +7,10 @@ class StringLit : public Expr { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + const std::string& Value() const noexcept; + void SetValue(std::string new_value); private: std::string value_; diff --git a/lib/parser/ast/nodes/stmts/Block.cpp b/lib/parser/ast/nodes/stmts/Block.cpp index 71b09e5..8f423f3 100644 --- a/lib/parser/ast/nodes/stmts/Block.cpp +++ b/lib/parser/ast/nodes/stmts/Block.cpp @@ -1 +1,47 @@ -#include "Block.hpp" +#include "lib/parser/ast/nodes/stmts/Block.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void Block::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +std::vector>& Block::GetStatements() { + return stmts_; +} + +const std::vector>& Block::GetStatements() const { + return stmts_; +} + +void Block::Append(std::unique_ptr statement) { + stmts_.emplace_back(std::move(statement)); +} + +void Block::Insert(std::size_t index, std::unique_ptr statement) { + if (index >= stmts_.size()) { + stmts_.emplace_back(std::move(statement)); + } else { + stmts_.insert(stmts_.begin() + static_cast(index), std::move(statement)); + } +} + +std::unique_ptr Block::ReleaseAt(std::size_t index) { + if (index >= stmts_.size()) { + return nullptr; + } + + auto old_stmt = std::move(stmts_[index]); + stmts_.erase(stmts_.begin() + static_cast(index)); + return old_stmt; +} + +void Block::Clear() noexcept { + stmts_.clear(); +} + +std::size_t Block::Size() const noexcept { + return stmts_.size(); +} diff --git a/lib/parser/ast/nodes/stmts/Block.hpp b/lib/parser/ast/nodes/stmts/Block.hpp index ef1712d..916534c 100644 --- a/lib/parser/ast/nodes/stmts/Block.hpp +++ b/lib/parser/ast/nodes/stmts/Block.hpp @@ -1,6 +1,7 @@ #ifndef BLOCK_HPP_ #define BLOCK_HPP_ +#include #include #include @@ -9,11 +10,16 @@ class Block : public Stmt { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; std::vector>& GetStatements(); const std::vector>& GetStatements() const; - void Append(std::unique_ptr s); + void Append(std::unique_ptr statement); + + void Insert(std::size_t index, std::unique_ptr statement); + std::unique_ptr ReleaseAt(std::size_t index); + void Clear() noexcept; + std::size_t Size() const noexcept; private: std::vector> stmts_; diff --git a/lib/parser/ast/nodes/stmts/Branch.cpp b/lib/parser/ast/nodes/stmts/Branch.cpp index 60d4ee0..3155ab5 100644 --- a/lib/parser/ast/nodes/stmts/Branch.cpp +++ b/lib/parser/ast/nodes/stmts/Branch.cpp @@ -1 +1,39 @@ -#include "Branch.hpp" +#include "lib/parser/ast/nodes/stmts/Branch.hpp" + +#include + +Branch::Branch(std::unique_ptr condition, std::unique_ptr then_block) : + condition_(std::move(condition)), then_block_(std::move(then_block)) { +} + +const Expr* Branch::Condition() const noexcept { + return condition_.get(); +} + +Expr* Branch::MutableCondition() noexcept { + return condition_.get(); +} + +void Branch::SetCondition(std::unique_ptr expression) { + condition_ = std::move(expression); +} + +std::unique_ptr Branch::ReleaseCondition() { + return std::move(condition_); +} + +const Block* Branch::Then() const noexcept { + return then_block_.get(); +} + +Block* Branch::MutableThen() noexcept { + return then_block_.get(); +} + +void Branch::SetThen(std::unique_ptr then_body) { + then_block_ = std::move(then_body); +} + +std::unique_ptr Branch::ReleaseThen() { + return std::move(then_block_); +} diff --git a/lib/parser/ast/nodes/stmts/Branch.hpp b/lib/parser/ast/nodes/stmts/Branch.hpp index 301168a..12290b0 100644 --- a/lib/parser/ast/nodes/stmts/Branch.hpp +++ b/lib/parser/ast/nodes/stmts/Branch.hpp @@ -17,12 +17,12 @@ class Branch { const Expr* Condition() const noexcept; Expr* MutableCondition() noexcept; - void SetCondition(std::unique_ptr e); + void SetCondition(std::unique_ptr expression); std::unique_ptr ReleaseCondition(); const Block* Then() const noexcept; Block* MutableThen() noexcept; - void SetThen(std::unique_ptr b); + void SetThen(std::unique_ptr then_body); std::unique_ptr ReleaseThen(); private: diff --git a/lib/parser/ast/nodes/stmts/BreakStmt.cpp b/lib/parser/ast/nodes/stmts/BreakStmt.cpp index 8e979c6..f1d7d2a 100644 --- a/lib/parser/ast/nodes/stmts/BreakStmt.cpp +++ b/lib/parser/ast/nodes/stmts/BreakStmt.cpp @@ -1 +1,7 @@ -#include "BreakStmt.hpp" +#include "lib/parser/ast/nodes/stmts/BreakStmt.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +void BreakStmt::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} diff --git a/lib/parser/ast/nodes/stmts/BreakStmt.hpp b/lib/parser/ast/nodes/stmts/BreakStmt.hpp index 1abc187..b0ffb8a 100644 --- a/lib/parser/ast/nodes/stmts/BreakStmt.hpp +++ b/lib/parser/ast/nodes/stmts/BreakStmt.hpp @@ -5,7 +5,7 @@ class BreakStmt : public Stmt { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; }; #endif // BREAKSTMT_HPP_ diff --git a/lib/parser/ast/nodes/stmts/ContinueStmt.cpp b/lib/parser/ast/nodes/stmts/ContinueStmt.cpp index 5de3431..be4e1df 100644 --- a/lib/parser/ast/nodes/stmts/ContinueStmt.cpp +++ b/lib/parser/ast/nodes/stmts/ContinueStmt.cpp @@ -1 +1,7 @@ -#include "ContinueStmt.hpp" +#include "lib/parser/ast/nodes/stmts/ContinueStmt.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +void ContinueStmt::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} diff --git a/lib/parser/ast/nodes/stmts/ContinueStmt.hpp b/lib/parser/ast/nodes/stmts/ContinueStmt.hpp index 2554d86..f81ff11 100644 --- a/lib/parser/ast/nodes/stmts/ContinueStmt.hpp +++ b/lib/parser/ast/nodes/stmts/ContinueStmt.hpp @@ -5,7 +5,7 @@ class ContinueStmt : public Stmt { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; }; #endif // CONTINUESTMT_HPP_ diff --git a/lib/parser/ast/nodes/stmts/ExprStmt.cpp b/lib/parser/ast/nodes/stmts/ExprStmt.cpp index b80efca..d9b7a12 100644 --- a/lib/parser/ast/nodes/stmts/ExprStmt.cpp +++ b/lib/parser/ast/nodes/stmts/ExprStmt.cpp @@ -1 +1,25 @@ -#include "ExprStmt.hpp" +#include "lib/parser/ast/nodes/stmts/ExprStmt.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void ExprStmt::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const Expr* ExprStmt::Expression() const noexcept { + return expr_.get(); +} + +Expr* ExprStmt::MutableExpression() noexcept { + return expr_.get(); +} + +void ExprStmt::SetExpression(std::unique_ptr expression) { + expr_ = std::move(expression); +} + +std::unique_ptr ExprStmt::ReleaseExpression() { + return std::move(expr_); +} diff --git a/lib/parser/ast/nodes/stmts/ExprStmt.hpp b/lib/parser/ast/nodes/stmts/ExprStmt.hpp index 4932301..b410d13 100644 --- a/lib/parser/ast/nodes/stmts/ExprStmt.hpp +++ b/lib/parser/ast/nodes/stmts/ExprStmt.hpp @@ -8,7 +8,12 @@ class ExprStmt : public Stmt { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + const Expr* Expression() const noexcept; + Expr* MutableExpression() noexcept; + void SetExpression(std::unique_ptr expression); + std::unique_ptr ReleaseExpression(); private: std::unique_ptr expr_; diff --git a/lib/parser/ast/nodes/stmts/ForStmt.cpp b/lib/parser/ast/nodes/stmts/ForStmt.cpp index fbce6f8..e42cd6c 100644 --- a/lib/parser/ast/nodes/stmts/ForStmt.cpp +++ b/lib/parser/ast/nodes/stmts/ForStmt.cpp @@ -1 +1,49 @@ -#include "ForStmt.hpp" +#include "lib/parser/ast/nodes/stmts/ForStmt.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void ForStmt::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const std::string& ForStmt::IteratorName() const noexcept { + return iter_name_; +} + +void ForStmt::SetIteratorName(std::string new_name) { + iter_name_ = std::move(new_name); +} + +const Expr* ForStmt::IteratorExpr() const noexcept { + return iter_expr_.get(); +} + +Expr* ForStmt::MutableIteratorExpr() noexcept { + return iter_expr_.get(); +} + +void ForStmt::SetIteratorExpr(std::unique_ptr expression) { + iter_expr_ = std::move(expression); +} + +std::unique_ptr ForStmt::ReleaseIteratorExpr() { + return std::move(iter_expr_); +} + +const Block* ForStmt::Body() const noexcept { + return body_.get(); +} + +Block* ForStmt::MutableBody() noexcept { + return body_.get(); +} + +void ForStmt::SetBody(std::unique_ptr body_block) { + body_ = std::move(body_block); +} + +std::unique_ptr ForStmt::ReleaseBody() { + return std::move(body_); +} diff --git a/lib/parser/ast/nodes/stmts/ForStmt.hpp b/lib/parser/ast/nodes/stmts/ForStmt.hpp index 396930a..b8f5ff8 100644 --- a/lib/parser/ast/nodes/stmts/ForStmt.hpp +++ b/lib/parser/ast/nodes/stmts/ForStmt.hpp @@ -9,7 +9,20 @@ class ForStmt : public Stmt { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + const std::string& IteratorName() const noexcept; + void SetIteratorName(std::string new_name); + + const Expr* IteratorExpr() const noexcept; + Expr* MutableIteratorExpr() noexcept; + void SetIteratorExpr(std::unique_ptr expression); + std::unique_ptr ReleaseIteratorExpr(); + + const Block* Body() const noexcept; + Block* MutableBody() noexcept; + void SetBody(std::unique_ptr body_block); + std::unique_ptr ReleaseBody(); private: std::string iter_name_; diff --git a/lib/parser/ast/nodes/stmts/IfStmt.cpp b/lib/parser/ast/nodes/stmts/IfStmt.cpp index 9d7e8da..7ca5133 100644 --- a/lib/parser/ast/nodes/stmts/IfStmt.cpp +++ b/lib/parser/ast/nodes/stmts/IfStmt.cpp @@ -1 +1,49 @@ -#include "IfStmt.hpp" +#include "lib/parser/ast/nodes/stmts/IfStmt.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void IfStmt::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const std::vector& IfStmt::Branches() const noexcept { + return branches_; +} + +std::vector& IfStmt::MutableBranches() noexcept { + return branches_; +} + +void IfStmt::AddBranch(Branch branch_value) { + branches_.emplace_back(std::move(branch_value)); +} + +void IfStmt::EmplaceBranch(std::unique_ptr cond, std::unique_ptr then_blk) { + branches_.emplace_back(std::move(cond), std::move(then_blk)); +} + +const Block* IfStmt::ElseBlock() const noexcept { + return else_block_.get(); +} + +Block* IfStmt::MutableElseBlock() noexcept { + return else_block_.get(); +} + +void IfStmt::SetElseBlock(std::unique_ptr else_body) { + else_block_ = std::move(else_body); +} + +std::unique_ptr IfStmt::ReleaseElseBlock() { + return std::move(else_block_); +} + +bool IfStmt::HasElse() const noexcept { + return static_cast(else_block_); +} + +bool IfStmt::Empty() const noexcept { + return branches_.empty() && !else_block_; +} diff --git a/lib/parser/ast/nodes/stmts/IfStmt.hpp b/lib/parser/ast/nodes/stmts/IfStmt.hpp index b78ffc9..c747a27 100644 --- a/lib/parser/ast/nodes/stmts/IfStmt.hpp +++ b/lib/parser/ast/nodes/stmts/IfStmt.hpp @@ -11,16 +11,16 @@ class IfStmt : public Stmt { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; const std::vector& Branches() const noexcept; std::vector& MutableBranches() noexcept; - void AddBranch(Branch b); + void AddBranch(Branch branch_value); void EmplaceBranch(std::unique_ptr cond, std::unique_ptr then_blk); const Block* ElseBlock() const noexcept; Block* MutableElseBlock() noexcept; - void SetElseBlock(std::unique_ptr b); + void SetElseBlock(std::unique_ptr else_body); std::unique_ptr ReleaseElseBlock(); bool HasElse() const noexcept; diff --git a/lib/parser/ast/nodes/stmts/ReturnStmt.cpp b/lib/parser/ast/nodes/stmts/ReturnStmt.cpp index 619814e..06c583c 100644 --- a/lib/parser/ast/nodes/stmts/ReturnStmt.cpp +++ b/lib/parser/ast/nodes/stmts/ReturnStmt.cpp @@ -1 +1,39 @@ -#include "ReturnStmt.hpp" +#include "lib/parser/ast/nodes/stmts/ReturnStmt.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void ReturnStmt::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +bool ReturnStmt::HasValue() const noexcept { + return value_.has_value() && static_cast(value_.value()); +} + +const Expr* ReturnStmt::Value() const noexcept { + return value_.has_value() ? value_.value().get() : nullptr; +} + +Expr* ReturnStmt::MutableValue() noexcept { + return value_.has_value() ? value_.value().get() : nullptr; +} + +void ReturnStmt::SetValue(std::unique_ptr new_value) { + value_.emplace(std::move(new_value)); +} + +void ReturnStmt::ResetValue() { + value_.reset(); +} + +std::unique_ptr ReturnStmt::ReleaseValue() { + if (!value_.has_value()) { + return nullptr; + } + + auto out = std::move(value_.value()); + value_.reset(); + return out; +} diff --git a/lib/parser/ast/nodes/stmts/ReturnStmt.hpp b/lib/parser/ast/nodes/stmts/ReturnStmt.hpp index a2ba0bb..f8f3f6d 100644 --- a/lib/parser/ast/nodes/stmts/ReturnStmt.hpp +++ b/lib/parser/ast/nodes/stmts/ReturnStmt.hpp @@ -2,18 +2,19 @@ #define RETURNSTMT_HPP_ #include +#include #include "lib/parser/ast/nodes/base/Expr.hpp" #include "lib/parser/ast/nodes/base/Stmt.hpp" class ReturnStmt : public Stmt { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; bool HasValue() const noexcept; const Expr* Value() const noexcept; Expr* MutableValue() noexcept; - void SetValue(std::unique_ptr e); + void SetValue(std::unique_ptr new_value); void ResetValue(); std::unique_ptr ReleaseValue(); diff --git a/lib/parser/ast/nodes/stmts/UnsafeBlock.cpp b/lib/parser/ast/nodes/stmts/UnsafeBlock.cpp index 3c8c05e..b71f347 100644 --- a/lib/parser/ast/nodes/stmts/UnsafeBlock.cpp +++ b/lib/parser/ast/nodes/stmts/UnsafeBlock.cpp @@ -1 +1,25 @@ -#include "UnsafeBlock.hpp" +#include "lib/parser/ast/nodes/stmts/UnsafeBlock.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void UnsafeBlock::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const Block* UnsafeBlock::Body() const noexcept { + return body_.get(); +} + +Block* UnsafeBlock::MutableBody() noexcept { + return body_.get(); +} + +void UnsafeBlock::SetBody(std::unique_ptr body_block) { + body_ = std::move(body_block); +} + +std::unique_ptr UnsafeBlock::ReleaseBody() { + return std::move(body_); +} diff --git a/lib/parser/ast/nodes/stmts/UnsafeBlock.hpp b/lib/parser/ast/nodes/stmts/UnsafeBlock.hpp index 002f3dc..a28fe55 100644 --- a/lib/parser/ast/nodes/stmts/UnsafeBlock.hpp +++ b/lib/parser/ast/nodes/stmts/UnsafeBlock.hpp @@ -8,7 +8,12 @@ class UnsafeBlock : public Stmt { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + const Block* Body() const noexcept; + Block* MutableBody() noexcept; + void SetBody(std::unique_ptr body_block); + std::unique_ptr ReleaseBody(); private: std::unique_ptr body_; diff --git a/lib/parser/ast/nodes/stmts/VarDeclStmt.cpp b/lib/parser/ast/nodes/stmts/VarDeclStmt.cpp index e285238..69bc2cb 100644 --- a/lib/parser/ast/nodes/stmts/VarDeclStmt.cpp +++ b/lib/parser/ast/nodes/stmts/VarDeclStmt.cpp @@ -1 +1,53 @@ -#include "VarDeclStmt.hpp" +#include "lib/parser/ast/nodes/stmts/VarDeclStmt.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void VarDeclStmt::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +bool VarDeclStmt::IsVar() const noexcept { + return is_var_; +} + +void VarDeclStmt::SetVar(bool is_var) noexcept { + is_var_ = is_var; +} + +const std::string& VarDeclStmt::Name() const noexcept { + return name_; +} + +void VarDeclStmt::SetName(std::string new_name) { + name_ = std::move(new_name); +} + +const TypeReference& VarDeclStmt::Type() const noexcept { + return type_; +} + +TypeReference& VarDeclStmt::MutableType() noexcept { + return type_; +} + +void VarDeclStmt::SetType(TypeReference new_type) { + type_ = std::move(new_type); +} + +const Expr* VarDeclStmt::Init() const noexcept { + return init_.get(); +} + +Expr* VarDeclStmt::MutableInit() noexcept { + return init_.get(); +} + +void VarDeclStmt::SetInit(std::unique_ptr init_expr) { + init_ = std::move(init_expr); +} + +std::unique_ptr VarDeclStmt::ReleaseInit() { + return std::move(init_); +} diff --git a/lib/parser/ast/nodes/stmts/VarDeclStmt.hpp b/lib/parser/ast/nodes/stmts/VarDeclStmt.hpp index 5f88bae..383d248 100644 --- a/lib/parser/ast/nodes/stmts/VarDeclStmt.hpp +++ b/lib/parser/ast/nodes/stmts/VarDeclStmt.hpp @@ -10,7 +10,22 @@ class VarDeclStmt : public Stmt { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + bool IsVar() const noexcept; + void SetVar(bool is_var) noexcept; + + const std::string& Name() const noexcept; + void SetName(std::string new_name); + + const TypeReference& Type() const noexcept; + TypeReference& MutableType() noexcept; + void SetType(TypeReference new_type); + + const Expr* Init() const noexcept; + Expr* MutableInit() noexcept; + void SetInit(std::unique_ptr init_expr); + std::unique_ptr ReleaseInit(); private: bool is_var_ = false; diff --git a/lib/parser/ast/nodes/stmts/WhileStmt.cpp b/lib/parser/ast/nodes/stmts/WhileStmt.cpp index 66d2995..28f4c9b 100644 --- a/lib/parser/ast/nodes/stmts/WhileStmt.cpp +++ b/lib/parser/ast/nodes/stmts/WhileStmt.cpp @@ -1 +1,41 @@ -#include "WhileStmt.hpp" +#include "lib/parser/ast/nodes/stmts/WhileStmt.hpp" + +#include "lib/parser/ast/AstVisitor.hpp" + +#include + +void WhileStmt::Accept(AstVisitor& visitor) { + visitor.Visit(*this); +} + +const Expr* WhileStmt::Condition() const noexcept { + return cond_.get(); +} + +Expr* WhileStmt::MutableCondition() noexcept { + return cond_.get(); +} + +void WhileStmt::SetCondition(std::unique_ptr condition_expr) { + cond_ = std::move(condition_expr); +} + +std::unique_ptr WhileStmt::ReleaseCondition() { + return std::move(cond_); +} + +const Block* WhileStmt::Body() const noexcept { + return body_.get(); +} + +Block* WhileStmt::MutableBody() noexcept { + return body_.get(); +} + +void WhileStmt::SetBody(std::unique_ptr body_block) { + body_ = std::move(body_block); +} + +std::unique_ptr WhileStmt::ReleaseBody() { + return std::move(body_); +} diff --git a/lib/parser/ast/nodes/stmts/WhileStmt.hpp b/lib/parser/ast/nodes/stmts/WhileStmt.hpp index 5594de3..3d0659b 100644 --- a/lib/parser/ast/nodes/stmts/WhileStmt.hpp +++ b/lib/parser/ast/nodes/stmts/WhileStmt.hpp @@ -9,7 +9,17 @@ class WhileStmt : public Stmt { public: - void Accept(AstVisitor& v) override; + void Accept(AstVisitor& visitor) override; + + const Expr* Condition() const noexcept; + Expr* MutableCondition() noexcept; + void SetCondition(std::unique_ptr condition_expr); + std::unique_ptr ReleaseCondition(); + + const Block* Body() const noexcept; + Block* MutableBody() noexcept; + void SetBody(std::unique_ptr body_block); + std::unique_ptr ReleaseBody(); private: std::unique_ptr cond_; From e191c32a11001aad360333d71603302fc8f2e0f4 Mon Sep 17 00:00:00 2001 From: biqiboqi Date: Sun, 9 Nov 2025 22:32:12 +0300 Subject: [PATCH 11/21] feat: add ast visitors and OpTags --- lib/parser/ast/nodes/exprs/tags/OpTags.cpp | 220 ++++++++++ lib/parser/ast/visitors/ConstWalkVisitor.cpp | 270 ++++++++++++ lib/parser/ast/visitors/ConstWalkVisitor.hpp | 54 +++ lib/parser/ast/visitors/PrintVisitor.cpp | 417 +++++++++++++++++++ lib/parser/ast/visitors/PrintVisitor.hpp | 74 ++++ lib/parser/ast/visitors/WalkVisitor.cpp | 270 ++++++++++++ lib/parser/ast/visitors/WalkVisitor.hpp | 54 +++ 7 files changed, 1359 insertions(+) create mode 100644 lib/parser/ast/nodes/exprs/tags/OpTags.cpp create mode 100644 lib/parser/ast/visitors/ConstWalkVisitor.cpp create mode 100644 lib/parser/ast/visitors/ConstWalkVisitor.hpp create mode 100644 lib/parser/ast/visitors/PrintVisitor.cpp create mode 100644 lib/parser/ast/visitors/PrintVisitor.hpp create mode 100644 lib/parser/ast/visitors/WalkVisitor.cpp create mode 100644 lib/parser/ast/visitors/WalkVisitor.hpp diff --git a/lib/parser/ast/nodes/exprs/tags/OpTags.cpp b/lib/parser/ast/nodes/exprs/tags/OpTags.cpp new file mode 100644 index 0000000..3638429 --- /dev/null +++ b/lib/parser/ast/nodes/exprs/tags/OpTags.cpp @@ -0,0 +1,220 @@ +#include "OpTags.hpp" + +#include + +namespace { + +struct BinaryAdd : IBinaryOpTag { + std::string_view Name() const override { + return "add"; + } +}; + +struct BinarySub : IBinaryOpTag { + std::string_view Name() const override { + return "sub"; + } +}; + +struct BinaryMul : IBinaryOpTag { + std::string_view Name() const override { + return "mul"; + } +}; + +struct BinaryDiv : IBinaryOpTag { + std::string_view Name() const override { + return "div"; + } +}; + +struct BinaryMod : IBinaryOpTag { + std::string_view Name() const override { + return "mod"; + } +}; + +struct BinaryLt : IBinaryOpTag { + std::string_view Name() const override { + return "lt"; + } +}; + +struct BinaryLe : IBinaryOpTag { + std::string_view Name() const override { + return "le"; + } +}; + +struct BinaryGt : IBinaryOpTag { + std::string_view Name() const override { + return "gt"; + } +}; + +struct BinaryGe : IBinaryOpTag { + std::string_view Name() const override { + return "ge"; + } +}; + +struct BinaryEq : IBinaryOpTag { + std::string_view Name() const override { + return "eq"; + } +}; + +struct BinaryNe : IBinaryOpTag { + std::string_view Name() const override { + return "ne"; + } +}; + +struct BinaryAnd : IBinaryOpTag { + std::string_view Name() const override { + return "and"; + } +}; + +struct BinaryOr : IBinaryOpTag { + std::string_view Name() const override { + return "or"; + } +}; + +struct BinaryXor : IBinaryOpTag { + std::string_view Name() const override { + return "xor"; + } +}; + +struct UnaryNeg : IUnaryOpTag { + std::string_view Name() const override { + return "neg"; + } +}; + +struct UnaryPlus : IUnaryOpTag { + std::string_view Name() const override { + return "plus"; + } +}; + +struct UnaryNot : IUnaryOpTag { + std::string_view Name() const override { + return "not"; + } +}; + +struct AssignRef : IAssignOpTag { + std::string_view Name() const override { + return "="; + } +}; + +struct AssignCopy : IAssignOpTag { + std::string_view Name() const override { + return ":="; + } +}; + +} // namespace + +namespace OpTags { + +const IBinaryOpTag& Add() { + static BinaryAdd t; + return t; +} + +const IBinaryOpTag& Sub() { + static BinarySub t; + return t; +} + +const IBinaryOpTag& Mul() { + static BinaryMul t; + return t; +} + +const IBinaryOpTag& Div() { + static BinaryDiv t; + return t; +} + +const IBinaryOpTag& Mod() { + static BinaryMod t; + return t; +} + +const IBinaryOpTag& Lt() { + static BinaryLt t; + return t; +} + +const IBinaryOpTag& Le() { + static BinaryLe t; + return t; +} + +const IBinaryOpTag& Gt() { + static BinaryGt t; + return t; +} + +const IBinaryOpTag& Ge() { + static BinaryGe t; + return t; +} + +const IBinaryOpTag& Eq() { + static BinaryEq t; + return t; +} + +const IBinaryOpTag& Ne() { + static BinaryNe t; + return t; +} + +const IBinaryOpTag& And() { + static BinaryAnd t; + return t; +} + +const IBinaryOpTag& Or() { + static BinaryOr t; + return t; +} + +const IBinaryOpTag& Xor() { + static BinaryXor t; + return t; +} + +const IUnaryOpTag& Neg() { + static UnaryNeg t; + return t; +} + +const IUnaryOpTag& Plus() { + static UnaryPlus t; + return t; +} + +const IUnaryOpTag& Not() { + static UnaryNot t; + return t; +} + +const IAssignOpTag& RefAssign() { + static AssignRef t; + return t; +} + +const IAssignOpTag& CopyAssign() { + static AssignCopy t; + return t; +} + +} // namespace OpTags diff --git a/lib/parser/ast/visitors/ConstWalkVisitor.cpp b/lib/parser/ast/visitors/ConstWalkVisitor.cpp new file mode 100644 index 0000000..535f070 --- /dev/null +++ b/lib/parser/ast/visitors/ConstWalkVisitor.cpp @@ -0,0 +1,270 @@ +#include "ConstWalkVisitor.hpp" + +#include "lib/parser/ast/nodes/decls/ClassDecl.hpp" +#include "lib/parser/ast/nodes/decls/FunctionDecl.hpp" +#include "lib/parser/ast/nodes/decls/GlobalVarDecl.hpp" +#include "lib/parser/ast/nodes/decls/InterfaceDecl.hpp" +#include "lib/parser/ast/nodes/decls/InterfaceMethod.hpp" +#include "lib/parser/ast/nodes/decls/Module.hpp" +#include "lib/parser/ast/nodes/decls/TypeAliasDecl.hpp" + +#include "lib/parser/ast/nodes/class_members/CallDecl.hpp" +#include "lib/parser/ast/nodes/class_members/DestructorDecl.hpp" +#include "lib/parser/ast/nodes/class_members/FieldDecl.hpp" +#include "lib/parser/ast/nodes/class_members/MethodDecl.hpp" +#include "lib/parser/ast/nodes/class_members/StaticFieldDecl.hpp" + +#include "lib/parser/ast/nodes/exprs/Assign.hpp" +#include "lib/parser/ast/nodes/exprs/Binary.hpp" +#include "lib/parser/ast/nodes/exprs/Call.hpp" +#include "lib/parser/ast/nodes/exprs/CastAs.hpp" +#include "lib/parser/ast/nodes/exprs/Elvis.hpp" +#include "lib/parser/ast/nodes/exprs/FieldAccess.hpp" +#include "lib/parser/ast/nodes/exprs/IdentRef.hpp" +#include "lib/parser/ast/nodes/exprs/IndexAccess.hpp" +#include "lib/parser/ast/nodes/exprs/NamespaceRef.hpp" +#include "lib/parser/ast/nodes/exprs/SafeCall.hpp" +#include "lib/parser/ast/nodes/exprs/TypeTestIs.hpp" +#include "lib/parser/ast/nodes/exprs/Unary.hpp" + +#include "lib/parser/ast/nodes/exprs/literals/BoolLit.hpp" +#include "lib/parser/ast/nodes/exprs/literals/CharLit.hpp" +#include "lib/parser/ast/nodes/exprs/literals/FloatLit.hpp" +#include "lib/parser/ast/nodes/exprs/literals/IntLit.hpp" +#include "lib/parser/ast/nodes/exprs/literals/NullLit.hpp" +#include "lib/parser/ast/nodes/exprs/literals/StringLit.hpp" + +#include "lib/parser/ast/nodes/stmts/Block.hpp" +#include "lib/parser/ast/nodes/stmts/BreakStmt.hpp" +#include "lib/parser/ast/nodes/stmts/ContinueStmt.hpp" +#include "lib/parser/ast/nodes/stmts/ExprStmt.hpp" +#include "lib/parser/ast/nodes/stmts/ForStmt.hpp" +#include "lib/parser/ast/nodes/stmts/IfStmt.hpp" +#include "lib/parser/ast/nodes/stmts/ReturnStmt.hpp" +#include "lib/parser/ast/nodes/stmts/UnsafeBlock.hpp" +#include "lib/parser/ast/nodes/stmts/VarDeclStmt.hpp" +#include "lib/parser/ast/nodes/stmts/WhileStmt.hpp" + +void ConstWalkVisitor::Visit(Module& node) { + for (auto& decl_ptr : node.MutableDecls()) { + decl_ptr->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(FunctionDecl& node) { + if (auto* body = node.MutableBody()) { + body->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(ClassDecl& node) { + for (auto& member : node.MutableMembers()) { + member->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(InterfaceMethod& node) { + (void) node; +} + +void ConstWalkVisitor::Visit(InterfaceDecl& node) { + for (auto& m : node.MutableMembers()) { + m->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(TypeAliasDecl& node) { + (void) node; +} + +void ConstWalkVisitor::Visit(GlobalVarDecl& node) { + if (auto* init = node.MutableInit()) { + init->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(FieldDecl& node) { + if (auto* init = node.MutableInit()) { + init->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(StaticFieldDecl& node) { + if (auto* init = node.MutableInit()) { + init->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(MethodDecl& node) { + if (auto* body = node.MutableBody()) { + body->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(CallDecl& node) { + if (auto* body = node.MutableBody()) { + body->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(DestructorDecl& node) { + if (auto* body = node.MutableBody()) { + body->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(Block& node) { + for (auto& stmt : node.GetStatements()) { + stmt->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(VarDeclStmt& node) { + if (auto* init = node.MutableInit()) { + init->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(ExprStmt& node) { + if (auto* e = node.MutableExpression()) { + e->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(ReturnStmt& node) { + if (auto* v = node.MutableValue()) { + v->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(BreakStmt& node) { + (void) node; +} + +void ConstWalkVisitor::Visit(ContinueStmt& node) { + (void) node; +} + +void ConstWalkVisitor::Visit(IfStmt& node) { + for (auto& br : node.MutableBranches()) { + if (auto* c = br.MutableCondition()) { + c->Accept(*this); + } + + if (auto* t = br.MutableThen()) { + t->Accept(*this); + } + } + + if (auto* eb = node.MutableElseBlock()) { + eb->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(WhileStmt& node) { + if (auto* c = node.MutableCondition()) { + c->Accept(*this); + } + + if (auto* b = node.MutableBody()) { + b->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(ForStmt& node) { + if (auto* it = node.MutableIteratorExpr()) { + it->Accept(*this); + } + + if (auto* b = node.MutableBody()) { + b->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(UnsafeBlock& node) { + if (auto* b = node.MutableBody()) { + b->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(Binary& node) { + node.MutableLhs().Accept(*this); + node.MutableRhs().Accept(*this); +} + +void ConstWalkVisitor::Visit(Unary& node) { + node.MutableOperand().Accept(*this); +} + +void ConstWalkVisitor::Visit(Assign& node) { + node.MutableTarget().Accept(*this); + node.MutableValue().Accept(*this); +} + +void ConstWalkVisitor::Visit(Call& node) { + node.MutableCallee().Accept(*this); + for (auto& a : node.MutableArgs()) { + a->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(FieldAccess& node) { + node.MutableObject().Accept(*this); +} + +void ConstWalkVisitor::Visit(IndexAccess& node) { + node.MutableObject().Accept(*this); + node.MutableIndexExpr().Accept(*this); +} + +void ConstWalkVisitor::Visit(NamespaceRef& node) { + node.MutableNamespaceExpr().Accept(*this); +} + +void ConstWalkVisitor::Visit(SafeCall& node) { + node.MutableObject().Accept(*this); + for (auto& a : node.MutableArgs()) { + a->Accept(*this); + } +} + +void ConstWalkVisitor::Visit(Elvis& node) { + node.MutableLhs().Accept(*this); + node.MutableRhs().Accept(*this); +} + +void ConstWalkVisitor::Visit(CastAs& node) { + node.MutableExpression().Accept(*this); +} + +void ConstWalkVisitor::Visit(TypeTestIs& node) { + node.MutableExpression().Accept(*this); +} + +void ConstWalkVisitor::Visit(IdentRef& node) { + (void) node; +} + +void ConstWalkVisitor::Visit(IntLit& node) { + (void) node; +} + +void ConstWalkVisitor::Visit(FloatLit& node) { + (void) node; +} + +void ConstWalkVisitor::Visit(StringLit& node) { + (void) node; +} + +void ConstWalkVisitor::Visit(CharLit& node) { + (void) node; +} + +void ConstWalkVisitor::Visit(BoolLit& node) { + (void) node; +} + +void ConstWalkVisitor::Visit(NullLit& node) { + (void) node; +} diff --git a/lib/parser/ast/visitors/ConstWalkVisitor.hpp b/lib/parser/ast/visitors/ConstWalkVisitor.hpp new file mode 100644 index 0000000..4c4b087 --- /dev/null +++ b/lib/parser/ast/visitors/ConstWalkVisitor.hpp @@ -0,0 +1,54 @@ +#ifndef CONSTWALKVISITOR_HPP_ +#define CONSTWALKVISITOR_HPP_ + +#include "lib/parser/ast/AstVisitor.hpp" + +class ConstWalkVisitor : public AstVisitor { +public: + ~ConstWalkVisitor() override = default; + + void Visit(Module& node) override; + void Visit(FunctionDecl& node) override; + void Visit(ClassDecl& node) override; + void Visit(InterfaceMethod& node) override; + void Visit(InterfaceDecl& node) override; + void Visit(TypeAliasDecl& node) override; + void Visit(GlobalVarDecl& node) override; + void Visit(FieldDecl& node) override; + void Visit(StaticFieldDecl& node) override; + void Visit(MethodDecl& node) override; + void Visit(CallDecl& node) override; + void Visit(DestructorDecl& node) override; + + void Visit(Block& node) override; + void Visit(VarDeclStmt& node) override; + void Visit(ExprStmt& node) override; + void Visit(ReturnStmt& node) override; + void Visit(BreakStmt& node) override; + void Visit(ContinueStmt& node) override; + void Visit(IfStmt& node) override; + void Visit(WhileStmt& node) override; + void Visit(ForStmt& node) override; + void Visit(UnsafeBlock& node) override; + + void Visit(Binary& node) override; + void Visit(Unary& node) override; + void Visit(Assign& node) override; + void Visit(Call& node) override; + void Visit(FieldAccess& node) override; + void Visit(IndexAccess& node) override; + void Visit(NamespaceRef& node) override; + void Visit(SafeCall& node) override; + void Visit(Elvis& node) override; + void Visit(CastAs& node) override; + void Visit(TypeTestIs& node) override; + void Visit(IdentRef& node) override; + void Visit(IntLit& node) override; + void Visit(FloatLit& node) override; + void Visit(StringLit& node) override; + void Visit(CharLit& node) override; + void Visit(BoolLit& node) override; + void Visit(NullLit& node) override; +}; + +#endif // CONSTWALKVISITOR_HPP_ diff --git a/lib/parser/ast/visitors/PrintVisitor.cpp b/lib/parser/ast/visitors/PrintVisitor.cpp new file mode 100644 index 0000000..4172a13 --- /dev/null +++ b/lib/parser/ast/visitors/PrintVisitor.cpp @@ -0,0 +1,417 @@ +#include "PrintVisitor.hpp" + +#include "lib/parser/ast/nodes/class_members/CallDecl.hpp" +#include "lib/parser/ast/nodes/class_members/DestructorDecl.hpp" +#include "lib/parser/ast/nodes/class_members/FieldDecl.hpp" +#include "lib/parser/ast/nodes/class_members/MethodDecl.hpp" +#include "lib/parser/ast/nodes/class_members/StaticFieldDecl.hpp" + +#include "lib/parser/ast/nodes/decls/ClassDecl.hpp" +#include "lib/parser/ast/nodes/decls/FunctionDecl.hpp" +#include "lib/parser/ast/nodes/decls/GlobalVarDecl.hpp" +#include "lib/parser/ast/nodes/decls/InterfaceDecl.hpp" +#include "lib/parser/ast/nodes/decls/InterfaceMethod.hpp" +#include "lib/parser/ast/nodes/decls/Module.hpp" +#include "lib/parser/ast/nodes/decls/TypeAliasDecl.hpp" + +#include "lib/parser/ast/nodes/stmts/Block.hpp" + +PrintVisitor::PrintVisitor(std::ostream& output) : out_(&output) { +} + +PrintVisitor::PrintVisitor() : out_(&buffer_), use_buffer_(true) { +} + +std::string PrintVisitor::Str() const { + return buffer_.str(); +} + +void PrintVisitor::WriteIndent() { + for (int i = 0; i < indent_; ++i) { + *out_ << " "; + } +} + +void PrintVisitor::WriteLine(const std::string& text) { + WriteIndent(); + *out_ << text << '\n'; +} + +void PrintVisitor::Open(const std::string& header) { + WriteLine(header + " {"); + ++indent_; +} + +void PrintVisitor::Close() { + --indent_; + WriteLine("}"); +} + +void PrintVisitor::Visit(Module& node) { + Open("Module name=\"" + node.Name() + "\""); + for (auto& declaration : node.MutableDecls()) { + declaration->Accept(*this); + } + Close(); +} + +void PrintVisitor::Visit(FunctionDecl& node) { + Open(std::string("FunctionDecl name=\"") + node.Name() + "\"" + (node.IsPure() ? " pure" : "")); + if (auto* body = node.MutableBody()) { + body->Accept(*this); + } + Close(); +} + +void PrintVisitor::Visit(ClassDecl& node) { + Open(std::string("ClassDecl name=\"") + node.Name() + "\""); + for (auto& member : node.MutableMembers()) { + member->Accept(*this); + } + Close(); +} + +void PrintVisitor::Visit(InterfaceMethod& node) { + WriteLine(std::string("InterfaceMethod name=\"") + node.Name() + "\""); +} + +void PrintVisitor::Visit(InterfaceDecl& node) { + Open(std::string("InterfaceDecl name=\"") + node.Name() + "\""); + for (auto& method_ptr : node.MutableMembers()) { + method_ptr->Accept(*this); + } + Close(); +} + +void PrintVisitor::Visit(TypeAliasDecl& node) { + WriteLine(std::string("TypeAliasDecl name=\"") + node.Name() + "\""); +} + +void PrintVisitor::Visit(GlobalVarDecl& node) { + Open(std::string("GlobalVarDecl ") + (node.IsVar() ? "var" : "val") + " name=\"" + node.Name() + "\""); + if (auto* init_expr = node.MutableInit()) { + init_expr->Accept(*this); + } + + Close(); +} + +void PrintVisitor::Visit(FieldDecl& node) { + Open(std::string("FieldDecl ") + (node.IsVar() ? "var" : "val") + " name=\"" + node.Name() + "\"" + + (node.IsPublic() ? " public" : "")); + if (auto* init_expr = node.MutableInit()) { + init_expr->Accept(*this); + } + + Close(); +} + +void PrintVisitor::Visit(StaticFieldDecl& node) { + Open(std::string("StaticFieldDecl ") + (node.IsVar() ? "var" : "val") + " name=\"" + node.Name() + "\"" + + (node.IsPublic() ? " public" : "")); + if (auto* init_expr = node.MutableInit()) { + init_expr->Accept(*this); + } + + Close(); +} + +void PrintVisitor::Visit(MethodDecl& node) { + std::string flags; + if (node.IsPublic()) { + flags += " public"; + } + + if (node.IsStatic()) { + flags += " static"; + } + + if (node.IsOverride()) { + flags += " override"; + } + + if (node.IsPure()) { + flags += " pure"; + } + + Open("MethodDecl name=\"" + node.Name() + "\"" + flags); + if (auto* body = node.MutableBody()) { + body->Accept(*this); + } + + Close(); +} + +void PrintVisitor::Visit(CallDecl& node) { + Open(std::string("CallDecl") + (node.IsPublic() ? " public" : "")); + + if (auto* body = node.MutableBody()) { + body->Accept(*this); + } + + Close(); +} + +void PrintVisitor::Visit(DestructorDecl& node) { + Open(std::string("DestructorDecl") + (node.IsPublic() ? " public" : "")); + + if (auto* body = node.MutableBody()) { + body->Accept(*this); + } + + Close(); +} + +void PrintVisitor::Visit(Block& node) { + Open("Block"); + + for (auto& statement : node.GetStatements()) { + statement->Accept(*this); + } + + Close(); +} + +void PrintVisitor::Visit(VarDeclStmt& node) { + Open(std::string("VarDeclStmt ") + (node.IsVar() ? "var" : "val") + " name=\"" + node.Name() + "\""); + + if (auto* init_expr = node.MutableInit()) { + init_expr->Accept(*this); + } + + Close(); +} + +void PrintVisitor::Visit(ExprStmt& node) { + Open("ExprStmt"); + + if (auto* expression = node.MutableExpression()) { + expression->Accept(*this); + } + + Close(); +} + +void PrintVisitor::Visit(ReturnStmt& node) { + if (!node.HasValue()) { + WriteLine("ReturnStmt"); + return; + } + + Open("ReturnStmt"); + if (auto* value_expr = node.MutableValue()) { + value_expr->Accept(*this); + } + + Close(); +} + +void PrintVisitor::Visit(BreakStmt& node) { + WriteLine("BreakStmt"); + (void) node; +} + +void PrintVisitor::Visit(ContinueStmt& node) { + WriteLine("ContinueStmt"); + (void) node; +} + +void PrintVisitor::Visit(IfStmt& node) { + Open("IfStmt"); + for (auto& branch_value : node.MutableBranches()) { + Open("Branch"); + + if (auto* cond_expr = branch_value.MutableCondition()) { + Open("Condition"); + cond_expr->Accept(*this); + Close(); + } + + if (auto* then_block = branch_value.MutableThen()) { + Open("Then"); + then_block->Accept(*this); + Close(); + } + + Close(); + } + + if (auto* else_block = node.MutableElseBlock()) { + Open("Else"); + else_block->Accept(*this); + Close(); + } + + Close(); +} + +void PrintVisitor::Visit(WhileStmt& node) { + Open("WhileStmt"); + + if (auto* cond_expr = node.MutableCondition()) { + Open("Condition"); + cond_expr->Accept(*this); + Close(); + } + + if (auto* body_block = node.MutableBody()) { + Open("Body"); + body_block->Accept(*this); + Close(); + } + + Close(); +} + +void PrintVisitor::Visit(ForStmt& node) { + Open(std::string("ForStmt iterator=\"") + node.IteratorName() + "\""); + + if (auto* iter_expr = node.MutableIteratorExpr()) { + Open("Iterable"); + iter_expr->Accept(*this); + Close(); + } + + if (auto* body_block = node.MutableBody()) { + Open("Body"); + body_block->Accept(*this); + Close(); + } + + Close(); +} + +void PrintVisitor::Visit(UnsafeBlock& node) { + Open("UnsafeBlock"); + + if (auto* body_block = node.MutableBody()) { + body_block->Accept(*this); + } + + Close(); +} + +void PrintVisitor::Visit(Binary& node) { + Open(std::string("Binary op=\"") + std::string(node.Op().Name()) + "\""); + node.MutableLhs().Accept(*this); + node.MutableRhs().Accept(*this); + Close(); +} + +void PrintVisitor::Visit(Unary& node) { + Open(std::string("Unary op=\"") + std::string(node.Op().Name()) + "\""); + node.MutableOperand().Accept(*this); + Close(); +} + +void PrintVisitor::Visit(Assign& node) { + Open(std::string("Assign kind=\"") + std::string(node.Kind().Name()) + "\""); + Open("Target"); + node.MutableTarget().Accept(*this); + Close(); + Open("Value"); + node.MutableValue().Accept(*this); + Close(); + Close(); +} + +void PrintVisitor::Visit(Call& node) { + Open("Call"); + Open("Callee"); + node.MutableCallee().Accept(*this); + Close(); + Open("Args"); + + for (auto& argument : node.MutableArgs()) { + argument->Accept(*this); + } + + Close(); + Close(); +} + +void PrintVisitor::Visit(FieldAccess& node) { + Open(std::string("FieldAccess name=\"") + node.Name() + "\""); + node.MutableObject().Accept(*this); + Close(); +} + +void PrintVisitor::Visit(IndexAccess& node) { + Open("IndexAccess"); + Open("Object"); + node.MutableObject().Accept(*this); + Close(); + Open("Index"); + node.MutableIndexExpr().Accept(*this); + Close(); + Close(); +} + +void PrintVisitor::Visit(NamespaceRef& node) { + Open(std::string("NamespaceRef name=\"") + node.Name() + "\""); + node.MutableNamespaceExpr().Accept(*this); + Close(); +} + +void PrintVisitor::Visit(SafeCall& node) { + Open(std::string("SafeCall method=\"") + node.Method() + "\""); + Open("Object"); + node.MutableObject().Accept(*this); + Close(); + Open("Args"); + + for (auto& argument : node.MutableArgs()) { + argument->Accept(*this); + } + + Close(); + Close(); +} + +void PrintVisitor::Visit(Elvis& node) { + Open("Elvis"); + node.MutableLhs().Accept(*this); + node.MutableRhs().Accept(*this); + Close(); +} + +void PrintVisitor::Visit(CastAs& node) { + Open("CastAs"); + node.MutableExpression().Accept(*this); + Close(); +} + +void PrintVisitor::Visit(TypeTestIs& node) { + Open("TypeTestIs"); + node.MutableExpression().Accept(*this); + Close(); +} + +void PrintVisitor::Visit(IdentRef& node) { + WriteLine(std::string("IdentRef name=\"") + node.Name() + "\""); +} + +void PrintVisitor::Visit(IntLit& node) { + WriteLine("IntLit value=" + std::to_string(node.Value())); +} + +void PrintVisitor::Visit(FloatLit& node) { + WriteLine("FloatLit value=" + std::to_string(node.Value())); +} + +void PrintVisitor::Visit(StringLit& node) { + WriteLine(std::string("StringLit value=\"") + node.Value() + "\""); +} + +void PrintVisitor::Visit(CharLit& node) { + std::string printable(1, node.Value()); + WriteLine(std::string("CharLit value='") + printable + "'"); +} + +void PrintVisitor::Visit(BoolLit& node) { + WriteLine(std::string("BoolLit value=") + (node.Value() ? "true" : "false")); +} + +void PrintVisitor::Visit(NullLit& node) { + WriteLine("NullLit"); +} diff --git a/lib/parser/ast/visitors/PrintVisitor.hpp b/lib/parser/ast/visitors/PrintVisitor.hpp new file mode 100644 index 0000000..97eb5eb --- /dev/null +++ b/lib/parser/ast/visitors/PrintVisitor.hpp @@ -0,0 +1,74 @@ +#ifndef PRINTVISITOR_HPP_ +#define PRINTVISITOR_HPP_ + +#include +#include +#include + +#include "lib/parser/ast/AstVisitor.hpp" + +class PrintVisitor : public AstVisitor { +public: + explicit PrintVisitor(std::ostream& output); + PrintVisitor(); + + ~PrintVisitor() override = default; + + std::string Str() const; + + void Visit(Module& node) override; + void Visit(FunctionDecl& node) override; + void Visit(ClassDecl& node) override; + void Visit(InterfaceMethod& node) override; + void Visit(InterfaceDecl& node) override; + void Visit(TypeAliasDecl& node) override; + void Visit(GlobalVarDecl& node) override; + void Visit(FieldDecl& node) override; + void Visit(StaticFieldDecl& node) override; + void Visit(MethodDecl& node) override; + void Visit(CallDecl& node) override; + void Visit(DestructorDecl& node) override; + + void Visit(Block& node) override; + void Visit(VarDeclStmt& node) override; + void Visit(ExprStmt& node) override; + void Visit(ReturnStmt& node) override; + void Visit(BreakStmt& node) override; + void Visit(ContinueStmt& node) override; + void Visit(IfStmt& node) override; + void Visit(WhileStmt& node) override; + void Visit(ForStmt& node) override; + void Visit(UnsafeBlock& node) override; + + void Visit(Binary& node) override; + void Visit(Unary& node) override; + void Visit(Assign& node) override; + void Visit(Call& node) override; + void Visit(FieldAccess& node) override; + void Visit(IndexAccess& node) override; + void Visit(NamespaceRef& node) override; + void Visit(SafeCall& node) override; + void Visit(Elvis& node) override; + void Visit(CastAs& node) override; + void Visit(TypeTestIs& node) override; + void Visit(IdentRef& node) override; + void Visit(IntLit& node) override; + void Visit(FloatLit& node) override; + void Visit(StringLit& node) override; + void Visit(CharLit& node) override; + void Visit(BoolLit& node) override; + void Visit(NullLit& node) override; + +private: + void WriteIndent(); + void WriteLine(const std::string& text); + void Open(const std::string& header); + void Close(); + + std::ostream* out_ = nullptr; + std::ostringstream buffer_; + int indent_ = 0; + bool use_buffer_ = false; +}; + +#endif // PRINTVISITOR_HPP_ diff --git a/lib/parser/ast/visitors/WalkVisitor.cpp b/lib/parser/ast/visitors/WalkVisitor.cpp new file mode 100644 index 0000000..ce6e145 --- /dev/null +++ b/lib/parser/ast/visitors/WalkVisitor.cpp @@ -0,0 +1,270 @@ +#include "WalkVisitor.hpp" + +#include "lib/parser/ast/nodes/decls/ClassDecl.hpp" +#include "lib/parser/ast/nodes/decls/FunctionDecl.hpp" +#include "lib/parser/ast/nodes/decls/GlobalVarDecl.hpp" +#include "lib/parser/ast/nodes/decls/InterfaceDecl.hpp" +#include "lib/parser/ast/nodes/decls/InterfaceMethod.hpp" +#include "lib/parser/ast/nodes/decls/Module.hpp" +#include "lib/parser/ast/nodes/decls/TypeAliasDecl.hpp" + +#include "lib/parser/ast/nodes/class_members/CallDecl.hpp" +#include "lib/parser/ast/nodes/class_members/DestructorDecl.hpp" +#include "lib/parser/ast/nodes/class_members/FieldDecl.hpp" +#include "lib/parser/ast/nodes/class_members/MethodDecl.hpp" +#include "lib/parser/ast/nodes/class_members/StaticFieldDecl.hpp" + +#include "lib/parser/ast/nodes/exprs/Assign.hpp" +#include "lib/parser/ast/nodes/exprs/Binary.hpp" +#include "lib/parser/ast/nodes/exprs/Call.hpp" +#include "lib/parser/ast/nodes/exprs/CastAs.hpp" +#include "lib/parser/ast/nodes/exprs/Elvis.hpp" +#include "lib/parser/ast/nodes/exprs/FieldAccess.hpp" +#include "lib/parser/ast/nodes/exprs/IdentRef.hpp" +#include "lib/parser/ast/nodes/exprs/IndexAccess.hpp" +#include "lib/parser/ast/nodes/exprs/NamespaceRef.hpp" +#include "lib/parser/ast/nodes/exprs/SafeCall.hpp" +#include "lib/parser/ast/nodes/exprs/TypeTestIs.hpp" +#include "lib/parser/ast/nodes/exprs/Unary.hpp" + +#include "lib/parser/ast/nodes/exprs/literals/BoolLit.hpp" +#include "lib/parser/ast/nodes/exprs/literals/CharLit.hpp" +#include "lib/parser/ast/nodes/exprs/literals/FloatLit.hpp" +#include "lib/parser/ast/nodes/exprs/literals/IntLit.hpp" +#include "lib/parser/ast/nodes/exprs/literals/NullLit.hpp" +#include "lib/parser/ast/nodes/exprs/literals/StringLit.hpp" + +#include "lib/parser/ast/nodes/stmts/Block.hpp" +#include "lib/parser/ast/nodes/stmts/BreakStmt.hpp" +#include "lib/parser/ast/nodes/stmts/ContinueStmt.hpp" +#include "lib/parser/ast/nodes/stmts/ExprStmt.hpp" +#include "lib/parser/ast/nodes/stmts/ForStmt.hpp" +#include "lib/parser/ast/nodes/stmts/IfStmt.hpp" +#include "lib/parser/ast/nodes/stmts/ReturnStmt.hpp" +#include "lib/parser/ast/nodes/stmts/UnsafeBlock.hpp" +#include "lib/parser/ast/nodes/stmts/VarDeclStmt.hpp" +#include "lib/parser/ast/nodes/stmts/WhileStmt.hpp" + +void WalkVisitor::Visit(Module& node) { + for (auto& decl_ptr : node.MutableDecls()) { + decl_ptr->Accept(*this); + } +} + +void WalkVisitor::Visit(FunctionDecl& node) { + if (auto* body = node.MutableBody()) { + body->Accept(*this); + } +} + +void WalkVisitor::Visit(ClassDecl& node) { + for (auto& member : node.MutableMembers()) { + member->Accept(*this); + } +} + +void WalkVisitor::Visit(InterfaceMethod& node) { + (void) node; +} + +void WalkVisitor::Visit(InterfaceDecl& node) { + for (auto& m : node.MutableMembers()) { + m->Accept(*this); + } +} + +void WalkVisitor::Visit(TypeAliasDecl& node) { + (void) node; +} + +void WalkVisitor::Visit(GlobalVarDecl& node) { + if (auto* init = node.MutableInit()) { + init->Accept(*this); + } +} + +void WalkVisitor::Visit(FieldDecl& node) { + if (auto* init = node.MutableInit()) { + init->Accept(*this); + } +} + +void WalkVisitor::Visit(StaticFieldDecl& node) { + if (auto* init = node.MutableInit()) { + init->Accept(*this); + } +} + +void WalkVisitor::Visit(MethodDecl& node) { + if (auto* body = node.MutableBody()) { + body->Accept(*this); + } +} + +void WalkVisitor::Visit(CallDecl& node) { + if (auto* body = node.MutableBody()) { + body->Accept(*this); + } +} + +void WalkVisitor::Visit(DestructorDecl& node) { + if (auto* body = node.MutableBody()) { + body->Accept(*this); + } +} + +void WalkVisitor::Visit(Block& node) { + for (auto& stmt : node.GetStatements()) { + stmt->Accept(*this); + } +} + +void WalkVisitor::Visit(VarDeclStmt& node) { + if (auto* init = node.MutableInit()) { + init->Accept(*this); + } +} + +void WalkVisitor::Visit(ExprStmt& node) { + if (auto* e = node.MutableExpression()) { + e->Accept(*this); + } +} + +void WalkVisitor::Visit(ReturnStmt& node) { + if (auto* v = node.MutableValue()) { + v->Accept(*this); + } +} + +void WalkVisitor::Visit(BreakStmt& node) { + (void) node; +} + +void WalkVisitor::Visit(ContinueStmt& node) { + (void) node; +} + +void WalkVisitor::Visit(IfStmt& node) { + for (auto& br : node.MutableBranches()) { + if (auto* c = br.MutableCondition()) { + c->Accept(*this); + } + + if (auto* t = br.MutableThen()) { + t->Accept(*this); + } + } + + if (auto* eb = node.MutableElseBlock()) { + eb->Accept(*this); + } +} + +void WalkVisitor::Visit(WhileStmt& node) { + if (auto* c = node.MutableCondition()) { + c->Accept(*this); + } + + if (auto* b = node.MutableBody()) { + b->Accept(*this); + } +} + +void WalkVisitor::Visit(ForStmt& node) { + if (auto* it = node.MutableIteratorExpr()) { + it->Accept(*this); + } + + if (auto* b = node.MutableBody()) { + b->Accept(*this); + } +} + +void WalkVisitor::Visit(UnsafeBlock& node) { + if (auto* b = node.MutableBody()) { + b->Accept(*this); + } +} + +void WalkVisitor::Visit(Binary& node) { + node.MutableLhs().Accept(*this); + node.MutableRhs().Accept(*this); +} + +void WalkVisitor::Visit(Unary& node) { + node.MutableOperand().Accept(*this); +} + +void WalkVisitor::Visit(Assign& node) { + node.MutableTarget().Accept(*this); + node.MutableValue().Accept(*this); +} + +void WalkVisitor::Visit(Call& node) { + node.MutableCallee().Accept(*this); + for (auto& a : node.MutableArgs()) { + a->Accept(*this); + } +} + +void WalkVisitor::Visit(FieldAccess& node) { + node.MutableObject().Accept(*this); +} + +void WalkVisitor::Visit(IndexAccess& node) { + node.MutableObject().Accept(*this); + node.MutableIndexExpr().Accept(*this); +} + +void WalkVisitor::Visit(NamespaceRef& node) { + node.MutableNamespaceExpr().Accept(*this); +} + +void WalkVisitor::Visit(SafeCall& node) { + node.MutableObject().Accept(*this); + for (auto& a : node.MutableArgs()) { + a->Accept(*this); + } +} + +void WalkVisitor::Visit(Elvis& node) { + node.MutableLhs().Accept(*this); + node.MutableRhs().Accept(*this); +} + +void WalkVisitor::Visit(CastAs& node) { + node.MutableExpression().Accept(*this); +} + +void WalkVisitor::Visit(TypeTestIs& node) { + node.MutableExpression().Accept(*this); +} + +void WalkVisitor::Visit(IdentRef& node) { + (void) node; +} + +void WalkVisitor::Visit(IntLit& node) { + (void) node; +} + +void WalkVisitor::Visit(FloatLit& node) { + (void) node; +} + +void WalkVisitor::Visit(StringLit& node) { + (void) node; +} + +void WalkVisitor::Visit(CharLit& node) { + (void) node; +} + +void WalkVisitor::Visit(BoolLit& node) { + (void) node; +} + +void WalkVisitor::Visit(NullLit& node) { + (void) node; +} diff --git a/lib/parser/ast/visitors/WalkVisitor.hpp b/lib/parser/ast/visitors/WalkVisitor.hpp new file mode 100644 index 0000000..7b8aea5 --- /dev/null +++ b/lib/parser/ast/visitors/WalkVisitor.hpp @@ -0,0 +1,54 @@ +#ifndef WALKVISITOR_HPP_ +#define WALKVISITOR_HPP_ + +#include "lib/parser/ast/AstVisitor.hpp" + +class WalkVisitor : public AstVisitor { +public: + ~WalkVisitor() override = default; + + void Visit(Module& node) override; + void Visit(FunctionDecl& node) override; + void Visit(ClassDecl& node) override; + void Visit(InterfaceMethod& node) override; + void Visit(InterfaceDecl& node) override; + void Visit(TypeAliasDecl& node) override; + void Visit(GlobalVarDecl& node) override; + void Visit(FieldDecl& node) override; + void Visit(StaticFieldDecl& node) override; + void Visit(MethodDecl& node) override; + void Visit(CallDecl& node) override; + void Visit(DestructorDecl& node) override; + + void Visit(Block& node) override; + void Visit(VarDeclStmt& node) override; + void Visit(ExprStmt& node) override; + void Visit(ReturnStmt& node) override; + void Visit(BreakStmt& node) override; + void Visit(ContinueStmt& node) override; + void Visit(IfStmt& node) override; + void Visit(WhileStmt& node) override; + void Visit(ForStmt& node) override; + void Visit(UnsafeBlock& node) override; + + void Visit(Binary& node) override; + void Visit(Unary& node) override; + void Visit(Assign& node) override; + void Visit(Call& node) override; + void Visit(FieldAccess& node) override; + void Visit(IndexAccess& node) override; + void Visit(NamespaceRef& node) override; + void Visit(SafeCall& node) override; + void Visit(Elvis& node) override; + void Visit(CastAs& node) override; + void Visit(TypeTestIs& node) override; + void Visit(IdentRef& node) override; + void Visit(IntLit& node) override; + void Visit(FloatLit& node) override; + void Visit(StringLit& node) override; + void Visit(CharLit& node) override; + void Visit(BoolLit& node) override; + void Visit(NullLit& node) override; +}; + +#endif // WALKVISITOR_HPP_ From aec527f870b865c62bbce023ca854474b19648ff Mon Sep 17 00:00:00 2001 From: biqiboqi Date: Sun, 9 Nov 2025 22:35:17 +0300 Subject: [PATCH 12/21] feat: add contexts --- lib/parser/context/ContextParser.cpp | 82 ++++++++++++++++++++++++++++ lib/parser/context/ContextParser.hpp | 24 +++++--- lib/parser/context/NodeEntry.cpp | 21 +++++++ lib/parser/context/NodeEntry.hpp | 8 ++- 4 files changed, 125 insertions(+), 10 deletions(-) diff --git a/lib/parser/context/ContextParser.cpp b/lib/parser/context/ContextParser.cpp index ee3ec25..9e410a9 100644 --- a/lib/parser/context/ContextParser.cpp +++ b/lib/parser/context/ContextParser.cpp @@ -1 +1,83 @@ #include "ContextParser.hpp" + +#include + +std::vector& ContextParser::StateStack() { + return state_stack_; +} + +std::vector& ContextParser::NodeStack() { + return node_stack_; +} + +void ContextParser::SetDiagnostics(IDiagnosticSink* diagnostics) { + diags_ = diagnostics; +} + +IDiagnosticSink* ContextParser::Diags() const { + return diags_; +} + +void ContextParser::SetExpr(IExpressionParser* parser) { + expr_ = parser; +} + +IExpressionParser* ContextParser::Expr() const { + return expr_; +} + +void ContextParser::SetTypeParser(ITypeParser* parser) { + typep_ = parser; +} + +ITypeParser* ContextParser::TypeParser() const { + return typep_; +} + +void ContextParser::PushState(const IState& state) { + state_stack_.push_back(&state); +} + +void ContextParser::PopState() { + if (!state_stack_.empty()) { + state_stack_.pop_back(); + } +} + +const IState* ContextParser::CurrentState() const { + if (state_stack_.empty()) { + return nullptr; + } + + return state_stack_.back(); +} + +void ContextParser::PushNode(std::unique_ptr node) { + node_stack_.emplace_back(std::move(node)); +} + +std::unique_ptr ContextParser::PopNode() { + if (node_stack_.empty()) { + return nullptr; + } + + auto out = node_stack_.back().ReleaseNode(); + node_stack_.pop_back(); + return out; +} + +bool ContextParser::HasStates() const noexcept { + return !state_stack_.empty(); +} + +bool ContextParser::HasNodes() const noexcept { + return !node_stack_.empty(); +} + +void ContextParser::Clear() { + state_stack_.clear(); + node_stack_.clear(); + diags_ = nullptr; + expr_ = nullptr; + typep_ = nullptr; +} diff --git a/lib/parser/context/ContextParser.hpp b/lib/parser/context/ContextParser.hpp index ccffc6f..7e7c8eb 100644 --- a/lib/parser/context/ContextParser.hpp +++ b/lib/parser/context/ContextParser.hpp @@ -21,28 +21,34 @@ class ContextParser { std::vector& StateStack(); std::vector& NodeStack(); - void SetDiagnostics(IDiagnosticSink* d); - [[nodiscard]] IDiagnosticSink* Diags() const; + void SetDiagnostics(IDiagnosticSink* diagnostics); + IDiagnosticSink* Diags() const; void SetExpr(IExpressionParser* parser); - [[nodiscard]] IExpressionParser* Expr() const; + IExpressionParser* Expr() const; void SetTypeParser(ITypeParser* parser); - [[nodiscard]] ITypeParser* TypeParser() const; + ITypeParser* TypeParser() const; void PushState(const IState& state); void PopState(); - [[nodiscard]] const IState* CurrentState() const; + const IState* CurrentState() const; template - [[nodiscard]] T* TopNodeAs() { - if (node_stack_.empty()) + T* TopNodeAs() { + if (node_stack_.empty()) { return nullptr; - return dynamic_cast(node_stack_.back().GetNode); + } + + return dynamic_cast(node_stack_.back().MutableNode()); } void PushNode(std::unique_ptr node); - [[nodiscard]] std::unique_ptr PopNode(); + std::unique_ptr PopNode(); + + bool HasStates() const noexcept; + bool HasNodes() const noexcept; + void Clear(); private: std::vector state_stack_; diff --git a/lib/parser/context/NodeEntry.cpp b/lib/parser/context/NodeEntry.cpp index 1261a37..0409b23 100644 --- a/lib/parser/context/NodeEntry.cpp +++ b/lib/parser/context/NodeEntry.cpp @@ -1 +1,22 @@ #include "NodeEntry.hpp" + +#include + +NodeEntry::NodeEntry(std::unique_ptr node) : node_(std::move(node)) { +} + +const AstNode* NodeEntry::GetNode() const noexcept { + return node_.get(); +} + +AstNode* NodeEntry::MutableNode() noexcept { + return node_.get(); +} + +void NodeEntry::SetNode(std::unique_ptr node) { + node_ = std::move(node); +} + +std::unique_ptr NodeEntry::ReleaseNode() { + return std::move(node_); +} diff --git a/lib/parser/context/NodeEntry.hpp b/lib/parser/context/NodeEntry.hpp index b59c332..195b394 100644 --- a/lib/parser/context/NodeEntry.hpp +++ b/lib/parser/context/NodeEntry.hpp @@ -7,7 +7,13 @@ class NodeEntry { public: - std::unique_ptr GetNode; + NodeEntry() = default; + explicit NodeEntry(std::unique_ptr node); + + const AstNode* GetNode() const noexcept; + AstNode* MutableNode() noexcept; + void SetNode(std::unique_ptr node); + std::unique_ptr ReleaseNode(); private: std::unique_ptr node_; From cadbe0e890b5065ebb7c809d07f4346983a0fa74 Mon Sep 17 00:00:00 2001 From: biqiboqi Date: Sun, 9 Nov 2025 22:41:55 +0300 Subject: [PATCH 13/21] feat: add types --- lib/parser/ast/nodes/base/AstNode.hpp | 2 + lib/parser/types/Nullable.cpp | 19 ++ lib/parser/types/Param.cpp | 14 +- lib/parser/types/ResolvedTypeHandle.cpp | 24 ++- lib/parser/types/TypeReference.cpp | 255 +++++++++++++++++++++++- lib/parser/types/TypeReference.hpp | 67 ++----- 6 files changed, 329 insertions(+), 52 deletions(-) diff --git a/lib/parser/ast/nodes/base/AstNode.hpp b/lib/parser/ast/nodes/base/AstNode.hpp index 1db12fd..fafbe77 100644 --- a/lib/parser/ast/nodes/base/AstNode.hpp +++ b/lib/parser/ast/nodes/base/AstNode.hpp @@ -7,6 +7,8 @@ class AstNode { public: virtual ~AstNode() = default; + // TODO: add positions...... + virtual void Accept(AstVisitor& visitor) = 0; }; diff --git a/lib/parser/types/Nullable.cpp b/lib/parser/types/Nullable.cpp index 576c114..9aacd66 100644 --- a/lib/parser/types/Nullable.cpp +++ b/lib/parser/types/Nullable.cpp @@ -1 +1,20 @@ #include "Nullable.hpp" + +Nullable::Nullable(bool on) noexcept : on_(on) { +} + +bool Nullable::IsOn() const noexcept { + return on_; +} + +void Nullable::Set(bool on) noexcept { + on_ = on; +} + +void Nullable::Enable() noexcept { + on_ = true; +} + +void Nullable::Disable() noexcept { + on_ = false; +} diff --git a/lib/parser/types/Param.cpp b/lib/parser/types/Param.cpp index 9fca7ad..44d5fb0 100644 --- a/lib/parser/types/Param.cpp +++ b/lib/parser/types/Param.cpp @@ -1 +1,13 @@ -#include "Param.hpp" +#include "lib/parser/types/Param.hpp" + +Param::Param(std::string name, TypeReference typeReference) : + name_(std::move(name)), reference_(std::move(typeReference)) { +} + +std::string Param::GetName() { + return name_; +} + +TypeReference Param::GetType() { + return reference_; +} diff --git a/lib/parser/types/ResolvedTypeHandle.cpp b/lib/parser/types/ResolvedTypeHandle.cpp index f8cc3af..6523121 100644 --- a/lib/parser/types/ResolvedTypeHandle.cpp +++ b/lib/parser/types/ResolvedTypeHandle.cpp @@ -1 +1,23 @@ -#include "ResolvedTypeHandle.hpp" +#include "lib/parser/types/ResolvedTypeHandle.hpp" + +#include + +ResolvedTypeHandle::ResolvedTypeHandle(const void* decl, std::string mangled, bool is_interface) : + decl_(decl), mangled_(std::move(mangled)), is_interface_(is_interface) { +} + +const void* ResolvedTypeHandle::Decl() const noexcept { + return decl_; +} + +std::string_view ResolvedTypeHandle::Mangled() const noexcept { + return mangled_; +} + +bool ResolvedTypeHandle::IsInterface() const noexcept { + return is_interface_; +} + +bool ResolvedTypeHandle::IsValid() const noexcept { + return decl_ != nullptr; +} diff --git a/lib/parser/types/TypeReference.cpp b/lib/parser/types/TypeReference.cpp index 13a7e1b..712dd8b 100644 --- a/lib/parser/types/TypeReference.cpp +++ b/lib/parser/types/TypeReference.cpp @@ -1 +1,254 @@ -#include "TypeReference.hpp" +#include "lib/parser/types/TypeReference.hpp" + +#include +#include +#include +#include + +TypeReference::TypeReference() = default; + +TypeReference::TypeReference(std::string name) { + qname_.clear(); + qname_.push_back(std::move(name)); +} + +TypeReference::TypeReference(std::vector qname) : qname_(std::move(qname)) { +} + +TypeReference::TypeReference(const TypeReference& ref) : + qname_(ref.qname_), type_args_(ref.type_args_), nullable_(ref.nullable_) { + if (ref.resolved_) { + resolved_ = std::make_unique(*ref.resolved_); + } +} + +TypeReference::TypeReference(TypeReference&& ref) noexcept : + qname_(std::move(ref.qname_)), type_args_(std::move(ref.type_args_)), nullable_(ref.nullable_), + resolved_(std::move(ref.resolved_)) { +} + +TypeReference::~TypeReference() = default; + +TypeReference& TypeReference::operator=(const TypeReference& ref) { + if (this == &ref) { + return *this; + } + + qname_ = ref.qname_; + type_args_ = ref.type_args_; + nullable_ = ref.nullable_; + + if (ref.resolved_) { + resolved_ = std::make_unique(*ref.resolved_); + } else { + resolved_.reset(); + } + + return *this; +} + +TypeReference& TypeReference::operator=(TypeReference&& ref) noexcept { + if (this == &ref) { + return *this; + } + + qname_ = std::move(ref.qname_); + type_args_ = std::move(ref.type_args_); + nullable_ = ref.nullable_; + resolved_ = std::move(ref.resolved_); + return *this; +} + +TypeReference& TypeReference::operator=(std::string name) { + qname_.clear(); + qname_.push_back(std::move(name)); + InvalidateResolution(); + return *this; +} + +bool TypeReference::StructurallyEquals(const TypeReference& other) const noexcept { + if (nullable_.IsOn() != other.nullable_.IsOn()) { + return false; + } + + if (qname_ != other.qname_) { + return false; + } + + if (type_args_.size() != other.type_args_.size()) { + return false; + } + + for (std::size_t i = 0; i < type_args_.size(); ++i) { + if (!type_args_[i].StructurallyEquals(other.type_args_[i])) { + return false; + } + } + return true; +} + +std::string TypeReference::StableKey() const { + std::ostringstream out; + out << JoinQualified(qname_); + if (!type_args_.empty()) { + out << '<' << ArgsToString(type_args_) << '>'; + } + + if (nullable_.IsOn()) { + out << '?'; + } + + return out.str(); +} + +std::string TypeReference::ToStringHuman() const { + return StableKey(); +} + +const std::vector& TypeReference::QualifiedName() const noexcept { + return qname_; +} + +std::string_view TypeReference::SimpleName() const noexcept { + if (qname_.empty()) { + return {}; + } + + return qname_.back(); +} + +void TypeReference::SetQualifiedName(std::vector qname) { + qname_ = std::move(qname); + InvalidateResolution(); +} + +void TypeReference::SetSimpleName(std::string name) { + if (qname_.empty()) { + qname_.push_back(std::move(name)); + } else { + qname_.back() = std::move(name); + } + + InvalidateResolution(); +} + +void TypeReference::PushQualifier(std::string qualifier) { + qname_.push_back(std::move(qualifier)); + InvalidateResolution(); +} + +bool TypeReference::PopFrontQualifier() { + if (qname_.empty()) { + return false; + } + + qname_.erase(qname_.begin()); + InvalidateResolution(); + return true; +} + +bool TypeReference::PopBackQualifier() { + if (qname_.empty()) { + return false; + } + + qname_.pop_back(); + InvalidateResolution(); + return true; +} + +const std::vector& TypeReference::TypeArguments() const noexcept { + return type_args_; +} + +std::vector& TypeReference::MutableTypeArguments() noexcept { + return type_args_; +} + +void TypeReference::ClearTypeArguments() { + type_args_.clear(); + InvalidateResolution(); +} + +void TypeReference::AddTypeArgument(TypeReference arg) { + type_args_.emplace_back(std::move(arg)); + InvalidateResolution(); +} + +std::size_t TypeReference::Arity() const noexcept { + return type_args_.size(); +} + +const Nullable& TypeReference::Nullability() const noexcept { + return nullable_; +} + +bool TypeReference::IsNullable() const noexcept { + return nullable_.IsOn(); +} + +void TypeReference::SetNullable(bool on) noexcept { + nullable_.Set(on); +} + +void TypeReference::MakeNullable() noexcept { + nullable_.Enable(); +} + +void TypeReference::MakeNonNullable() noexcept { + nullable_.Disable(); +} + +TypeReference TypeReference::WithoutNullable() const { + TypeReference copy{*this}; + copy.MakeNonNullable(); + copy.InvalidateResolution(); + return copy; +} + +bool TypeReference::IsResolved() const noexcept { + return static_cast(resolved_); +} + +const ResolvedTypeHandle* TypeReference::Resolved() const noexcept { + return resolved_ ? resolved_.get() : nullptr; +} + +void TypeReference::SetResolvedHandle(const void* decl, std::string mangled, bool is_interface) { + resolved_ = std::make_unique(decl, std::move(mangled), is_interface); +} + +void TypeReference::ResetResolvedHandle() noexcept { + resolved_.reset(); +} + +std::string TypeReference::JoinQualified(const std::vector& parts) { + if (parts.empty()) { + return {}; + } + + std::ostringstream out; + for (std::size_t i = 0; i < parts.size(); ++i) { + if (i) { + out << "."; + } + out << parts[i]; + } + return out.str(); +} + +std::string TypeReference::ArgsToString(const std::vector& args) { + std::ostringstream out; + for (std::size_t i = 0; i < args.size(); ++i) { + if (i) { + out << ", "; + } + + out << args[i].StableKey(); + } + return out.str(); +} + +void TypeReference::InvalidateResolution() noexcept { + resolved_.reset(); +} diff --git a/lib/parser/types/TypeReference.hpp b/lib/parser/types/TypeReference.hpp index fccc013..7a0a716 100644 --- a/lib/parser/types/TypeReference.hpp +++ b/lib/parser/types/TypeReference.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include "Nullable.hpp" @@ -27,76 +28,44 @@ class TypeReference { bool StructurallyEquals(const TypeReference& other) const noexcept; std::string StableKey() const; + std::string ToStringHuman() const; - std::string ToStringHuman() const { - return StableKey(); - } - - const std::vector& QualifiedName() const noexcept { - return qname_; - } - + const std::vector& QualifiedName() const noexcept; std::string_view SimpleName() const noexcept; void SetQualifiedName(std::vector qname); - void SetSimpleName(std::string name); - void PushQualifier(std::string qualifier); bool PopFrontQualifier(); bool PopBackQualifier(); - const std::vector& TypeArguments() const noexcept { - return type_args_; - } - std::vector& MutableTypeArguments() noexcept { - return type_args_; - } + const std::vector& TypeArguments() const noexcept; + std::vector& MutableTypeArguments() noexcept; void ClearTypeArguments(); void AddTypeArgument(TypeReference arg); - std::size_t Arity() const noexcept { - return type_args_.size(); - } - - const Nullable& Nullability() const noexcept { - return nullable_; - } - bool IsNullable() const noexcept { - return nullable_.IsOn(); - } - void SetNullable(bool on) noexcept { - nullable_.Set(on); - } - void MakeNullable() noexcept { - nullable_.Enable(); - } - void MakeNonNullable() noexcept { - nullable_.Disable(); - } + std::size_t Arity() const noexcept; + + const Nullable& Nullability() const noexcept; + bool IsNullable() const noexcept; + void SetNullable(bool on) noexcept; + void MakeNullable() noexcept; + void MakeNonNullable() noexcept; TypeReference WithoutNullable() const; - bool IsResolved() const noexcept { - return static_cast(resolved_); - } - const ResolvedTypeHandle* Resolved() const noexcept { - return resolved_ ? resolved_.get() : nullptr; - } + bool IsResolved() const noexcept; + const ResolvedTypeHandle* Resolved() const noexcept; void SetResolvedHandle(const void* decl, std::string mangled, bool is_interface); - - void ResetResolvedHandle() noexcept { - resolved_.reset(); - } + void ResetResolvedHandle() noexcept; private: - static std::string JoinQualified(const std::vector& parts, std::string_view sep); + static std::string JoinQualified(const std::vector& parts); static std::string ArgsToString(const std::vector& args); - void InvalidateResolution() noexcept { - resolved_.reset(); - } + void InvalidateResolution() noexcept; + std::vector qname_; std::vector type_args_; Nullable nullable_; From 2a390fe9f3d45603773d98d0a1629b6a22054a5c Mon Sep 17 00:00:00 2001 From: biqiboqi Date: Sun, 9 Nov 2025 22:50:13 +0300 Subject: [PATCH 14/21] feat: add diagnostics --- .../ast/visitors/StructuralValidator.cpp | 114 +++++++++++++ .../ast/visitors/StructuralValidator.hpp | 31 ++++ lib/parser/diagnostics/Diagnostic.cpp | 128 +++++++++++++++ lib/parser/diagnostics/Diagnostic.hpp | 19 ++- .../diagnostics/DiagnosticCollector.cpp | 155 +++++++++++++++++- lib/parser/diagnostics/FixIt.cpp | 8 +- lib/parser/diagnostics/FixIt.hpp | 2 +- lib/parser/diagnostics/IDiagnosticSink.hpp | 3 + lib/parser/diagnostics/RelatedInfo.cpp | 8 +- lib/parser/diagnostics/severity/ISeverity.cpp | 1 - lib/parser/diagnostics/severity/Severity.cpp | 46 +++++- lib/parser/diagnostics/severity/Severity.hpp | 1 + 12 files changed, 501 insertions(+), 15 deletions(-) create mode 100644 lib/parser/ast/visitors/StructuralValidator.cpp create mode 100644 lib/parser/ast/visitors/StructuralValidator.hpp delete mode 100644 lib/parser/diagnostics/severity/ISeverity.cpp diff --git a/lib/parser/ast/visitors/StructuralValidator.cpp b/lib/parser/ast/visitors/StructuralValidator.cpp new file mode 100644 index 0000000..0f6bc25 --- /dev/null +++ b/lib/parser/ast/visitors/StructuralValidator.cpp @@ -0,0 +1,114 @@ +#include "StructuralValidator.hpp" + +#include "lib/parser/diagnostics/severity/Severity.hpp" + +#include "lib/parser/ast/nodes/class_members/CallDecl.hpp" +#include "lib/parser/ast/nodes/class_members/DestructorDecl.hpp" +#include "lib/parser/ast/nodes/class_members/MethodDecl.hpp" + +#include "lib/parser/ast/nodes/decls/ClassDecl.hpp" +#include "lib/parser/ast/nodes/decls/FunctionDecl.hpp" +#include "lib/parser/ast/nodes/decls/Module.hpp" + +#include "lib/parser/ast/nodes/exprs/Binary.hpp" +#include "lib/parser/ast/nodes/exprs/Call.hpp" +#include "lib/parser/ast/nodes/exprs/FieldAccess.hpp" +#include "lib/parser/ast/nodes/exprs/IndexAccess.hpp" +#include "lib/parser/ast/nodes/exprs/NamespaceRef.hpp" +#include "lib/parser/ast/nodes/exprs/SafeCall.hpp" +#include "lib/parser/ast/nodes/exprs/Unary.hpp" + +void StructuralValidator::Visit(Module& node) { + if (node.Name().empty()) { + sink_.Error("E0001", "module name must not be empty"); + } + + WalkVisitor::Visit(node); +} + +void StructuralValidator::Visit(FunctionDecl& node) { + if (!node.IsPure() && node.MutableBody() == nullptr) { + sink_.Error("E1001", "function must have a body"); + } + + WalkVisitor::Visit(node); +} + +void StructuralValidator::Visit(ClassDecl& node) { + if (node.Name().empty()) { + sink_.Error("E1002", "class name must not be empty"); + } + + WalkVisitor::Visit(node); +} + +void StructuralValidator::Visit(CallDecl& node) { + if (node.MutableBody() == nullptr) { + sink_.Error("E1101", "call declaration must have a body"); + } + + WalkVisitor::Visit(node); +} + +void StructuralValidator::Visit(MethodDecl& node) { + if (!node.IsPure() && node.MutableBody() == nullptr) { + sink_.Error("E1201", "method must have a body"); + } + + WalkVisitor::Visit(node); +} + +void StructuralValidator::Visit(DestructorDecl& node) { + if (node.MutableBody() == nullptr) { + sink_.Error("E1301", "destructor must have a body"); + } + + WalkVisitor::Visit(node); +} + +void StructuralValidator::Visit(Call& node) { + if (&node.MutableCallee() == nullptr) { + sink_.Error("E2001", "call must have callee"); + } + + WalkVisitor::Visit(node); +} + +void StructuralValidator::Visit(Binary& node) { + (void) node.MutableLhs(); + (void) node.MutableRhs(); + WalkVisitor::Visit(node); +} + +void StructuralValidator::Visit(Unary& node) { + (void) node.MutableOperand(); + WalkVisitor::Visit(node); +} + +void StructuralValidator::Visit(FieldAccess& node) { + (void) node.MutableObject(); + WalkVisitor::Visit(node); +} + +void StructuralValidator::Visit(IndexAccess& node) { + (void) node.MutableObject(); + (void) node.MutableIndexExpr(); + WalkVisitor::Visit(node); +} + +void StructuralValidator::Visit(NamespaceRef& node) { + if (node.Name().empty()) { + sink_.Error("E2101", "namespace reference must have name"); + } + + WalkVisitor::Visit(node); +} + +void StructuralValidator::Visit(SafeCall& node) { + if (node.Method().empty()) { + sink_.Error("E2201", "safecall must have method name"); + } + + (void) node.MutableObject(); + WalkVisitor::Visit(node); +} diff --git a/lib/parser/ast/visitors/StructuralValidator.hpp b/lib/parser/ast/visitors/StructuralValidator.hpp new file mode 100644 index 0000000..ffd4f91 --- /dev/null +++ b/lib/parser/ast/visitors/StructuralValidator.hpp @@ -0,0 +1,31 @@ +#ifndef STRUCTURALVALIDATOR_HPP_ +#define STRUCTURALVALIDATOR_HPP_ + +#include "WalkVisitor.hpp" +#include "lib/parser/diagnostics/IDiagnosticSink.hpp" + +class StructuralValidator : public WalkVisitor { +public: + explicit StructuralValidator(IDiagnosticSink& sink) : sink_(sink) { + } + + void Visit(Module& node) override; + void Visit(FunctionDecl& node) override; + void Visit(ClassDecl& node) override; + void Visit(CallDecl& node) override; + void Visit(MethodDecl& node) override; + void Visit(DestructorDecl& node) override; + + void Visit(Call& node) override; + void Visit(Binary& node) override; + void Visit(Unary& node) override; + void Visit(FieldAccess& node) override; + void Visit(IndexAccess& node) override; + void Visit(NamespaceRef& node) override; + void Visit(SafeCall& node) override; + +private: + IDiagnosticSink& sink_; +}; + +#endif // STRUCTURALVALIDATOR_HPP_ diff --git a/lib/parser/diagnostics/Diagnostic.cpp b/lib/parser/diagnostics/Diagnostic.cpp index 2f0f694..d6fc397 100644 --- a/lib/parser/diagnostics/Diagnostic.cpp +++ b/lib/parser/diagnostics/Diagnostic.cpp @@ -1 +1,129 @@ #include "Diagnostic.hpp" + +#include + +Diagnostic::Diagnostic() = default; + +Diagnostic::Diagnostic(std::shared_ptr sev, std::string code, std::string message) : + severity_(std::move(sev)), code_(std::move(code)), message_(std::move(message)) { +} + +Diagnostic::Diagnostic(const Diagnostic& other) : + severity_(other.severity_), code_(other.code_), message_(other.message_), category_(other.category_), + where_(other.where_), notes_(other.notes_), fixes_(other.fixes_), is_fatal_(other.is_fatal_), + is_suppressed_(other.is_suppressed_) { +} + +Diagnostic::Diagnostic(Diagnostic&& other) noexcept : + severity_(std::move(other.severity_)), code_(std::move(other.code_)), message_(std::move(other.message_)), + category_(std::move(other.category_)), where_(std::move(other.where_)), notes_(std::move(other.notes_)), + fixes_(std::move(other.fixes_)), is_fatal_(other.is_fatal_), is_suppressed_(other.is_suppressed_) { +} + +Diagnostic& Diagnostic::operator=(const Diagnostic& other) { + if (this == &other) { + return *this; + } + + severity_ = other.severity_; + code_ = other.code_; + message_ = other.message_; + category_ = other.category_; + where_ = other.where_; + notes_ = other.notes_; + fixes_ = other.fixes_; + is_fatal_ = other.is_fatal_; + is_suppressed_ = other.is_suppressed_; + return *this; +} + +Diagnostic& Diagnostic::operator=(Diagnostic&& other) noexcept { + if (this == &other) { + return *this; + } + + severity_ = std::move(other.severity_); + code_ = std::move(other.code_); + message_ = std::move(other.message_); + category_ = std::move(other.category_); + where_ = std::move(other.where_); + notes_ = std::move(other.notes_); + fixes_ = std::move(other.fixes_); + is_fatal_ = other.is_fatal_; + is_suppressed_ = other.is_suppressed_; + return *this; +} + +Diagnostic::~Diagnostic() = default; + +void Diagnostic::SetSeverity(std::shared_ptr sev) { + severity_ = std::move(sev); +} +const std::shared_ptr& Diagnostic::GetSeverity() const noexcept { + return severity_; +} + +void Diagnostic::SetCode(std::string c) { + code_ = std::move(c); +} +const std::string& Diagnostic::GetCode() const noexcept { + return code_; +} + +void Diagnostic::SetMessage(std::string m) { + message_ = std::move(m); +} +const std::string& Diagnostic::GetMessage() const noexcept { + return message_; +} + +void Diagnostic::SetCategory(std::string cat) { + category_ = std::move(cat); +} +const std::string& Diagnostic::GetCategory() const noexcept { + return category_; +} + +void Diagnostic::SetWhere(SourceSpan sp) { + where_ = sp; +} +void Diagnostic::ResetWhere() { + where_.reset(); +} +const std::optional& Diagnostic::GetWhere() const noexcept { + return where_; +} + +void Diagnostic::AddNote(RelatedInfo note) { + notes_.emplace_back(std::move(note)); +} +void Diagnostic::ClearNotes() { + notes_.clear(); +} +const std::vector& Diagnostic::GetNotes() const noexcept { + return notes_; +} + +void Diagnostic::AddFix(FixIt fix) { + fixes_.emplace_back(std::move(fix)); +} +void Diagnostic::ClearFixes() { + fixes_.clear(); +} +const std::vector& Diagnostic::GetFixes() const noexcept { + return fixes_; +} + +void Diagnostic::SetFatal(bool on) { + is_fatal_ = on; +} +bool Diagnostic::IsFatal() const noexcept { + return is_fatal_; +} + +void Diagnostic::SetSuppressed(bool on) { + is_suppressed_ = on; +} +bool Diagnostic::IsSuppressed() const noexcept { + return is_suppressed_; +} diff --git a/lib/parser/diagnostics/Diagnostic.hpp b/lib/parser/diagnostics/Diagnostic.hpp index 8f33b4f..1acc7cf 100644 --- a/lib/parser/diagnostics/Diagnostic.hpp +++ b/lib/parser/diagnostics/Diagnostic.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "FixIt.hpp" @@ -22,34 +23,34 @@ class Diagnostic { ~Diagnostic(); void SetSeverity(std::shared_ptr sev); - [[nodiscard]] const std::shared_ptr& GetSeverity() const noexcept; + const std::shared_ptr& GetSeverity() const noexcept; void SetCode(std::string c); - [[nodiscard]] const std::string& GetCode() const noexcept; + const std::string& GetCode() const noexcept; void SetMessage(std::string m); - [[nodiscard]] const std::string& GetMessage() const noexcept; + const std::string& GetMessage() const noexcept; void SetCategory(std::string cat); - [[nodiscard]] const std::string& GetCategory() const noexcept; + const std::string& GetCategory() const noexcept; void SetWhere(SourceSpan sp); void ResetWhere(); - [[nodiscard]] const std::optional& GetWhere() const noexcept; + const std::optional& GetWhere() const noexcept; void AddNote(RelatedInfo note); void ClearNotes(); - [[nodiscard]] const std::vector& GetNotes() const noexcept; + const std::vector& GetNotes() const noexcept; void AddFix(FixIt fix); void ClearFixes(); - [[nodiscard]] const std::vector& GetFixes() const noexcept; + const std::vector& GetFixes() const noexcept; void SetFatal(bool on); - [[nodiscard]] bool IsFatal() const noexcept; + bool IsFatal() const noexcept; void SetSuppressed(bool on); - [[nodiscard]] bool IsSuppressed() const noexcept; + bool IsSuppressed() const noexcept; private: std::shared_ptr severity_; diff --git a/lib/parser/diagnostics/DiagnosticCollector.cpp b/lib/parser/diagnostics/DiagnosticCollector.cpp index a50f3f6..f1670f5 100644 --- a/lib/parser/diagnostics/DiagnosticCollector.cpp +++ b/lib/parser/diagnostics/DiagnosticCollector.cpp @@ -1 +1,154 @@ -#include "DiagnosticCollector.hpp" +#include "lib/parser/diagnostics/DiagnosticCollector.hpp" + +#include + +#include "lib/parser/diagnostics/severity/Severity.hpp" + +void DiagnosticCollector::Report(Diagnostic d) { + bool suppressed = !ShouldKeep(d); + d.SetSuppressed(suppressed); + if (suppressed) { + return; + } + + if (dedup_ && IsDuplicate(d)) { + return; + } + + if (capacity_ && diags_.size() >= *capacity_) { + return; + } + + int err_level = Severity::Error()->Level(); + int warn_level = Severity::Warning()->Level(); + int level = d.GetSeverity() ? d.GetSeverity()->Level() : 0; + + if (level >= err_level) { + if (error_limit_ && errors_ >= *error_limit_) { + return; + } + + ++errors_; + } else if (level >= warn_level) { + if (warning_limit_ && warnings_ >= *warning_limit_) { + return; + } + + ++warnings_; + } + + diags_.emplace_back(std::move(d)); +} + +bool DiagnosticCollector::HasErrors() const { + return errors_ > 0; +} +std::size_t DiagnosticCollector::Count() const { + return diags_.size(); +} +std::size_t DiagnosticCollector::ErrorCount() const { + return errors_; +} +std::size_t DiagnosticCollector::WarningCount() const { + return warnings_; +} + +void DiagnosticCollector::Note(std::string_view code, std::string_view msg, std::optional where) { + Diagnostic d{Severity::Note(), std::string{code}, std::string{msg}}; + if (where) { + d.SetWhere(*where); + } + + Report(std::move(d)); +} + +void DiagnosticCollector::Warn(std::string_view code, std::string_view msg, std::optional where) { + Diagnostic d{Severity::Warning(), std::string{code}, std::string{msg}}; + if (where) { + d.SetWhere(*where); + } + + Report(std::move(d)); +} + +void DiagnosticCollector::Error(std::string_view code, std::string_view msg, std::optional where) { + Diagnostic d{Severity::Error(), std::string{code}, std::string{msg}}; + if (where) { + d.SetWhere(*where); + } + + Report(std::move(d)); +} + +const std::vector& DiagnosticCollector::All() const { + return diags_; +} + +void DiagnosticCollector::Clear() { + diags_.clear(); + errors_ = 0; + warnings_ = 0; +} + +void DiagnosticCollector::SuppressCode(std::string code) { + suppressed_codes_.insert(std::move(code)); +} + +void DiagnosticCollector::SuppressCategory(std::string category) { + suppressed_categories_.insert(std::move(category)); +} + +void DiagnosticCollector::SetGlobalFilter(Predicate p) { + global_filter_ = std::move(p); +} + +void DiagnosticCollector::ResetGlobalFilter() { + global_filter_.reset(); +} + +void DiagnosticCollector::EnableDeduplication(bool on) { + dedup_ = on; +} + +void DiagnosticCollector::SetCapacity(std::optional max_total) { + capacity_ = max_total; +} + +void DiagnosticCollector::SetErrorLimit(std::optional max_errors) { + error_limit_ = max_errors; +} + +void DiagnosticCollector::SetWarningLimit(std::optional max_warnings) { + warning_limit_ = max_warnings; +} + +bool DiagnosticCollector::IsSuppressed(const Diagnostic& d) const { + if (!d.GetCode().empty() && suppressed_codes_.count(d.GetCode()) > 0) { + return true; + } + + if (!d.GetCategory().empty() && suppressed_categories_.count(d.GetCategory()) > 0) { + return true; + } + + if (global_filter_ && !(*global_filter_)(d)) { + return true; + } + + return false; +} + +bool DiagnosticCollector::ShouldKeep(const Diagnostic& d) const { + return !IsSuppressed(d); +} + +bool DiagnosticCollector::IsDuplicate(const Diagnostic& d) const { + for (const auto& prev : diags_) { + if (prev.GetCode() == d.GetCode() && prev.GetMessage() == d.GetMessage() && + ((prev.GetSeverity() && d.GetSeverity()) ? prev.GetSeverity()->Level() == d.GetSeverity()->Level() + : prev.GetSeverity() == d.GetSeverity())) { + return true; + } + } + return false; +} diff --git a/lib/parser/diagnostics/FixIt.cpp b/lib/parser/diagnostics/FixIt.cpp index 9b04ed9..2adb9a7 100644 --- a/lib/parser/diagnostics/FixIt.cpp +++ b/lib/parser/diagnostics/FixIt.cpp @@ -1 +1,7 @@ -#include "FixIt.hpp" +#include "lib/parser/diagnostics/FixIt.hpp" + +#include + +FixIt::FixIt(SourceSpan source_span, std::string replacement) : + where_(source_span), replacement_(std::move(replacement)) { +} diff --git a/lib/parser/diagnostics/FixIt.hpp b/lib/parser/diagnostics/FixIt.hpp index ae6b2b6..8f67301 100644 --- a/lib/parser/diagnostics/FixIt.hpp +++ b/lib/parser/diagnostics/FixIt.hpp @@ -8,7 +8,7 @@ class FixIt { public: FixIt(SourceSpan source_span, std::string replacement); - // TODO: make methods + private: SourceSpan where_; std::string replacement_; diff --git a/lib/parser/diagnostics/IDiagnosticSink.hpp b/lib/parser/diagnostics/IDiagnosticSink.hpp index ed54085..57ad10b 100644 --- a/lib/parser/diagnostics/IDiagnosticSink.hpp +++ b/lib/parser/diagnostics/IDiagnosticSink.hpp @@ -1,9 +1,12 @@ #ifndef IDIAGNOSTICSINK_HPP_ #define IDIAGNOSTICSINK_HPP_ +#include #include +#include #include "Diagnostic.hpp" +#include "lib/parser/tokens/SourceSpan.hpp" class IDiagnosticSink { public: diff --git a/lib/parser/diagnostics/RelatedInfo.cpp b/lib/parser/diagnostics/RelatedInfo.cpp index 6d462e0..7449a2e 100644 --- a/lib/parser/diagnostics/RelatedInfo.cpp +++ b/lib/parser/diagnostics/RelatedInfo.cpp @@ -1 +1,7 @@ -#include "RelatedInfo.hpp" +#include "lib/parser/diagnostics/RelatedInfo.hpp" + +#include + +RelatedInfo::RelatedInfo(std::string&& message, SourceSpan&& span) : + message_(std::move(message)), where_(std::move(span)) { +} diff --git a/lib/parser/diagnostics/severity/ISeverity.cpp b/lib/parser/diagnostics/severity/ISeverity.cpp deleted file mode 100644 index d69e1d5..0000000 --- a/lib/parser/diagnostics/severity/ISeverity.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "ISeverity.hpp" diff --git a/lib/parser/diagnostics/severity/Severity.cpp b/lib/parser/diagnostics/severity/Severity.cpp index 631496f..eba7c4c 100644 --- a/lib/parser/diagnostics/severity/Severity.cpp +++ b/lib/parser/diagnostics/severity/Severity.cpp @@ -1 +1,45 @@ -#include "Severity.hpp" +#include "lib/parser/diagnostics/severity/Severity.hpp" + +#include +#include +#include +#include + +namespace { +class SimpleSeverity : public ISeverity { +public: + SimpleSeverity(std::string name, int level) : name_(std::move(name)), level_(level) { + } + + std::string_view Name() const override { + return name_; + } + + int Level() const override { + return level_; + } + +private: + std::string name_; + int level_; +}; +} // namespace + +const std::shared_ptr& Severity::Note() { + static const auto inst = std::make_shared("note", 10); + return inst; +} + +const std::shared_ptr& Severity::Warning() { + static const auto inst = std::make_shared("warning", 20); + return inst; +} + +const std::shared_ptr& Severity::Error() { + static const auto inst = std::make_shared("error", 30); + return inst; +} + +std::shared_ptr Severity::Custom(std::string_view name, int level) { + return std::make_shared(std::string{name}, level); +} diff --git a/lib/parser/diagnostics/severity/Severity.hpp b/lib/parser/diagnostics/severity/Severity.hpp index aa95556..9fe5ab8 100644 --- a/lib/parser/diagnostics/severity/Severity.hpp +++ b/lib/parser/diagnostics/severity/Severity.hpp @@ -3,6 +3,7 @@ #include #include + #include "ISeverity.hpp" class Severity { From 1850ca9162078993a4e0d9dc8ee3dabe9a46e41a Mon Sep 17 00:00:00 2001 From: biqiboqi Date: Sun, 9 Nov 2025 22:55:04 +0300 Subject: [PATCH 15/21] feat: add LintVisitor --- lib/parser/ast/visitors/LintVisitor.cpp | 326 ++++++++++++++++++++++++ lib/parser/ast/visitors/LintVisitor.hpp | 76 ++++++ 2 files changed, 402 insertions(+) create mode 100644 lib/parser/ast/visitors/LintVisitor.cpp create mode 100644 lib/parser/ast/visitors/LintVisitor.hpp diff --git a/lib/parser/ast/visitors/LintVisitor.cpp b/lib/parser/ast/visitors/LintVisitor.cpp new file mode 100644 index 0000000..b53326d --- /dev/null +++ b/lib/parser/ast/visitors/LintVisitor.cpp @@ -0,0 +1,326 @@ +#include "LintVisitor.hpp" + +#include + +#include "lib/parser/diagnostics/severity/Severity.hpp" + +#include "lib/parser/ast/nodes/class_members/CallDecl.hpp" +#include "lib/parser/ast/nodes/class_members/DestructorDecl.hpp" +#include "lib/parser/ast/nodes/class_members/FieldDecl.hpp" +#include "lib/parser/ast/nodes/class_members/MethodDecl.hpp" +#include "lib/parser/ast/nodes/class_members/StaticFieldDecl.hpp" + +#include "lib/parser/ast/nodes/decls/ClassDecl.hpp" +#include "lib/parser/ast/nodes/decls/FunctionDecl.hpp" +#include "lib/parser/ast/nodes/decls/GlobalVarDecl.hpp" +#include "lib/parser/ast/nodes/decls/Module.hpp" + +#include "lib/parser/ast/nodes/exprs/Assign.hpp" +#include "lib/parser/ast/nodes/exprs/Call.hpp" +#include "lib/parser/ast/nodes/exprs/SafeCall.hpp" + +#include "lib/parser/ast/nodes/exprs/literals/BoolLit.hpp" +#include "lib/parser/ast/nodes/exprs/literals/StringLit.hpp" + +#include "lib/parser/ast/nodes/stmts/Block.hpp" +#include "lib/parser/ast/nodes/stmts/BreakStmt.hpp" +#include "lib/parser/ast/nodes/stmts/ContinueStmt.hpp" +#include "lib/parser/ast/nodes/stmts/ExprStmt.hpp" +#include "lib/parser/ast/nodes/stmts/ForStmt.hpp" +#include "lib/parser/ast/nodes/stmts/IfStmt.hpp" +#include "lib/parser/ast/nodes/stmts/ReturnStmt.hpp" +#include "lib/parser/ast/nodes/stmts/UnsafeBlock.hpp" +#include "lib/parser/ast/nodes/stmts/WhileStmt.hpp" + +void LintVisitor::EnterBody() { + ++nesting_depth_; +} + +void LintVisitor::LeaveBody() { + if (nesting_depth_ > 0) { + --nesting_depth_; + } +} + +void LintVisitor::EnterLoop() { + ++loop_depth_; +} + +void LintVisitor::LeaveLoop() { + if (loop_depth_ > 0) { + --loop_depth_; + } +} + +void LintVisitor::CheckNestingDepth(SourceSpan) const { + if (opts_.warn_deep_nesting && nesting_depth_ > opts_.max_nesting) { + sink_.Warn("W0201", "deep nesting"); + } +} + +bool LintVisitor::IsPureExpr(Expr& expression) const { + if (dynamic_cast(&expression)) { + return false; + } + + if (dynamic_cast(&expression)) { + return false; + } + + if (dynamic_cast(&expression)) { + return false; + } + + return true; +} + +void LintVisitor::Visit(Module& node) { + if (opts_.warn_module_without_decls && node.MutableDecls().empty()) { + sink_.Warn("W0001", "module has no declarations"); + } + + WalkVisitor::Visit(node); +} + +void LintVisitor::Visit(ClassDecl& node) { + if (opts_.warn_large_class && node.MutableMembers().size() > opts_.max_class_members) { + sink_.Warn("W0101", "class has too many members"); + } + + WalkVisitor::Visit(node); +} + +void LintVisitor::Visit(FunctionDecl& node) { + if (opts_.warn_empty_bodies) { + if (auto* b = node.MutableBody()) { + if (b->Size() == 0) { + sink_.Warn("W0102", "function body is empty"); + } + } + } + + WalkVisitor::Visit(node); +} + +void LintVisitor::Visit(MethodDecl& node) { + if (opts_.warn_empty_bodies) { + if (!node.IsPure()) { + if (auto* b = node.MutableBody()) { + if (b->Size() == 0) { + sink_.Warn("W0103", "method body is empty"); + } + } + } + } + + WalkVisitor::Visit(node); +} + +void LintVisitor::Visit(CallDecl& node) { + if (opts_.warn_empty_bodies) { + if (auto* b = node.MutableBody()) { + if (b->Size() == 0) { + sink_.Warn("W0104", "call body is empty"); + } + } + } + + WalkVisitor::Visit(node); +} + +void LintVisitor::Visit(DestructorDecl& node) { + if (opts_.warn_empty_bodies) { + if (auto* b = node.MutableBody()) { + if (b->Size() == 0) { + sink_.Warn("W0105", "destructor body is empty"); + } + } + } + + WalkVisitor::Visit(node); +} + +void LintVisitor::Visit(Block& node) { + EnterBody(); + + if (opts_.warn_empty_blocks && node.GetStatements().empty()) { + sink_.Warn("W0202", "empty block"); + } + + CheckNestingDepth(); + + bool terminated = false; + if (opts_.warn_unreachable) { + for (const auto& stmt : node.GetStatements()) { + if (terminated) { + sink_.Warn("W0301", "unreachable statement"); + continue; + } + + if (dynamic_cast(stmt.get())) { + terminated = true; + } else if (dynamic_cast(stmt.get())) { + terminated = true; + } else if (dynamic_cast(stmt.get())) { + terminated = true; + } + } + } + + if (opts_.max_block_len > 0 && node.GetStatements().size() > opts_.max_block_len) { + sink_.Warn("W0203", "block is too long"); + } + + for (auto& stmt : node.GetStatements()) { + stmt->Accept(*this); + } + + LeaveBody(); +} + +void LintVisitor::Visit(ExprStmt& node) { + if (opts_.warn_pure_expr_stmt) { + if (auto* e = node.MutableExpression()) { + if (IsPureExpr(*e)) { + sink_.Warn("W0401", "expression statement has no effect"); + } + } + } + WalkVisitor::Visit(node); +} + +void LintVisitor::Visit(ReturnStmt& node) { + WalkVisitor::Visit(node); +} + +void LintVisitor::Visit(BreakStmt& node) { + if (opts_.warn_break_continue_outside_loop && loop_depth_ == 0) { + sink_.Error("E0301", "break outside of loop"); + } + WalkVisitor::Visit(node); +} + +void LintVisitor::Visit(ContinueStmt& node) { + if (opts_.warn_break_continue_outside_loop && loop_depth_ == 0) { + sink_.Error("E0302", "continue outside of loop"); + } + + WalkVisitor::Visit(node); +} + +void LintVisitor::Visit(IfStmt& node) { + EnterBody(); + + if (opts_.warn_if_without_branches && node.MutableBranches().empty() && !node.HasElse()) { + sink_.Warn("W0501", "if statement has no branches"); + } + + for (auto& br : node.MutableBranches()) { + if (auto* then_blk = br.MutableThen()) { + if (opts_.warn_empty_blocks && then_blk->GetStatements().empty()) { + sink_.Warn("W0502", "then-branch is empty"); + } + } + } + + if (opts_.warn_empty_else && node.HasElse()) { + if (auto* eb = node.MutableElseBlock()) { + if (eb->GetStatements().empty()) { + sink_.Warn("W0503", "else-branch is empty"); + } + } + } + + WalkVisitor::Visit(node); + + LeaveBody(); +} + +void LintVisitor::Visit(WhileStmt& node) { + EnterBody(); + + if (opts_.warn_missing_loop_cond_or_iterable && node.MutableCondition() == nullptr) { + sink_.Error("E0401", "while loop without condition"); + } + + if (opts_.warn_while_true) { + if (auto* cond = node.MutableCondition()) { + if (auto* bl = dynamic_cast(cond)) { + if (bl->Value()) { + sink_.Warn("W0601", "while(true) loop"); + } + } + } + } + + EnterLoop(); + WalkVisitor::Visit(node); + LeaveLoop(); + + LeaveBody(); +} + +void LintVisitor::Visit(ForStmt& node) { + EnterBody(); + + if (opts_.warn_missing_loop_cond_or_iterable && node.MutableIteratorExpr() == nullptr) { + sink_.Error("E0402", "for loop without iterable expression"); + } + + EnterLoop(); + WalkVisitor::Visit(node); + LeaveLoop(); + + LeaveBody(); +} + +void LintVisitor::Visit(UnsafeBlock& node) { + EnterBody(); + if (opts_.warn_empty_blocks) { + if (auto* b = node.MutableBody()) { + if (b->GetStatements().empty()) { + sink_.Warn("W0701", "empty unsafe block"); + } + } + } + + WalkVisitor::Visit(node); + LeaveBody(); +} + +void LintVisitor::Visit(GlobalVarDecl& node) { + if (opts_.warn_mutable_globals && node.IsVar()) { + sink_.Warn("W0801", "mutable global variable"); + } + + WalkVisitor::Visit(node); +} + +void LintVisitor::Visit(FieldDecl& node) { + if (opts_.warn_public_fields && node.IsPublic()) { + sink_.Warn("W0802", "public field"); + } + + WalkVisitor::Visit(node); +} + +void LintVisitor::Visit(StaticFieldDecl& node) { + if (opts_.warn_static_mutable_fields && node.IsVar()) { + sink_.Warn("W0803", "static mutable field"); + } + + WalkVisitor::Visit(node); +} + +void LintVisitor::Visit(StringLit& node) { + if (opts_.warn_empty_string_literal && node.Value().empty()) { + sink_.Warn("W0901", "empty string literal"); + } + + WalkVisitor::Visit(node); +} + +void LintVisitor::Visit(BoolLit& node) { + (void) node; + WalkVisitor::Visit(node); +} diff --git a/lib/parser/ast/visitors/LintVisitor.hpp b/lib/parser/ast/visitors/LintVisitor.hpp new file mode 100644 index 0000000..a6fdeb6 --- /dev/null +++ b/lib/parser/ast/visitors/LintVisitor.hpp @@ -0,0 +1,76 @@ +#ifndef LINTVISITOR_HPP_ +#define LINTVISITOR_HPP_ + +#include +#include + +#include "WalkVisitor.hpp" + +#include "lib/parser/diagnostics/IDiagnosticSink.hpp" + +struct LintOptions { + std::size_t max_block_len = 200; + std::size_t max_nesting = 4; + std::size_t max_class_members = 64; + bool warn_empty_blocks = true; + bool warn_public_fields = true; + bool warn_mutable_globals = true; + bool warn_static_mutable_fields = true; + bool warn_unreachable = true; + bool warn_pure_expr_stmt = true; + bool warn_break_continue_outside_loop = true; + bool warn_empty_bodies = true; + bool warn_empty_else = true; + bool warn_missing_loop_cond_or_iterable = true; + bool warn_module_without_decls = true; + bool warn_empty_string_literal = true; + bool warn_deep_nesting = true; + bool warn_large_class = true; + bool warn_if_without_branches = true; + bool warn_while_true = true; +}; + +class LintVisitor : public WalkVisitor { +public: + explicit LintVisitor(IDiagnosticSink& sink, LintOptions options = {}) : sink_(sink), opts_(options) { + } + + void Visit(Module& node) override; + void Visit(ClassDecl& node) override; + void Visit(FunctionDecl& node) override; + void Visit(MethodDecl& node) override; + void Visit(CallDecl& node) override; + void Visit(DestructorDecl& node) override; + + void Visit(Block& node) override; + void Visit(ExprStmt& node) override; + void Visit(ReturnStmt& node) override; + void Visit(BreakStmt& node) override; + void Visit(ContinueStmt& node) override; + void Visit(IfStmt& node) override; + void Visit(WhileStmt& node) override; + void Visit(ForStmt& node) override; + void Visit(UnsafeBlock& node) override; + + void Visit(GlobalVarDecl& node) override; + void Visit(FieldDecl& node) override; + void Visit(StaticFieldDecl& node) override; + + void Visit(StringLit& node) override; + void Visit(BoolLit& node) override; + +private: + bool IsPureExpr(Expr& expression) const; + void EnterBody(); + void LeaveBody(); + void EnterLoop(); + void LeaveLoop(); + void CheckNestingDepth(SourceSpan where_hint = {}) const; + + IDiagnosticSink& sink_; + LintOptions opts_; + std::size_t loop_depth_ = 0; + std::size_t nesting_depth_ = 0; +}; + +#endif // LINTVISITOR_HPP_ From 0a2a9d71bfe79a18b1cc750c384baa78b659bd23 Mon Sep 17 00:00:00 2001 From: biqiboqi Date: Sun, 9 Nov 2025 23:03:30 +0300 Subject: [PATCH 16/21] feat: add vector token stream --- project_tree_raw.txt | Bin 0 -> 72686 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 project_tree_raw.txt diff --git a/project_tree_raw.txt b/project_tree_raw.txt new file mode 100644 index 0000000000000000000000000000000000000000..cf60cebcf39a420509c4a3518ec279ede99faa91 GIT binary patch literal 72686 zcmdU2TTdprcC91v6e)ioPoqfZDFQXn*YlKahMwUJeaXO?IZ_fK_@HT~xg9Ru%~5`P zlC{dLw#(&m*)A8!2!Z*ay!P_;_V)I*{^!3B|G2dDho#-6KQAre=O32-b?Lk29e%!B zdW*k(*DN93ZvVR7Tr`Vjzd3EbYrbzzn(O9M^N-CxG>3oOM7|%FwwB&3{nI~hA{TH6$*9zpELehD2jAuVU?p1S!&o_~$ zZoufUx$~Yl#$WlLV_*^GsPq4g<_%Ck;ji0md2Udq6+HjdleE#iYx%wc_Z{%w!_VF3 z=jJcqzKfqf<8Lcqc<7;|GTyda zwwhPSvD-fTL*U61Xy6*|CUrnZmp+ZAMfLB76TskfIX8Tq)9+*bdp9eeN6x zXTS-e96#MFnIPtuikS?r;;9H9*RZC&03%b}b~i7qW3K<0VJ+VoVa zFVyq&$HbyPxWree46fv`*AUtkkm&Po+dAHCJut0~?c=od&IkCkkM9$dpSBjq+rd*e zJsW-po~na!j4=CU_@CRh<__U`Xp#JU+$q0oF~<*3m%pKedoAWA%IIp;BA&NeH&4>j zg(X{#E5H?Y2+`L22Ba_GVc9nOzFG{d-uGHuw@+S}TUl3MDjG!(dIjvTdTd7|w z%h2UPv)(+%&wYHY;`e9GV|;qtJc6Vr?biaIR^W-QQRZ$>F~nB#lv*#~*W@y6;|c0x zfv+|6Vu$!dCJX#d>Cf?X1WwN;GYHo{#}fQHg!Jc-dWdpAg50MVkL=^O1L%ZI)+fqm z+{U_NkI(4s4)(HwZ}#1$w_wjf*)O2&eMqG21F(98vaf>Mqqc6QYa+zN!~k73>)`Vo zTqyB5bg+-#*YLI4GCsto$KcAIVTxsh*cdqxQdZ>1?I-lBRd-GJDoq6fJDgezSc& zyJts>TgT@^aGj4CM}zcTjG!6qU$_3`%4-9w;P3*vVmmm%_Y1Im)V2)TA6vu`o?xF` z+v@Dd&(|D1A#HuF)z~AX2hipVSR1|70_g|XUwuY4?qk%*9_P5NUoq{*on24Dk>VOO zwU3k@hU4xRU`zd)-xjm@v>eXeQn6oNpttX8e;ur!;_C@YvyPwihR<8wucA)qrw{QL z##goNp}Zuve%v})2miy?W@tGdTC0BCTF((m^c4TyN4ci1auuzV&mQk*8Z%JRoo_`gx8F#RUV^7QPMQg3> z|DIIkb&MH{kexk)B=-0w=3(^1w!;wyt&Bd0t!8Q~@9M~M*#j#RM|Q2BJsNus#zFh| zrk`E|6I#+^e6u~T<9nL*`>ewD%jn2NB#Z#qU(iFF7~umrP=oY2^Kco`5NnJz#~$_w zEEvTdwRTOP%_w0F>3&sKb_Ch2twYG87Ct~PY|)f%dNEoOV>|Y0FRF5}Bg`%rMw+x9 z#^tdSjJAvwvGvugEz8B7NA2>W2VVyl^5NLSMBKC}M%nDA9^<#E(RSCO(;^SH zBkGA(PQS_4MbEqny|EWMM9PS1s>X&!jvR?_d`1gihxL2_7sipa8hS&ogU zHP-LX0cvEY)Q;eC=XiwR%GKXAsx7(PzXQ_HnuTz#Fz;mMddB7{m17sEiafse#@U)1 z9-h4vN~mnAQMNhL&pr)WhcJ}7x}Y|?t0uNBg=7jtsf+d3=C8dO?6H|b;V89uj856t zb0w8=<53U$B~}o(U;!H_|G1BsCV^!ulkzk8)zk70tduj@N6<>HZe#T+Tk@H1!E~@{`x^3rU(axB( zkd9P`c61baO2|roU-f3oR#I0g^Y*>><_hF^_?nqmZ9<~m0fYAjdNS9gDddJ!H`^GZnW!mSIv(d%o?>}C2)O^ z5%_#E7{A$lA9KpNK<(4E7{^{f_uPYYG-Juw)0%P12};Nqe$Kjj3;ZvrEfWLJr^mNw zr)TXP$DF0%juz^Rdn38hIA3|*f9qT3ld*$(*@wk_oUt6-KW6qb9?vM{SCqlD8@uCe zKE365M|oJ<3wK*^S7=WDzIUak(9FZ`H`JQ`brYx`u$m#pHLG4kRjQb99`x7XeF5G#GqK--*PaX8?H}jSPROd% zE7^URp{LaMDE)nnCvDkE#*?z#H|R|^?&G9s<1->- zpT}LnA&p4G(>3BF#iWT<>b#Y(l`6m4>zu#RCLHE3os$yAQ0kUnwz}K)Ih?sT$DFDA zg!HeH@kukfHD?Oa_SbI1qjRl5a=8C)X#c&>+hrrZ990u9-cL?%MLeTsBq`-6n=>oy zQMC%_GL*iQ+Lc##t^eP#TK=D?Vd~qS-AKg@$y5F0TCVbidB$3>Ur$0(-Rz-uXvgNW z%UMs_on!8Qg{!(A+tC-wEA1<_b(K1MFBU>jnr{(9NHN|j`&e!I*vjYXO#8TE@>Dvd z_4FmgdU_LLN_k{yxq6fg>DTI2+6=V}J+0hI{lAOVHO95wwM73(A3-@oJK{N%xDGHc7psT zVI+LvrZZ9w?tl)Oofqi{?|`-wZMR|{iW%$} zfV%j5q8^5C+!*2=zb|7b*RQy@MnJ%r+?LNzC87-w)U*X(XJ z=;pjMZFdSj_wiVtID7+%&rhGlGeu+St#lJd{GJp^P%24EZSTv|KRgqk&R!TwW9(%w zlo5N*=V{jWZ`+emW@eA$_yf##a4g@y{YG4bqf~$AED39iabFF*FW_;GNWt7x6UL-h zjmOVE%b13wNy_AHEwN{XtCZ&oxvq1LoQ!Y!cNa-d%HgNf9ao9DKaDkThgQ01+bPRP zO!`nV$%VV04aZ) zhF7DU=6)^epSouY;cQ_(FJsZ%tDK+L>~rnSjY*bu zxMNLOc`ToFqIBv=TR3IoPUTKvtP7gV_7x!tkC6pYt)UsUxPRS{~t3_IU=j0Ywp;?q=w9)Q4nC@+-eoi4M_5bf8 z^Mow-auM_FZNlrES5XPc?#RT=|{0qEh$s^mXuX(ob81%mTvVd!HTs@AAh-xOmvQ^ zOoK6bTH!3!X&jq36x887MbD}`&dbp5Ig2v|Pi0o>`qlnAS}m_3X@2TTujM;@8T#zX zs%1ICR$8a$7n+}@)Vt!B^wlfB$946jyUKZAWQz0{R;<^qfgkriH6-*cv<7Od*O`vb zwaKY<=SJYAXerhsU#EG1lstYLDPI-!k-F``jb-f~%yjL>(1%+_ZAtN%wzjlbi>xin zZ=3A7IFrmb7H~G1BZLKJ{bgHqYOXKUth4ZreM&cX_+CGdTt;_hX2e9T^Osg{KA2mQ zi=P&g#oC)4uXM*mu9Q?i7n2hnvDB8LSf)Pir8Cyzj{Z3$)W#g{P3ub9Lrdd(Txz3o z^{zH1SDJXUyNi2?nd6fF)%J>eD7}xnhcVOimP2Ir9iMPJpUb%>@^U3uvs_Cs?>ge@n5(54_EIxmu-tgl5+f3G z!%Jyx#@er9X?F!fWr^jJo;8vyUBU2i@h$YXaN>5|3E?8uUNAHOe@g zyHe{R$}YH9lcVdp7RWtDT>arY#U!a7`+7)wV9B*M?$_i#IqoB?jm}%`rec_PTwU(4AtxhdDb=A^uGx|J~x;>5NzowSkm(`VFZmcu@p}eQ`W$3e& zRl5{*Vp;L@(-a#~MBd%^oRZUzQtjSwj->gP5AC5Yi!xkBe5=dYe1S^u^YU?Ouz7_3 zU1v3HJW_|@T*!PIO$~;`nXW_59nPaczR`)jY^T}+; zPiI4ZHXHKu*^pmUMNa7fY~L|<$H|aywmmCjNUqqF`=~C~*z8EDCv>HRMk;mq_eUZ% z`1)gyseaPcMSl*b24fpN)aj#Ki; z*C@^3bEKSp8d|O4Ze-5*a0Pd{U7g^&%ec?u6!DGOcioS3#@3X4$@*N2LCQL!Wcq2C z19wT8^J>e`##cm|e7~(d3Fpq+)h&Ixrku&*EE9J% z-OOf-;}Yj6rMZn$v$FG9rHQ)Oj%gqB>yqUfl2%`jd|x)->Bu;nK05Sf#Q95I#-hG9 z^oDHq*_mf<}(j!fAdrvBJZ!~L_nC(YGm-DoN-rM?xq zYj9SR)Ewgv>X2*MJRwUfTAk7Blr#Ci*2>!d8?CrZ9!e{+n%Z6~Y!6mi+FWv4n-0lx zby>e`T9LNpsp}*CT{*6&sw>Id>vP-se)coGMMQV4yHe}=$26(++W>D6yG0bnd-r&X z+}2<1@`dHpFOf=M=LOwT)wLDwR=Gki{RNui4%z-3l-Sgsok*8lU&AJXH6z}W`(Bmz ztaf*a)wQ1#nbS(DnxeJzXMpq0T=-sEpWT`Jkd!*JF!7SOGD`a#E5RSla6O5899=2p zZBBAZ&J{`LeroH0sCK=IcOUZp$Nns8?XsBr$@5NS_@&Tiw>06oh1%?SGukQY*u^bx z>$ZmbxOLj|UZe59W!&A*UxU3yzy9eYzkN*U2du7ArPb0PR~b2mnzDC97OT{?+;jAv z-P-ZzQ)~E;{vP4E%3~y%wOQTIOuxc8Vq4BBQMk1Q%HrPS+PGXpqE>|CS#j-R@INc!N~GG~f-cE#0xjh-Sdqn5{(6tiQ1cUsfKax}}+{cLT#kC^XNw@Vh*Q%AQgNS;IQGuVamE1#iGh$tUKLZR>9b^+em} z`*CR*99ezJ)TmHXn5YD4H644uo2062O7-_`l;i*s zY#;Ox(&)Q%46Ale;?19zy#2`0ftb zi&9ywJ}XVNC&Pu&5J!S3F@+>dl}wqY?eOHuL+Jesy@l~m45|b#P_opySizEp0R#AIa%?*CK_k5rx^A&AC;M ze%&XPTO4zxDqmO<=h@g#+1Uf5y-Tk>_$+eJJ_4)Tx_mUZywVKESYPpd71V|haD?|K3PY;)-|MVu=)SZJ_)dz5dwAn^K-Jgz>Ui1ato9d=iTxjC3B~LvNhj zHusB_Yb1`7!-uN|v^e&HeD^40+M@|t`W`4Li0K@CEv4t&YTMB{_?$rNoF|!Lg(+z1 z+}?uC7qlbBJyY}@$I0QtH*<3KVv3I5!m@`@(z(35_j!!G=|O!9825x~_hU%uoZf%y zE6dpoW2E!gxgTYIoS>Cqj2s^34Mn{5sazvFs9|%SypNL3D)p+qfaQWJe)sW{#M)DmX*ApH(jn98wT3a#QMl2#NC}T$!7Kv57fIu2)aNU<*9M z_Y{m2O-lPLGxg8ic{~%Xv3Ob?`t`wGPF&Tq+Dz|*$Fas+Tda23g1FAAT`)B`5@Q&* zoGc;~80JBXo{b2afKBO|UN zaGtA%R%56&xw^g~Hg@s*4*zqXZx^|o^PAQWa&;y?y7C$MpSw8ae0DM4#Pz?IV8qke z*H!8}hFZ=w$Ln!@m$6lz*OQhRCvrS~jFDN19ffi7OD>#O;KEyIu25T3_;hi~`6zCi z!o^}t^^{))B@l=l7Q?9^XYN=aSO%s*W^Mz<9#0A(i#R8lP)!*{mr|@nJ)V<+>4~ z=Ai3w^bx0Xv@_N9SoSfe^EB~yJ&s+x>HN40h4WVR82X6QIdb+vJ0I%t453cvn&V$I zd(2szWBBZxylSgT%{;dveXh^)9GrQ}*x3BOFy#02S?N~eM%%TqtzWN;Y|$7i=GUxB zuQa80RHKZ*IhUPQr;?!b724wk;-F7iQKWJ!by3nwuiQ#sq2+NO>jE0g>8+GsX%}Z2 zAM^GDwqLVekRFAJjZ(7m(aO1TZ>ZTpc@D37)YM=&j9Jc8t2H(F^|8u%y@vj-s%Xte zD(AF?h`)xOx;W*0I5N6{b@9EVHF$;b%K6co^L=IqQ}i0oKVGU>e%2Mo9QIy@sfi-8s9`8yVKg(BFWDxx6%;G zczKmusoCXh-oG=A&k#~Rr%+v}MPIcwaB=TJ)4byN_4_rS95;0wo6)37^#F4TW0F)dtbvgcbV)vjCL38X-?a3 zbIzvJQ! z-5KX;USSBUgqNLZp4PjDaMJmN#t5<%jjU2mUn!5qFq}!cxgRm4jG_GeHuo!_@a$Jk z&mtBflyu$nQ8p1V>i(s4yN=^b;lo~vH?v*0P8|i+z*t5fRt+PIL zET@l_UuJ7JLaSjlK2{B0CQ9cHFy~ylI)#;{KF10_UwsttH z+QrGSA>k=a!yVU%@RY`IbLL%yr!)<>F5mQaAeKsFY=xT7HDovPN?Zv_xphfx!o5|I zrx2^ z6r~oyp6M~GX$EmzyFyYrhB&TSAt|+p+pq`TC3}YNl-45zr9(*5IJsvprn2f{P2t6| zo+CzohZ)#uthz{3IOWV%s?Llhz(}ihyqk;mTuubS@#wvVPy_ZLjV5 zbsthFhpb&Gy}ik6YLGKBaZj(DO8>3VNy9MGu!k3d(lMlAT?s*{TXN}$JA?RMQ_l0~ z1T9zj+-H?vY3i-vdR{4atoX7xUOB$MugaYEG^392*$g2m4Iz?!sC6ih(&4;)jVh1Q zp}d=rtr@d%6dPL+udDx1R3Yg~-MHqmhhW|TsW>h?TP7r>xrpOsq#eoDShLsPA}`5Q zBs*qR{`2(y=7xK|shl?%u$@zn$M|U?X#38{?ifCXWm&pb6c#lm6}%d8#u5PXhy?Whu-#B#;_E5oHO Date: Sun, 9 Nov 2025 23:03:43 +0300 Subject: [PATCH 17/21] feat: add vector token stream --- .../tokens/token_streams/ITokenStream.hpp | 10 +-- .../token_streams/VectorTokenStream.cpp | 71 ++++++++++++++++++- 2 files changed, 73 insertions(+), 8 deletions(-) diff --git a/lib/parser/tokens/token_streams/ITokenStream.hpp b/lib/parser/tokens/token_streams/ITokenStream.hpp index 87f0a46..28e8a3d 100644 --- a/lib/parser/tokens/token_streams/ITokenStream.hpp +++ b/lib/parser/tokens/token_streams/ITokenStream.hpp @@ -9,19 +9,15 @@ class ITokenStream { public: virtual ~ITokenStream() = default; - virtual const Token& Peek(std::size_t k = 0) = 0; - + virtual const Token& Peek(size_t k = 0) = 0; virtual TokenPtr Consume() = 0; virtual size_t Position() const = 0; - - virtual void Rewind(std::size_t n) = 0; - + virtual void Rewind(size_t n) = 0; virtual bool IsEof() const = 0; virtual const Token* LastConsumed() const = 0; - - virtual const Token* TryPeek(std::size_t k = 0) = 0; + virtual const Token* TryPeek(size_t k = 0) = 0; }; #endif // ITOKENSTREAM_HPP_ diff --git a/lib/parser/tokens/token_streams/VectorTokenStream.cpp b/lib/parser/tokens/token_streams/VectorTokenStream.cpp index 41e54fc..909c75d 100644 --- a/lib/parser/tokens/token_streams/VectorTokenStream.cpp +++ b/lib/parser/tokens/token_streams/VectorTokenStream.cpp @@ -1 +1,70 @@ -#include "VectorTokenStream.hpp" +#include "lib/parser/tokens/token_streams/VectorTokenStream.hpp" + +#include +#include + +VectorTokenStream::VectorTokenStream(std::vector tokens) : tokens_(std::move(tokens)) { +} + +const Token& VectorTokenStream::Peek(size_t k) { + const Token* token = TryPeek(k); + + if (token != nullptr) { + return *token; + } + + if (last_ != nullptr) { + return *last_; + } + + if (!tokens_.empty()) { + return *tokens_.back(); + } + + throw std::out_of_range("VectorTokenStream::Peek out of range"); +} + +TokenPtr VectorTokenStream::Consume() { + if (index_ < tokens_.size()) { + last_ = tokens_[index_].get(); + return std::move(tokens_[index_++]); + } + + last_ = nullptr; + return nullptr; +} + +size_t VectorTokenStream::Position() const { + return index_; +} + +void VectorTokenStream::Rewind(size_t n) { + if (n > index_) { + index_ = 0; + } else { + index_ -= n; + } + + last_ = nullptr; +} + +bool VectorTokenStream::IsEof() const { + return index_ >= tokens_.size(); +} + +const Token* VectorTokenStream::LastConsumed() const { + return last_; +} + +const Token* VectorTokenStream::TryPeek(size_t k) { + const size_t pos = index_ + k; + if (pos < tokens_.size()) { + return tokens_[pos].get(); + } + + return nullptr; +} + +size_t VectorTokenStream::Size() const { + return tokens_.size(); +} From 0a0a249783b97eae315917fd57eb3fb9a41398ff Mon Sep 17 00:00:00 2001 From: biqiboqi Date: Sun, 9 Nov 2025 23:03:58 +0300 Subject: [PATCH 18/21] fix: deleted accidentally added file --- project_tree_raw.txt | Bin 72686 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 project_tree_raw.txt diff --git a/project_tree_raw.txt b/project_tree_raw.txt deleted file mode 100644 index cf60cebcf39a420509c4a3518ec279ede99faa91..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 72686 zcmdU2TTdprcC91v6e)ioPoqfZDFQXn*YlKahMwUJeaXO?IZ_fK_@HT~xg9Ru%~5`P zlC{dLw#(&m*)A8!2!Z*ay!P_;_V)I*{^!3B|G2dDho#-6KQAre=O32-b?Lk29e%!B zdW*k(*DN93ZvVR7Tr`Vjzd3EbYrbzzn(O9M^N-CxG>3oOM7|%FwwB&3{nI~hA{TH6$*9zpELehD2jAuVU?p1S!&o_~$ zZoufUx$~Yl#$WlLV_*^GsPq4g<_%Ck;ji0md2Udq6+HjdleE#iYx%wc_Z{%w!_VF3 z=jJcqzKfqf<8Lcqc<7;|GTyda zwwhPSvD-fTL*U61Xy6*|CUrnZmp+ZAMfLB76TskfIX8Tq)9+*bdp9eeN6x zXTS-e96#MFnIPtuikS?r;;9H9*RZC&03%b}b~i7qW3K<0VJ+VoVa zFVyq&$HbyPxWree46fv`*AUtkkm&Po+dAHCJut0~?c=od&IkCkkM9$dpSBjq+rd*e zJsW-po~na!j4=CU_@CRh<__U`Xp#JU+$q0oF~<*3m%pKedoAWA%IIp;BA&NeH&4>j zg(X{#E5H?Y2+`L22Ba_GVc9nOzFG{d-uGHuw@+S}TUl3MDjG!(dIjvTdTd7|w z%h2UPv)(+%&wYHY;`e9GV|;qtJc6Vr?biaIR^W-QQRZ$>F~nB#lv*#~*W@y6;|c0x zfv+|6Vu$!dCJX#d>Cf?X1WwN;GYHo{#}fQHg!Jc-dWdpAg50MVkL=^O1L%ZI)+fqm z+{U_NkI(4s4)(HwZ}#1$w_wjf*)O2&eMqG21F(98vaf>Mqqc6QYa+zN!~k73>)`Vo zTqyB5bg+-#*YLI4GCsto$KcAIVTxsh*cdqxQdZ>1?I-lBRd-GJDoq6fJDgezSc& zyJts>TgT@^aGj4CM}zcTjG!6qU$_3`%4-9w;P3*vVmmm%_Y1Im)V2)TA6vu`o?xF` z+v@Dd&(|D1A#HuF)z~AX2hipVSR1|70_g|XUwuY4?qk%*9_P5NUoq{*on24Dk>VOO zwU3k@hU4xRU`zd)-xjm@v>eXeQn6oNpttX8e;ur!;_C@YvyPwihR<8wucA)qrw{QL z##goNp}Zuve%v})2miy?W@tGdTC0BCTF((m^c4TyN4ci1auuzV&mQk*8Z%JRoo_`gx8F#RUV^7QPMQg3> z|DIIkb&MH{kexk)B=-0w=3(^1w!;wyt&Bd0t!8Q~@9M~M*#j#RM|Q2BJsNus#zFh| zrk`E|6I#+^e6u~T<9nL*`>ewD%jn2NB#Z#qU(iFF7~umrP=oY2^Kco`5NnJz#~$_w zEEvTdwRTOP%_w0F>3&sKb_Ch2twYG87Ct~PY|)f%dNEoOV>|Y0FRF5}Bg`%rMw+x9 z#^tdSjJAvwvGvugEz8B7NA2>W2VVyl^5NLSMBKC}M%nDA9^<#E(RSCO(;^SH zBkGA(PQS_4MbEqny|EWMM9PS1s>X&!jvR?_d`1gihxL2_7sipa8hS&ogU zHP-LX0cvEY)Q;eC=XiwR%GKXAsx7(PzXQ_HnuTz#Fz;mMddB7{m17sEiafse#@U)1 z9-h4vN~mnAQMNhL&pr)WhcJ}7x}Y|?t0uNBg=7jtsf+d3=C8dO?6H|b;V89uj856t zb0w8=<53U$B~}o(U;!H_|G1BsCV^!ulkzk8)zk70tduj@N6<>HZe#T+Tk@H1!E~@{`x^3rU(axB( zkd9P`c61baO2|roU-f3oR#I0g^Y*>><_hF^_?nqmZ9<~m0fYAjdNS9gDddJ!H`^GZnW!mSIv(d%o?>}C2)O^ z5%_#E7{A$lA9KpNK<(4E7{^{f_uPYYG-Juw)0%P12};Nqe$Kjj3;ZvrEfWLJr^mNw zr)TXP$DF0%juz^Rdn38hIA3|*f9qT3ld*$(*@wk_oUt6-KW6qb9?vM{SCqlD8@uCe zKE365M|oJ<3wK*^S7=WDzIUak(9FZ`H`JQ`brYx`u$m#pHLG4kRjQb99`x7XeF5G#GqK--*PaX8?H}jSPROd% zE7^URp{LaMDE)nnCvDkE#*?z#H|R|^?&G9s<1->- zpT}LnA&p4G(>3BF#iWT<>b#Y(l`6m4>zu#RCLHE3os$yAQ0kUnwz}K)Ih?sT$DFDA zg!HeH@kukfHD?Oa_SbI1qjRl5a=8C)X#c&>+hrrZ990u9-cL?%MLeTsBq`-6n=>oy zQMC%_GL*iQ+Lc##t^eP#TK=D?Vd~qS-AKg@$y5F0TCVbidB$3>Ur$0(-Rz-uXvgNW z%UMs_on!8Qg{!(A+tC-wEA1<_b(K1MFBU>jnr{(9NHN|j`&e!I*vjYXO#8TE@>Dvd z_4FmgdU_LLN_k{yxq6fg>DTI2+6=V}J+0hI{lAOVHO95wwM73(A3-@oJK{N%xDGHc7psT zVI+LvrZZ9w?tl)Oofqi{?|`-wZMR|{iW%$} zfV%j5q8^5C+!*2=zb|7b*RQy@MnJ%r+?LNzC87-w)U*X(XJ z=;pjMZFdSj_wiVtID7+%&rhGlGeu+St#lJd{GJp^P%24EZSTv|KRgqk&R!TwW9(%w zlo5N*=V{jWZ`+emW@eA$_yf##a4g@y{YG4bqf~$AED39iabFF*FW_;GNWt7x6UL-h zjmOVE%b13wNy_AHEwN{XtCZ&oxvq1LoQ!Y!cNa-d%HgNf9ao9DKaDkThgQ01+bPRP zO!`nV$%VV04aZ) zhF7DU=6)^epSouY;cQ_(FJsZ%tDK+L>~rnSjY*bu zxMNLOc`ToFqIBv=TR3IoPUTKvtP7gV_7x!tkC6pYt)UsUxPRS{~t3_IU=j0Ywp;?q=w9)Q4nC@+-eoi4M_5bf8 z^Mow-auM_FZNlrES5XPc?#RT=|{0qEh$s^mXuX(ob81%mTvVd!HTs@AAh-xOmvQ^ zOoK6bTH!3!X&jq36x887MbD}`&dbp5Ig2v|Pi0o>`qlnAS}m_3X@2TTujM;@8T#zX zs%1ICR$8a$7n+}@)Vt!B^wlfB$946jyUKZAWQz0{R;<^qfgkriH6-*cv<7Od*O`vb zwaKY<=SJYAXerhsU#EG1lstYLDPI-!k-F``jb-f~%yjL>(1%+_ZAtN%wzjlbi>xin zZ=3A7IFrmb7H~G1BZLKJ{bgHqYOXKUth4ZreM&cX_+CGdTt;_hX2e9T^Osg{KA2mQ zi=P&g#oC)4uXM*mu9Q?i7n2hnvDB8LSf)Pir8Cyzj{Z3$)W#g{P3ub9Lrdd(Txz3o z^{zH1SDJXUyNi2?nd6fF)%J>eD7}xnhcVOimP2Ir9iMPJpUb%>@^U3uvs_Cs?>ge@n5(54_EIxmu-tgl5+f3G z!%Jyx#@er9X?F!fWr^jJo;8vyUBU2i@h$YXaN>5|3E?8uUNAHOe@g zyHe{R$}YH9lcVdp7RWtDT>arY#U!a7`+7)wV9B*M?$_i#IqoB?jm}%`rec_PTwU(4AtxhdDb=A^uGx|J~x;>5NzowSkm(`VFZmcu@p}eQ`W$3e& zRl5{*Vp;L@(-a#~MBd%^oRZUzQtjSwj->gP5AC5Yi!xkBe5=dYe1S^u^YU?Ouz7_3 zU1v3HJW_|@T*!PIO$~;`nXW_59nPaczR`)jY^T}+; zPiI4ZHXHKu*^pmUMNa7fY~L|<$H|aywmmCjNUqqF`=~C~*z8EDCv>HRMk;mq_eUZ% z`1)gyseaPcMSl*b24fpN)aj#Ki; z*C@^3bEKSp8d|O4Ze-5*a0Pd{U7g^&%ec?u6!DGOcioS3#@3X4$@*N2LCQL!Wcq2C z19wT8^J>e`##cm|e7~(d3Fpq+)h&Ixrku&*EE9J% z-OOf-;}Yj6rMZn$v$FG9rHQ)Oj%gqB>yqUfl2%`jd|x)->Bu;nK05Sf#Q95I#-hG9 z^oDHq*_mf<}(j!fAdrvBJZ!~L_nC(YGm-DoN-rM?xq zYj9SR)Ewgv>X2*MJRwUfTAk7Blr#Ci*2>!d8?CrZ9!e{+n%Z6~Y!6mi+FWv4n-0lx zby>e`T9LNpsp}*CT{*6&sw>Id>vP-se)coGMMQV4yHe}=$26(++W>D6yG0bnd-r&X z+}2<1@`dHpFOf=M=LOwT)wLDwR=Gki{RNui4%z-3l-Sgsok*8lU&AJXH6z}W`(Bmz ztaf*a)wQ1#nbS(DnxeJzXMpq0T=-sEpWT`Jkd!*JF!7SOGD`a#E5RSla6O5899=2p zZBBAZ&J{`LeroH0sCK=IcOUZp$Nns8?XsBr$@5NS_@&Tiw>06oh1%?SGukQY*u^bx z>$ZmbxOLj|UZe59W!&A*UxU3yzy9eYzkN*U2du7ArPb0PR~b2mnzDC97OT{?+;jAv z-P-ZzQ)~E;{vP4E%3~y%wOQTIOuxc8Vq4BBQMk1Q%HrPS+PGXpqE>|CS#j-R@INc!N~GG~f-cE#0xjh-Sdqn5{(6tiQ1cUsfKax}}+{cLT#kC^XNw@Vh*Q%AQgNS;IQGuVamE1#iGh$tUKLZR>9b^+em} z`*CR*99ezJ)TmHXn5YD4H644uo2062O7-_`l;i*s zY#;Ox(&)Q%46Ale;?19zy#2`0ftb zi&9ywJ}XVNC&Pu&5J!S3F@+>dl}wqY?eOHuL+Jesy@l~m45|b#P_opySizEp0R#AIa%?*CK_k5rx^A&AC;M ze%&XPTO4zxDqmO<=h@g#+1Uf5y-Tk>_$+eJJ_4)Tx_mUZywVKESYPpd71V|haD?|K3PY;)-|MVu=)SZJ_)dz5dwAn^K-Jgz>Ui1ato9d=iTxjC3B~LvNhj zHusB_Yb1`7!-uN|v^e&HeD^40+M@|t`W`4Li0K@CEv4t&YTMB{_?$rNoF|!Lg(+z1 z+}?uC7qlbBJyY}@$I0QtH*<3KVv3I5!m@`@(z(35_j!!G=|O!9825x~_hU%uoZf%y zE6dpoW2E!gxgTYIoS>Cqj2s^34Mn{5sazvFs9|%SypNL3D)p+qfaQWJe)sW{#M)DmX*ApH(jn98wT3a#QMl2#NC}T$!7Kv57fIu2)aNU<*9M z_Y{m2O-lPLGxg8ic{~%Xv3Ob?`t`wGPF&Tq+Dz|*$Fas+Tda23g1FAAT`)B`5@Q&* zoGc;~80JBXo{b2afKBO|UN zaGtA%R%56&xw^g~Hg@s*4*zqXZx^|o^PAQWa&;y?y7C$MpSw8ae0DM4#Pz?IV8qke z*H!8}hFZ=w$Ln!@m$6lz*OQhRCvrS~jFDN19ffi7OD>#O;KEyIu25T3_;hi~`6zCi z!o^}t^^{))B@l=l7Q?9^XYN=aSO%s*W^Mz<9#0A(i#R8lP)!*{mr|@nJ)V<+>4~ z=Ai3w^bx0Xv@_N9SoSfe^EB~yJ&s+x>HN40h4WVR82X6QIdb+vJ0I%t453cvn&V$I zd(2szWBBZxylSgT%{;dveXh^)9GrQ}*x3BOFy#02S?N~eM%%TqtzWN;Y|$7i=GUxB zuQa80RHKZ*IhUPQr;?!b724wk;-F7iQKWJ!by3nwuiQ#sq2+NO>jE0g>8+GsX%}Z2 zAM^GDwqLVekRFAJjZ(7m(aO1TZ>ZTpc@D37)YM=&j9Jc8t2H(F^|8u%y@vj-s%Xte zD(AF?h`)xOx;W*0I5N6{b@9EVHF$;b%K6co^L=IqQ}i0oKVGU>e%2Mo9QIy@sfi-8s9`8yVKg(BFWDxx6%;G zczKmusoCXh-oG=A&k#~Rr%+v}MPIcwaB=TJ)4byN_4_rS95;0wo6)37^#F4TW0F)dtbvgcbV)vjCL38X-?a3 zbIzvJQ! z-5KX;USSBUgqNLZp4PjDaMJmN#t5<%jjU2mUn!5qFq}!cxgRm4jG_GeHuo!_@a$Jk z&mtBflyu$nQ8p1V>i(s4yN=^b;lo~vH?v*0P8|i+z*t5fRt+PIL zET@l_UuJ7JLaSjlK2{B0CQ9cHFy~ylI)#;{KF10_UwsttH z+QrGSA>k=a!yVU%@RY`IbLL%yr!)<>F5mQaAeKsFY=xT7HDovPN?Zv_xphfx!o5|I zrx2^ z6r~oyp6M~GX$EmzyFyYrhB&TSAt|+p+pq`TC3}YNl-45zr9(*5IJsvprn2f{P2t6| zo+CzohZ)#uthz{3IOWV%s?Llhz(}ihyqk;mTuubS@#wvVPy_ZLjV5 zbsthFhpb&Gy}ik6YLGKBaZj(DO8>3VNy9MGu!k3d(lMlAT?s*{TXN}$JA?RMQ_l0~ z1T9zj+-H?vY3i-vdR{4atoX7xUOB$MugaYEG^392*$g2m4Iz?!sC6ih(&4;)jVh1Q zp}d=rtr@d%6dPL+udDx1R3Yg~-MHqmhhW|TsW>h?TP7r>xrpOsq#eoDShLsPA}`5Q zBs*qR{`2(y=7xK|shl?%u$@zn$M|U?X#38{?ifCXWm&pb6c#lm6}%d8#u5PXhy?Whu-#B#;_E5oHO Date: Sun, 9 Nov 2025 23:36:00 +0300 Subject: [PATCH 19/21] fix: update include directive for MethodDecl.cpp --- lib/parser/ast/nodes/class_members/MethodDecl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/parser/ast/nodes/class_members/MethodDecl.cpp b/lib/parser/ast/nodes/class_members/MethodDecl.cpp index cd64a85..b957847 100644 --- a/lib/parser/ast/nodes/class_members/MethodDecl.cpp +++ b/lib/parser/ast/nodes/class_members/MethodDecl.cpp @@ -1,5 +1,5 @@ -#include "lib/parser/ast/nodes/class_members/MethodDecl.hpp" #include "lib/parser/ast/AstVisitor.hpp" +#include "lib/parser/ast/nodes/class_members/MethodDecl.hpp" #include From 4e4f4972a38b07cc3ad7b93cdf2e02aab98dbd16 Mon Sep 17 00:00:00 2001 From: bialger Date: Mon, 10 Nov 2025 00:35:25 +0300 Subject: [PATCH 20/21] refactor: add namespace closure to parser files and update header guards --- lib/parser/IParser.hpp | 10 +++-- lib/parser/ParserFsm.cpp | 2 + lib/parser/ParserFsm.hpp | 10 +++-- lib/parser/ast/AstVisitor.hpp | 10 +++-- lib/parser/ast/IAstFactory.hpp | 10 +++-- lib/parser/ast/nodes/base/AstNode.hpp | 10 +++-- lib/parser/ast/nodes/base/Decl.hpp | 10 +++-- lib/parser/ast/nodes/base/Expr.hpp | 10 +++-- lib/parser/ast/nodes/base/Stmt.hpp | 10 +++-- .../ast/nodes/class_members/CallDecl.cpp | 4 ++ .../ast/nodes/class_members/CallDecl.hpp | 10 +++-- .../nodes/class_members/DestructorDecl.cpp | 4 ++ .../nodes/class_members/DestructorDecl.hpp | 10 +++-- .../ast/nodes/class_members/FieldDecl.cpp | 4 ++ .../ast/nodes/class_members/FieldDecl.hpp | 10 +++-- .../ast/nodes/class_members/MethodDecl.cpp | 4 ++ .../ast/nodes/class_members/MethodDecl.hpp | 10 +++-- .../nodes/class_members/StaticFieldDecl.cpp | 4 ++ .../nodes/class_members/StaticFieldDecl.hpp | 10 +++-- lib/parser/ast/nodes/decls/ClassDecl.cpp | 4 ++ lib/parser/ast/nodes/decls/ClassDecl.hpp | 10 +++-- lib/parser/ast/nodes/decls/FunctionDecl.cpp | 4 ++ lib/parser/ast/nodes/decls/FunctionDecl.hpp | 10 +++-- lib/parser/ast/nodes/decls/GlobalVarDecl.cpp | 4 ++ lib/parser/ast/nodes/decls/GlobalVarDecl.hpp | 10 +++-- lib/parser/ast/nodes/decls/InterfaceDecl.cpp | 4 ++ lib/parser/ast/nodes/decls/InterfaceDecl.hpp | 10 +++-- .../ast/nodes/decls/InterfaceMethod.cpp | 4 ++ .../ast/nodes/decls/InterfaceMethod.hpp | 10 +++-- lib/parser/ast/nodes/decls/Module.cpp | 8 +++- lib/parser/ast/nodes/decls/Module.hpp | 10 +++-- lib/parser/ast/nodes/decls/TypeAliasDecl.cpp | 6 ++- lib/parser/ast/nodes/decls/TypeAliasDecl.hpp | 10 +++-- lib/parser/ast/nodes/exprs/Assign.cpp | 4 ++ lib/parser/ast/nodes/exprs/Assign.hpp | 10 +++-- lib/parser/ast/nodes/exprs/Binary.cpp | 4 ++ lib/parser/ast/nodes/exprs/Binary.hpp | 10 +++-- lib/parser/ast/nodes/exprs/Call.cpp | 4 ++ lib/parser/ast/nodes/exprs/Call.hpp | 10 +++-- lib/parser/ast/nodes/exprs/CastAs.cpp | 4 ++ lib/parser/ast/nodes/exprs/CastAs.hpp | 10 +++-- lib/parser/ast/nodes/exprs/Elvis.cpp | 4 ++ lib/parser/ast/nodes/exprs/Elvis.hpp | 10 +++-- lib/parser/ast/nodes/exprs/FieldAccess.cpp | 4 ++ lib/parser/ast/nodes/exprs/FieldAccess.hpp | 10 +++-- lib/parser/ast/nodes/exprs/IdentRef.cpp | 4 ++ lib/parser/ast/nodes/exprs/IdentRef.hpp | 10 +++-- lib/parser/ast/nodes/exprs/IndexAccess.cpp | 4 ++ lib/parser/ast/nodes/exprs/IndexAccess.hpp | 10 +++-- lib/parser/ast/nodes/exprs/NamespaceRef.cpp | 4 ++ lib/parser/ast/nodes/exprs/NamespaceRef.hpp | 10 +++-- lib/parser/ast/nodes/exprs/SafeCall.cpp | 4 ++ lib/parser/ast/nodes/exprs/SafeCall.hpp | 10 +++-- lib/parser/ast/nodes/exprs/TypeTestIs.cpp | 4 ++ lib/parser/ast/nodes/exprs/TypeTestIs.hpp | 10 +++-- lib/parser/ast/nodes/exprs/Unary.cpp | 4 ++ lib/parser/ast/nodes/exprs/Unary.hpp | 10 +++-- .../ast/nodes/exprs/literals/BoolLit.cpp | 4 ++ .../ast/nodes/exprs/literals/BoolLit.hpp | 10 +++-- .../ast/nodes/exprs/literals/CharLit.cpp | 4 ++ .../ast/nodes/exprs/literals/CharLit.hpp | 10 +++-- .../ast/nodes/exprs/literals/FloatLit.cpp | 4 ++ .../ast/nodes/exprs/literals/FloatLit.hpp | 10 +++-- .../ast/nodes/exprs/literals/IntLit.cpp | 4 ++ .../ast/nodes/exprs/literals/IntLit.hpp | 10 +++-- .../ast/nodes/exprs/literals/NullLit.cpp | 4 ++ .../ast/nodes/exprs/literals/NullLit.hpp | 10 +++-- .../ast/nodes/exprs/literals/StringLit.cpp | 4 ++ .../ast/nodes/exprs/literals/StringLit.hpp | 10 +++-- .../ast/nodes/exprs/tags/IAssignOpTag.hpp | 10 +++-- .../ast/nodes/exprs/tags/IBinaryOpTag.hpp | 10 +++-- .../ast/nodes/exprs/tags/IUnaryOpTag.hpp | 10 +++-- lib/parser/ast/nodes/exprs/tags/OpTags.cpp | 42 ++++++++++--------- lib/parser/ast/nodes/exprs/tags/OpTags.hpp | 10 +++-- lib/parser/ast/nodes/stmts/Block.cpp | 4 ++ lib/parser/ast/nodes/stmts/Block.hpp | 10 +++-- lib/parser/ast/nodes/stmts/Branch.cpp | 4 ++ lib/parser/ast/nodes/stmts/Branch.hpp | 10 +++-- lib/parser/ast/nodes/stmts/BreakStmt.cpp | 4 ++ lib/parser/ast/nodes/stmts/BreakStmt.hpp | 10 +++-- lib/parser/ast/nodes/stmts/ContinueStmt.cpp | 4 ++ lib/parser/ast/nodes/stmts/ContinueStmt.hpp | 10 +++-- lib/parser/ast/nodes/stmts/ExprStmt.cpp | 4 ++ lib/parser/ast/nodes/stmts/ExprStmt.hpp | 10 +++-- lib/parser/ast/nodes/stmts/ForStmt.cpp | 4 ++ lib/parser/ast/nodes/stmts/ForStmt.hpp | 10 +++-- lib/parser/ast/nodes/stmts/IfStmt.cpp | 4 ++ lib/parser/ast/nodes/stmts/IfStmt.hpp | 10 +++-- lib/parser/ast/nodes/stmts/ReturnStmt.cpp | 4 ++ lib/parser/ast/nodes/stmts/ReturnStmt.hpp | 10 +++-- lib/parser/ast/nodes/stmts/UnsafeBlock.cpp | 4 ++ lib/parser/ast/nodes/stmts/UnsafeBlock.hpp | 10 +++-- lib/parser/ast/nodes/stmts/VarDeclStmt.cpp | 4 ++ lib/parser/ast/nodes/stmts/VarDeclStmt.hpp | 10 +++-- lib/parser/ast/nodes/stmts/WhileStmt.cpp | 4 ++ lib/parser/ast/nodes/stmts/WhileStmt.hpp | 10 +++-- lib/parser/ast/visitors/ConstWalkVisitor.cpp | 4 ++ lib/parser/ast/visitors/ConstWalkVisitor.hpp | 10 +++-- lib/parser/ast/visitors/LintVisitor.cpp | 13 +++--- lib/parser/ast/visitors/LintVisitor.hpp | 12 ++++-- lib/parser/ast/visitors/PrintVisitor.cpp | 4 ++ lib/parser/ast/visitors/PrintVisitor.hpp | 10 +++-- .../ast/visitors/StructuralValidator.cpp | 4 ++ .../ast/visitors/StructuralValidator.hpp | 10 +++-- lib/parser/ast/visitors/WalkVisitor.cpp | 4 ++ lib/parser/ast/visitors/WalkVisitor.hpp | 10 +++-- lib/parser/context/ContextParser.cpp | 4 ++ lib/parser/context/ContextParser.hpp | 10 +++-- lib/parser/context/NodeEntry.cpp | 4 ++ lib/parser/context/NodeEntry.hpp | 10 +++-- lib/parser/diagnostics/Diagnostic.cpp | 12 +++--- lib/parser/diagnostics/Diagnostic.hpp | 10 +++-- .../diagnostics/DiagnosticCollector.cpp | 4 ++ .../diagnostics/DiagnosticCollector.hpp | 10 +++-- lib/parser/diagnostics/FixIt.cpp | 6 ++- lib/parser/diagnostics/FixIt.hpp | 10 +++-- lib/parser/diagnostics/IDiagnosticSink.hpp | 10 +++-- lib/parser/diagnostics/RelatedInfo.cpp | 4 ++ lib/parser/diagnostics/RelatedInfo.hpp | 10 +++-- lib/parser/diagnostics/severity/ISeverity.hpp | 10 +++-- lib/parser/diagnostics/severity/Severity.cpp | 20 +++++---- lib/parser/diagnostics/severity/Severity.hpp | 10 +++-- lib/parser/pratt/DefaultOperatorResolver.cpp | 2 + lib/parser/pratt/DefaultOperatorResolver.hpp | 10 +++-- lib/parser/pratt/IExpressionParser.hpp | 10 +++-- lib/parser/pratt/IOperatorResolver.hpp | 10 +++-- lib/parser/pratt/PrattExpressionParser.cpp | 2 + lib/parser/pratt/PrattExpressionParser.hpp | 10 +++-- lib/parser/pratt/specifications/InfixSpec.cpp | 2 + lib/parser/pratt/specifications/InfixSpec.hpp | 13 ++++-- .../pratt/specifications/PostfixSpec.cpp | 2 + .../pratt/specifications/PostfixSpec.hpp | 12 ++++-- lib/parser/recovery/IRecoveryStrategy.hpp | 10 +++-- lib/parser/recovery/SimpleRecovery.cpp | 2 + lib/parser/recovery/SimpleRecovery.hpp | 10 +++-- lib/parser/states/StateBlock.cpp | 2 + lib/parser/states/StateBlock.hpp | 10 +++-- lib/parser/states/StateCallDeclHdr.cpp | 2 + lib/parser/states/StateCallDeclHdr.hpp | 10 +++-- lib/parser/states/StateDestructorDecl.cpp | 2 + lib/parser/states/StateDestructorDecl.hpp | 10 +++-- lib/parser/states/StateExpr.cpp | 2 + lib/parser/states/StateExpr.hpp | 10 +++-- lib/parser/states/StateFieldDecl.cpp | 2 + lib/parser/states/StateFieldDecl.hpp | 10 +++-- lib/parser/states/StateForHead.cpp | 2 + lib/parser/states/StateForHead.hpp | 10 +++-- lib/parser/states/StateGlobalVarDecl.cpp | 2 + lib/parser/states/StateGlobalVarDecl.hpp | 10 +++-- lib/parser/states/StateIfHead.cpp | 2 + lib/parser/states/StateIfHead.hpp | 10 +++-- lib/parser/states/StateIfTail.cpp | 2 + lib/parser/states/StateIfTail.hpp | 10 +++-- lib/parser/states/StateMethodHdr.cpp | 2 + lib/parser/states/StateMethodHdr.hpp | 10 +++-- lib/parser/states/StateModule.cpp | 2 + lib/parser/states/StateModule.hpp | 10 +++-- lib/parser/states/StateParseType.cpp | 2 + lib/parser/states/StateParseType.hpp | 10 +++-- lib/parser/states/StateReturnTail.cpp | 2 + lib/parser/states/StateReturnTail.hpp | 10 +++-- lib/parser/states/StateStmt.cpp | 2 + lib/parser/states/StateStmt.hpp | 10 +++-- lib/parser/states/StateSyncToBlockEnd.cpp | 2 + lib/parser/states/StateSyncToBlockEnd.hpp | 10 +++-- lib/parser/states/StateSyncToStmtEnd.cpp | 2 + lib/parser/states/StateSyncToStmtEnd.hpp | 10 +++-- lib/parser/states/StateTopDecl.cpp | 2 + lib/parser/states/StateTopDecl.hpp | 10 +++-- lib/parser/states/StateTypeAliasDecl.cpp | 2 + lib/parser/states/StateTypeAliasDecl.hpp | 10 +++-- lib/parser/states/StateUnsafeBlock.cpp | 2 + lib/parser/states/StateUnsafeBlock.hpp | 10 +++-- lib/parser/states/StateVarDeclTail.cpp | 2 + lib/parser/states/StateVarDeclTail.hpp | 10 +++-- lib/parser/states/StateWhileHead.cpp | 2 + lib/parser/states/StateWhileHead.hpp | 10 +++-- lib/parser/states/base/IState.hpp | 10 +++-- lib/parser/states/base/StateBase.cpp | 2 + lib/parser/states/base/StateBase.hpp | 10 +++-- lib/parser/states/base/StateError.cpp | 2 + lib/parser/states/base/StateError.hpp | 10 +++-- lib/parser/states/base/StateRegistry.cpp | 2 + lib/parser/states/base/StateRegistry.hpp | 10 +++-- lib/parser/states/class/StateClassBody.cpp | 2 + lib/parser/states/class/StateClassBody.hpp | 10 +++-- lib/parser/states/class/StateClassHdr.cpp | 2 + lib/parser/states/class/StateClassHdr.hpp | 10 +++-- lib/parser/states/class/StateClassMember.cpp | 2 + lib/parser/states/class/StateClassMember.hpp | 12 ++++-- lib/parser/states/func/StateFuncBody.cpp | 2 + lib/parser/states/func/StateFuncBody.hpp | 10 +++-- lib/parser/states/func/StateFuncHdr.cpp | 2 + lib/parser/states/func/StateFuncHdr.hpp | 10 +++-- lib/parser/states/func/StateFuncParams.cpp | 2 + lib/parser/states/func/StateFuncParams.hpp | 10 +++-- .../states/interface/StateInterfaceBody.cpp | 2 + .../states/interface/StateInterfaceBody.hpp | 10 +++-- .../states/interface/StateInterfaceDecl.cpp | 2 + .../states/interface/StateInterfaceDecl.hpp | 10 +++-- .../states/interface/StateInterfaceHdr.cpp | 2 + .../states/interface/StateInterfaceHdr.hpp | 10 +++-- lib/parser/tokens/SourceId.cpp | 2 + lib/parser/tokens/SourceId.hpp | 10 +++-- lib/parser/tokens/SourceSpan.cpp | 2 + lib/parser/tokens/SourceSpan.hpp | 12 ++++-- .../tokens/token_streams/ITokenStream.hpp | 12 ++++-- .../token_streams/VectorTokenStream.cpp | 4 ++ .../token_streams/VectorTokenStream.hpp | 13 ++++-- .../tokens/token_traits/ITokenMatcher.hpp | 12 ++++-- .../tokens/token_traits/MatchIdentifier.cpp | 2 + .../tokens/token_traits/MatchIdentifier.hpp | 13 ++++-- .../tokens/token_traits/MatchLexeme.cpp | 2 + .../tokens/token_traits/MatchLexeme.hpp | 13 ++++-- .../tokens/token_traits/MatchLiteral.cpp | 2 + .../tokens/token_traits/MatchLiteral.hpp | 13 ++++-- .../tokens/token_traits/MatchManyOf.cpp | 2 + .../tokens/token_traits/MatchManyOf.hpp | 13 ++++-- lib/parser/tokens/token_traits/MatchType.cpp | 2 + lib/parser/tokens/token_traits/MatchType.hpp | 13 ++++-- lib/parser/type_parser/ITypeParser.hpp | 10 +++-- lib/parser/type_parser/QNameTypeParser.cpp | 2 + lib/parser/type_parser/QNameTypeParser.hpp | 10 +++-- lib/parser/types/Nullable.cpp | 4 ++ lib/parser/types/Nullable.hpp | 10 +++-- lib/parser/types/Param.cpp | 4 ++ lib/parser/types/Param.hpp | 10 +++-- lib/parser/types/ResolvedTypeHandle.cpp | 4 ++ lib/parser/types/ResolvedTypeHandle.hpp | 10 +++-- lib/parser/types/TypeReference.cpp | 4 ++ lib/parser/types/TypeReference.hpp | 10 +++-- 231 files changed, 1261 insertions(+), 431 deletions(-) diff --git a/lib/parser/IParser.hpp b/lib/parser/IParser.hpp index d166499..50bb764 100644 --- a/lib/parser/IParser.hpp +++ b/lib/parser/IParser.hpp @@ -1,5 +1,5 @@ -#ifndef IPARSER_HPP_ -#define IPARSER_HPP_ +#ifndef PARSER_IPARSER_HPP_ +#define PARSER_IPARSER_HPP_ #include @@ -7,10 +7,14 @@ #include "diagnostics/IDiagnosticSink.hpp" #include "tokens/token_streams/ITokenStream.hpp" +namespace ovum::compiler::parser { + class IParser { public: virtual ~IParser() = default; virtual std::unique_ptr Parse(ITokenStream& ts, IDiagnosticSink& diags) = 0; }; -#endif // IPARSER_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_IPARSER_HPP_ diff --git a/lib/parser/ParserFsm.cpp b/lib/parser/ParserFsm.cpp index 2cbd8d1..2989de7 100644 --- a/lib/parser/ParserFsm.cpp +++ b/lib/parser/ParserFsm.cpp @@ -1 +1,3 @@ #include "ParserFsm.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/ParserFsm.hpp b/lib/parser/ParserFsm.hpp index cb6a4c4..a4cba9a 100644 --- a/lib/parser/ParserFsm.hpp +++ b/lib/parser/ParserFsm.hpp @@ -1,5 +1,5 @@ -#ifndef PARSERFSM_HPP_ -#define PARSERFSM_HPP_ +#ifndef PARSER_PARSERFSM_HPP_ +#define PARSER_PARSERFSM_HPP_ #include @@ -8,6 +8,8 @@ #include "pratt/IExpressionParser.hpp" #include "type_parser/ITypeParser.hpp" +namespace ovum::compiler::parser { + class ParserFsm : public IParser { public: ParserFsm(std::unique_ptr expr, @@ -24,4 +26,6 @@ class ParserFsm : public IParser { std::unique_ptr factory_; }; -#endif // PARSERFSM_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_PARSERFSM_HPP_ diff --git a/lib/parser/ast/AstVisitor.hpp b/lib/parser/ast/AstVisitor.hpp index 9bd93ca..a260ca1 100644 --- a/lib/parser/ast/AstVisitor.hpp +++ b/lib/parser/ast/AstVisitor.hpp @@ -1,5 +1,5 @@ -#ifndef ASTVISITOR_HPP_ -#define ASTVISITOR_HPP_ +#ifndef PARSER_ASTVISITOR_HPP_ +#define PARSER_ASTVISITOR_HPP_ #include "nodes/class_members/CallDecl.hpp" #include "nodes/class_members/DestructorDecl.hpp" @@ -40,6 +40,8 @@ #include "nodes/stmts/VarDeclStmt.hpp" #include "nodes/stmts/WhileStmt.hpp" +namespace ovum::compiler::parser { + class AstVisitor { public: virtual ~AstVisitor() = default; @@ -91,4 +93,6 @@ class AstVisitor { virtual void Visit(NullLit&) = 0; }; -#endif // ASTVISITOR_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_ASTVISITOR_HPP_ diff --git a/lib/parser/ast/IAstFactory.hpp b/lib/parser/ast/IAstFactory.hpp index 1d8afdf..203cb6f 100644 --- a/lib/parser/ast/IAstFactory.hpp +++ b/lib/parser/ast/IAstFactory.hpp @@ -1,5 +1,5 @@ -#ifndef IASTFACTORY_HPP_ -#define IASTFACTORY_HPP_ +#ifndef PARSER_IASTFACTORY_HPP_ +#define PARSER_IASTFACTORY_HPP_ #include @@ -41,6 +41,8 @@ #include "nodes/stmts/VarDeclStmt.hpp" #include "nodes/stmts/WhileStmt.hpp" +namespace ovum::compiler::parser { + class IAstFactory { public: virtual ~IAstFactory() = default; @@ -89,4 +91,6 @@ class IAstFactory { virtual std::unique_ptr MakeNull() = 0; }; -#endif // IASTFACTORY_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_IASTFACTORY_HPP_ diff --git a/lib/parser/ast/nodes/base/AstNode.hpp b/lib/parser/ast/nodes/base/AstNode.hpp index fafbe77..8e54691 100644 --- a/lib/parser/ast/nodes/base/AstNode.hpp +++ b/lib/parser/ast/nodes/base/AstNode.hpp @@ -1,5 +1,7 @@ -#ifndef ASTNODE_HPP_ -#define ASTNODE_HPP_ +#ifndef PARSER_ASTNODE_HPP_ +#define PARSER_ASTNODE_HPP_ + +namespace ovum::compiler::parser { class AstVisitor; // forward @@ -12,4 +14,6 @@ class AstNode { virtual void Accept(AstVisitor& visitor) = 0; }; -#endif // ASTNODE_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_ASTNODE_HPP_ diff --git a/lib/parser/ast/nodes/base/Decl.hpp b/lib/parser/ast/nodes/base/Decl.hpp index 4e504cd..26ac67e 100644 --- a/lib/parser/ast/nodes/base/Decl.hpp +++ b/lib/parser/ast/nodes/base/Decl.hpp @@ -1,8 +1,12 @@ -#ifndef DECL_HPP_ -#define DECL_HPP_ +#ifndef PARSER_DECL_HPP_ +#define PARSER_DECL_HPP_ #include "AstNode.hpp" +namespace ovum::compiler::parser { + class Decl : public AstNode {}; -#endif // DECL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_DECL_HPP_ diff --git a/lib/parser/ast/nodes/base/Expr.hpp b/lib/parser/ast/nodes/base/Expr.hpp index c448b75..2d1917f 100644 --- a/lib/parser/ast/nodes/base/Expr.hpp +++ b/lib/parser/ast/nodes/base/Expr.hpp @@ -1,8 +1,12 @@ -#ifndef EXPR_HPP_ -#define EXPR_HPP_ +#ifndef PARSER_EXPR_HPP_ +#define PARSER_EXPR_HPP_ #include "AstNode.hpp" +namespace ovum::compiler::parser { + class Expr : public AstNode {}; -#endif // EXPR_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_EXPR_HPP_ diff --git a/lib/parser/ast/nodes/base/Stmt.hpp b/lib/parser/ast/nodes/base/Stmt.hpp index 1236050..19a7b2d 100644 --- a/lib/parser/ast/nodes/base/Stmt.hpp +++ b/lib/parser/ast/nodes/base/Stmt.hpp @@ -1,8 +1,12 @@ -#ifndef STMT_HPP_ -#define STMT_HPP_ +#ifndef PARSER_STMT_HPP_ +#define PARSER_STMT_HPP_ #include "AstNode.hpp" +namespace ovum::compiler::parser { + class Stmt : public AstNode {}; -#endif // STMT_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STMT_HPP_ diff --git a/lib/parser/ast/nodes/class_members/CallDecl.cpp b/lib/parser/ast/nodes/class_members/CallDecl.cpp index d2fe8bb..a84e922 100644 --- a/lib/parser/ast/nodes/class_members/CallDecl.cpp +++ b/lib/parser/ast/nodes/class_members/CallDecl.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void CallDecl::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -59,3 +61,5 @@ void CallDecl::SetBody(std::unique_ptr block) { std::unique_ptr CallDecl::ReleaseBody() { return std::move(body_); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/class_members/CallDecl.hpp b/lib/parser/ast/nodes/class_members/CallDecl.hpp index eab872e..6082620 100644 --- a/lib/parser/ast/nodes/class_members/CallDecl.hpp +++ b/lib/parser/ast/nodes/class_members/CallDecl.hpp @@ -1,5 +1,5 @@ -#ifndef CALLDECL_HPP_ -#define CALLDECL_HPP_ +#ifndef PARSER_CALLDECL_HPP_ +#define PARSER_CALLDECL_HPP_ #include #include @@ -9,6 +9,8 @@ #include "lib/parser/types/Param.hpp" #include "lib/parser/types/TypeReference.hpp" +namespace ovum::compiler::parser { + class CallDecl : public Decl { public: void Accept(AstVisitor& visitor) override; @@ -37,4 +39,6 @@ class CallDecl : public Decl { std::unique_ptr body_; }; -#endif // CALLDECL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_CALLDECL_HPP_ diff --git a/lib/parser/ast/nodes/class_members/DestructorDecl.cpp b/lib/parser/ast/nodes/class_members/DestructorDecl.cpp index d5b1f2c..f965f0a 100644 --- a/lib/parser/ast/nodes/class_members/DestructorDecl.cpp +++ b/lib/parser/ast/nodes/class_members/DestructorDecl.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void DestructorDecl::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -31,3 +33,5 @@ void DestructorDecl::SetBody(std::unique_ptr block) { std::unique_ptr DestructorDecl::ReleaseBody() { return std::move(body_); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/class_members/DestructorDecl.hpp b/lib/parser/ast/nodes/class_members/DestructorDecl.hpp index 7246a44..24f780a 100644 --- a/lib/parser/ast/nodes/class_members/DestructorDecl.hpp +++ b/lib/parser/ast/nodes/class_members/DestructorDecl.hpp @@ -1,11 +1,13 @@ -#ifndef DESTRUCTORDECL_HPP_ -#define DESTRUCTORDECL_HPP_ +#ifndef PARSER_DESTRUCTORDECL_HPP_ +#define PARSER_DESTRUCTORDECL_HPP_ #include #include "lib/parser/ast/nodes/base/Decl.hpp" #include "lib/parser/ast/nodes/stmts/Block.hpp" +namespace ovum::compiler::parser { + class DestructorDecl : public Decl { public: void Accept(AstVisitor& visitor) override; @@ -23,4 +25,6 @@ class DestructorDecl : public Decl { std::unique_ptr body_; }; -#endif // DESTRUCTORDECL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_DESTRUCTORDECL_HPP_ diff --git a/lib/parser/ast/nodes/class_members/FieldDecl.cpp b/lib/parser/ast/nodes/class_members/FieldDecl.cpp index 3c6e105..e073caa 100644 --- a/lib/parser/ast/nodes/class_members/FieldDecl.cpp +++ b/lib/parser/ast/nodes/class_members/FieldDecl.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void FieldDecl::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -59,3 +61,5 @@ void FieldDecl::SetInit(std::unique_ptr expr) { std::unique_ptr FieldDecl::ReleaseInit() { return std::move(init_); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/class_members/FieldDecl.hpp b/lib/parser/ast/nodes/class_members/FieldDecl.hpp index ce7e5e6..aa63002 100644 --- a/lib/parser/ast/nodes/class_members/FieldDecl.hpp +++ b/lib/parser/ast/nodes/class_members/FieldDecl.hpp @@ -1,5 +1,5 @@ -#ifndef FIELDDECL_HPP_ -#define FIELDDECL_HPP_ +#ifndef PARSER_FIELDDECL_HPP_ +#define PARSER_FIELDDECL_HPP_ #include #include @@ -8,6 +8,8 @@ #include "lib/parser/ast/nodes/base/Expr.hpp" #include "lib/parser/types/TypeReference.hpp" +namespace ovum::compiler::parser { + class FieldDecl : public Decl { public: void Accept(AstVisitor& visitor) override; @@ -38,4 +40,6 @@ class FieldDecl : public Decl { std::unique_ptr init_; // optional }; -#endif // FIELDDECL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_FIELDDECL_HPP_ diff --git a/lib/parser/ast/nodes/class_members/MethodDecl.cpp b/lib/parser/ast/nodes/class_members/MethodDecl.cpp index b957847..8689d25 100644 --- a/lib/parser/ast/nodes/class_members/MethodDecl.cpp +++ b/lib/parser/ast/nodes/class_members/MethodDecl.cpp @@ -3,6 +3,8 @@ #include +namespace ovum::compiler::parser { + void MethodDecl::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -90,3 +92,5 @@ void MethodDecl::SetBody(std::unique_ptr block) { std::unique_ptr MethodDecl::ReleaseBody() { return std::move(body); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/class_members/MethodDecl.hpp b/lib/parser/ast/nodes/class_members/MethodDecl.hpp index 48cf063..7702c00 100644 --- a/lib/parser/ast/nodes/class_members/MethodDecl.hpp +++ b/lib/parser/ast/nodes/class_members/MethodDecl.hpp @@ -1,5 +1,5 @@ -#ifndef METHODDECL_HPP_ -#define METHODDECL_HPP_ +#ifndef PARSER_METHODDECL_HPP_ +#define PARSER_METHODDECL_HPP_ #include #include @@ -10,6 +10,8 @@ #include "lib/parser/types/Param.hpp" #include "lib/parser/types/TypeReference.hpp" +namespace ovum::compiler::parser { + class MethodDecl : public Decl { public: void Accept(AstVisitor& visitor) override; @@ -55,4 +57,6 @@ class MethodDecl : public Decl { std::unique_ptr body; // optional }; -#endif // METHODDECL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_METHODDECL_HPP_ diff --git a/lib/parser/ast/nodes/class_members/StaticFieldDecl.cpp b/lib/parser/ast/nodes/class_members/StaticFieldDecl.cpp index dd666db..a2470b4 100644 --- a/lib/parser/ast/nodes/class_members/StaticFieldDecl.cpp +++ b/lib/parser/ast/nodes/class_members/StaticFieldDecl.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void StaticFieldDecl::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -59,3 +61,5 @@ void StaticFieldDecl::SetInit(std::unique_ptr expr) { std::unique_ptr StaticFieldDecl::ReleaseInit() { return std::move(init_); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/class_members/StaticFieldDecl.hpp b/lib/parser/ast/nodes/class_members/StaticFieldDecl.hpp index 0075ea1..09b5cd7 100644 --- a/lib/parser/ast/nodes/class_members/StaticFieldDecl.hpp +++ b/lib/parser/ast/nodes/class_members/StaticFieldDecl.hpp @@ -1,5 +1,5 @@ -#ifndef STATICFIELDDECL_HPP_ -#define STATICFIELDDECL_HPP_ +#ifndef PARSER_STATICFIELDDECL_HPP_ +#define PARSER_STATICFIELDDECL_HPP_ #include #include @@ -8,6 +8,8 @@ #include "lib/parser/ast/nodes/base/Expr.hpp" #include "lib/parser/types/TypeReference.hpp" +namespace ovum::compiler::parser { + class StaticFieldDecl : public Decl { public: void Accept(AstVisitor& visitor) override; @@ -38,4 +40,6 @@ class StaticFieldDecl : public Decl { std::unique_ptr init_; }; -#endif // STATICFIELDDECL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATICFIELDDECL_HPP_ diff --git a/lib/parser/ast/nodes/decls/ClassDecl.cpp b/lib/parser/ast/nodes/decls/ClassDecl.cpp index f4011e3..5f1f049 100644 --- a/lib/parser/ast/nodes/decls/ClassDecl.cpp +++ b/lib/parser/ast/nodes/decls/ClassDecl.cpp @@ -2,6 +2,8 @@ #include "lib/parser/ast/AstVisitor.hpp" +namespace ovum::compiler::parser { + void ClassDecl::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -47,3 +49,5 @@ std::unique_ptr ClassDecl::ReleaseMember(std::size_t index) { members_.erase(members_.begin() + static_cast(index)); return old; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/decls/ClassDecl.hpp b/lib/parser/ast/nodes/decls/ClassDecl.hpp index a74c7ef..6e93ebe 100644 --- a/lib/parser/ast/nodes/decls/ClassDecl.hpp +++ b/lib/parser/ast/nodes/decls/ClassDecl.hpp @@ -1,5 +1,5 @@ -#ifndef CLASSDECL_HPP_ -#define CLASSDECL_HPP_ +#ifndef PARSER_CLASSDECL_HPP_ +#define PARSER_CLASSDECL_HPP_ #include #include @@ -8,6 +8,8 @@ #include "lib/parser/ast/nodes/base/Decl.hpp" #include "lib/parser/types/TypeReference.hpp" +namespace ovum::compiler::parser { + class ClassDecl : public Decl { public: void Accept(AstVisitor& visitor) override; @@ -30,4 +32,6 @@ class ClassDecl : public Decl { std::vector> members_; // Field/StaticField/Method/Call/Destructor }; -#endif // CLASSDECL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_CLASSDECL_HPP_ diff --git a/lib/parser/ast/nodes/decls/FunctionDecl.cpp b/lib/parser/ast/nodes/decls/FunctionDecl.cpp index 484b281..12afb59 100644 --- a/lib/parser/ast/nodes/decls/FunctionDecl.cpp +++ b/lib/parser/ast/nodes/decls/FunctionDecl.cpp @@ -2,6 +2,8 @@ #include "lib/parser/ast/AstVisitor.hpp" +namespace ovum::compiler::parser { + void FunctionDecl::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -64,3 +66,5 @@ void FunctionDecl::SetBody(std::unique_ptr block) { std::unique_ptr FunctionDecl::ReleaseBody() { return std::move(body_); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/decls/FunctionDecl.hpp b/lib/parser/ast/nodes/decls/FunctionDecl.hpp index b8c0e4a..198e234 100644 --- a/lib/parser/ast/nodes/decls/FunctionDecl.hpp +++ b/lib/parser/ast/nodes/decls/FunctionDecl.hpp @@ -1,5 +1,5 @@ -#ifndef FUNCTIONDECL_HPP_ -#define FUNCTIONDECL_HPP_ +#ifndef PARSER_FUNCTIONDECL_HPP_ +#define PARSER_FUNCTIONDECL_HPP_ #include #include @@ -9,6 +9,8 @@ #include "lib/parser/ast/nodes/stmts/Block.hpp" #include "lib/parser/types/Param.hpp" +namespace ovum::compiler::parser { + class TypeReference; class FunctionDecl : public Decl { @@ -43,4 +45,6 @@ class FunctionDecl : public Decl { std::unique_ptr body_; // optional }; -#endif // FUNCTIONDECL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_FUNCTIONDECL_HPP_ diff --git a/lib/parser/ast/nodes/decls/GlobalVarDecl.cpp b/lib/parser/ast/nodes/decls/GlobalVarDecl.cpp index 17ecb7a..97964dd 100644 --- a/lib/parser/ast/nodes/decls/GlobalVarDecl.cpp +++ b/lib/parser/ast/nodes/decls/GlobalVarDecl.cpp @@ -2,6 +2,8 @@ #include "lib/parser/ast/AstVisitor.hpp" +namespace ovum::compiler::parser { + void GlobalVarDecl::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -49,3 +51,5 @@ void GlobalVarDecl::SetInit(std::unique_ptr expr) { std::unique_ptr GlobalVarDecl::ReleaseInit() { return std::move(init_); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/decls/GlobalVarDecl.hpp b/lib/parser/ast/nodes/decls/GlobalVarDecl.hpp index a7605c7..ebcba68 100644 --- a/lib/parser/ast/nodes/decls/GlobalVarDecl.hpp +++ b/lib/parser/ast/nodes/decls/GlobalVarDecl.hpp @@ -1,5 +1,5 @@ -#ifndef GLOBALVARDECL_HPP_ -#define GLOBALVARDECL_HPP_ +#ifndef PARSER_GLOBALVARDECL_HPP_ +#define PARSER_GLOBALVARDECL_HPP_ #include #include @@ -8,6 +8,8 @@ #include "lib/parser/ast/nodes/base/Expr.hpp" #include "lib/parser/types/TypeReference.hpp" +namespace ovum::compiler::parser { + class GlobalVarDecl : public Decl { public: void Accept(AstVisitor& visitor) override; @@ -34,4 +36,6 @@ class GlobalVarDecl : public Decl { std::unique_ptr init_; // optional }; -#endif // GLOBALVARDECL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_GLOBALVARDECL_HPP_ diff --git a/lib/parser/ast/nodes/decls/InterfaceDecl.cpp b/lib/parser/ast/nodes/decls/InterfaceDecl.cpp index 00ec1a0..8a36942 100644 --- a/lib/parser/ast/nodes/decls/InterfaceDecl.cpp +++ b/lib/parser/ast/nodes/decls/InterfaceDecl.cpp @@ -2,6 +2,8 @@ #include "lib/parser/ast/AstVisitor.hpp" +namespace ovum::compiler::parser { + void InterfaceDecl::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -35,3 +37,5 @@ std::unique_ptr InterfaceDecl::ReleaseMember(std::size_t index) methods_.erase(methods_.begin() + static_cast(index)); return old; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/decls/InterfaceDecl.hpp b/lib/parser/ast/nodes/decls/InterfaceDecl.hpp index 706c7cc..752810a 100644 --- a/lib/parser/ast/nodes/decls/InterfaceDecl.hpp +++ b/lib/parser/ast/nodes/decls/InterfaceDecl.hpp @@ -1,5 +1,5 @@ -#ifndef INTERFACEDECL_HPP_ -#define INTERFACEDECL_HPP_ +#ifndef PARSER_INTERFACEDECL_HPP_ +#define PARSER_INTERFACEDECL_HPP_ #include #include @@ -8,6 +8,8 @@ #include "InterfaceMethod.hpp" #include "lib/parser/ast/nodes/base/Decl.hpp" +namespace ovum::compiler::parser { + class InterfaceDecl : public Decl { public: void Accept(AstVisitor& visitor) override; @@ -26,4 +28,6 @@ class InterfaceDecl : public Decl { std::vector> methods_; }; -#endif // INTERFACEDECL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_INTERFACEDECL_HPP_ diff --git a/lib/parser/ast/nodes/decls/InterfaceMethod.cpp b/lib/parser/ast/nodes/decls/InterfaceMethod.cpp index 07787ec..a5d49bc 100644 --- a/lib/parser/ast/nodes/decls/InterfaceMethod.cpp +++ b/lib/parser/ast/nodes/decls/InterfaceMethod.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void InterfaceMethod::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -31,3 +33,5 @@ const TypeReference* InterfaceMethod::ReturnType() const noexcept { void InterfaceMethod::SetReturnType(std::unique_ptr type) { ret_type_ = std::move(type); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/decls/InterfaceMethod.hpp b/lib/parser/ast/nodes/decls/InterfaceMethod.hpp index 6406b92..b94dfbc 100644 --- a/lib/parser/ast/nodes/decls/InterfaceMethod.hpp +++ b/lib/parser/ast/nodes/decls/InterfaceMethod.hpp @@ -1,5 +1,5 @@ -#ifndef INTERFACEMETHOD_HPP_ -#define INTERFACEMETHOD_HPP_ +#ifndef PARSER_INTERFACEMETHOD_HPP_ +#define PARSER_INTERFACEMETHOD_HPP_ #include #include @@ -8,6 +8,8 @@ #include "lib/parser/ast/nodes/base/Decl.hpp" #include "lib/parser/types/TypeReference.hpp" +namespace ovum::compiler::parser { + class InterfaceMethod : public Decl { public: struct Param { @@ -32,4 +34,6 @@ class InterfaceMethod : public Decl { std::unique_ptr ret_type_; }; -#endif // INTERFACEMETHOD_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_INTERFACEMETHOD_HPP_ diff --git a/lib/parser/ast/nodes/decls/Module.cpp b/lib/parser/ast/nodes/decls/Module.cpp index 39e766a..1cfbb33 100644 --- a/lib/parser/ast/nodes/decls/Module.cpp +++ b/lib/parser/ast/nodes/decls/Module.cpp @@ -2,6 +2,8 @@ #include "lib/parser/ast/AstVisitor.hpp" +namespace ovum::compiler::parser { + void Module::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -19,7 +21,7 @@ const SourceId& Module::Source() const noexcept { } void Module::SetSource(SourceId id) { - source_ = id; + source_ = std::move(id); } const std::vector>& Module::Decls() const noexcept { @@ -40,6 +42,8 @@ std::unique_ptr Module::ReleaseDecl(std::size_t index) { } auto old = std::move(decls_[index]); - decls_.erase(decls_.begin() + index); + decls_.erase(decls_.begin() + static_cast(index)); return std::move(old); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/decls/Module.hpp b/lib/parser/ast/nodes/decls/Module.hpp index 4537df7..341b246 100644 --- a/lib/parser/ast/nodes/decls/Module.hpp +++ b/lib/parser/ast/nodes/decls/Module.hpp @@ -1,5 +1,5 @@ -#ifndef MODULE_HPP_ -#define MODULE_HPP_ +#ifndef PARSER_MODULE_HPP_ +#define PARSER_MODULE_HPP_ #include #include @@ -9,6 +9,8 @@ #include "lib/parser/ast/nodes/base/Decl.hpp" #include "lib/parser/tokens/SourceId.hpp" +namespace ovum::compiler::parser { + class Module : public AstNode { public: void Accept(AstVisitor& visitor) override; @@ -30,4 +32,6 @@ class Module : public AstNode { std::vector> decls_; }; -#endif // MODULE_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_MODULE_HPP_ diff --git a/lib/parser/ast/nodes/decls/TypeAliasDecl.cpp b/lib/parser/ast/nodes/decls/TypeAliasDecl.cpp index 6c14fc9..7cdc767 100644 --- a/lib/parser/ast/nodes/decls/TypeAliasDecl.cpp +++ b/lib/parser/ast/nodes/decls/TypeAliasDecl.cpp @@ -4,8 +4,10 @@ #include +namespace ovum::compiler::parser { + void TypeAliasDecl::Accept(AstVisitor& visitor) { - v.Visit(*this); + visitor.Visit(*this); } const std::string& TypeAliasDecl::Name() const noexcept { @@ -27,3 +29,5 @@ TypeReference& TypeAliasDecl::MutableAliasedType() noexcept { void TypeAliasDecl::SetAliasedType(TypeReference type) { aliased_type_ = std::move(type); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/decls/TypeAliasDecl.hpp b/lib/parser/ast/nodes/decls/TypeAliasDecl.hpp index c0c8d40..352a355 100644 --- a/lib/parser/ast/nodes/decls/TypeAliasDecl.hpp +++ b/lib/parser/ast/nodes/decls/TypeAliasDecl.hpp @@ -1,11 +1,13 @@ -#ifndef TYPEALIASDECL_HPP_ -#define TYPEALIASDECL_HPP_ +#ifndef PARSER_TYPEALIASDECL_HPP_ +#define PARSER_TYPEALIASDECL_HPP_ #include #include "lib/parser/ast/nodes/base/Decl.hpp" #include "lib/parser/types/TypeReference.hpp" +namespace ovum::compiler::parser { + class TypeAliasDecl : public Decl { public: void Accept(AstVisitor& visitor) override; @@ -22,4 +24,6 @@ class TypeAliasDecl : public Decl { TypeReference aliased_type_; }; -#endif // TYPEALIASDECL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_TYPEALIASDECL_HPP_ diff --git a/lib/parser/ast/nodes/exprs/Assign.cpp b/lib/parser/ast/nodes/exprs/Assign.cpp index 01f19ad..c38d05e 100644 --- a/lib/parser/ast/nodes/exprs/Assign.cpp +++ b/lib/parser/ast/nodes/exprs/Assign.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void Assign::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -51,3 +53,5 @@ std::unique_ptr Assign::ReplaceValue(std::unique_ptr new_value) { value_ = std::move(new_value); return old_value; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/exprs/Assign.hpp b/lib/parser/ast/nodes/exprs/Assign.hpp index adebde9..a49ae85 100644 --- a/lib/parser/ast/nodes/exprs/Assign.hpp +++ b/lib/parser/ast/nodes/exprs/Assign.hpp @@ -1,5 +1,5 @@ -#ifndef ASSIGN_HPP_ -#define ASSIGN_HPP_ +#ifndef PARSER_ASSIGN_HPP_ +#define PARSER_ASSIGN_HPP_ #include @@ -7,6 +7,8 @@ #include "tags/IAssignOpTag.hpp" #include "tags/OpTags.hpp" +namespace ovum::compiler::parser { + class Assign : public Expr { public: void Accept(AstVisitor& visitor) override; @@ -30,4 +32,6 @@ class Assign : public Expr { std::unique_ptr value_; }; -#endif // ASSIGN_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_ASSIGN_HPP_ diff --git a/lib/parser/ast/nodes/exprs/Binary.cpp b/lib/parser/ast/nodes/exprs/Binary.cpp index 9d42d2f..c6e843e 100644 --- a/lib/parser/ast/nodes/exprs/Binary.cpp +++ b/lib/parser/ast/nodes/exprs/Binary.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void Binary::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -51,3 +53,5 @@ std::unique_ptr Binary::ReplaceRhs(std::unique_ptr new_rhs) { rhs_ = std::move(new_rhs); return old_rhs; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/exprs/Binary.hpp b/lib/parser/ast/nodes/exprs/Binary.hpp index 53cc9f7..ac2dcf6 100644 --- a/lib/parser/ast/nodes/exprs/Binary.hpp +++ b/lib/parser/ast/nodes/exprs/Binary.hpp @@ -1,5 +1,5 @@ -#ifndef BINARY_HPP_ -#define BINARY_HPP_ +#ifndef PARSER_BINARY_HPP_ +#define PARSER_BINARY_HPP_ #include @@ -7,6 +7,8 @@ #include "tags/IBinaryOpTag.hpp" #include "tags/OpTags.hpp" +namespace ovum::compiler::parser { + class Binary : public Expr { public: void Accept(AstVisitor& visitor) override; @@ -30,4 +32,6 @@ class Binary : public Expr { std::unique_ptr rhs_; }; -#endif // BINARY_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_BINARY_HPP_ diff --git a/lib/parser/ast/nodes/exprs/Call.cpp b/lib/parser/ast/nodes/exprs/Call.cpp index cb88ab1..12f4417 100644 --- a/lib/parser/ast/nodes/exprs/Call.cpp +++ b/lib/parser/ast/nodes/exprs/Call.cpp @@ -3,6 +3,8 @@ #include +namespace ovum::compiler::parser { + void Call::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -40,3 +42,5 @@ void Call::AddArg(std::unique_ptr new_arg) { void Call::ClearArgs() { args_.clear(); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/exprs/Call.hpp b/lib/parser/ast/nodes/exprs/Call.hpp index b2725f8..6217740 100644 --- a/lib/parser/ast/nodes/exprs/Call.hpp +++ b/lib/parser/ast/nodes/exprs/Call.hpp @@ -1,11 +1,13 @@ -#ifndef CALL_HPP_ -#define CALL_HPP_ +#ifndef PARSER_CALL_HPP_ +#define PARSER_CALL_HPP_ #include #include #include "lib/parser/ast/nodes/base/Expr.hpp" +namespace ovum::compiler::parser { + class Call : public Expr { public: void Accept(AstVisitor& visitor) override; @@ -25,4 +27,6 @@ class Call : public Expr { std::vector> args_; }; -#endif // CALL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_CALL_HPP_ diff --git a/lib/parser/ast/nodes/exprs/CastAs.cpp b/lib/parser/ast/nodes/exprs/CastAs.cpp index abf545f..9bf495f 100644 --- a/lib/parser/ast/nodes/exprs/CastAs.cpp +++ b/lib/parser/ast/nodes/exprs/CastAs.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void CastAs::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -37,3 +39,5 @@ TypeReference& CastAs::MutableType() noexcept { void CastAs::SetType(TypeReference new_type) { type_ = std::move(new_type); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/exprs/CastAs.hpp b/lib/parser/ast/nodes/exprs/CastAs.hpp index ac1c2e9..fc9c88a 100644 --- a/lib/parser/ast/nodes/exprs/CastAs.hpp +++ b/lib/parser/ast/nodes/exprs/CastAs.hpp @@ -1,11 +1,13 @@ -#ifndef CASTAS_HPP_ -#define CASTAS_HPP_ +#ifndef PARSER_CASTAS_HPP_ +#define PARSER_CASTAS_HPP_ #include #include "lib/parser/ast/nodes/base/Expr.hpp" #include "lib/parser/types/TypeReference.hpp" +namespace ovum::compiler::parser { + class CastAs : public Expr { public: void Accept(AstVisitor& visitor) override; @@ -24,4 +26,6 @@ class CastAs : public Expr { TypeReference type_; }; -#endif // CASTAS_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_CASTAS_HPP_ diff --git a/lib/parser/ast/nodes/exprs/Elvis.cpp b/lib/parser/ast/nodes/exprs/Elvis.cpp index 304a02a..c19b6d3 100644 --- a/lib/parser/ast/nodes/exprs/Elvis.cpp +++ b/lib/parser/ast/nodes/exprs/Elvis.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void Elvis::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -43,3 +45,5 @@ std::unique_ptr Elvis::ReplaceRhs(std::unique_ptr new_rhs) { rhs_ = std::move(new_rhs); return old_rhs; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/exprs/Elvis.hpp b/lib/parser/ast/nodes/exprs/Elvis.hpp index cdba57d..0511bb8 100644 --- a/lib/parser/ast/nodes/exprs/Elvis.hpp +++ b/lib/parser/ast/nodes/exprs/Elvis.hpp @@ -1,10 +1,12 @@ -#ifndef ELVIS_HPP_ -#define ELVIS_HPP_ +#ifndef PARSER_ELVIS_HPP_ +#define PARSER_ELVIS_HPP_ #include #include "lib/parser/ast/nodes/base/Expr.hpp" +namespace ovum::compiler::parser { + class Elvis : public Expr { public: void Accept(AstVisitor& visitor) override; @@ -24,4 +26,6 @@ class Elvis : public Expr { std::unique_ptr rhs_; }; -#endif // ELVIS_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_ELVIS_HPP_ diff --git a/lib/parser/ast/nodes/exprs/FieldAccess.cpp b/lib/parser/ast/nodes/exprs/FieldAccess.cpp index cd553d6..67395ae 100644 --- a/lib/parser/ast/nodes/exprs/FieldAccess.cpp +++ b/lib/parser/ast/nodes/exprs/FieldAccess.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void FieldAccess::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -33,3 +35,5 @@ const std::string& FieldAccess::Name() const noexcept { void FieldAccess::SetName(std::string new_name) { name_ = std::move(new_name); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/exprs/FieldAccess.hpp b/lib/parser/ast/nodes/exprs/FieldAccess.hpp index 2a9a123..96cbed9 100644 --- a/lib/parser/ast/nodes/exprs/FieldAccess.hpp +++ b/lib/parser/ast/nodes/exprs/FieldAccess.hpp @@ -1,11 +1,13 @@ -#ifndef FIELDACCESS_HPP_ -#define FIELDACCESS_HPP_ +#ifndef PARSER_FIELDACCESS_HPP_ +#define PARSER_FIELDACCESS_HPP_ #include #include #include "lib/parser/ast/nodes/base/Expr.hpp" +namespace ovum::compiler::parser { + class FieldAccess : public Expr { public: void Accept(AstVisitor& visitor) override; @@ -23,4 +25,6 @@ class FieldAccess : public Expr { std::string name_; }; -#endif // FIELDACCESS_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_FIELDACCESS_HPP_ diff --git a/lib/parser/ast/nodes/exprs/IdentRef.cpp b/lib/parser/ast/nodes/exprs/IdentRef.cpp index 711acbb..37cb13c 100644 --- a/lib/parser/ast/nodes/exprs/IdentRef.cpp +++ b/lib/parser/ast/nodes/exprs/IdentRef.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void IdentRef::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -15,3 +17,5 @@ const std::string& IdentRef::Name() const noexcept { void IdentRef::SetName(std::string new_name) { name_ = std::move(new_name); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/exprs/IdentRef.hpp b/lib/parser/ast/nodes/exprs/IdentRef.hpp index 1494077..288aeb6 100644 --- a/lib/parser/ast/nodes/exprs/IdentRef.hpp +++ b/lib/parser/ast/nodes/exprs/IdentRef.hpp @@ -1,10 +1,12 @@ -#ifndef IDENTREF_HPP_ -#define IDENTREF_HPP_ +#ifndef PARSER_IDENTREF_HPP_ +#define PARSER_IDENTREF_HPP_ #include #include "lib/parser/ast/nodes/base/Expr.hpp" +namespace ovum::compiler::parser { + class IdentRef : public Expr { public: void Accept(AstVisitor& visitor) override; @@ -16,4 +18,6 @@ class IdentRef : public Expr { std::string name_; }; -#endif // IDENTREF_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_IDENTREF_HPP_ diff --git a/lib/parser/ast/nodes/exprs/IndexAccess.cpp b/lib/parser/ast/nodes/exprs/IndexAccess.cpp index ccd0b61..2f59348 100644 --- a/lib/parser/ast/nodes/exprs/IndexAccess.cpp +++ b/lib/parser/ast/nodes/exprs/IndexAccess.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void IndexAccess::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -43,3 +45,5 @@ std::unique_ptr IndexAccess::ReplaceIndexExpr(std::unique_ptr new_in index_ = std::move(new_index); return old_index; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/exprs/IndexAccess.hpp b/lib/parser/ast/nodes/exprs/IndexAccess.hpp index 7c68dfe..694c789 100644 --- a/lib/parser/ast/nodes/exprs/IndexAccess.hpp +++ b/lib/parser/ast/nodes/exprs/IndexAccess.hpp @@ -1,10 +1,12 @@ -#ifndef INDEXACCESS_HPP_ -#define INDEXACCESS_HPP_ +#ifndef PARSER_INDEXACCESS_HPP_ +#define PARSER_INDEXACCESS_HPP_ #include #include "lib/parser/ast/nodes/base/Expr.hpp" +namespace ovum::compiler::parser { + class IndexAccess : public Expr { public: void Accept(AstVisitor& visitor) override; @@ -24,4 +26,6 @@ class IndexAccess : public Expr { std::unique_ptr index_; }; -#endif // INDEXACCESS_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_INDEXACCESS_HPP_ diff --git a/lib/parser/ast/nodes/exprs/NamespaceRef.cpp b/lib/parser/ast/nodes/exprs/NamespaceRef.cpp index c07a10d..7443147 100644 --- a/lib/parser/ast/nodes/exprs/NamespaceRef.cpp +++ b/lib/parser/ast/nodes/exprs/NamespaceRef.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void NamespaceRef::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -33,3 +35,5 @@ const std::string& NamespaceRef::Name() const noexcept { void NamespaceRef::SetName(std::string new_name) { name_ = std::move(new_name); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/exprs/NamespaceRef.hpp b/lib/parser/ast/nodes/exprs/NamespaceRef.hpp index bfb11ce..0f0eb72 100644 --- a/lib/parser/ast/nodes/exprs/NamespaceRef.hpp +++ b/lib/parser/ast/nodes/exprs/NamespaceRef.hpp @@ -1,11 +1,13 @@ -#ifndef NAMESPACEREF_HPP_ -#define NAMESPACEREF_HPP_ +#ifndef PARSER_NAMESPACEREF_HPP_ +#define PARSER_NAMESPACEREF_HPP_ #include #include #include "lib/parser/ast/nodes/base/Expr.hpp" +namespace ovum::compiler::parser { + class NamespaceRef : public Expr { public: void Accept(AstVisitor& visitor) override; @@ -23,4 +25,6 @@ class NamespaceRef : public Expr { std::string name_; }; -#endif // NAMESPACEREF_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_NAMESPACEREF_HPP_ diff --git a/lib/parser/ast/nodes/exprs/SafeCall.cpp b/lib/parser/ast/nodes/exprs/SafeCall.cpp index ba32860..a0a158a 100644 --- a/lib/parser/ast/nodes/exprs/SafeCall.cpp +++ b/lib/parser/ast/nodes/exprs/SafeCall.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void SafeCall::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -61,3 +63,5 @@ void SafeCall::SetInferredType(TypeReference inferred) { const std::optional& SafeCall::InferredType() const noexcept { return inferred_type_; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/exprs/SafeCall.hpp b/lib/parser/ast/nodes/exprs/SafeCall.hpp index 37f435e..1052a7d 100644 --- a/lib/parser/ast/nodes/exprs/SafeCall.hpp +++ b/lib/parser/ast/nodes/exprs/SafeCall.hpp @@ -1,5 +1,5 @@ -#ifndef SAFECALL_HPP_ -#define SAFECALL_HPP_ +#ifndef PARSER_SAFECALL_HPP_ +#define PARSER_SAFECALL_HPP_ #include #include @@ -9,6 +9,8 @@ #include "lib/parser/ast/nodes/base/Expr.hpp" #include "lib/parser/types/TypeReference.hpp" +namespace ovum::compiler::parser { + class AstVisitor; class SafeCall : public Expr { @@ -40,4 +42,6 @@ class SafeCall : public Expr { std::optional inferred_type_; }; -#endif // SAFECALL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_SAFECALL_HPP_ diff --git a/lib/parser/ast/nodes/exprs/TypeTestIs.cpp b/lib/parser/ast/nodes/exprs/TypeTestIs.cpp index 63b9721..9b360d8 100644 --- a/lib/parser/ast/nodes/exprs/TypeTestIs.cpp +++ b/lib/parser/ast/nodes/exprs/TypeTestIs.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void TypeTestIs::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -37,3 +39,5 @@ TypeReference& TypeTestIs::MutableType() noexcept { void TypeTestIs::SetType(TypeReference new_type) { type = std::move(new_type); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/exprs/TypeTestIs.hpp b/lib/parser/ast/nodes/exprs/TypeTestIs.hpp index e0840e9..4cff1ce 100644 --- a/lib/parser/ast/nodes/exprs/TypeTestIs.hpp +++ b/lib/parser/ast/nodes/exprs/TypeTestIs.hpp @@ -1,11 +1,13 @@ -#ifndef TYPETESTIS_HPP_ -#define TYPETESTIS_HPP_ +#ifndef PARSER_TYPETESTIS_HPP_ +#define PARSER_TYPETESTIS_HPP_ #include #include "lib/parser/ast/nodes/base/Expr.hpp" #include "lib/parser/types/TypeReference.hpp" +namespace ovum::compiler::parser { + class TypeTestIs : public Expr { public: void Accept(AstVisitor& visitor) override; @@ -24,4 +26,6 @@ class TypeTestIs : public Expr { TypeReference type; }; -#endif // TYPETESTIS_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_TYPETESTIS_HPP_ diff --git a/lib/parser/ast/nodes/exprs/Unary.cpp b/lib/parser/ast/nodes/exprs/Unary.cpp index 4f2384a..e7f5edd 100644 --- a/lib/parser/ast/nodes/exprs/Unary.cpp +++ b/lib/parser/ast/nodes/exprs/Unary.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void Unary::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -33,3 +35,5 @@ std::unique_ptr Unary::ReplaceOperand(std::unique_ptr new_operand) { operand_ = std::move(new_operand); return old_operand; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/exprs/Unary.hpp b/lib/parser/ast/nodes/exprs/Unary.hpp index b746563..60319cc 100644 --- a/lib/parser/ast/nodes/exprs/Unary.hpp +++ b/lib/parser/ast/nodes/exprs/Unary.hpp @@ -1,5 +1,5 @@ -#ifndef UNARY_HPP_ -#define UNARY_HPP_ +#ifndef PARSER_UNARY_HPP_ +#define PARSER_UNARY_HPP_ #include @@ -7,6 +7,8 @@ #include "tags/IUnaryOpTag.hpp" #include "tags/OpTags.hpp" +namespace ovum::compiler::parser { + class Unary : public Expr { public: void Accept(AstVisitor& visitor) override; @@ -24,4 +26,6 @@ class Unary : public Expr { std::unique_ptr operand_; }; -#endif // UNARY_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_UNARY_HPP_ diff --git a/lib/parser/ast/nodes/exprs/literals/BoolLit.cpp b/lib/parser/ast/nodes/exprs/literals/BoolLit.cpp index 424027a..51a7488 100644 --- a/lib/parser/ast/nodes/exprs/literals/BoolLit.cpp +++ b/lib/parser/ast/nodes/exprs/literals/BoolLit.cpp @@ -2,6 +2,8 @@ #include "lib/parser/ast/AstVisitor.hpp" +namespace ovum::compiler::parser { + void BoolLit::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -13,3 +15,5 @@ bool BoolLit::Value() const noexcept { void BoolLit::SetValue(bool new_value) noexcept { value = new_value; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/exprs/literals/BoolLit.hpp b/lib/parser/ast/nodes/exprs/literals/BoolLit.hpp index 7cdfbdb..399b701 100644 --- a/lib/parser/ast/nodes/exprs/literals/BoolLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/BoolLit.hpp @@ -1,8 +1,10 @@ -#ifndef BOOLLIT_HPP_ -#define BOOLLIT_HPP_ +#ifndef PARSER_BOOLLIT_HPP_ +#define PARSER_BOOLLIT_HPP_ #include "lib/parser/ast/nodes/base/Expr.hpp" +namespace ovum::compiler::parser { + class BoolLit : public Expr { public: void Accept(AstVisitor& visitor) override; @@ -14,4 +16,6 @@ class BoolLit : public Expr { bool value = false; }; -#endif // BOOLLIT_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_BOOLLIT_HPP_ diff --git a/lib/parser/ast/nodes/exprs/literals/CharLit.cpp b/lib/parser/ast/nodes/exprs/literals/CharLit.cpp index 437ff5c..4c45367 100644 --- a/lib/parser/ast/nodes/exprs/literals/CharLit.cpp +++ b/lib/parser/ast/nodes/exprs/literals/CharLit.cpp @@ -2,6 +2,8 @@ #include "lib/parser/ast/AstVisitor.hpp" +namespace ovum::compiler::parser { + void CharLit::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -13,3 +15,5 @@ char CharLit::Value() const noexcept { void CharLit::SetValue(char new_value) noexcept { value_ = new_value; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/exprs/literals/CharLit.hpp b/lib/parser/ast/nodes/exprs/literals/CharLit.hpp index 68fd980..6393dd8 100644 --- a/lib/parser/ast/nodes/exprs/literals/CharLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/CharLit.hpp @@ -1,8 +1,10 @@ -#ifndef CHARLIT_HPP_ -#define CHARLIT_HPP_ +#ifndef PARSER_CHARLIT_HPP_ +#define PARSER_CHARLIT_HPP_ #include "lib/parser/ast/nodes/base/Expr.hpp" +namespace ovum::compiler::parser { + class CharLit : public Expr { public: void Accept(AstVisitor& visitor) override; @@ -14,4 +16,6 @@ class CharLit : public Expr { char value_ = '\0'; }; -#endif // CHARLIT_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_CHARLIT_HPP_ diff --git a/lib/parser/ast/nodes/exprs/literals/FloatLit.cpp b/lib/parser/ast/nodes/exprs/literals/FloatLit.cpp index 062e7ca..99631d6 100644 --- a/lib/parser/ast/nodes/exprs/literals/FloatLit.cpp +++ b/lib/parser/ast/nodes/exprs/literals/FloatLit.cpp @@ -2,6 +2,8 @@ #include "lib/parser/ast/AstVisitor.hpp" +namespace ovum::compiler::parser { + void FloatLit::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -13,3 +15,5 @@ double FloatLit::Value() const noexcept { void FloatLit::SetValue(double new_value) noexcept { value = new_value; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/exprs/literals/FloatLit.hpp b/lib/parser/ast/nodes/exprs/literals/FloatLit.hpp index 77af064..4dba68a 100644 --- a/lib/parser/ast/nodes/exprs/literals/FloatLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/FloatLit.hpp @@ -1,8 +1,10 @@ -#ifndef FLOATLIT_HPP_ -#define FLOATLIT_HPP_ +#ifndef PARSER_FLOATLIT_HPP_ +#define PARSER_FLOATLIT_HPP_ #include "lib/parser/ast/nodes/base/Expr.hpp" +namespace ovum::compiler::parser { + class FloatLit : public Expr { public: void Accept(AstVisitor& visitor) override; @@ -14,4 +16,6 @@ class FloatLit : public Expr { double value = 0.0; }; -#endif // FLOATLIT_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_FLOATLIT_HPP_ diff --git a/lib/parser/ast/nodes/exprs/literals/IntLit.cpp b/lib/parser/ast/nodes/exprs/literals/IntLit.cpp index 9d6d2b7..8c9d681 100644 --- a/lib/parser/ast/nodes/exprs/literals/IntLit.cpp +++ b/lib/parser/ast/nodes/exprs/literals/IntLit.cpp @@ -2,6 +2,8 @@ #include "lib/parser/ast/AstVisitor.hpp" +namespace ovum::compiler::parser { + void IntLit::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -13,3 +15,5 @@ int64_t IntLit::Value() const noexcept { void IntLit::SetValue(int64_t new_value) noexcept { value = new_value; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/exprs/literals/IntLit.hpp b/lib/parser/ast/nodes/exprs/literals/IntLit.hpp index 9fc1977..3ec55e5 100644 --- a/lib/parser/ast/nodes/exprs/literals/IntLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/IntLit.hpp @@ -1,9 +1,11 @@ -#ifndef INTLIT_HPP_ -#define INTLIT_HPP_ +#ifndef PARSER_INTLIT_HPP_ +#define PARSER_INTLIT_HPP_ #include #include "lib/parser/ast/nodes/base/Expr.hpp" +namespace ovum::compiler::parser { + class IntLit : public Expr { public: void Accept(AstVisitor& visitor) override; @@ -15,4 +17,6 @@ class IntLit : public Expr { int64_t value = 0; }; -#endif // INTLIT_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_INTLIT_HPP_ diff --git a/lib/parser/ast/nodes/exprs/literals/NullLit.cpp b/lib/parser/ast/nodes/exprs/literals/NullLit.cpp index 5cef978..1af96fd 100644 --- a/lib/parser/ast/nodes/exprs/literals/NullLit.cpp +++ b/lib/parser/ast/nodes/exprs/literals/NullLit.cpp @@ -2,6 +2,10 @@ #include "lib/parser/ast/AstVisitor.hpp" +namespace ovum::compiler::parser { + void NullLit::Accept(AstVisitor& visitor) { visitor.Visit(*this); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/exprs/literals/NullLit.hpp b/lib/parser/ast/nodes/exprs/literals/NullLit.hpp index 8966ceb..21ac8a4 100644 --- a/lib/parser/ast/nodes/exprs/literals/NullLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/NullLit.hpp @@ -1,11 +1,15 @@ -#ifndef NULLLIT_HPP_ -#define NULLLIT_HPP_ +#ifndef PARSER_NULLLIT_HPP_ +#define PARSER_NULLLIT_HPP_ #include "lib/parser/ast/nodes/base/Expr.hpp" +namespace ovum::compiler::parser { + class NullLit : public Expr { public: void Accept(AstVisitor& visitor) override; }; -#endif // NULLLIT_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_NULLLIT_HPP_ diff --git a/lib/parser/ast/nodes/exprs/literals/StringLit.cpp b/lib/parser/ast/nodes/exprs/literals/StringLit.cpp index c8cb2d7..db667f2 100644 --- a/lib/parser/ast/nodes/exprs/literals/StringLit.cpp +++ b/lib/parser/ast/nodes/exprs/literals/StringLit.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void StringLit::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -15,3 +17,5 @@ const std::string& StringLit::Value() const noexcept { void StringLit::SetValue(std::string new_value) { value_ = std::move(new_value); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/exprs/literals/StringLit.hpp b/lib/parser/ast/nodes/exprs/literals/StringLit.hpp index 9374e85..e6a1ed5 100644 --- a/lib/parser/ast/nodes/exprs/literals/StringLit.hpp +++ b/lib/parser/ast/nodes/exprs/literals/StringLit.hpp @@ -1,10 +1,12 @@ -#ifndef STRINGLIT_HPP_ -#define STRINGLIT_HPP_ +#ifndef PARSER_STRINGLIT_HPP_ +#define PARSER_STRINGLIT_HPP_ #include #include "lib/parser/ast/nodes/base/Expr.hpp" +namespace ovum::compiler::parser { + class StringLit : public Expr { public: void Accept(AstVisitor& visitor) override; @@ -16,4 +18,6 @@ class StringLit : public Expr { std::string value_; }; -#endif // STRINGLIT_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STRINGLIT_HPP_ diff --git a/lib/parser/ast/nodes/exprs/tags/IAssignOpTag.hpp b/lib/parser/ast/nodes/exprs/tags/IAssignOpTag.hpp index 8da40ad..a7f6116 100644 --- a/lib/parser/ast/nodes/exprs/tags/IAssignOpTag.hpp +++ b/lib/parser/ast/nodes/exprs/tags/IAssignOpTag.hpp @@ -1,8 +1,10 @@ -#ifndef IASSIGNOPTAG_HPP_ -#define IASSIGNOPTAG_HPP_ +#ifndef PARSER_IASSIGNOPTAG_HPP_ +#define PARSER_IASSIGNOPTAG_HPP_ #include +namespace ovum::compiler::parser { + class IAssignOpTag { public: virtual ~IAssignOpTag() = default; @@ -10,4 +12,6 @@ class IAssignOpTag { virtual std::string_view Name() const = 0; }; -#endif // IASSIGNOPTAG_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_IASSIGNOPTAG_HPP_ diff --git a/lib/parser/ast/nodes/exprs/tags/IBinaryOpTag.hpp b/lib/parser/ast/nodes/exprs/tags/IBinaryOpTag.hpp index 22760ad..0032c50 100644 --- a/lib/parser/ast/nodes/exprs/tags/IBinaryOpTag.hpp +++ b/lib/parser/ast/nodes/exprs/tags/IBinaryOpTag.hpp @@ -1,8 +1,10 @@ -#ifndef IBINARYOPTAG_HPP_ -#define IBINARYOPTAG_HPP_ +#ifndef PARSER_IBINARYOPTAG_HPP_ +#define PARSER_IBINARYOPTAG_HPP_ #include +namespace ovum::compiler::parser { + class IBinaryOpTag { public: virtual ~IBinaryOpTag() = default; @@ -10,4 +12,6 @@ class IBinaryOpTag { virtual std::string_view Name() const = 0; }; -#endif // IBINARYOPTAG_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_IBINARYOPTAG_HPP_ diff --git a/lib/parser/ast/nodes/exprs/tags/IUnaryOpTag.hpp b/lib/parser/ast/nodes/exprs/tags/IUnaryOpTag.hpp index 50dc98c..d5d42ee 100644 --- a/lib/parser/ast/nodes/exprs/tags/IUnaryOpTag.hpp +++ b/lib/parser/ast/nodes/exprs/tags/IUnaryOpTag.hpp @@ -1,8 +1,10 @@ -#ifndef IUNARYOPTAG_HPP_ -#define IUNARYOPTAG_HPP_ +#ifndef PARSER_IUNARYOPTAG_HPP_ +#define PARSER_IUNARYOPTAG_HPP_ #include +namespace ovum::compiler::parser { + class IUnaryOpTag { public: virtual ~IUnaryOpTag() = default; @@ -10,4 +12,6 @@ class IUnaryOpTag { virtual std::string_view Name() const = 0; }; -#endif // IUNARYOPTAG_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_IUNARYOPTAG_HPP_ diff --git a/lib/parser/ast/nodes/exprs/tags/OpTags.cpp b/lib/parser/ast/nodes/exprs/tags/OpTags.cpp index 3638429..53c8c87 100644 --- a/lib/parser/ast/nodes/exprs/tags/OpTags.cpp +++ b/lib/parser/ast/nodes/exprs/tags/OpTags.cpp @@ -2,118 +2,120 @@ #include +namespace ovum::compiler::parser { + namespace { struct BinaryAdd : IBinaryOpTag { - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return "add"; } }; struct BinarySub : IBinaryOpTag { - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return "sub"; } }; struct BinaryMul : IBinaryOpTag { - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return "mul"; } }; struct BinaryDiv : IBinaryOpTag { - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return "div"; } }; struct BinaryMod : IBinaryOpTag { - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return "mod"; } }; struct BinaryLt : IBinaryOpTag { - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return "lt"; } }; struct BinaryLe : IBinaryOpTag { - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return "le"; } }; struct BinaryGt : IBinaryOpTag { - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return "gt"; } }; struct BinaryGe : IBinaryOpTag { - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return "ge"; } }; struct BinaryEq : IBinaryOpTag { - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return "eq"; } }; struct BinaryNe : IBinaryOpTag { - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return "ne"; } }; struct BinaryAnd : IBinaryOpTag { - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return "and"; } }; struct BinaryOr : IBinaryOpTag { - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return "or"; } }; struct BinaryXor : IBinaryOpTag { - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return "xor"; } }; struct UnaryNeg : IUnaryOpTag { - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return "neg"; } }; struct UnaryPlus : IUnaryOpTag { - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return "plus"; } }; struct UnaryNot : IUnaryOpTag { - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return "not"; } }; struct AssignRef : IAssignOpTag { - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return "="; } }; struct AssignCopy : IAssignOpTag { - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return ":="; } }; @@ -218,3 +220,5 @@ const IAssignOpTag& CopyAssign() { } } // namespace OpTags + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/exprs/tags/OpTags.hpp b/lib/parser/ast/nodes/exprs/tags/OpTags.hpp index e80a0ce..7879e2e 100644 --- a/lib/parser/ast/nodes/exprs/tags/OpTags.hpp +++ b/lib/parser/ast/nodes/exprs/tags/OpTags.hpp @@ -1,10 +1,12 @@ -#ifndef OPTAGS_HPP_ -#define OPTAGS_HPP_ +#ifndef PARSER_OPTAGS_HPP_ +#define PARSER_OPTAGS_HPP_ #include "IAssignOpTag.hpp" #include "IBinaryOpTag.hpp" #include "IUnaryOpTag.hpp" +namespace ovum::compiler::parser { + namespace OpTags { // binary const IBinaryOpTag& Add(); @@ -32,4 +34,6 @@ const IAssignOpTag& RefAssign(); // = const IAssignOpTag& CopyAssign(); // := } // namespace OpTags -#endif // OPTAGS_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_OPTAGS_HPP_ diff --git a/lib/parser/ast/nodes/stmts/Block.cpp b/lib/parser/ast/nodes/stmts/Block.cpp index 8f423f3..e572b0a 100644 --- a/lib/parser/ast/nodes/stmts/Block.cpp +++ b/lib/parser/ast/nodes/stmts/Block.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void Block::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -45,3 +47,5 @@ void Block::Clear() noexcept { std::size_t Block::Size() const noexcept { return stmts_.size(); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/stmts/Block.hpp b/lib/parser/ast/nodes/stmts/Block.hpp index 916534c..046ba3e 100644 --- a/lib/parser/ast/nodes/stmts/Block.hpp +++ b/lib/parser/ast/nodes/stmts/Block.hpp @@ -1,5 +1,5 @@ -#ifndef BLOCK_HPP_ -#define BLOCK_HPP_ +#ifndef PARSER_BLOCK_HPP_ +#define PARSER_BLOCK_HPP_ #include #include @@ -8,6 +8,8 @@ #include "lib/parser/ast/nodes/base/AstNode.hpp" #include "lib/parser/ast/nodes/base/Stmt.hpp" +namespace ovum::compiler::parser { + class Block : public Stmt { public: void Accept(AstVisitor& visitor) override; @@ -25,4 +27,6 @@ class Block : public Stmt { std::vector> stmts_; }; -#endif // BLOCK_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_BLOCK_HPP_ diff --git a/lib/parser/ast/nodes/stmts/Branch.cpp b/lib/parser/ast/nodes/stmts/Branch.cpp index 3155ab5..8dbdecf 100644 --- a/lib/parser/ast/nodes/stmts/Branch.cpp +++ b/lib/parser/ast/nodes/stmts/Branch.cpp @@ -2,6 +2,8 @@ #include +namespace ovum::compiler::parser { + Branch::Branch(std::unique_ptr condition, std::unique_ptr then_block) : condition_(std::move(condition)), then_block_(std::move(then_block)) { } @@ -37,3 +39,5 @@ void Branch::SetThen(std::unique_ptr then_body) { std::unique_ptr Branch::ReleaseThen() { return std::move(then_block_); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/stmts/Branch.hpp b/lib/parser/ast/nodes/stmts/Branch.hpp index 12290b0..cc679ff 100644 --- a/lib/parser/ast/nodes/stmts/Branch.hpp +++ b/lib/parser/ast/nodes/stmts/Branch.hpp @@ -1,11 +1,13 @@ -#ifndef BRANCH_HPP_ -#define BRANCH_HPP_ +#ifndef PARSER_BRANCH_HPP_ +#define PARSER_BRANCH_HPP_ #include #include "Block.hpp" #include "lib/parser/ast/nodes/base/Expr.hpp" +namespace ovum::compiler::parser { + class Branch { public: Branch(std::unique_ptr condition, std::unique_ptr then_block); @@ -30,4 +32,6 @@ class Branch { std::unique_ptr then_block_; }; -#endif // BRANCH_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_BRANCH_HPP_ diff --git a/lib/parser/ast/nodes/stmts/BreakStmt.cpp b/lib/parser/ast/nodes/stmts/BreakStmt.cpp index f1d7d2a..1685231 100644 --- a/lib/parser/ast/nodes/stmts/BreakStmt.cpp +++ b/lib/parser/ast/nodes/stmts/BreakStmt.cpp @@ -2,6 +2,10 @@ #include "lib/parser/ast/AstVisitor.hpp" +namespace ovum::compiler::parser { + void BreakStmt::Accept(AstVisitor& visitor) { visitor.Visit(*this); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/stmts/BreakStmt.hpp b/lib/parser/ast/nodes/stmts/BreakStmt.hpp index b0ffb8a..bf3fbf5 100644 --- a/lib/parser/ast/nodes/stmts/BreakStmt.hpp +++ b/lib/parser/ast/nodes/stmts/BreakStmt.hpp @@ -1,11 +1,15 @@ -#ifndef BREAKSTMT_HPP_ -#define BREAKSTMT_HPP_ +#ifndef PARSER_BREAKSTMT_HPP_ +#define PARSER_BREAKSTMT_HPP_ #include "lib/parser/ast/nodes/base/Stmt.hpp" +namespace ovum::compiler::parser { + class BreakStmt : public Stmt { public: void Accept(AstVisitor& visitor) override; }; -#endif // BREAKSTMT_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_BREAKSTMT_HPP_ diff --git a/lib/parser/ast/nodes/stmts/ContinueStmt.cpp b/lib/parser/ast/nodes/stmts/ContinueStmt.cpp index be4e1df..aa19fc7 100644 --- a/lib/parser/ast/nodes/stmts/ContinueStmt.cpp +++ b/lib/parser/ast/nodes/stmts/ContinueStmt.cpp @@ -2,6 +2,10 @@ #include "lib/parser/ast/AstVisitor.hpp" +namespace ovum::compiler::parser { + void ContinueStmt::Accept(AstVisitor& visitor) { visitor.Visit(*this); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/stmts/ContinueStmt.hpp b/lib/parser/ast/nodes/stmts/ContinueStmt.hpp index f81ff11..11ce141 100644 --- a/lib/parser/ast/nodes/stmts/ContinueStmt.hpp +++ b/lib/parser/ast/nodes/stmts/ContinueStmt.hpp @@ -1,11 +1,15 @@ -#ifndef CONTINUESTMT_HPP_ -#define CONTINUESTMT_HPP_ +#ifndef PARSER_CONTINUESTMT_HPP_ +#define PARSER_CONTINUESTMT_HPP_ #include "lib/parser/ast/nodes/base/Stmt.hpp" +namespace ovum::compiler::parser { + class ContinueStmt : public Stmt { public: void Accept(AstVisitor& visitor) override; }; -#endif // CONTINUESTMT_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_CONTINUESTMT_HPP_ diff --git a/lib/parser/ast/nodes/stmts/ExprStmt.cpp b/lib/parser/ast/nodes/stmts/ExprStmt.cpp index d9b7a12..80424ef 100644 --- a/lib/parser/ast/nodes/stmts/ExprStmt.cpp +++ b/lib/parser/ast/nodes/stmts/ExprStmt.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void ExprStmt::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -23,3 +25,5 @@ void ExprStmt::SetExpression(std::unique_ptr expression) { std::unique_ptr ExprStmt::ReleaseExpression() { return std::move(expr_); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/stmts/ExprStmt.hpp b/lib/parser/ast/nodes/stmts/ExprStmt.hpp index b410d13..420b611 100644 --- a/lib/parser/ast/nodes/stmts/ExprStmt.hpp +++ b/lib/parser/ast/nodes/stmts/ExprStmt.hpp @@ -1,11 +1,13 @@ -#ifndef EXPRSTMT_HPP_ -#define EXPRSTMT_HPP_ +#ifndef PARSER_EXPRSTMT_HPP_ +#define PARSER_EXPRSTMT_HPP_ #include #include "lib/parser/ast/nodes/base/Expr.hpp" #include "lib/parser/ast/nodes/base/Stmt.hpp" +namespace ovum::compiler::parser { + class ExprStmt : public Stmt { public: void Accept(AstVisitor& visitor) override; @@ -19,4 +21,6 @@ class ExprStmt : public Stmt { std::unique_ptr expr_; }; -#endif // EXPRSTMT_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_EXPRSTMT_HPP_ diff --git a/lib/parser/ast/nodes/stmts/ForStmt.cpp b/lib/parser/ast/nodes/stmts/ForStmt.cpp index e42cd6c..3767dbc 100644 --- a/lib/parser/ast/nodes/stmts/ForStmt.cpp +++ b/lib/parser/ast/nodes/stmts/ForStmt.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void ForStmt::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -47,3 +49,5 @@ void ForStmt::SetBody(std::unique_ptr body_block) { std::unique_ptr ForStmt::ReleaseBody() { return std::move(body_); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/stmts/ForStmt.hpp b/lib/parser/ast/nodes/stmts/ForStmt.hpp index b8f5ff8..954f0e4 100644 --- a/lib/parser/ast/nodes/stmts/ForStmt.hpp +++ b/lib/parser/ast/nodes/stmts/ForStmt.hpp @@ -1,5 +1,5 @@ -#ifndef FORSTMT_HPP_ -#define FORSTMT_HPP_ +#ifndef PARSER_FORSTMT_HPP_ +#define PARSER_FORSTMT_HPP_ #include #include @@ -7,6 +7,8 @@ #include "Block.hpp" #include "lib/parser/ast/nodes/base/Expr.hpp" +namespace ovum::compiler::parser { + class ForStmt : public Stmt { public: void Accept(AstVisitor& visitor) override; @@ -30,4 +32,6 @@ class ForStmt : public Stmt { std::unique_ptr body_; }; -#endif // FORSTMT_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_FORSTMT_HPP_ diff --git a/lib/parser/ast/nodes/stmts/IfStmt.cpp b/lib/parser/ast/nodes/stmts/IfStmt.cpp index 7ca5133..dd768dd 100644 --- a/lib/parser/ast/nodes/stmts/IfStmt.cpp +++ b/lib/parser/ast/nodes/stmts/IfStmt.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void IfStmt::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -47,3 +49,5 @@ bool IfStmt::HasElse() const noexcept { bool IfStmt::Empty() const noexcept { return branches_.empty() && !else_block_; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/stmts/IfStmt.hpp b/lib/parser/ast/nodes/stmts/IfStmt.hpp index c747a27..e272bc3 100644 --- a/lib/parser/ast/nodes/stmts/IfStmt.hpp +++ b/lib/parser/ast/nodes/stmts/IfStmt.hpp @@ -1,5 +1,5 @@ -#ifndef IFSTMT_HPP_ -#define IFSTMT_HPP_ +#ifndef PARSER_IFSTMT_HPP_ +#define PARSER_IFSTMT_HPP_ #include #include @@ -9,6 +9,8 @@ #include "lib/parser/ast/nodes/base/Expr.hpp" #include "lib/parser/ast/nodes/base/Stmt.hpp" +namespace ovum::compiler::parser { + class IfStmt : public Stmt { public: void Accept(AstVisitor& visitor) override; @@ -31,4 +33,6 @@ class IfStmt : public Stmt { std::unique_ptr else_block_; }; -#endif // IFSTMT_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_IFSTMT_HPP_ diff --git a/lib/parser/ast/nodes/stmts/ReturnStmt.cpp b/lib/parser/ast/nodes/stmts/ReturnStmt.cpp index 06c583c..0ad75a8 100644 --- a/lib/parser/ast/nodes/stmts/ReturnStmt.cpp +++ b/lib/parser/ast/nodes/stmts/ReturnStmt.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void ReturnStmt::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -37,3 +39,5 @@ std::unique_ptr ReturnStmt::ReleaseValue() { value_.reset(); return out; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/stmts/ReturnStmt.hpp b/lib/parser/ast/nodes/stmts/ReturnStmt.hpp index f8f3f6d..3437e5a 100644 --- a/lib/parser/ast/nodes/stmts/ReturnStmt.hpp +++ b/lib/parser/ast/nodes/stmts/ReturnStmt.hpp @@ -1,5 +1,5 @@ -#ifndef RETURNSTMT_HPP_ -#define RETURNSTMT_HPP_ +#ifndef PARSER_RETURNSTMT_HPP_ +#define PARSER_RETURNSTMT_HPP_ #include #include @@ -7,6 +7,8 @@ #include "lib/parser/ast/nodes/base/Expr.hpp" #include "lib/parser/ast/nodes/base/Stmt.hpp" +namespace ovum::compiler::parser { + class ReturnStmt : public Stmt { public: void Accept(AstVisitor& visitor) override; @@ -22,4 +24,6 @@ class ReturnStmt : public Stmt { std::optional> value_; }; -#endif // RETURNSTMT_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_RETURNSTMT_HPP_ diff --git a/lib/parser/ast/nodes/stmts/UnsafeBlock.cpp b/lib/parser/ast/nodes/stmts/UnsafeBlock.cpp index b71f347..e15365d 100644 --- a/lib/parser/ast/nodes/stmts/UnsafeBlock.cpp +++ b/lib/parser/ast/nodes/stmts/UnsafeBlock.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void UnsafeBlock::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -23,3 +25,5 @@ void UnsafeBlock::SetBody(std::unique_ptr body_block) { std::unique_ptr UnsafeBlock::ReleaseBody() { return std::move(body_); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/stmts/UnsafeBlock.hpp b/lib/parser/ast/nodes/stmts/UnsafeBlock.hpp index a28fe55..76a8d92 100644 --- a/lib/parser/ast/nodes/stmts/UnsafeBlock.hpp +++ b/lib/parser/ast/nodes/stmts/UnsafeBlock.hpp @@ -1,11 +1,13 @@ -#ifndef UNSAFEBLOCK_HPP_ -#define UNSAFEBLOCK_HPP_ +#ifndef PARSER_UNSAFEBLOCK_HPP_ +#define PARSER_UNSAFEBLOCK_HPP_ #include #include "Block.hpp" #include "lib/parser/ast/nodes/base/Stmt.hpp" +namespace ovum::compiler::parser { + class UnsafeBlock : public Stmt { public: void Accept(AstVisitor& visitor) override; @@ -19,4 +21,6 @@ class UnsafeBlock : public Stmt { std::unique_ptr body_; }; -#endif // UNSAFEBLOCK_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_UNSAFEBLOCK_HPP_ diff --git a/lib/parser/ast/nodes/stmts/VarDeclStmt.cpp b/lib/parser/ast/nodes/stmts/VarDeclStmt.cpp index 69bc2cb..2d3e9a1 100644 --- a/lib/parser/ast/nodes/stmts/VarDeclStmt.cpp +++ b/lib/parser/ast/nodes/stmts/VarDeclStmt.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void VarDeclStmt::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -51,3 +53,5 @@ void VarDeclStmt::SetInit(std::unique_ptr init_expr) { std::unique_ptr VarDeclStmt::ReleaseInit() { return std::move(init_); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/stmts/VarDeclStmt.hpp b/lib/parser/ast/nodes/stmts/VarDeclStmt.hpp index 383d248..6c86680 100644 --- a/lib/parser/ast/nodes/stmts/VarDeclStmt.hpp +++ b/lib/parser/ast/nodes/stmts/VarDeclStmt.hpp @@ -1,5 +1,5 @@ -#ifndef VARDECLSTMT_HPP_ -#define VARDECLSTMT_HPP_ +#ifndef PARSER_VARDECLSTMT_HPP_ +#define PARSER_VARDECLSTMT_HPP_ #include #include @@ -8,6 +8,8 @@ #include "lib/parser/ast/nodes/base/Stmt.hpp" #include "lib/parser/types/TypeReference.hpp" +namespace ovum::compiler::parser { + class VarDeclStmt : public Stmt { public: void Accept(AstVisitor& visitor) override; @@ -34,4 +36,6 @@ class VarDeclStmt : public Stmt { std::unique_ptr init_; }; -#endif // VARDECLSTMT_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_VARDECLSTMT_HPP_ diff --git a/lib/parser/ast/nodes/stmts/WhileStmt.cpp b/lib/parser/ast/nodes/stmts/WhileStmt.cpp index 28f4c9b..952b0f8 100644 --- a/lib/parser/ast/nodes/stmts/WhileStmt.cpp +++ b/lib/parser/ast/nodes/stmts/WhileStmt.cpp @@ -4,6 +4,8 @@ #include +namespace ovum::compiler::parser { + void WhileStmt::Accept(AstVisitor& visitor) { visitor.Visit(*this); } @@ -39,3 +41,5 @@ void WhileStmt::SetBody(std::unique_ptr body_block) { std::unique_ptr WhileStmt::ReleaseBody() { return std::move(body_); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/nodes/stmts/WhileStmt.hpp b/lib/parser/ast/nodes/stmts/WhileStmt.hpp index 3d0659b..8a4c65b 100644 --- a/lib/parser/ast/nodes/stmts/WhileStmt.hpp +++ b/lib/parser/ast/nodes/stmts/WhileStmt.hpp @@ -1,5 +1,5 @@ -#ifndef WHILESTMT_HPP_ -#define WHILESTMT_HPP_ +#ifndef PARSER_WHILESTMT_HPP_ +#define PARSER_WHILESTMT_HPP_ #include @@ -7,6 +7,8 @@ #include "lib/parser/ast/nodes/base/Expr.hpp" #include "lib/parser/ast/nodes/base/Stmt.hpp" +namespace ovum::compiler::parser { + class WhileStmt : public Stmt { public: void Accept(AstVisitor& visitor) override; @@ -26,4 +28,6 @@ class WhileStmt : public Stmt { std::unique_ptr body_; }; -#endif // WHILESTMT_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_WHILESTMT_HPP_ diff --git a/lib/parser/ast/visitors/ConstWalkVisitor.cpp b/lib/parser/ast/visitors/ConstWalkVisitor.cpp index 535f070..37f244e 100644 --- a/lib/parser/ast/visitors/ConstWalkVisitor.cpp +++ b/lib/parser/ast/visitors/ConstWalkVisitor.cpp @@ -45,6 +45,8 @@ #include "lib/parser/ast/nodes/stmts/VarDeclStmt.hpp" #include "lib/parser/ast/nodes/stmts/WhileStmt.hpp" +namespace ovum::compiler::parser { + void ConstWalkVisitor::Visit(Module& node) { for (auto& decl_ptr : node.MutableDecls()) { decl_ptr->Accept(*this); @@ -268,3 +270,5 @@ void ConstWalkVisitor::Visit(BoolLit& node) { void ConstWalkVisitor::Visit(NullLit& node) { (void) node; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/visitors/ConstWalkVisitor.hpp b/lib/parser/ast/visitors/ConstWalkVisitor.hpp index 4c4b087..c1b3f6d 100644 --- a/lib/parser/ast/visitors/ConstWalkVisitor.hpp +++ b/lib/parser/ast/visitors/ConstWalkVisitor.hpp @@ -1,8 +1,10 @@ -#ifndef CONSTWALKVISITOR_HPP_ -#define CONSTWALKVISITOR_HPP_ +#ifndef PARSER_CONSTWALKVISITOR_HPP_ +#define PARSER_CONSTWALKVISITOR_HPP_ #include "lib/parser/ast/AstVisitor.hpp" +namespace ovum::compiler::parser { + class ConstWalkVisitor : public AstVisitor { public: ~ConstWalkVisitor() override = default; @@ -51,4 +53,6 @@ class ConstWalkVisitor : public AstVisitor { void Visit(NullLit& node) override; }; -#endif // CONSTWALKVISITOR_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_CONSTWALKVISITOR_HPP_ diff --git a/lib/parser/ast/visitors/LintVisitor.cpp b/lib/parser/ast/visitors/LintVisitor.cpp index b53326d..f7f5d82 100644 --- a/lib/parser/ast/visitors/LintVisitor.cpp +++ b/lib/parser/ast/visitors/LintVisitor.cpp @@ -32,6 +32,8 @@ #include "lib/parser/ast/nodes/stmts/UnsafeBlock.hpp" #include "lib/parser/ast/nodes/stmts/WhileStmt.hpp" +namespace ovum::compiler::parser { + void LintVisitor::EnterBody() { ++nesting_depth_; } @@ -52,7 +54,7 @@ void LintVisitor::LeaveLoop() { } } -void LintVisitor::CheckNestingDepth(SourceSpan) const { +void LintVisitor::CheckNestingDepth(const SourceSpan&) const { if (opts_.warn_deep_nesting && nesting_depth_ > opts_.max_nesting) { sink_.Warn("W0201", "deep nesting"); } @@ -157,11 +159,8 @@ void LintVisitor::Visit(Block& node) { continue; } - if (dynamic_cast(stmt.get())) { - terminated = true; - } else if (dynamic_cast(stmt.get())) { - terminated = true; - } else if (dynamic_cast(stmt.get())) { + if (dynamic_cast(stmt.get()) || dynamic_cast(stmt.get()) || + dynamic_cast(stmt.get())) { terminated = true; } } @@ -324,3 +323,5 @@ void LintVisitor::Visit(BoolLit& node) { (void) node; WalkVisitor::Visit(node); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/visitors/LintVisitor.hpp b/lib/parser/ast/visitors/LintVisitor.hpp index a6fdeb6..22cd71c 100644 --- a/lib/parser/ast/visitors/LintVisitor.hpp +++ b/lib/parser/ast/visitors/LintVisitor.hpp @@ -1,5 +1,5 @@ -#ifndef LINTVISITOR_HPP_ -#define LINTVISITOR_HPP_ +#ifndef PARSER_LINTVISITOR_HPP_ +#define PARSER_LINTVISITOR_HPP_ #include #include @@ -8,6 +8,8 @@ #include "lib/parser/diagnostics/IDiagnosticSink.hpp" +namespace ovum::compiler::parser { + struct LintOptions { std::size_t max_block_len = 200; std::size_t max_nesting = 4; @@ -65,7 +67,7 @@ class LintVisitor : public WalkVisitor { void LeaveBody(); void EnterLoop(); void LeaveLoop(); - void CheckNestingDepth(SourceSpan where_hint = {}) const; + void CheckNestingDepth(const SourceSpan& where_hint = {}) const; IDiagnosticSink& sink_; LintOptions opts_; @@ -73,4 +75,6 @@ class LintVisitor : public WalkVisitor { std::size_t nesting_depth_ = 0; }; -#endif // LINTVISITOR_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_LINTVISITOR_HPP_ diff --git a/lib/parser/ast/visitors/PrintVisitor.cpp b/lib/parser/ast/visitors/PrintVisitor.cpp index 4172a13..9f47964 100644 --- a/lib/parser/ast/visitors/PrintVisitor.cpp +++ b/lib/parser/ast/visitors/PrintVisitor.cpp @@ -16,6 +16,8 @@ #include "lib/parser/ast/nodes/stmts/Block.hpp" +namespace ovum::compiler::parser { + PrintVisitor::PrintVisitor(std::ostream& output) : out_(&output) { } @@ -415,3 +417,5 @@ void PrintVisitor::Visit(BoolLit& node) { void PrintVisitor::Visit(NullLit& node) { WriteLine("NullLit"); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/visitors/PrintVisitor.hpp b/lib/parser/ast/visitors/PrintVisitor.hpp index 97eb5eb..fef2fce 100644 --- a/lib/parser/ast/visitors/PrintVisitor.hpp +++ b/lib/parser/ast/visitors/PrintVisitor.hpp @@ -1,5 +1,5 @@ -#ifndef PRINTVISITOR_HPP_ -#define PRINTVISITOR_HPP_ +#ifndef PARSER_PRINTVISITOR_HPP_ +#define PARSER_PRINTVISITOR_HPP_ #include #include @@ -7,6 +7,8 @@ #include "lib/parser/ast/AstVisitor.hpp" +namespace ovum::compiler::parser { + class PrintVisitor : public AstVisitor { public: explicit PrintVisitor(std::ostream& output); @@ -71,4 +73,6 @@ class PrintVisitor : public AstVisitor { bool use_buffer_ = false; }; -#endif // PRINTVISITOR_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_PRINTVISITOR_HPP_ diff --git a/lib/parser/ast/visitors/StructuralValidator.cpp b/lib/parser/ast/visitors/StructuralValidator.cpp index 0f6bc25..0a9b5b4 100644 --- a/lib/parser/ast/visitors/StructuralValidator.cpp +++ b/lib/parser/ast/visitors/StructuralValidator.cpp @@ -18,6 +18,8 @@ #include "lib/parser/ast/nodes/exprs/SafeCall.hpp" #include "lib/parser/ast/nodes/exprs/Unary.hpp" +namespace ovum::compiler::parser { + void StructuralValidator::Visit(Module& node) { if (node.Name().empty()) { sink_.Error("E0001", "module name must not be empty"); @@ -112,3 +114,5 @@ void StructuralValidator::Visit(SafeCall& node) { (void) node.MutableObject(); WalkVisitor::Visit(node); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/visitors/StructuralValidator.hpp b/lib/parser/ast/visitors/StructuralValidator.hpp index ffd4f91..7079927 100644 --- a/lib/parser/ast/visitors/StructuralValidator.hpp +++ b/lib/parser/ast/visitors/StructuralValidator.hpp @@ -1,9 +1,11 @@ -#ifndef STRUCTURALVALIDATOR_HPP_ -#define STRUCTURALVALIDATOR_HPP_ +#ifndef PARSER_STRUCTURALVALIDATOR_HPP_ +#define PARSER_STRUCTURALVALIDATOR_HPP_ #include "WalkVisitor.hpp" #include "lib/parser/diagnostics/IDiagnosticSink.hpp" +namespace ovum::compiler::parser { + class StructuralValidator : public WalkVisitor { public: explicit StructuralValidator(IDiagnosticSink& sink) : sink_(sink) { @@ -28,4 +30,6 @@ class StructuralValidator : public WalkVisitor { IDiagnosticSink& sink_; }; -#endif // STRUCTURALVALIDATOR_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STRUCTURALVALIDATOR_HPP_ diff --git a/lib/parser/ast/visitors/WalkVisitor.cpp b/lib/parser/ast/visitors/WalkVisitor.cpp index ce6e145..f9d4216 100644 --- a/lib/parser/ast/visitors/WalkVisitor.cpp +++ b/lib/parser/ast/visitors/WalkVisitor.cpp @@ -45,6 +45,8 @@ #include "lib/parser/ast/nodes/stmts/VarDeclStmt.hpp" #include "lib/parser/ast/nodes/stmts/WhileStmt.hpp" +namespace ovum::compiler::parser { + void WalkVisitor::Visit(Module& node) { for (auto& decl_ptr : node.MutableDecls()) { decl_ptr->Accept(*this); @@ -268,3 +270,5 @@ void WalkVisitor::Visit(BoolLit& node) { void WalkVisitor::Visit(NullLit& node) { (void) node; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/ast/visitors/WalkVisitor.hpp b/lib/parser/ast/visitors/WalkVisitor.hpp index 7b8aea5..448217a 100644 --- a/lib/parser/ast/visitors/WalkVisitor.hpp +++ b/lib/parser/ast/visitors/WalkVisitor.hpp @@ -1,8 +1,10 @@ -#ifndef WALKVISITOR_HPP_ -#define WALKVISITOR_HPP_ +#ifndef PARSER_WALKVISITOR_HPP_ +#define PARSER_WALKVISITOR_HPP_ #include "lib/parser/ast/AstVisitor.hpp" +namespace ovum::compiler::parser { + class WalkVisitor : public AstVisitor { public: ~WalkVisitor() override = default; @@ -51,4 +53,6 @@ class WalkVisitor : public AstVisitor { void Visit(NullLit& node) override; }; -#endif // WALKVISITOR_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_WALKVISITOR_HPP_ diff --git a/lib/parser/context/ContextParser.cpp b/lib/parser/context/ContextParser.cpp index 9e410a9..b4f763f 100644 --- a/lib/parser/context/ContextParser.cpp +++ b/lib/parser/context/ContextParser.cpp @@ -2,6 +2,8 @@ #include +namespace ovum::compiler::parser { + std::vector& ContextParser::StateStack() { return state_stack_; } @@ -81,3 +83,5 @@ void ContextParser::Clear() { expr_ = nullptr; typep_ = nullptr; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/context/ContextParser.hpp b/lib/parser/context/ContextParser.hpp index 7e7c8eb..43458ac 100644 --- a/lib/parser/context/ContextParser.hpp +++ b/lib/parser/context/ContextParser.hpp @@ -1,5 +1,5 @@ -#ifndef CONTEXTPARSER_HPP_ -#define CONTEXTPARSER_HPP_ +#ifndef PARSER_CONTEXTPARSER_HPP_ +#define PARSER_CONTEXTPARSER_HPP_ #include #include @@ -9,6 +9,8 @@ #include "lib/parser/diagnostics/IDiagnosticSink.hpp" #include "lib/parser/states/base/IState.hpp" +namespace ovum::compiler::parser { + class AstNode; class IExpressionParser; class ITypeParser; @@ -58,4 +60,6 @@ class ContextParser { ITypeParser* typep_ = nullptr; }; -#endif // CONTEXTPARSER_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_CONTEXTPARSER_HPP_ diff --git a/lib/parser/context/NodeEntry.cpp b/lib/parser/context/NodeEntry.cpp index 0409b23..659e4d7 100644 --- a/lib/parser/context/NodeEntry.cpp +++ b/lib/parser/context/NodeEntry.cpp @@ -2,6 +2,8 @@ #include +namespace ovum::compiler::parser { + NodeEntry::NodeEntry(std::unique_ptr node) : node_(std::move(node)) { } @@ -20,3 +22,5 @@ void NodeEntry::SetNode(std::unique_ptr node) { std::unique_ptr NodeEntry::ReleaseNode() { return std::move(node_); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/context/NodeEntry.hpp b/lib/parser/context/NodeEntry.hpp index 195b394..0261c30 100644 --- a/lib/parser/context/NodeEntry.hpp +++ b/lib/parser/context/NodeEntry.hpp @@ -1,10 +1,12 @@ -#ifndef NODEENTRY_HPP_ -#define NODEENTRY_HPP_ +#ifndef PARSER_NODEENTRY_HPP_ +#define PARSER_NODEENTRY_HPP_ #include #include "lib/parser/ast/nodes/base/AstNode.hpp" +namespace ovum::compiler::parser { + class NodeEntry { public: NodeEntry() = default; @@ -19,4 +21,6 @@ class NodeEntry { std::unique_ptr node_; }; -#endif // NODEENTRY_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_NODEENTRY_HPP_ diff --git a/lib/parser/diagnostics/Diagnostic.cpp b/lib/parser/diagnostics/Diagnostic.cpp index d6fc397..e33180c 100644 --- a/lib/parser/diagnostics/Diagnostic.cpp +++ b/lib/parser/diagnostics/Diagnostic.cpp @@ -2,17 +2,15 @@ #include +namespace ovum::compiler::parser { + Diagnostic::Diagnostic() = default; Diagnostic::Diagnostic(std::shared_ptr sev, std::string code, std::string message) : severity_(std::move(sev)), code_(std::move(code)), message_(std::move(message)) { } -Diagnostic::Diagnostic(const Diagnostic& other) : - severity_(other.severity_), code_(other.code_), message_(other.message_), category_(other.category_), - where_(other.where_), notes_(other.notes_), fixes_(other.fixes_), is_fatal_(other.is_fatal_), - is_suppressed_(other.is_suppressed_) { -} +Diagnostic::Diagnostic(const Diagnostic& other) = default; Diagnostic::Diagnostic(Diagnostic&& other) noexcept : severity_(std::move(other.severity_)), code_(std::move(other.code_)), message_(std::move(other.message_)), @@ -84,7 +82,7 @@ const std::string& Diagnostic::GetCategory() const noexcept { return category_; } -void Diagnostic::SetWhere(SourceSpan sp) { +void Diagnostic::SetWhere(const SourceSpan& sp) { where_ = sp; } void Diagnostic::ResetWhere() { @@ -127,3 +125,5 @@ void Diagnostic::SetSuppressed(bool on) { bool Diagnostic::IsSuppressed() const noexcept { return is_suppressed_; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/diagnostics/Diagnostic.hpp b/lib/parser/diagnostics/Diagnostic.hpp index 1acc7cf..b067547 100644 --- a/lib/parser/diagnostics/Diagnostic.hpp +++ b/lib/parser/diagnostics/Diagnostic.hpp @@ -1,5 +1,5 @@ -#ifndef DIAGNOSTIC_HPP_ -#define DIAGNOSTIC_HPP_ +#ifndef PARSER_DIAGNOSTIC_HPP_ +#define PARSER_DIAGNOSTIC_HPP_ #include #include @@ -12,6 +12,8 @@ #include "lib/parser/tokens/SourceSpan.hpp" #include "severity/ISeverity.hpp" +namespace ovum::compiler::parser { + class Diagnostic { public: Diagnostic(); @@ -64,4 +66,6 @@ class Diagnostic { bool is_suppressed_ = false; }; -#endif // DIAGNOSTIC_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_DIAGNOSTIC_HPP_ diff --git a/lib/parser/diagnostics/DiagnosticCollector.cpp b/lib/parser/diagnostics/DiagnosticCollector.cpp index f1670f5..43991ff 100644 --- a/lib/parser/diagnostics/DiagnosticCollector.cpp +++ b/lib/parser/diagnostics/DiagnosticCollector.cpp @@ -4,6 +4,8 @@ #include "lib/parser/diagnostics/severity/Severity.hpp" +namespace ovum::compiler::parser { + void DiagnosticCollector::Report(Diagnostic d) { bool suppressed = !ShouldKeep(d); d.SetSuppressed(suppressed); @@ -152,3 +154,5 @@ bool DiagnosticCollector::IsDuplicate(const Diagnostic& d) const { } return false; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/diagnostics/DiagnosticCollector.hpp b/lib/parser/diagnostics/DiagnosticCollector.hpp index 83e7232..9ed4f7c 100644 --- a/lib/parser/diagnostics/DiagnosticCollector.hpp +++ b/lib/parser/diagnostics/DiagnosticCollector.hpp @@ -1,5 +1,5 @@ -#ifndef DIAGNOSTICCOLLECTOR_HPP_ -#define DIAGNOSTICCOLLECTOR_HPP_ +#ifndef PARSER_DIAGNOSTICCOLLECTOR_HPP_ +#define PARSER_DIAGNOSTICCOLLECTOR_HPP_ #include #include @@ -11,6 +11,8 @@ #include "IDiagnosticSink.hpp" +namespace ovum::compiler::parser { + class DiagnosticCollector : public IDiagnosticSink { public: using Predicate = std::function; @@ -60,4 +62,6 @@ class DiagnosticCollector : public IDiagnosticSink { std::size_t warnings_ = 0; }; -#endif // DIAGNOSTICCOLLECTOR_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_DIAGNOSTICCOLLECTOR_HPP_ diff --git a/lib/parser/diagnostics/FixIt.cpp b/lib/parser/diagnostics/FixIt.cpp index 2adb9a7..f110531 100644 --- a/lib/parser/diagnostics/FixIt.cpp +++ b/lib/parser/diagnostics/FixIt.cpp @@ -2,6 +2,10 @@ #include +namespace ovum::compiler::parser { + FixIt::FixIt(SourceSpan source_span, std::string replacement) : - where_(source_span), replacement_(std::move(replacement)) { + where_(std::move(source_span)), replacement_(std::move(replacement)) { } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/diagnostics/FixIt.hpp b/lib/parser/diagnostics/FixIt.hpp index 8f67301..fcd23f2 100644 --- a/lib/parser/diagnostics/FixIt.hpp +++ b/lib/parser/diagnostics/FixIt.hpp @@ -1,10 +1,12 @@ -#ifndef FIXIT_HPP_ -#define FIXIT_HPP_ +#ifndef PARSER_FIXIT_HPP_ +#define PARSER_FIXIT_HPP_ #include #include "lib/parser/tokens/SourceSpan.hpp" +namespace ovum::compiler::parser { + class FixIt { public: FixIt(SourceSpan source_span, std::string replacement); @@ -14,4 +16,6 @@ class FixIt { std::string replacement_; }; -#endif // FIXIT_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_FIXIT_HPP_ diff --git a/lib/parser/diagnostics/IDiagnosticSink.hpp b/lib/parser/diagnostics/IDiagnosticSink.hpp index 57ad10b..a9bfe62 100644 --- a/lib/parser/diagnostics/IDiagnosticSink.hpp +++ b/lib/parser/diagnostics/IDiagnosticSink.hpp @@ -1,5 +1,5 @@ -#ifndef IDIAGNOSTICSINK_HPP_ -#define IDIAGNOSTICSINK_HPP_ +#ifndef PARSER_IDIAGNOSTICSINK_HPP_ +#define PARSER_IDIAGNOSTICSINK_HPP_ #include #include @@ -8,6 +8,8 @@ #include "Diagnostic.hpp" #include "lib/parser/tokens/SourceSpan.hpp" +namespace ovum::compiler::parser { + class IDiagnosticSink { public: virtual ~IDiagnosticSink() = default; @@ -24,4 +26,6 @@ class IDiagnosticSink { virtual void Error(std::string_view code, std::string_view msg, std::optional where = std::nullopt) = 0; }; -#endif // IDIAGNOSTICSINK_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_IDIAGNOSTICSINK_HPP_ diff --git a/lib/parser/diagnostics/RelatedInfo.cpp b/lib/parser/diagnostics/RelatedInfo.cpp index 7449a2e..6878cad 100644 --- a/lib/parser/diagnostics/RelatedInfo.cpp +++ b/lib/parser/diagnostics/RelatedInfo.cpp @@ -2,6 +2,10 @@ #include +namespace ovum::compiler::parser { + RelatedInfo::RelatedInfo(std::string&& message, SourceSpan&& span) : message_(std::move(message)), where_(std::move(span)) { } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/diagnostics/RelatedInfo.hpp b/lib/parser/diagnostics/RelatedInfo.hpp index 8abee5f..7ec6707 100644 --- a/lib/parser/diagnostics/RelatedInfo.hpp +++ b/lib/parser/diagnostics/RelatedInfo.hpp @@ -1,11 +1,13 @@ -#ifndef RELATEDINFO_HPP_ -#define RELATEDINFO_HPP_ +#ifndef PARSER_RELATEDINFO_HPP_ +#define PARSER_RELATEDINFO_HPP_ #include #include #include "lib/parser/tokens/SourceSpan.hpp" +namespace ovum::compiler::parser { + class RelatedInfo { public: RelatedInfo(std::string&& message, SourceSpan&& span); @@ -15,4 +17,6 @@ class RelatedInfo { std::optional where_; }; -#endif // RELATEDINFO_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_RELATEDINFO_HPP_ diff --git a/lib/parser/diagnostics/severity/ISeverity.hpp b/lib/parser/diagnostics/severity/ISeverity.hpp index 5381598..1738dcf 100644 --- a/lib/parser/diagnostics/severity/ISeverity.hpp +++ b/lib/parser/diagnostics/severity/ISeverity.hpp @@ -1,8 +1,10 @@ -#ifndef ISEVERITY_HPP_ -#define ISEVERITY_HPP_ +#ifndef PARSER_ISEVERITY_HPP_ +#define PARSER_ISEVERITY_HPP_ #include +namespace ovum::compiler::parser { + class ISeverity { public: virtual ~ISeverity() = default; @@ -10,4 +12,6 @@ class ISeverity { virtual int Level() const = 0; }; -#endif // ISEVERITY_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_ISEVERITY_HPP_ diff --git a/lib/parser/diagnostics/severity/Severity.cpp b/lib/parser/diagnostics/severity/Severity.cpp index eba7c4c..860ef58 100644 --- a/lib/parser/diagnostics/severity/Severity.cpp +++ b/lib/parser/diagnostics/severity/Severity.cpp @@ -5,17 +5,19 @@ #include #include +namespace ovum::compiler::parser { + namespace { class SimpleSeverity : public ISeverity { public: SimpleSeverity(std::string name, int level) : name_(std::move(name)), level_(level) { } - std::string_view Name() const override { + [[nodiscard]] std::string_view Name() const override { return name_; } - int Level() const override { + [[nodiscard]] int Level() const override { return level_; } @@ -26,20 +28,22 @@ class SimpleSeverity : public ISeverity { } // namespace const std::shared_ptr& Severity::Note() { - static const auto inst = std::make_shared("note", 10); - return inst; + static const auto kInst = std::make_shared("note", 10); + return kInst; } const std::shared_ptr& Severity::Warning() { - static const auto inst = std::make_shared("warning", 20); - return inst; + static const auto kInst = std::make_shared("warning", 20); + return kInst; } const std::shared_ptr& Severity::Error() { - static const auto inst = std::make_shared("error", 30); - return inst; + static const auto kInst = std::make_shared("error", 30); + return kInst; } std::shared_ptr Severity::Custom(std::string_view name, int level) { return std::make_shared(std::string{name}, level); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/diagnostics/severity/Severity.hpp b/lib/parser/diagnostics/severity/Severity.hpp index 9fe5ab8..c7bfa8b 100644 --- a/lib/parser/diagnostics/severity/Severity.hpp +++ b/lib/parser/diagnostics/severity/Severity.hpp @@ -1,11 +1,13 @@ -#ifndef SEVERITY_HPP_ -#define SEVERITY_HPP_ +#ifndef PARSER_SEVERITY_HPP_ +#define PARSER_SEVERITY_HPP_ #include #include #include "ISeverity.hpp" +namespace ovum::compiler::parser { + class Severity { public: static const std::shared_ptr& Note(); @@ -15,4 +17,6 @@ class Severity { static std::shared_ptr Custom(std::string_view name, int level); }; -#endif // SEVERITY_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_SEVERITY_HPP_ diff --git a/lib/parser/pratt/DefaultOperatorResolver.cpp b/lib/parser/pratt/DefaultOperatorResolver.cpp index 0b8d708..78ad1df 100644 --- a/lib/parser/pratt/DefaultOperatorResolver.cpp +++ b/lib/parser/pratt/DefaultOperatorResolver.cpp @@ -1 +1,3 @@ #include "DefaultOperatorResolver.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/pratt/DefaultOperatorResolver.hpp b/lib/parser/pratt/DefaultOperatorResolver.hpp index f46cfc2..1e64948 100644 --- a/lib/parser/pratt/DefaultOperatorResolver.hpp +++ b/lib/parser/pratt/DefaultOperatorResolver.hpp @@ -1,5 +1,5 @@ -#ifndef DEFAULTOPERATORRESOLVER_HPP_ -#define DEFAULTOPERATORRESOLVER_HPP_ +#ifndef PARSER_DEFAULTOPERATORRESOLVER_HPP_ +#define PARSER_DEFAULTOPERATORRESOLVER_HPP_ #include #include @@ -10,6 +10,8 @@ #include "specifications/InfixSpec.hpp" #include "specifications/PostfixSpec.hpp" +namespace ovum::compiler::parser { + class DefaultOperatorResolver : public IOperatorResolver { public: ~DefaultOperatorResolver() override = default; @@ -24,4 +26,6 @@ class DefaultOperatorResolver : public IOperatorResolver { std::vector postfix_; }; -#endif // DEFAULTOPERATORRESOLVER_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_DEFAULTOPERATORRESOLVER_HPP_ diff --git a/lib/parser/pratt/IExpressionParser.hpp b/lib/parser/pratt/IExpressionParser.hpp index 4aa1134..4cdc336 100644 --- a/lib/parser/pratt/IExpressionParser.hpp +++ b/lib/parser/pratt/IExpressionParser.hpp @@ -1,5 +1,5 @@ -#ifndef IEXPRESSIONPARSER_HPP_ -#define IEXPRESSIONPARSER_HPP_ +#ifndef PARSER_IEXPRESSIONPARSER_HPP_ +#define PARSER_IEXPRESSIONPARSER_HPP_ #include @@ -7,6 +7,8 @@ #include "lib/parser/diagnostics/IDiagnosticSink.hpp" #include "lib/parser/tokens/token_streams/ITokenStream.hpp" +namespace ovum::compiler::parser { + class IExpressionParser { public: virtual ~IExpressionParser() = default; @@ -14,4 +16,6 @@ class IExpressionParser { virtual std::unique_ptr Parse(ITokenStream& ts, IDiagnosticSink& diags) = 0; }; -#endif // IEXPRESSIONPARSER_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_IEXPRESSIONPARSER_HPP_ diff --git a/lib/parser/pratt/IOperatorResolver.hpp b/lib/parser/pratt/IOperatorResolver.hpp index a210cb2..ffb761a 100644 --- a/lib/parser/pratt/IOperatorResolver.hpp +++ b/lib/parser/pratt/IOperatorResolver.hpp @@ -1,9 +1,11 @@ -#ifndef IOPERATORRESOLVER_HPP_ -#define IOPERATORRESOLVER_HPP_ +#ifndef PARSER_IOPERATORRESOLVER_HPP_ +#define PARSER_IOPERATORRESOLVER_HPP_ #include #include +namespace ovum::compiler::parser { + class Token; class IUnaryOpTag; class InfixSpec; @@ -19,4 +21,6 @@ class IOperatorResolver { virtual bool IsContinuation(const Token& t) const = 0; }; -#endif // IOPERATORRESOLVER_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_IOPERATORRESOLVER_HPP_ diff --git a/lib/parser/pratt/PrattExpressionParser.cpp b/lib/parser/pratt/PrattExpressionParser.cpp index 4ea1b64..f63be8b 100644 --- a/lib/parser/pratt/PrattExpressionParser.cpp +++ b/lib/parser/pratt/PrattExpressionParser.cpp @@ -1 +1,3 @@ #include "PrattExpressionParser.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/pratt/PrattExpressionParser.hpp b/lib/parser/pratt/PrattExpressionParser.hpp index b183b6c..daadd0b 100644 --- a/lib/parser/pratt/PrattExpressionParser.hpp +++ b/lib/parser/pratt/PrattExpressionParser.hpp @@ -1,5 +1,5 @@ -#ifndef PRATTEXPRESSIONPARSER_HPP_ -#define PRATTEXPRESSIONPARSER_HPP_ +#ifndef PARSER_PRATTEXPRESSIONPARSER_HPP_ +#define PARSER_PRATTEXPRESSIONPARSER_HPP_ #include #include @@ -11,6 +11,8 @@ #include "lib/parser/diagnostics/IDiagnosticSink.hpp" #include "lib/parser/tokens/token_streams/ITokenStream.hpp" +namespace ovum::compiler::parser { + class PrattExpressionParser : public IExpressionParser { public: explicit PrattExpressionParser(std::unique_ptr resolver, std::shared_ptr factory); @@ -31,4 +33,6 @@ class PrattExpressionParser : public IExpressionParser { std::shared_ptr factory_; }; -#endif // PRATTEXPRESSIONPARSER_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_PRATTEXPRESSIONPARSER_HPP_ diff --git a/lib/parser/pratt/specifications/InfixSpec.cpp b/lib/parser/pratt/specifications/InfixSpec.cpp index aa3ca96..9b3f161 100644 --- a/lib/parser/pratt/specifications/InfixSpec.cpp +++ b/lib/parser/pratt/specifications/InfixSpec.cpp @@ -1 +1,3 @@ #include "InfixSpec.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/pratt/specifications/InfixSpec.hpp b/lib/parser/pratt/specifications/InfixSpec.hpp index f667581..c2d2b8c 100644 --- a/lib/parser/pratt/specifications/InfixSpec.hpp +++ b/lib/parser/pratt/specifications/InfixSpec.hpp @@ -1,11 +1,14 @@ -#ifndef INFIXSPEC_HPP_ -#define INFIXSPEC_HPP_ +#ifndef PARSER_INFIXSPEC_HPP_ +#define PARSER_INFIXSPEC_HPP_ #include -#include "lib/lexer/tokens/Token.hpp" +#include + #include "lib/parser/ast/nodes/exprs/tags/IBinaryOpTag.hpp" +namespace ovum::compiler::parser { + class InfixSpec { public: InfixSpec(int lbp, int rbp, bool right_associative, IBinaryOpTag* tag, bool is_elvis); @@ -19,4 +22,6 @@ class InfixSpec { bool is_elvis_ = false; }; -#endif // INFIXSPEC_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_INFIXSPEC_HPP_ diff --git a/lib/parser/pratt/specifications/PostfixSpec.cpp b/lib/parser/pratt/specifications/PostfixSpec.cpp index e0e3efd..ebbc6b2 100644 --- a/lib/parser/pratt/specifications/PostfixSpec.cpp +++ b/lib/parser/pratt/specifications/PostfixSpec.cpp @@ -1 +1,3 @@ #include "PostfixSpec.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/pratt/specifications/PostfixSpec.hpp b/lib/parser/pratt/specifications/PostfixSpec.hpp index 922db19..380137c 100644 --- a/lib/parser/pratt/specifications/PostfixSpec.hpp +++ b/lib/parser/pratt/specifications/PostfixSpec.hpp @@ -1,9 +1,11 @@ -#ifndef POSTFIXSPEC_HPP_ -#define POSTFIXSPEC_HPP_ +#ifndef PARSER_POSTFIXSPEC_HPP_ +#define PARSER_POSTFIXSPEC_HPP_ #include -#include "lib/lexer/tokens/Token.hpp" +#include + +namespace ovum::compiler::parser { class PostfixSpec { public: @@ -15,4 +17,6 @@ class PostfixSpec { bool keyword = false; }; -#endif // POSTFIXSPEC_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_POSTFIXSPEC_HPP_ diff --git a/lib/parser/recovery/IRecoveryStrategy.hpp b/lib/parser/recovery/IRecoveryStrategy.hpp index ad75813..8df9f3e 100644 --- a/lib/parser/recovery/IRecoveryStrategy.hpp +++ b/lib/parser/recovery/IRecoveryStrategy.hpp @@ -1,8 +1,10 @@ -#ifndef IRECOVERYSTRATEGY_HPP_ -#define IRECOVERYSTRATEGY_HPP_ +#ifndef PARSER_IRECOVERYSTRATEGY_HPP_ +#define PARSER_IRECOVERYSTRATEGY_HPP_ #include "lib/parser/tokens/token_streams/ITokenStream.hpp" +namespace ovum::compiler::parser { + class IRecoveryStrategy { public: virtual ~IRecoveryStrategy() { @@ -11,4 +13,6 @@ class IRecoveryStrategy { virtual void SyncToBlockEnd(ITokenStream& ts) = 0; }; -#endif // IRECOVERYSTRATEGY_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_IRECOVERYSTRATEGY_HPP_ diff --git a/lib/parser/recovery/SimpleRecovery.cpp b/lib/parser/recovery/SimpleRecovery.cpp index a5ad7a5..b942901 100644 --- a/lib/parser/recovery/SimpleRecovery.cpp +++ b/lib/parser/recovery/SimpleRecovery.cpp @@ -1 +1,3 @@ #include "SimpleRecovery.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/recovery/SimpleRecovery.hpp b/lib/parser/recovery/SimpleRecovery.hpp index 3cf9688..f59f4fa 100644 --- a/lib/parser/recovery/SimpleRecovery.hpp +++ b/lib/parser/recovery/SimpleRecovery.hpp @@ -1,8 +1,10 @@ -#ifndef SIMPLERECOVERY_HPP_ -#define SIMPLERECOVERY_HPP_ +#ifndef PARSER_SIMPLERECOVERY_HPP_ +#define PARSER_SIMPLERECOVERY_HPP_ #include "IRecoveryStrategy.hpp" +namespace ovum::compiler::parser { + class SimpleRecovery : IRecoveryStrategy { public: ~SimpleRecovery() override = default; @@ -11,4 +13,6 @@ class SimpleRecovery : IRecoveryStrategy { void SyncToBlockEnd(ITokenStream& ts) override; }; -#endif // SIMPLERECOVERY_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_SIMPLERECOVERY_HPP_ diff --git a/lib/parser/states/StateBlock.cpp b/lib/parser/states/StateBlock.cpp index 279fa35..39f5929 100644 --- a/lib/parser/states/StateBlock.cpp +++ b/lib/parser/states/StateBlock.cpp @@ -1 +1,3 @@ #include "StateBlock.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateBlock.hpp b/lib/parser/states/StateBlock.hpp index 8cde812..c132b65 100644 --- a/lib/parser/states/StateBlock.hpp +++ b/lib/parser/states/StateBlock.hpp @@ -1,13 +1,17 @@ -#ifndef STATEBLOCK_HPP_ -#define STATEBLOCK_HPP_ +#ifndef PARSER_STATEBLOCK_HPP_ +#define PARSER_STATEBLOCK_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateBlock : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEBLOCK_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEBLOCK_HPP_ diff --git a/lib/parser/states/StateCallDeclHdr.cpp b/lib/parser/states/StateCallDeclHdr.cpp index 7d1ae4a..bef8fc0 100644 --- a/lib/parser/states/StateCallDeclHdr.cpp +++ b/lib/parser/states/StateCallDeclHdr.cpp @@ -1 +1,3 @@ #include "StateCallDeclHdr.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateCallDeclHdr.hpp b/lib/parser/states/StateCallDeclHdr.hpp index 3229de2..f1025be 100644 --- a/lib/parser/states/StateCallDeclHdr.hpp +++ b/lib/parser/states/StateCallDeclHdr.hpp @@ -1,13 +1,17 @@ -#ifndef STATECALLDECLHDR_HPP_ -#define STATECALLDECLHDR_HPP_ +#ifndef PARSER_STATECALLDECLHDR_HPP_ +#define PARSER_STATECALLDECLHDR_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateCallDeclHdr : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATECALLDECLHDR_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATECALLDECLHDR_HPP_ diff --git a/lib/parser/states/StateDestructorDecl.cpp b/lib/parser/states/StateDestructorDecl.cpp index 0972e30..d054df6 100644 --- a/lib/parser/states/StateDestructorDecl.cpp +++ b/lib/parser/states/StateDestructorDecl.cpp @@ -1 +1,3 @@ #include "StateDestructorDecl.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateDestructorDecl.hpp b/lib/parser/states/StateDestructorDecl.hpp index 0335977..805c7c0 100644 --- a/lib/parser/states/StateDestructorDecl.hpp +++ b/lib/parser/states/StateDestructorDecl.hpp @@ -1,13 +1,17 @@ -#ifndef STATEDESTRUCTORDECL_HPP_ -#define STATEDESTRUCTORDECL_HPP_ +#ifndef PARSER_STATEDESTRUCTORDECL_HPP_ +#define PARSER_STATEDESTRUCTORDECL_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateDestructorDecl : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEDESTRUCTORDECL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEDESTRUCTORDECL_HPP_ diff --git a/lib/parser/states/StateExpr.cpp b/lib/parser/states/StateExpr.cpp index 811a990..732bb7e 100644 --- a/lib/parser/states/StateExpr.cpp +++ b/lib/parser/states/StateExpr.cpp @@ -1 +1,3 @@ #include "StateExpr.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateExpr.hpp b/lib/parser/states/StateExpr.hpp index 01e6d98..f520915 100644 --- a/lib/parser/states/StateExpr.hpp +++ b/lib/parser/states/StateExpr.hpp @@ -1,13 +1,17 @@ -#ifndef STATEEXPR_HPP_ -#define STATEEXPR_HPP_ +#ifndef PARSER_STATEEXPR_HPP_ +#define PARSER_STATEEXPR_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateExpr : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEEXPR_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEEXPR_HPP_ diff --git a/lib/parser/states/StateFieldDecl.cpp b/lib/parser/states/StateFieldDecl.cpp index b0c7a3f..ad88b6e 100644 --- a/lib/parser/states/StateFieldDecl.cpp +++ b/lib/parser/states/StateFieldDecl.cpp @@ -1 +1,3 @@ #include "StateFieldDecl.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateFieldDecl.hpp b/lib/parser/states/StateFieldDecl.hpp index 52eba65..672c16f 100644 --- a/lib/parser/states/StateFieldDecl.hpp +++ b/lib/parser/states/StateFieldDecl.hpp @@ -1,13 +1,17 @@ -#ifndef STATEFIELDDECL_HPP_ -#define STATEFIELDDECL_HPP_ +#ifndef PARSER_STATEFIELDDECL_HPP_ +#define PARSER_STATEFIELDDECL_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateFieldDecl : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEFIELDDECL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEFIELDDECL_HPP_ diff --git a/lib/parser/states/StateForHead.cpp b/lib/parser/states/StateForHead.cpp index 8548873..cae61dc 100644 --- a/lib/parser/states/StateForHead.cpp +++ b/lib/parser/states/StateForHead.cpp @@ -1 +1,3 @@ #include "StateForHead.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateForHead.hpp b/lib/parser/states/StateForHead.hpp index 8e23608..6df6546 100644 --- a/lib/parser/states/StateForHead.hpp +++ b/lib/parser/states/StateForHead.hpp @@ -1,13 +1,17 @@ -#ifndef STATEFORHEAD_HPP_ -#define STATEFORHEAD_HPP_ +#ifndef PARSER_STATEFORHEAD_HPP_ +#define PARSER_STATEFORHEAD_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateForHead : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEFORHEAD_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEFORHEAD_HPP_ diff --git a/lib/parser/states/StateGlobalVarDecl.cpp b/lib/parser/states/StateGlobalVarDecl.cpp index 53f53e3..767bacd 100644 --- a/lib/parser/states/StateGlobalVarDecl.cpp +++ b/lib/parser/states/StateGlobalVarDecl.cpp @@ -1 +1,3 @@ #include "StateGlobalVarDecl.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateGlobalVarDecl.hpp b/lib/parser/states/StateGlobalVarDecl.hpp index dfb15c1..ecd860a 100644 --- a/lib/parser/states/StateGlobalVarDecl.hpp +++ b/lib/parser/states/StateGlobalVarDecl.hpp @@ -1,13 +1,17 @@ -#ifndef STATEGLOBALVARDECL_HPP_ -#define STATEGLOBALVARDECL_HPP_ +#ifndef PARSER_STATEGLOBALVARDECL_HPP_ +#define PARSER_STATEGLOBALVARDECL_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateGlobalVarDecl : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEGLOBALVARDECL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEGLOBALVARDECL_HPP_ diff --git a/lib/parser/states/StateIfHead.cpp b/lib/parser/states/StateIfHead.cpp index c365675..250ddad 100644 --- a/lib/parser/states/StateIfHead.cpp +++ b/lib/parser/states/StateIfHead.cpp @@ -1 +1,3 @@ #include "StateIfHead.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateIfHead.hpp b/lib/parser/states/StateIfHead.hpp index 3edd58d..9ef894d 100644 --- a/lib/parser/states/StateIfHead.hpp +++ b/lib/parser/states/StateIfHead.hpp @@ -1,13 +1,17 @@ -#ifndef STATEIFHEAD_HPP_ -#define STATEIFHEAD_HPP_ +#ifndef PARSER_STATEIFHEAD_HPP_ +#define PARSER_STATEIFHEAD_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateIfHead : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEIFHEAD_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEIFHEAD_HPP_ diff --git a/lib/parser/states/StateIfTail.cpp b/lib/parser/states/StateIfTail.cpp index b849664..0025258 100644 --- a/lib/parser/states/StateIfTail.cpp +++ b/lib/parser/states/StateIfTail.cpp @@ -1 +1,3 @@ #include "StateIfTail.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateIfTail.hpp b/lib/parser/states/StateIfTail.hpp index 93a3e6b..e1b2ef2 100644 --- a/lib/parser/states/StateIfTail.hpp +++ b/lib/parser/states/StateIfTail.hpp @@ -1,13 +1,17 @@ -#ifndef STATEIFTAIL_HPP_ -#define STATEIFTAIL_HPP_ +#ifndef PARSER_STATEIFTAIL_HPP_ +#define PARSER_STATEIFTAIL_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateIfTail : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEIFTAIL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEIFTAIL_HPP_ diff --git a/lib/parser/states/StateMethodHdr.cpp b/lib/parser/states/StateMethodHdr.cpp index 414d0d8..a896051 100644 --- a/lib/parser/states/StateMethodHdr.cpp +++ b/lib/parser/states/StateMethodHdr.cpp @@ -1 +1,3 @@ #include "StateMethodHdr.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateMethodHdr.hpp b/lib/parser/states/StateMethodHdr.hpp index e2b19a3..0584002 100644 --- a/lib/parser/states/StateMethodHdr.hpp +++ b/lib/parser/states/StateMethodHdr.hpp @@ -1,13 +1,17 @@ -#ifndef STATEMETHODHDR_HPP_ -#define STATEMETHODHDR_HPP_ +#ifndef PARSER_STATEMETHODHDR_HPP_ +#define PARSER_STATEMETHODHDR_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateMethodHdr : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEMETHODHDR_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEMETHODHDR_HPP_ diff --git a/lib/parser/states/StateModule.cpp b/lib/parser/states/StateModule.cpp index 25c44a5..fad4833 100644 --- a/lib/parser/states/StateModule.cpp +++ b/lib/parser/states/StateModule.cpp @@ -1 +1,3 @@ #include "StateModule.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateModule.hpp b/lib/parser/states/StateModule.hpp index 0293bc2..6d05277 100644 --- a/lib/parser/states/StateModule.hpp +++ b/lib/parser/states/StateModule.hpp @@ -1,13 +1,17 @@ -#ifndef STATEMODULE_HPP_ -#define STATEMODULE_HPP_ +#ifndef PARSER_STATEMODULE_HPP_ +#define PARSER_STATEMODULE_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateModule : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEMODULE_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEMODULE_HPP_ diff --git a/lib/parser/states/StateParseType.cpp b/lib/parser/states/StateParseType.cpp index 2e91214..0cb32e0 100644 --- a/lib/parser/states/StateParseType.cpp +++ b/lib/parser/states/StateParseType.cpp @@ -1 +1,3 @@ #include "StateParseType.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateParseType.hpp b/lib/parser/states/StateParseType.hpp index bfb6223..f4ad9ac 100644 --- a/lib/parser/states/StateParseType.hpp +++ b/lib/parser/states/StateParseType.hpp @@ -1,13 +1,17 @@ -#ifndef STATEPARSETYPE_HPP_ -#define STATEPARSETYPE_HPP_ +#ifndef PARSER_STATEPARSETYPE_HPP_ +#define PARSER_STATEPARSETYPE_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateParseType : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEPARSETYPE_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEPARSETYPE_HPP_ diff --git a/lib/parser/states/StateReturnTail.cpp b/lib/parser/states/StateReturnTail.cpp index 228c916..b74db84 100644 --- a/lib/parser/states/StateReturnTail.cpp +++ b/lib/parser/states/StateReturnTail.cpp @@ -1 +1,3 @@ #include "StateReturnTail.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateReturnTail.hpp b/lib/parser/states/StateReturnTail.hpp index 942a58d..9fda688 100644 --- a/lib/parser/states/StateReturnTail.hpp +++ b/lib/parser/states/StateReturnTail.hpp @@ -1,13 +1,17 @@ -#ifndef STATERETURNTAIL_HPP_ -#define STATERETURNTAIL_HPP_ +#ifndef PARSER_STATERETURNTAIL_HPP_ +#define PARSER_STATERETURNTAIL_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateReturnTail : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATERETURNTAIL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATERETURNTAIL_HPP_ diff --git a/lib/parser/states/StateStmt.cpp b/lib/parser/states/StateStmt.cpp index df33c70..5be5521 100644 --- a/lib/parser/states/StateStmt.cpp +++ b/lib/parser/states/StateStmt.cpp @@ -1 +1,3 @@ #include "StateStmt.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateStmt.hpp b/lib/parser/states/StateStmt.hpp index 7e4970b..0bb7d88 100644 --- a/lib/parser/states/StateStmt.hpp +++ b/lib/parser/states/StateStmt.hpp @@ -1,13 +1,17 @@ -#ifndef STATESTMT_HPP_ -#define STATESTMT_HPP_ +#ifndef PARSER_STATESTMT_HPP_ +#define PARSER_STATESTMT_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateStmt : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATESTMT_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATESTMT_HPP_ diff --git a/lib/parser/states/StateSyncToBlockEnd.cpp b/lib/parser/states/StateSyncToBlockEnd.cpp index aaddd56..c68ba42 100644 --- a/lib/parser/states/StateSyncToBlockEnd.cpp +++ b/lib/parser/states/StateSyncToBlockEnd.cpp @@ -1 +1,3 @@ #include "StateSyncToBlockEnd.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateSyncToBlockEnd.hpp b/lib/parser/states/StateSyncToBlockEnd.hpp index c9736f0..d0aa0a2 100644 --- a/lib/parser/states/StateSyncToBlockEnd.hpp +++ b/lib/parser/states/StateSyncToBlockEnd.hpp @@ -1,13 +1,17 @@ -#ifndef STATESYNCTOBLOCKEND_HPP_ -#define STATESYNCTOBLOCKEND_HPP_ +#ifndef PARSER_STATESYNCTOBLOCKEND_HPP_ +#define PARSER_STATESYNCTOBLOCKEND_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateSyncToBlockEnd : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATESYNCTOBLOCKEND_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATESYNCTOBLOCKEND_HPP_ diff --git a/lib/parser/states/StateSyncToStmtEnd.cpp b/lib/parser/states/StateSyncToStmtEnd.cpp index caee1ca..edbb355 100644 --- a/lib/parser/states/StateSyncToStmtEnd.cpp +++ b/lib/parser/states/StateSyncToStmtEnd.cpp @@ -1 +1,3 @@ #include "StateSyncToStmtEnd.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateSyncToStmtEnd.hpp b/lib/parser/states/StateSyncToStmtEnd.hpp index f585bfc..9de6e54 100644 --- a/lib/parser/states/StateSyncToStmtEnd.hpp +++ b/lib/parser/states/StateSyncToStmtEnd.hpp @@ -1,13 +1,17 @@ -#ifndef STATESYNCTOSTMTEND_HPP_ -#define STATESYNCTOSTMTEND_HPP_ +#ifndef PARSER_STATESYNCTOSTMTEND_HPP_ +#define PARSER_STATESYNCTOSTMTEND_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateSyncToStmtEnd : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATESYNCTOSTMTEND_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATESYNCTOSTMTEND_HPP_ diff --git a/lib/parser/states/StateTopDecl.cpp b/lib/parser/states/StateTopDecl.cpp index e7c08da..a977e77 100644 --- a/lib/parser/states/StateTopDecl.cpp +++ b/lib/parser/states/StateTopDecl.cpp @@ -1 +1,3 @@ #include "StateTopDecl.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateTopDecl.hpp b/lib/parser/states/StateTopDecl.hpp index 28ed858..3731c46 100644 --- a/lib/parser/states/StateTopDecl.hpp +++ b/lib/parser/states/StateTopDecl.hpp @@ -1,13 +1,17 @@ -#ifndef STATETOPDECL_HPP_ -#define STATETOPDECL_HPP_ +#ifndef PARSER_STATETOPDECL_HPP_ +#define PARSER_STATETOPDECL_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateTopDecl : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATETOPDECL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATETOPDECL_HPP_ diff --git a/lib/parser/states/StateTypeAliasDecl.cpp b/lib/parser/states/StateTypeAliasDecl.cpp index 9510c96..8030979 100644 --- a/lib/parser/states/StateTypeAliasDecl.cpp +++ b/lib/parser/states/StateTypeAliasDecl.cpp @@ -1 +1,3 @@ #include "StateTypeAliasDecl.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateTypeAliasDecl.hpp b/lib/parser/states/StateTypeAliasDecl.hpp index c6cc2bf..72ad630 100644 --- a/lib/parser/states/StateTypeAliasDecl.hpp +++ b/lib/parser/states/StateTypeAliasDecl.hpp @@ -1,13 +1,17 @@ -#ifndef STATETYPEALIASDECL_HPP_ -#define STATETYPEALIASDECL_HPP_ +#ifndef PARSER_STATETYPEALIASDECL_HPP_ +#define PARSER_STATETYPEALIASDECL_HPP_ #include "lib/parser/states/base/StateBase.hpp" #include "lib/parser/tokens/token_streams/ITokenStream.hpp" +namespace ovum::compiler::parser { + class StateTypeAliasDecl : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATETYPEALIASDECL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATETYPEALIASDECL_HPP_ diff --git a/lib/parser/states/StateUnsafeBlock.cpp b/lib/parser/states/StateUnsafeBlock.cpp index 118a37a..9ca09da 100644 --- a/lib/parser/states/StateUnsafeBlock.cpp +++ b/lib/parser/states/StateUnsafeBlock.cpp @@ -1 +1,3 @@ #include "StateUnsafeBlock.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateUnsafeBlock.hpp b/lib/parser/states/StateUnsafeBlock.hpp index 469c8c0..931773e 100644 --- a/lib/parser/states/StateUnsafeBlock.hpp +++ b/lib/parser/states/StateUnsafeBlock.hpp @@ -1,13 +1,17 @@ -#ifndef STATEUNSAFEBLOCK_HPP_ -#define STATEUNSAFEBLOCK_HPP_ +#ifndef PARSER_STATEUNSAFEBLOCK_HPP_ +#define PARSER_STATEUNSAFEBLOCK_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateUnsafeBlock : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEUNSAFEBLOCK_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEUNSAFEBLOCK_HPP_ diff --git a/lib/parser/states/StateVarDeclTail.cpp b/lib/parser/states/StateVarDeclTail.cpp index 68f85c5..41f64d5 100644 --- a/lib/parser/states/StateVarDeclTail.cpp +++ b/lib/parser/states/StateVarDeclTail.cpp @@ -1 +1,3 @@ #include "StateVarDeclTail.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateVarDeclTail.hpp b/lib/parser/states/StateVarDeclTail.hpp index 51f4c80..d81d64e 100644 --- a/lib/parser/states/StateVarDeclTail.hpp +++ b/lib/parser/states/StateVarDeclTail.hpp @@ -1,13 +1,17 @@ -#ifndef STATEVARDECLTAIL_HPP_ -#define STATEVARDECLTAIL_HPP_ +#ifndef PARSER_STATEVARDECLTAIL_HPP_ +#define PARSER_STATEVARDECLTAIL_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateVarDeclTail : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEVARDECLTAIL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEVARDECLTAIL_HPP_ diff --git a/lib/parser/states/StateWhileHead.cpp b/lib/parser/states/StateWhileHead.cpp index bdc5f9b..ff319f1 100644 --- a/lib/parser/states/StateWhileHead.cpp +++ b/lib/parser/states/StateWhileHead.cpp @@ -1 +1,3 @@ #include "StateWhileHead.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/StateWhileHead.hpp b/lib/parser/states/StateWhileHead.hpp index 94deceb..685924f 100644 --- a/lib/parser/states/StateWhileHead.hpp +++ b/lib/parser/states/StateWhileHead.hpp @@ -1,13 +1,17 @@ -#ifndef STATEWHILEHEAD_HPP_ -#define STATEWHILEHEAD_HPP_ +#ifndef PARSER_STATEWHILEHEAD_HPP_ +#define PARSER_STATEWHILEHEAD_HPP_ #include "base/StateBase.hpp" #include "lib/parser/context/ContextParser.hpp" +namespace ovum::compiler::parser { + class StateWhileHead : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEWHILEHEAD_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEWHILEHEAD_HPP_ diff --git a/lib/parser/states/base/IState.hpp b/lib/parser/states/base/IState.hpp index 5f1e559..a077b10 100644 --- a/lib/parser/states/base/IState.hpp +++ b/lib/parser/states/base/IState.hpp @@ -1,11 +1,13 @@ -#ifndef ISTATE_HPP_ -#define ISTATE_HPP_ +#ifndef PARSER_ISTATE_HPP_ +#define PARSER_ISTATE_HPP_ #include #include "StateError.hpp" #include "lib/parser/tokens/token_streams/ITokenStream.hpp" +namespace ovum::compiler::parser { + class ContextParser; // forward class IState { @@ -17,4 +19,6 @@ class IState { virtual StepResult TryStep(ContextParser& ctx, ITokenStream& ts) = 0; }; -#endif // ISTATE_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_ISTATE_HPP_ diff --git a/lib/parser/states/base/StateBase.cpp b/lib/parser/states/base/StateBase.cpp index fc5ccc2..fd524e4 100644 --- a/lib/parser/states/base/StateBase.cpp +++ b/lib/parser/states/base/StateBase.cpp @@ -1 +1,3 @@ #include "StateBase.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/base/StateBase.hpp b/lib/parser/states/base/StateBase.hpp index 3d98892..cee25f8 100644 --- a/lib/parser/states/base/StateBase.hpp +++ b/lib/parser/states/base/StateBase.hpp @@ -1,9 +1,11 @@ -#ifndef STATEBASE_HPP_ -#define STATEBASE_HPP_ +#ifndef PARSER_STATEBASE_HPP_ +#define PARSER_STATEBASE_HPP_ #include "IState.hpp" #include "lib/parser/tokens/SourceSpan.hpp" +namespace ovum::compiler::parser { + class StateBase : IState { public: using StepResult = StepResult; @@ -12,4 +14,6 @@ class StateBase : IState { static SourceSpan Union(const SourceSpan& lhs, const SourceSpan& rhs); }; -#endif // STATEBASE_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEBASE_HPP_ diff --git a/lib/parser/states/base/StateError.cpp b/lib/parser/states/base/StateError.cpp index 384ace5..9aae614 100644 --- a/lib/parser/states/base/StateError.cpp +++ b/lib/parser/states/base/StateError.cpp @@ -1 +1,3 @@ #include "StateError.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/base/StateError.hpp b/lib/parser/states/base/StateError.hpp index 997bbbc..a751e38 100644 --- a/lib/parser/states/base/StateError.hpp +++ b/lib/parser/states/base/StateError.hpp @@ -1,8 +1,10 @@ -#ifndef STATEERROR_HPP_ -#define STATEERROR_HPP_ +#ifndef PARSER_STATEERROR_HPP_ +#define PARSER_STATEERROR_HPP_ #include +namespace ovum::compiler::parser { + class StateError { public: StateError(); @@ -13,4 +15,6 @@ class StateError { std::string message_; }; -#endif // STATEERROR_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEERROR_HPP_ diff --git a/lib/parser/states/base/StateRegistry.cpp b/lib/parser/states/base/StateRegistry.cpp index aa6279d..870dc88 100644 --- a/lib/parser/states/base/StateRegistry.cpp +++ b/lib/parser/states/base/StateRegistry.cpp @@ -1 +1,3 @@ #include "StateRegistry.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/base/StateRegistry.hpp b/lib/parser/states/base/StateRegistry.hpp index 4deb850..a5368e3 100644 --- a/lib/parser/states/base/StateRegistry.hpp +++ b/lib/parser/states/base/StateRegistry.hpp @@ -1,8 +1,10 @@ -#ifndef STATEREGISTRY_HPP_ -#define STATEREGISTRY_HPP_ +#ifndef PARSER_STATEREGISTRY_HPP_ +#define PARSER_STATEREGISTRY_HPP_ #include "IState.hpp" +namespace ovum::compiler::parser { + class StateRegistry { static const IState& Module(); static const IState& TopDecl(); @@ -42,4 +44,6 @@ class StateRegistry { static const IState& SyncToBlockEnd(); }; -#endif // STATEREGISTRY_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEREGISTRY_HPP_ diff --git a/lib/parser/states/class/StateClassBody.cpp b/lib/parser/states/class/StateClassBody.cpp index 818ced4..32f31bf 100644 --- a/lib/parser/states/class/StateClassBody.cpp +++ b/lib/parser/states/class/StateClassBody.cpp @@ -1 +1,3 @@ #include "StateClassBody.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/class/StateClassBody.hpp b/lib/parser/states/class/StateClassBody.hpp index b7faa38..f5780ae 100644 --- a/lib/parser/states/class/StateClassBody.hpp +++ b/lib/parser/states/class/StateClassBody.hpp @@ -1,13 +1,17 @@ -#ifndef STATECLASSBODY_HPP_ -#define STATECLASSBODY_HPP_ +#ifndef PARSER_STATECLASSBODY_HPP_ +#define PARSER_STATECLASSBODY_HPP_ #include "lib/parser/states/base/StateBase.hpp" #include "lib/parser/tokens/token_streams/ITokenStream.hpp" +namespace ovum::compiler::parser { + class StateClassBody : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATECLASSBODY_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATECLASSBODY_HPP_ diff --git a/lib/parser/states/class/StateClassHdr.cpp b/lib/parser/states/class/StateClassHdr.cpp index 62189e1..8926460 100644 --- a/lib/parser/states/class/StateClassHdr.cpp +++ b/lib/parser/states/class/StateClassHdr.cpp @@ -1 +1,3 @@ #include "StateClassHdr.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/class/StateClassHdr.hpp b/lib/parser/states/class/StateClassHdr.hpp index eb00f41..cc6342f 100644 --- a/lib/parser/states/class/StateClassHdr.hpp +++ b/lib/parser/states/class/StateClassHdr.hpp @@ -1,13 +1,17 @@ -#ifndef STATECLASSHDR_HPP_ -#define STATECLASSHDR_HPP_ +#ifndef PARSER_STATECLASSHDR_HPP_ +#define PARSER_STATECLASSHDR_HPP_ #include "lib/parser/states/base/StateBase.hpp" #include "lib/parser/tokens/token_streams/ITokenStream.hpp" +namespace ovum::compiler::parser { + class StateClassHdr : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATECLASSHDR_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATECLASSHDR_HPP_ diff --git a/lib/parser/states/class/StateClassMember.cpp b/lib/parser/states/class/StateClassMember.cpp index f299633..bb4b51d 100644 --- a/lib/parser/states/class/StateClassMember.cpp +++ b/lib/parser/states/class/StateClassMember.cpp @@ -1 +1,3 @@ #include "StateClassMember.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/class/StateClassMember.hpp b/lib/parser/states/class/StateClassMember.hpp index ae02ff6..2901748 100644 --- a/lib/parser/states/class/StateClassMember.hpp +++ b/lib/parser/states/class/StateClassMember.hpp @@ -1,13 +1,17 @@ -#ifndef STATECLASSMEMBER_HPP_ -#define STATECLASSMEMBER_HPP_ +#ifndef PARSER_STATECLASSMEMBER_HPP_ +#define PARSER_STATECLASSMEMBER_HPP_ #include "lib/parser/states/base/StateBase.hpp" #include "lib/parser/tokens/token_streams/ITokenStream.hpp" -class StateClassHdr : public StateBase { +namespace ovum::compiler::parser { + +class StateClassMember : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATECLASSMEMBER_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATECLASSMEMBER_HPP_ diff --git a/lib/parser/states/func/StateFuncBody.cpp b/lib/parser/states/func/StateFuncBody.cpp index 8babe7f..3c5f696 100644 --- a/lib/parser/states/func/StateFuncBody.cpp +++ b/lib/parser/states/func/StateFuncBody.cpp @@ -1 +1,3 @@ #include "StateFuncBody.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/func/StateFuncBody.hpp b/lib/parser/states/func/StateFuncBody.hpp index 0c8f0ab..0397629 100644 --- a/lib/parser/states/func/StateFuncBody.hpp +++ b/lib/parser/states/func/StateFuncBody.hpp @@ -1,13 +1,17 @@ -#ifndef STATEFUNCBODY_HPP_ -#define STATEFUNCBODY_HPP_ +#ifndef PARSER_STATEFUNCBODY_HPP_ +#define PARSER_STATEFUNCBODY_HPP_ #include "lib/parser/states/base/StateBase.hpp" #include "lib/parser/tokens/token_streams/ITokenStream.hpp" +namespace ovum::compiler::parser { + class StateFuncBody : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEFUNCBODY_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEFUNCBODY_HPP_ diff --git a/lib/parser/states/func/StateFuncHdr.cpp b/lib/parser/states/func/StateFuncHdr.cpp index e11bddf..807ce70 100644 --- a/lib/parser/states/func/StateFuncHdr.cpp +++ b/lib/parser/states/func/StateFuncHdr.cpp @@ -1 +1,3 @@ #include "StateFuncHdr.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/func/StateFuncHdr.hpp b/lib/parser/states/func/StateFuncHdr.hpp index 1ce85f8..1b57c8d 100644 --- a/lib/parser/states/func/StateFuncHdr.hpp +++ b/lib/parser/states/func/StateFuncHdr.hpp @@ -1,13 +1,17 @@ -#ifndef STATEFUNCHDR_HPP_ -#define STATEFUNCHDR_HPP_ +#ifndef PARSER_STATEFUNCHDR_HPP_ +#define PARSER_STATEFUNCHDR_HPP_ #include "lib/parser/states/base/StateBase.hpp" #include "lib/parser/tokens/token_streams/ITokenStream.hpp" +namespace ovum::compiler::parser { + class StateFuncHdr : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEFUNCHDR_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEFUNCHDR_HPP_ diff --git a/lib/parser/states/func/StateFuncParams.cpp b/lib/parser/states/func/StateFuncParams.cpp index 6932a35..9fdf6b8 100644 --- a/lib/parser/states/func/StateFuncParams.cpp +++ b/lib/parser/states/func/StateFuncParams.cpp @@ -1 +1,3 @@ #include "StateFuncParams.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/func/StateFuncParams.hpp b/lib/parser/states/func/StateFuncParams.hpp index 869b5f6..7929508 100644 --- a/lib/parser/states/func/StateFuncParams.hpp +++ b/lib/parser/states/func/StateFuncParams.hpp @@ -1,13 +1,17 @@ -#ifndef STATEFUNCPARAMS_HPP_ -#define STATEFUNCPARAMS_HPP_ +#ifndef PARSER_STATEFUNCPARAMS_HPP_ +#define PARSER_STATEFUNCPARAMS_HPP_ #include "lib/parser/states/base/StateBase.hpp" #include "lib/parser/tokens/token_streams/ITokenStream.hpp" +namespace ovum::compiler::parser { + class StateFuncParams : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEFUNCPARAMS_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEFUNCPARAMS_HPP_ diff --git a/lib/parser/states/interface/StateInterfaceBody.cpp b/lib/parser/states/interface/StateInterfaceBody.cpp index f79c31a..08cfa87 100644 --- a/lib/parser/states/interface/StateInterfaceBody.cpp +++ b/lib/parser/states/interface/StateInterfaceBody.cpp @@ -1 +1,3 @@ #include "StateInterfaceBody.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/interface/StateInterfaceBody.hpp b/lib/parser/states/interface/StateInterfaceBody.hpp index 10accc9..fd84410 100644 --- a/lib/parser/states/interface/StateInterfaceBody.hpp +++ b/lib/parser/states/interface/StateInterfaceBody.hpp @@ -1,13 +1,17 @@ -#ifndef STATEINTERFACEBODY_HPP_ -#define STATEINTERFACEBODY_HPP_ +#ifndef PARSER_STATEINTERFACEBODY_HPP_ +#define PARSER_STATEINTERFACEBODY_HPP_ #include "lib/parser/states/base/StateBase.hpp" #include "lib/parser/tokens/token_streams/ITokenStream.hpp" +namespace ovum::compiler::parser { + class StateInterfaceBody : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEINTERFACEBODY_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEINTERFACEBODY_HPP_ diff --git a/lib/parser/states/interface/StateInterfaceDecl.cpp b/lib/parser/states/interface/StateInterfaceDecl.cpp index c8d556c..85c9bf3 100644 --- a/lib/parser/states/interface/StateInterfaceDecl.cpp +++ b/lib/parser/states/interface/StateInterfaceDecl.cpp @@ -1 +1,3 @@ #include "StateInterfaceDecl.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/interface/StateInterfaceDecl.hpp b/lib/parser/states/interface/StateInterfaceDecl.hpp index 09c0b64..4f92a61 100644 --- a/lib/parser/states/interface/StateInterfaceDecl.hpp +++ b/lib/parser/states/interface/StateInterfaceDecl.hpp @@ -1,13 +1,17 @@ -#ifndef STATEINTERFACEDECL_HPP_ -#define STATEINTERFACEDECL_HPP_ +#ifndef PARSER_STATEINTERFACEDECL_HPP_ +#define PARSER_STATEINTERFACEDECL_HPP_ #include "lib/parser/states/base/StateBase.hpp" #include "lib/parser/tokens/token_streams/ITokenStream.hpp" +namespace ovum::compiler::parser { + class StateInterfaceDecl : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEINTERFACEDECL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEINTERFACEDECL_HPP_ diff --git a/lib/parser/states/interface/StateInterfaceHdr.cpp b/lib/parser/states/interface/StateInterfaceHdr.cpp index 74e2c1d..cebb5fb 100644 --- a/lib/parser/states/interface/StateInterfaceHdr.cpp +++ b/lib/parser/states/interface/StateInterfaceHdr.cpp @@ -1 +1,3 @@ #include "StateInterfaceHdr.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/states/interface/StateInterfaceHdr.hpp b/lib/parser/states/interface/StateInterfaceHdr.hpp index bc6aba7..a0887e3 100644 --- a/lib/parser/states/interface/StateInterfaceHdr.hpp +++ b/lib/parser/states/interface/StateInterfaceHdr.hpp @@ -1,13 +1,17 @@ -#ifndef STATEINTERFACEHDR_HPP_ -#define STATEINTERFACEHDR_HPP_ +#ifndef PARSER_STATEINTERFACEHDR_HPP_ +#define PARSER_STATEINTERFACEHDR_HPP_ #include "lib/parser/states/base/StateBase.hpp" #include "lib/parser/tokens/token_streams/ITokenStream.hpp" +namespace ovum::compiler::parser { + class StateInterfaceHdr : public StateBase { public: std::string_view Name() const override; StepResult TryStep(ContextParser& ctx, ITokenStream& ts) override; }; -#endif // STATEINTERFACEHDR_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_STATEINTERFACEHDR_HPP_ diff --git a/lib/parser/tokens/SourceId.cpp b/lib/parser/tokens/SourceId.cpp index 202a610..5d6d911 100644 --- a/lib/parser/tokens/SourceId.cpp +++ b/lib/parser/tokens/SourceId.cpp @@ -1 +1,3 @@ #include "SourceId.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/tokens/SourceId.hpp b/lib/parser/tokens/SourceId.hpp index 0c56078..4d2dea3 100644 --- a/lib/parser/tokens/SourceId.hpp +++ b/lib/parser/tokens/SourceId.hpp @@ -1,10 +1,12 @@ -#ifndef SOURCEID_HPP_ -#define SOURCEID_HPP_ +#ifndef PARSER_SOURCEID_HPP_ +#define PARSER_SOURCEID_HPP_ #include #include #include +namespace ovum::compiler::parser { + class SourceId { public: SourceId() = default; @@ -23,4 +25,6 @@ class SourceId { std::string path_; }; -#endif // SOURCEID_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_SOURCEID_HPP_ diff --git a/lib/parser/tokens/SourceSpan.cpp b/lib/parser/tokens/SourceSpan.cpp index aaa1a2a..742d499 100644 --- a/lib/parser/tokens/SourceSpan.cpp +++ b/lib/parser/tokens/SourceSpan.cpp @@ -1 +1,3 @@ #include "SourceSpan.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/tokens/SourceSpan.hpp b/lib/parser/tokens/SourceSpan.hpp index 3594de1..cac2607 100644 --- a/lib/parser/tokens/SourceSpan.hpp +++ b/lib/parser/tokens/SourceSpan.hpp @@ -1,9 +1,11 @@ -#ifndef SOURCESPAN_HPP_ -#define SOURCESPAN_HPP_ +#ifndef PARSER_SOURCESPAN_HPP_ +#define PARSER_SOURCESPAN_HPP_ + +#include #include "SourceId.hpp" -#include "lib/lexer/tokens/TokenPosition.hpp" +namespace ovum::compiler::parser { class SourceSpan { public: @@ -26,4 +28,6 @@ class SourceSpan { TokenPosition end_{}; }; -#endif // SOURCESPAN_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_SOURCESPAN_HPP_ diff --git a/lib/parser/tokens/token_streams/ITokenStream.hpp b/lib/parser/tokens/token_streams/ITokenStream.hpp index 28e8a3d..37eaeba 100644 --- a/lib/parser/tokens/token_streams/ITokenStream.hpp +++ b/lib/parser/tokens/token_streams/ITokenStream.hpp @@ -1,9 +1,11 @@ -#ifndef ITOKENSTREAM_HPP_ -#define ITOKENSTREAM_HPP_ +#ifndef PARSER_ITOKENSTREAM_HPP_ +#define PARSER_ITOKENSTREAM_HPP_ #include -#include "lib/lexer/tokens/Token.hpp" +#include + +namespace ovum::compiler::parser { class ITokenStream { public: @@ -20,4 +22,6 @@ class ITokenStream { virtual const Token* TryPeek(size_t k = 0) = 0; }; -#endif // ITOKENSTREAM_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_ITOKENSTREAM_HPP_ diff --git a/lib/parser/tokens/token_streams/VectorTokenStream.cpp b/lib/parser/tokens/token_streams/VectorTokenStream.cpp index 909c75d..6fafb4f 100644 --- a/lib/parser/tokens/token_streams/VectorTokenStream.cpp +++ b/lib/parser/tokens/token_streams/VectorTokenStream.cpp @@ -3,6 +3,8 @@ #include #include +namespace ovum::compiler::parser { + VectorTokenStream::VectorTokenStream(std::vector tokens) : tokens_(std::move(tokens)) { } @@ -68,3 +70,5 @@ const Token* VectorTokenStream::TryPeek(size_t k) { size_t VectorTokenStream::Size() const { return tokens_.size(); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/tokens/token_streams/VectorTokenStream.hpp b/lib/parser/tokens/token_streams/VectorTokenStream.hpp index d92fe89..a5caae6 100644 --- a/lib/parser/tokens/token_streams/VectorTokenStream.hpp +++ b/lib/parser/tokens/token_streams/VectorTokenStream.hpp @@ -1,12 +1,15 @@ -#ifndef VECTORTOKENSTREAM_HPP_ -#define VECTORTOKENSTREAM_HPP_ +#ifndef PARSER_VECTORTOKENSTREAM_HPP_ +#define PARSER_VECTORTOKENSTREAM_HPP_ #include #include #include +#include + #include "ITokenStream.hpp" -#include "lib/lexer/tokens/Token.hpp" + +namespace ovum::compiler::parser { class VectorTokenStream : public ITokenStream { public: @@ -34,4 +37,6 @@ class VectorTokenStream : public ITokenStream { const Token* last_ = nullptr; }; -#endif // VECTORTOKENSTREAM_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_VECTORTOKENSTREAM_HPP_ diff --git a/lib/parser/tokens/token_traits/ITokenMatcher.hpp b/lib/parser/tokens/token_traits/ITokenMatcher.hpp index c8e24d5..ed22b1f 100644 --- a/lib/parser/tokens/token_traits/ITokenMatcher.hpp +++ b/lib/parser/tokens/token_traits/ITokenMatcher.hpp @@ -1,7 +1,9 @@ -#ifndef ITOKENMATCHER_HPP_ -#define ITOKENMATCHER_HPP_ +#ifndef PARSER_ITOKENMATCHER_HPP_ +#define PARSER_ITOKENMATCHER_HPP_ -#include "lib/lexer/tokens/Token.hpp" +#include + +namespace ovum::compiler::parser { class ITokenMatcher { public: @@ -10,4 +12,6 @@ class ITokenMatcher { virtual bool Match(const Token& t) const = 0; }; -#endif // ITOKENMATCHER_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_ITOKENMATCHER_HPP_ diff --git a/lib/parser/tokens/token_traits/MatchIdentifier.cpp b/lib/parser/tokens/token_traits/MatchIdentifier.cpp index 6fe57af..fb49f8b 100644 --- a/lib/parser/tokens/token_traits/MatchIdentifier.cpp +++ b/lib/parser/tokens/token_traits/MatchIdentifier.cpp @@ -1 +1,3 @@ #include "MatchIdentifier.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/tokens/token_traits/MatchIdentifier.hpp b/lib/parser/tokens/token_traits/MatchIdentifier.hpp index fea595f..2719634 100644 --- a/lib/parser/tokens/token_traits/MatchIdentifier.hpp +++ b/lib/parser/tokens/token_traits/MatchIdentifier.hpp @@ -1,12 +1,17 @@ -#ifndef MATCHIDENTIFIER_HPP_ -#define MATCHIDENTIFIER_HPP_ +#ifndef PARSER_MATCHIDENTIFIER_HPP_ +#define PARSER_MATCHIDENTIFIER_HPP_ + +#include #include "ITokenMatcher.hpp" -#include "lib/lexer/tokens/Token.hpp" + +namespace ovum::compiler::parser { class MatchIdentifier : public ITokenMatcher { public: bool Match(const Token& tok) const override; }; -#endif // MATCHIDENTIFIER_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_MATCHIDENTIFIER_HPP_ diff --git a/lib/parser/tokens/token_traits/MatchLexeme.cpp b/lib/parser/tokens/token_traits/MatchLexeme.cpp index d7a6c5d..e6866c2 100644 --- a/lib/parser/tokens/token_traits/MatchLexeme.cpp +++ b/lib/parser/tokens/token_traits/MatchLexeme.cpp @@ -1 +1,3 @@ #include "MatchLexeme.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/tokens/token_traits/MatchLexeme.hpp b/lib/parser/tokens/token_traits/MatchLexeme.hpp index 42b71bb..5b1f783 100644 --- a/lib/parser/tokens/token_traits/MatchLexeme.hpp +++ b/lib/parser/tokens/token_traits/MatchLexeme.hpp @@ -1,10 +1,13 @@ -#ifndef MATCHLEXEME_HPP_ -#define MATCHLEXEME_HPP_ +#ifndef PARSER_MATCHLEXEME_HPP_ +#define PARSER_MATCHLEXEME_HPP_ #include +#include + #include "ITokenMatcher.hpp" -#include "lib/lexer/tokens/Token.hpp" + +namespace ovum::compiler::parser { class MatchLexeme : public ITokenMatcher { public: @@ -16,4 +19,6 @@ class MatchLexeme : public ITokenMatcher { std::string_view lexeme_; }; -#endif // MATCHLEXEME_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_MATCHLEXEME_HPP_ diff --git a/lib/parser/tokens/token_traits/MatchLiteral.cpp b/lib/parser/tokens/token_traits/MatchLiteral.cpp index bd21b50..ceaff66 100644 --- a/lib/parser/tokens/token_traits/MatchLiteral.cpp +++ b/lib/parser/tokens/token_traits/MatchLiteral.cpp @@ -1 +1,3 @@ #include "MatchLiteral.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/tokens/token_traits/MatchLiteral.hpp b/lib/parser/tokens/token_traits/MatchLiteral.hpp index 74b031a..2cd5959 100644 --- a/lib/parser/tokens/token_traits/MatchLiteral.hpp +++ b/lib/parser/tokens/token_traits/MatchLiteral.hpp @@ -1,12 +1,17 @@ -#ifndef MATCHLITERAL_HPP_ -#define MATCHLITERAL_HPP_ +#ifndef PARSER_MATCHLITERAL_HPP_ +#define PARSER_MATCHLITERAL_HPP_ + +#include #include "ITokenMatcher.hpp" -#include "lib/lexer/tokens/Token.hpp" + +namespace ovum::compiler::parser { class MatchLiteral : public ITokenMatcher { public: bool Match(const Token& tok) const override; }; -#endif // MATCHLITERAL_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_MATCHLITERAL_HPP_ diff --git a/lib/parser/tokens/token_traits/MatchManyOf.cpp b/lib/parser/tokens/token_traits/MatchManyOf.cpp index 56a0342..8f2fb8c 100644 --- a/lib/parser/tokens/token_traits/MatchManyOf.cpp +++ b/lib/parser/tokens/token_traits/MatchManyOf.cpp @@ -1 +1,3 @@ #include "MatchManyOf.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/tokens/token_traits/MatchManyOf.hpp b/lib/parser/tokens/token_traits/MatchManyOf.hpp index 52df6e0..adfecaf 100644 --- a/lib/parser/tokens/token_traits/MatchManyOf.hpp +++ b/lib/parser/tokens/token_traits/MatchManyOf.hpp @@ -1,11 +1,14 @@ -#ifndef MATCHMANYOF_HPP_ -#define MATCHMANYOF_HPP_ +#ifndef PARSER_MATCHMANYOF_HPP_ +#define PARSER_MATCHMANYOF_HPP_ #include #include +#include + #include "ITokenMatcher.hpp" -#include "lib/lexer/tokens/Token.hpp" + +namespace ovum::compiler::parser { class MatchAnyOf : public ITokenMatcher { public: @@ -17,4 +20,6 @@ class MatchAnyOf : public ITokenMatcher { std::vector> ms_; }; -#endif // MATCHMANYOF_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_MATCHMANYOF_HPP_ diff --git a/lib/parser/tokens/token_traits/MatchType.cpp b/lib/parser/tokens/token_traits/MatchType.cpp index 691d217..413410a 100644 --- a/lib/parser/tokens/token_traits/MatchType.cpp +++ b/lib/parser/tokens/token_traits/MatchType.cpp @@ -1 +1,3 @@ #include "MatchType.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/tokens/token_traits/MatchType.hpp b/lib/parser/tokens/token_traits/MatchType.hpp index 736191e..3554129 100644 --- a/lib/parser/tokens/token_traits/MatchType.hpp +++ b/lib/parser/tokens/token_traits/MatchType.hpp @@ -1,10 +1,13 @@ -#ifndef MATCHTYPE_HPP_ -#define MATCHTYPE_HPP_ +#ifndef PARSER_MATCHTYPE_HPP_ +#define PARSER_MATCHTYPE_HPP_ #include +#include + #include "ITokenMatcher.hpp" -#include "lib/lexer/tokens/Token.hpp" + +namespace ovum::compiler::parser { class MatchType : public ITokenMatcher { public: @@ -16,4 +19,6 @@ class MatchType : public ITokenMatcher { std::string_view type_; }; -#endif // MATCHTYPE_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_MATCHTYPE_HPP_ diff --git a/lib/parser/type_parser/ITypeParser.hpp b/lib/parser/type_parser/ITypeParser.hpp index 1b2802b..fabbbd9 100644 --- a/lib/parser/type_parser/ITypeParser.hpp +++ b/lib/parser/type_parser/ITypeParser.hpp @@ -1,5 +1,5 @@ -#ifndef ITYPEPARSER_HPP_ -#define ITYPEPARSER_HPP_ +#ifndef PARSER_ITYPEPARSER_HPP_ +#define PARSER_ITYPEPARSER_HPP_ #include @@ -7,10 +7,14 @@ #include "lib/parser/tokens/token_streams/ITokenStream.hpp" #include "lib/parser/types/TypeReference.hpp" +namespace ovum::compiler::parser { + class ITypeParser { public: virtual ~ITypeParser() = default; virtual std::unique_ptr ParseType(ITokenStream& ts, IDiagnosticSink& diags) = 0; }; -#endif // ITYPEPARSER_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_ITYPEPARSER_HPP_ diff --git a/lib/parser/type_parser/QNameTypeParser.cpp b/lib/parser/type_parser/QNameTypeParser.cpp index b29a329..dbcb469 100644 --- a/lib/parser/type_parser/QNameTypeParser.cpp +++ b/lib/parser/type_parser/QNameTypeParser.cpp @@ -1 +1,3 @@ #include "QNameTypeParser.hpp" + +namespace ovum::compiler::parser {} // namespace ovum::compiler::parser diff --git a/lib/parser/type_parser/QNameTypeParser.hpp b/lib/parser/type_parser/QNameTypeParser.hpp index b5068b1..f54cbed 100644 --- a/lib/parser/type_parser/QNameTypeParser.hpp +++ b/lib/parser/type_parser/QNameTypeParser.hpp @@ -1,5 +1,5 @@ -#ifndef QNAMETYPEPARSER_HPP_ -#define QNAMETYPEPARSER_HPP_ +#ifndef PARSER_QNAMETYPEPARSER_HPP_ +#define PARSER_QNAMETYPEPARSER_HPP_ #include @@ -9,6 +9,8 @@ #include "lib/parser/tokens/token_streams/ITokenStream.hpp" #include "lib/parser/types/TypeReference.hpp" +namespace ovum::compiler::parser { + class QNameTypeParser final : public ITypeParser { public: explicit QNameTypeParser(IAstFactory& factory) noexcept; @@ -26,4 +28,6 @@ class QNameTypeParser final : public ITypeParser { std::shared_ptr factory_; }; -#endif // QNAMETYPEPARSER_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_QNAMETYPEPARSER_HPP_ diff --git a/lib/parser/types/Nullable.cpp b/lib/parser/types/Nullable.cpp index 9aacd66..b231e1b 100644 --- a/lib/parser/types/Nullable.cpp +++ b/lib/parser/types/Nullable.cpp @@ -1,5 +1,7 @@ #include "Nullable.hpp" +namespace ovum::compiler::parser { + Nullable::Nullable(bool on) noexcept : on_(on) { } @@ -18,3 +20,5 @@ void Nullable::Enable() noexcept { void Nullable::Disable() noexcept { on_ = false; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/types/Nullable.hpp b/lib/parser/types/Nullable.hpp index 6e629a5..ffc8c3e 100644 --- a/lib/parser/types/Nullable.hpp +++ b/lib/parser/types/Nullable.hpp @@ -1,5 +1,7 @@ -#ifndef NULLABLE_HPP_ -#define NULLABLE_HPP_ +#ifndef PARSER_NULLABLE_HPP_ +#define PARSER_NULLABLE_HPP_ + +namespace ovum::compiler::parser { class Nullable { public: @@ -19,4 +21,6 @@ class Nullable { bool on_ = false; }; -#endif // NULLABLE_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_NULLABLE_HPP_ diff --git a/lib/parser/types/Param.cpp b/lib/parser/types/Param.cpp index 44d5fb0..bef600d 100644 --- a/lib/parser/types/Param.cpp +++ b/lib/parser/types/Param.cpp @@ -1,5 +1,7 @@ #include "lib/parser/types/Param.hpp" +namespace ovum::compiler::parser { + Param::Param(std::string name, TypeReference typeReference) : name_(std::move(name)), reference_(std::move(typeReference)) { } @@ -11,3 +13,5 @@ std::string Param::GetName() { TypeReference Param::GetType() { return reference_; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/types/Param.hpp b/lib/parser/types/Param.hpp index a104f82..8fd2c52 100644 --- a/lib/parser/types/Param.hpp +++ b/lib/parser/types/Param.hpp @@ -1,10 +1,12 @@ -#ifndef PARAM_HPP_ -#define PARAM_HPP_ +#ifndef PARSER_PARAM_HPP_ +#define PARSER_PARAM_HPP_ #include #include "TypeReference.hpp" +namespace ovum::compiler::parser { + class Param { public: Param(std::string name, TypeReference typeReference); @@ -18,4 +20,6 @@ class Param { TypeReference reference_; }; -#endif // PARAM_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_PARAM_HPP_ diff --git a/lib/parser/types/ResolvedTypeHandle.cpp b/lib/parser/types/ResolvedTypeHandle.cpp index 6523121..b228d66 100644 --- a/lib/parser/types/ResolvedTypeHandle.cpp +++ b/lib/parser/types/ResolvedTypeHandle.cpp @@ -2,6 +2,8 @@ #include +namespace ovum::compiler::parser { + ResolvedTypeHandle::ResolvedTypeHandle(const void* decl, std::string mangled, bool is_interface) : decl_(decl), mangled_(std::move(mangled)), is_interface_(is_interface) { } @@ -21,3 +23,5 @@ bool ResolvedTypeHandle::IsInterface() const noexcept { bool ResolvedTypeHandle::IsValid() const noexcept { return decl_ != nullptr; } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/types/ResolvedTypeHandle.hpp b/lib/parser/types/ResolvedTypeHandle.hpp index f68747f..74b9f57 100644 --- a/lib/parser/types/ResolvedTypeHandle.hpp +++ b/lib/parser/types/ResolvedTypeHandle.hpp @@ -1,8 +1,10 @@ -#ifndef RESOLVEDTYPEHANDLE_HPP_ -#define RESOLVEDTYPEHANDLE_HPP_ +#ifndef PARSER_RESOLVEDTYPEHANDLE_HPP_ +#define PARSER_RESOLVEDTYPEHANDLE_HPP_ #include +namespace ovum::compiler::parser { + class ResolvedTypeHandle { public: ResolvedTypeHandle() = default; @@ -26,4 +28,6 @@ class ResolvedTypeHandle { bool is_interface_ = false; }; -#endif // RESOLVEDTYPEHANDLE_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_RESOLVEDTYPEHANDLE_HPP_ diff --git a/lib/parser/types/TypeReference.cpp b/lib/parser/types/TypeReference.cpp index 712dd8b..9e5bfc2 100644 --- a/lib/parser/types/TypeReference.cpp +++ b/lib/parser/types/TypeReference.cpp @@ -5,6 +5,8 @@ #include #include +namespace ovum::compiler::parser { + TypeReference::TypeReference() = default; TypeReference::TypeReference(std::string name) { @@ -252,3 +254,5 @@ std::string TypeReference::ArgsToString(const std::vector& args) void TypeReference::InvalidateResolution() noexcept { resolved_.reset(); } + +} // namespace ovum::compiler::parser diff --git a/lib/parser/types/TypeReference.hpp b/lib/parser/types/TypeReference.hpp index 7a0a716..3aaf354 100644 --- a/lib/parser/types/TypeReference.hpp +++ b/lib/parser/types/TypeReference.hpp @@ -1,5 +1,5 @@ -#ifndef TYPEREFERENCE_HPP_ -#define TYPEREFERENCE_HPP_ +#ifndef PARSER_TYPEREFERENCE_HPP_ +#define PARSER_TYPEREFERENCE_HPP_ #include #include @@ -9,6 +9,8 @@ #include "Nullable.hpp" #include "ResolvedTypeHandle.hpp" +namespace ovum::compiler::parser { + class TypeReference { public: TypeReference(); @@ -72,4 +74,6 @@ class TypeReference { std::unique_ptr resolved_; }; -#endif // TYPEREFERENCE_HPP_ +} // namespace ovum::compiler::parser + +#endif // PARSER_TYPEREFERENCE_HPP_ From 5ec413aa0e7c6f4f9740bc86fdf3f23f2438effd Mon Sep 17 00:00:00 2001 From: bialger Date: Mon, 10 Nov 2025 00:55:18 +0300 Subject: [PATCH 21/21] refactor: optimize Diagnostic methods by using move semantics and adding [[nodiscard]] attributes --- lib/parser/diagnostics/Diagnostic.cpp | 4 ++-- lib/parser/diagnostics/Diagnostic.hpp | 19 +++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/parser/diagnostics/Diagnostic.cpp b/lib/parser/diagnostics/Diagnostic.cpp index e33180c..21bdf3f 100644 --- a/lib/parser/diagnostics/Diagnostic.cpp +++ b/lib/parser/diagnostics/Diagnostic.cpp @@ -82,8 +82,8 @@ const std::string& Diagnostic::GetCategory() const noexcept { return category_; } -void Diagnostic::SetWhere(const SourceSpan& sp) { - where_ = sp; +void Diagnostic::SetWhere(SourceSpan sp) { + where_ = std::move(sp); } void Diagnostic::ResetWhere() { where_.reset(); diff --git a/lib/parser/diagnostics/Diagnostic.hpp b/lib/parser/diagnostics/Diagnostic.hpp index b067547..d4b766d 100644 --- a/lib/parser/diagnostics/Diagnostic.hpp +++ b/lib/parser/diagnostics/Diagnostic.hpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include "FixIt.hpp" @@ -25,34 +24,34 @@ class Diagnostic { ~Diagnostic(); void SetSeverity(std::shared_ptr sev); - const std::shared_ptr& GetSeverity() const noexcept; + [[nodiscard]] const std::shared_ptr& GetSeverity() const noexcept; void SetCode(std::string c); - const std::string& GetCode() const noexcept; + [[nodiscard]] const std::string& GetCode() const noexcept; void SetMessage(std::string m); - const std::string& GetMessage() const noexcept; + [[nodiscard]] const std::string& GetMessage() const noexcept; void SetCategory(std::string cat); - const std::string& GetCategory() const noexcept; + [[nodiscard]] const std::string& GetCategory() const noexcept; void SetWhere(SourceSpan sp); void ResetWhere(); - const std::optional& GetWhere() const noexcept; + [[nodiscard]] const std::optional& GetWhere() const noexcept; void AddNote(RelatedInfo note); void ClearNotes(); - const std::vector& GetNotes() const noexcept; + [[nodiscard]] const std::vector& GetNotes() const noexcept; void AddFix(FixIt fix); void ClearFixes(); - const std::vector& GetFixes() const noexcept; + [[nodiscard]] const std::vector& GetFixes() const noexcept; void SetFatal(bool on); - bool IsFatal() const noexcept; + [[nodiscard]] bool IsFatal() const noexcept; void SetSuppressed(bool on); - bool IsSuppressed() const noexcept; + [[nodiscard]] bool IsSuppressed() const noexcept; private: std::shared_ptr severity_;