-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.h
139 lines (119 loc) · 3.26 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#pragma once
#include <vector>
#include <cassert>
#include <iostream>
#include <unordered_map>
#include "Token.h"
#include "Ast.h"
#include "Utils.h"
#include "Type.h"
namespace lwscript
{
enum class Precedence
{
LOWEST = 0, // ,
ASSIGN, // = += -= *= /= %= &= |= ^= <<= >>=
CONDITION, // ?:
OR, // ||
AND, // &&
BIT_OR, // |
BIT_XOR, // ^
BIT_AND, // &
EQUAL, // == !=
COMPARE, // < <= > >=
BIT_SHIFT, // >> <<
ADD_PLUS, // + -
MUL_DIV_MOD, // * / %
PREFIX, // ! ~ - & ++ --
INFIX, // [] () . !
POSTFIX, // ++ -- !
};
enum class Associativity
{
L2R, // left->right
R2L // right->left
};
struct ClassInfo
{
ClassInfo *enclosing = nullptr;
bool hasSuperClass = false;
STD_STRING name;
};
class Parser;
typedef Expr *(Parser::*PrefixFn)();
typedef Expr *(Parser::*InfixFn)(Expr *);
typedef Expr *(Parser::*PostfixFn)(Expr *);
class LWSCRIPT_API Parser
{
NON_COPYABLE(Parser)
public:
Parser();
~Parser();
Stmt *Parse(const std::vector<Token *> &tokens);
private:
void ResetStatus();
Stmt *ParseDecl();
Stmt *ParseVarDecl();
Stmt *ParseFunctionDecl();
Stmt *ParseClassDecl();
Stmt *ParseEnumDecl();
Stmt *ParseModuleDecl();
Stmt *ParseStmt();
Stmt *ParseExprStmt();
Stmt *ParseReturnStmt();
Stmt *ParseIfStmt();
Stmt *ParseScopeStmt();
Stmt *ParseWhileStmt();
Stmt *ParseForStmt();
Stmt *ParseBreakStmt();
Stmt *ParseContinueStmt();
Stmt *ParseSwitchStmt();
Expr *ParseExpr(Precedence precedence = Precedence::LOWEST);
Expr *ParseIdentifierExpr();
Expr *ParseLiteralExpr();
Expr *ParseGroupExpr();
Expr *ParseArrayExpr();
Expr *ParseDictExpr();
Expr *ParseStructExpr();
Expr *ParsePrefixExpr();
Expr *ParseRefExpr();
Expr *ParseLambdaExpr();
Expr *ParseNewExpr();
Expr *ParseThisExpr();
Expr *ParseBaseExpr();
Expr *ParseMatchExpr();
Expr *ParseCompoundExpr();
Expr *ParseInfixExpr(Expr *prefixExpr);
Expr *ParsePostfixExpr(Expr *prefixExpr);
Expr *ParseConditionExpr(Expr *prefixExpr);
Expr *ParseIndexExpr(Expr *prefixExpr);
Expr *ParseCallExpr(Expr *prefixExpr);
Expr *ParseDotExpr(Expr *prefixExpr);
Expr *ParseFactorialExpr(Expr *prefixExpr);
Expr *ParseVarDescExpr();
Expr *ParseVarArgExpr();
std::pair<Expr *, Expr *> ParseDestructuringAssignmentExpr();
Type ParseType();
Token *GetCurToken();
Token *GetCurTokenAndStepOnce();
Precedence GetCurTokenPrecedence();
Associativity GetCurTokenAssociativity();
Token *GetNextToken();
Token *GetNextTokenAndStepOnce();
Precedence GetNextTokenPrecedence();
bool IsMatchCurToken(TokenKind kind);
bool IsMatchCurTokenAndStepOnce(TokenKind kind);
bool IsMatchNextToken(TokenKind kind);
bool IsMatchNextTokenAndStepOnce(TokenKind kind);
Token *Consume(TokenKind kind, STD_STRING_VIEW errMsg);
Token *Consume(const std::vector<TokenKind> &kinds, STD_STRING_VIEW errMsg);
bool IsAtEnd();
ClassInfo *mCurClassInfo;
std::vector<TokenKind> mSkippingConsumeTokenKindStack; // skip token while call consume function
int64_t mCurPos;
std::vector<Token *> mTokens;
static std::unordered_map<TokenKind, PrefixFn> mPrefixFunctions;
static std::unordered_map<TokenKind, InfixFn> mInfixFunctions;
static std::unordered_map<TokenKind, PostfixFn> mPostfixFunctions;
};
}