Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/parser/IParser.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef PARSER_IPARSER_HPP_
#define PARSER_IPARSER_HPP_

#include <memory>

#include "ast/nodes/decls/Module.hpp"
#include "diagnostics/IDiagnosticSink.hpp"
#include "tokens/token_streams/ITokenStream.hpp"

namespace ovum::compiler::parser {

class IParser {
public:
virtual ~IParser() = default;
virtual std::unique_ptr<Module> Parse(ITokenStream& ts, IDiagnosticSink& diags) = 0;
};

} // namespace ovum::compiler::parser

#endif // PARSER_IPARSER_HPP_
3 changes: 3 additions & 0 deletions lib/parser/ParserFsm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "ParserFsm.hpp"

namespace ovum::compiler::parser {} // namespace ovum::compiler::parser
31 changes: 31 additions & 0 deletions lib/parser/ParserFsm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef PARSER_PARSERFSM_HPP_
#define PARSER_PARSERFSM_HPP_

#include <memory>

#include "IParser.hpp"
#include "ast/IAstFactory.hpp"
#include "pratt/IExpressionParser.hpp"
#include "type_parser/ITypeParser.hpp"

namespace ovum::compiler::parser {

class ParserFsm : public IParser {
public:
ParserFsm(std::unique_ptr<IExpressionParser> expr,
std::unique_ptr<ITypeParser> typep,
std::unique_ptr<IAstFactory> factory);

~ParserFsm() override = default;

std::unique_ptr<Module> Parse(ITokenStream& ts, IDiagnosticSink& diags) override;

private:
std::unique_ptr<IExpressionParser> expr_parser_;
std::unique_ptr<ITypeParser> type_parser_;
std::unique_ptr<IAstFactory> factory_;
};

} // namespace ovum::compiler::parser

#endif // PARSER_PARSERFSM_HPP_
98 changes: 98 additions & 0 deletions lib/parser/ast/AstVisitor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#ifndef PARSER_ASTVISITOR_HPP_
#define PARSER_ASTVISITOR_HPP_

#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"

namespace ovum::compiler::parser {

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(InterfaceMethod&) = 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;
};

} // namespace ovum::compiler::parser

#endif // PARSER_ASTVISITOR_HPP_
96 changes: 96 additions & 0 deletions lib/parser/ast/IAstFactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#ifndef PARSER_IASTFACTORY_HPP_
#define PARSER_IASTFACTORY_HPP_

#include <memory>

#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/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"

