-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.h
51 lines (43 loc) · 1.77 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
#ifndef __PARSER_H__
#define __PARSER_H__
#include "ast.h"
#include "scanner.h"
class Parser {
std::vector<Token> tokens;
size_t current;
Token advance();
Token peek();
Token consume(TokenType type, std::string error);
bool match(int count, ...);
void checkEof();
bool eof() { return current == tokens.size(); };
std::vector<Declaration*> program();
Declaration* decl(); // STMT | VAR_DECL
Statement* stmt(); // PRINT_STMT | BLOCK_STMT | EXPR_STMT | IF_STMT |
// FOR_STMT | WHILE_STMT | ASSERT_STMT
AssertStmt* assertStmt(); // ASSERT EXPR ;
PrintStmt* printStmt(); // PRINT EXPRESSION ;
BlockStmt* blockStmt(); // '{' PROGRAM '}';
ExprStmt* exprStmt(); // EXPRESSION ;
IfStmt* ifStmt(); // IF EXPRESSION STMT (ELSE STMT)?
BlockStmt* forStmt(); // FOR '(' VAR_DECL EXPRESSION; EXPRESSION ')'
WhileStmt* whileStmt(); // WHILE '(' EXPRESSION ')' STMT
ReturnStmt* returnStmt(); // RETURN EXPR;
VarDecl* varDecl(); // VAR IDENTIFIER (EQUAL EXPRESSION)? ;
FunDecl* funDecl(); // FUN IDENTIFIER '(' ARGS? ')' BLOCK
Args args(); // ID (, ID)*
RealArgs real_args(); // EXPR (, EXPR)*
Expr* expression(); // ASSIGN
Expr* assignment(); // LVAL '=' EQUALITY | EQUALITY
Expr* equality(); // COMP ('==' | '!=') COMP | COMP
Expr* comparsion(); // TERM ('>' | '>=' | '<' | '<=') TERM | TERM
Expr* term(); // FACTOR (('+' | '-') FACTOR)*
Expr* factor(); // UNARY (('/' | '*') UNARY)*
Expr* unary(); // (! | - | -- | ++)* POSTFIX
Expr* postfix(); // CALL (++ | --)*
Expr* call(); // PRIM ('(' ARGS? ')')*
Expr* primary();
public:
std::vector<Declaration*> parse(const std::vector<Token>& tokens);
};
#endif