-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.h
70 lines (60 loc) · 1.15 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
#pragma once
#include <list>
#include <deque>
#include "lexer.h"
enum NodeType
{
Program,
Decl,
Ident,
Expr,
Block,
Statement,
OpAssign,
OpBool,
OpEq,
OpAdd,
OpMult,
OpUnaryPrefix,
OpUnaryPostfix,
OpCallLookup,
OpEnd
};
extern std::array<std::string, 15> NodeTypeStrings;
struct Node
{
NodeType t; // type
std::vector<Node> c; // children
optional<Token> tok;
};
class Parser
{
public:
Parser(std::vector<Token> t) : tokens(t.begin(), t.end()) {}
Node Parse();
std::string ToString();
private:
Token Expect(TokenType tt);
Token Expect(std::vector<TokenType> tt);
optional<Token> Accept(TokenType tt);
optional<Token> Accept(std::vector<TokenType> tt);
//void Check();
std::string ToString(int indent, Node n);
Node Program();
Node Decl();
Node Ident();
Node Expr();
Node Block();
Node Statement();
Node OpAssign();
Node OpBool();
Node OpEq();
Node OpAdd();
Node OpMult();
Node OpUnary();
Node OpCallLookup();
Node OpEnd();
std::deque<Token> tokens;
std::list<Token>::iterator end;
Node p;
};