-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.h
96 lines (79 loc) · 2.08 KB
/
Parser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma once
#include <vector>
#include <cassert>
#include <iostream>
#include <unordered_map>
#include "Token.h"
#include "Ast.h"
#include "Utils.h"
#include "ConstantFolder.h"
enum class Precedence
{
LOWEST = 0, // ,
ASSIGN, // =
OR, // or
AND, // and
BIT_OR, // |
BIT_XOR, // ^
BIT_AND, // &
EQUAL, // == !=
COMPARE, // < <= > >=
ADD_PLUS, // + -
MUL_DIV, // * /
UNARY, // not - ~
CALL, // [] () .
};
class Parser;
typedef Expr *(Parser::*PrefixFn)();
typedef Expr *(Parser::*InfixFn)(Expr *);
class COMPUTE_DUCK_API Parser
{
public:
Parser();
~Parser();
std::vector<Stmt *> Parse(const std::vector<Token> &tokens);
private:
Stmt *ParseStmt();
Stmt *ParseExprStmt();
Stmt *ParseReturnStmt();
Stmt *ParseIfStmt();
Stmt *ParseScopeStmt();
Stmt *ParseWhileStmt();
Stmt *ParseStructStmt();
Stmt *ParseDllImportStmt();
Expr *ParseExpr(Precedence precedence = Precedence::LOWEST);
Expr *ParseIdentifierExpr();
Expr *ParseNumExpr();
Expr *ParseStrExpr();
Expr *ParseNilExpr();
Expr *ParseBoolExpr();
Expr *ParseGroupExpr();
Expr *ParseArrayExpr();
Expr *ParseUnaryExpr();
Expr *ParseRefExpr();
Expr *ParseFunctionExpr();
Expr *ParseStructExpr();
Expr *ParseBinaryExpr(Expr *unaryExpr);
Expr *ParseIndexExpr(Expr *unaryExpr);
Expr *ParseFunctionCallExpr(Expr *unaryExpr);
Expr *ParseStructCallExpr(Expr *unaryExpr);
Token GetCurToken();
Token GetCurTokenAndStepOnce();
Precedence GetCurTokenPrecedence();
Token GetNextToken();
Token GetNextTokenAndStepOnce();
Precedence GetNextTokenPrecedence();
bool IsMatchCurToken(TokenType type);
bool IsMatchCurTokenAndStepOnce(TokenType type);
bool IsMatchNextToken(TokenType type);
bool IsMatchNextTokenAndStepOnce(TokenType type);
Token Consume(TokenType type, std::string_view errMsg);
bool IsAtEnd();
int64_t m_CurPos;
std::vector<Token> m_Tokens;
int32_t m_FunctionScopeDepth;
ConstantFolder m_ConstantFolder;
static std::unordered_map<TokenType, PrefixFn> m_PrefixFunctions;
static std::unordered_map<TokenType, InfixFn> m_InfixFunctions;
static std::unordered_map<TokenType, Precedence> m_Precedence;
};