sammine-lang
Loading...
Searching...
No Matches
Parser.h
Go to the documentation of this file.
1#pragma once
2#include "ast/AstDecl.h"
3#include "lex/Token.h"
4#include "util/Utilities.h"
5#include <memory>
6#include <optional>
7#include <utility>
10
11namespace sammine_lang {
12enum ParserError {
13 SUCCESS,
14 COMMITTED_NO_MORE_ERROR,
15 COMMITTED_EMIT_MORE_ERROR,
16 NONCOMMITTED,
17};
18using namespace AST;
19using namespace sammine_util;
20class Parser : public Reportee {
21
22 void error(const std::string &msg, Location loc = Location::NonPrintable()) {
23 if (reporter.has_value()) {
24 reporter->get().immediate_error(msg, loc);
25 }
26 this->error_count++;
27 }
28 void diag(const std::string &msg, Location loc = Location::NonPrintable()) {
29 if (reporter.has_value()) {
30 reporter->get().immediate_diag(msg, loc);
31 }
32 }
33
34public:
35 template <class T> using p = std::pair<std::unique_ptr<T>, ParserError>;
36 template <class T> using u = std::unique_ptr<T>;
37 std::optional<std::reference_wrapper<Reporter>> reporter;
38 std::shared_ptr<TokenStream> tokStream;
39 [[nodiscard]] auto ParseProgram() -> u<ProgramAST>;
40
41 // Parse definition
42 [[nodiscard]] auto ParseDefinition() -> p<DefinitionAST>;
43 [[nodiscard]] auto ParsePrototype() -> p<PrototypeAST>;
44 [[nodiscard]] auto ParseFuncDef() -> p<DefinitionAST>;
45 [[nodiscard]] auto ParseVarDef() -> p<ExprAST>;
46 [[nodiscard]] auto ParseRecordDef() -> p<DefinitionAST>;
47
48 // Parse type
49 [[nodiscard]] auto ParseTypedVar() -> p<TypedVarAST>;
50
51 // Parse pressions
52 [[nodiscard]] auto ParseExpr() -> p<ExprAST>;
53 [[nodiscard]] auto ParsePrimaryExpr() -> p<ExprAST>;
54 [[nodiscard]] auto ParseBinaryExpr(int prededence, u<ExprAST> LHS)
55 -> p<ExprAST>;
56 [[nodiscard]] auto ParseBoolExpr() -> p<ExprAST>;
57
58 [[nodiscard]] auto ParseCallExpr() -> p<ExprAST>;
59 [[nodiscard]] auto ParseReturnExpr() -> p<ExprAST>;
60 [[nodiscard]] auto ParseArguments()
61 -> std::pair<std::vector<u<ExprAST>>, ParserError>;
62 [[nodiscard]] auto ParseParenExpr() -> p<ExprAST>;
63 [[nodiscard]] auto ParseIfExpr() -> p<ExprAST>;
64 [[nodiscard]] auto ParseNumberExpr() -> p<ExprAST>;
65 [[nodiscard]] auto ParseStringExpr() -> p<ExprAST>;
66 [[nodiscard]] auto ParseVariableExpr() -> p<ExprAST>;
67
68 // Parse block
69 [[nodiscard]] auto ParseBlock() -> p<BlockAST>;
70
71 // Parse parameters
72 [[nodiscard]] auto ParseParams()
73 -> std::pair<std::vector<u<TypedVarAST>>, ParserError>;
74
75 // Utilities
76 [[nodiscard]] auto expect(TokenType tokType, bool exhausts = false,
77 TokenType until = TokenType::TokEOF,
78 const std::string &message = "")
79 -> std::shared_ptr<Token>;
80
81 [[nodiscard]] Parser(
82 std::optional<std::reference_wrapper<Reporter>> reporter = std::nullopt)
83 : reporter(reporter) {}
84 [[nodiscard]] Parser(
85 std::shared_ptr<TokenStream> tokStream,
86 std::optional<std::reference_wrapper<Reporter>> reporter = std::nullopt)
87 : reporter(reporter), tokStream(tokStream) {}
88
89 [[nodiscard]] auto Parse() -> u<ProgramAST>;
90};
91} // namespace sammine_lang
Holds declaration for all the AST Nodes.
Defines the token structure (TokenType, TokStream, TokenMap)
Holds classes and functionalities for dealing with Error handling, source locations caching & indexin...
auto ParseVarDef() -> p< ExprAST >
Parsing implementation for a variable decl/def.
Definition Parser.cpp:258
Definition Utilities.h:56
Definition Utilities.h:107