namespace ovum::compiler::parser {

class IAstFactory {
public:
virtual ~IAstFactory() = default;

virtual std::unique_ptr<FunctionDecl> MakeFunction() = 0;
virtual std::unique_ptr<ClassDecl> MakeClass() = 0;
virtual std::unique_ptr<InterfaceDecl> MakeInterface() = 0;
virtual std::unique_ptr<InterfaceMethod> MakeInterfaceMethod() = 0;
virtual std::unique_ptr<TypeAliasDecl> MakeTypeAlias() = 0;
virtual std::unique_ptr<GlobalVarDecl> MakeGlobalVar() = 0;

virtual std::unique_ptr<FieldDecl> MakeField() = 0;
virtual std::unique_ptr<StaticFieldDecl> MakeStaticField() = 0;
virtual std::unique_ptr<MethodDecl> MakeMethod() = 0;
virtual std::unique_ptr<CallDecl> MakeCallDecl() = 0;
virtual std::unique_ptr<DestructorDecl> MakeDestructor() = 0;

virtual std::unique_ptr<Block> MakeBlock() = 0;
virtual std::unique_ptr<VarDeclStmt> MakeVarDeclStmt() = 0;
virtual std::unique_ptr<ExprStmt> MakeExprStmt() = 0;
virtual std::unique_ptr<ReturnStmt> MakeReturnStmt() = 0;
virtual std::unique_ptr<BreakStmt> MakeBreakStmt() = 0;
virtual std::unique_ptr<ContinueStmt> MakeContinueStmt() = 0;
virtual std::unique_ptr<IfStmt> MakeIfStmt() = 0;
virtual std::unique_ptr<WhileStmt> MakeWhileStmt() = 0;
virtual std::unique_ptr<ForStmt> MakeForStmt() = 0;
virtual std::unique_ptr<UnsafeBlock> MakeUnsafeBlock() = 0;

virtual std::unique_ptr<Binary> MakeBinary(const IBinaryOpTag& op) = 0;
virtual std::unique_ptr<Unary> MakeUnary(const IUnaryOpTag& op) = 0;
virtual std::unique_ptr<Assign> MakeAssign(const IAssignOpTag& op) = 0;
virtual std::unique_ptr<Call> MakeCall() = 0;
virtual std::unique_ptr<FieldAccess> MakeFieldAccess() = 0;
virtual std::unique_ptr<IndexAccess> MakeIndexAccess() = 0;
virtual std::unique_ptr<NamespaceRef> MakeNamespaceRef() = 0;
virtual std::unique_ptr<SafeCall> MakeSafeCall() = 0;
virtual std::unique_ptr<Elvis> MakeElvis() = 0;
virtual std::unique_ptr<CastAs> MakeCastAs() = 0;
virtual std::unique_ptr<TypeTestIs> MakeTypeTestIs() = 0;
virtual std::unique_ptr<IdentRef> MakeIdent(std::string name) = 0;
virtual std::unique_ptr<IntLit> MakeInt(long long v) = 0;
virtual std::unique_ptr<FloatLit> MakeFloat(long double v) = 0;
virtual std::unique_ptr<StringLit> MakeString(std::u32string v) = 0;
virtual std::unique_ptr<CharLit> MakeChar(char32_t v) = 0;
virtual std::unique_ptr<BoolLit> MakeBool(bool v) = 0;
virtual std::unique_ptr<NullLit> MakeNull() = 0;
};

} // namespace ovum::compiler::parser

#endif // PARSER_IASTFACTORY_HPP_
19 changes: 19 additions & 0 deletions lib/parser/ast/nodes/base/AstNode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef PARSER_ASTNODE_HPP_
#define PARSER_ASTNODE_HPP_

namespace ovum::compiler::parser {

class AstVisitor; // forward

class AstNode {
public:
virtual ~AstNode() = default;

// TODO: add positions......

virtual void Accept(AstVisitor& visitor) = 0;
};

} // namespace ovum::compiler::parser

#endif // PARSER_ASTNODE_HPP_
12 changes: 12 additions & 0 deletions lib/parser/ast/nodes/base/Decl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef PARSER_DECL_HPP_
#define PARSER_DECL_HPP_

#include "AstNode.hpp"

namespace ovum::compiler::parser {

class Decl : public AstNode {};

} // namespace ovum::compiler::parser

#endif // PARSER_DECL_HPP_
12 changes: 12 additions & 0 deletions lib/parser/ast/nodes/base/Expr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef PARSER_EXPR_HPP_
#define PARSER_EXPR_HPP_

#include "AstNode.hpp"

namespace ovum::compiler::parser {

class Expr : public AstNode {};

} // namespace ovum::compiler::parser

#endif // PARSER_EXPR_HPP_
12 changes: 12 additions & 0 deletions lib/parser/ast/nodes/base/Stmt.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef PARSER_STMT_HPP_
#define PARSER_STMT_HPP_

#include "AstNode.hpp"

namespace ovum::compiler::parser {

class Stmt : public AstNode {};

} // namespace ovum::compiler::parser

#endif // PARSER_STMT_HPP_
65 changes: 65 additions & 0 deletions lib/parser/ast/nodes/class_members/CallDecl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "lib/parser/ast/nodes/class_members/CallDecl.hpp"

#include "lib/parser/ast/AstVisitor.hpp"

#include <utility>

namespace ovum::compiler::parser {

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<Param>& CallDecl::Params() const noexcept {
return params_;
}

std::vector<Param>& 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<TypeReference> type) {
ret_type_ = std::move(type);
}

std::unique_ptr<TypeReference> 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> block) {
body_ = std::move(block);
}

std::unique_ptr<Block> CallDecl::ReleaseBody() {
return std::move(body_);
}

} // namespace ovum::compiler::parser
Loading