-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.h
More file actions
58 lines (48 loc) · 1.34 KB
/
Copy pathparser.h
File metadata and controls
58 lines (48 loc) · 1.34 KB
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
#ifndef PARSER_H
#define PARSER_H
#include <vector>
#include <initializer_list>
#include "token.h"
#include "ast.h"
// Parser turns a flat list of tokens into an abstract syntax tree. It handles
// declarations, statements and expressions for FunLang.
class Parser {
public:
explicit Parser(const std::vector<Token>& tokens);
std::vector<StmtPtr> parseProgram();
private:
std::vector<Token> tks;
size_t cur;
bool isAtEnd() const;
const Token& peek() const;
const Token& prev() const;
const Token& advance();
bool check(TokenType t) const;
bool match(TokenType t);
bool matchAny(std::initializer_list<TokenType> types);
const Token& consume(TokenType t, const std::string& msg);
bool isTypeToken(const Token& tk) const;
// top‑level declarations or statements
StmtPtr declOrStmt();
StmtPtr funcDecl();
StmtPtr varDecl();
StmtPtr printStmt();
StmtPtr assignOrExprStmt();
// new statements
StmtPtr whileStmt();
StmtPtr forStmt();
StmtPtr ifStmt();
StmtPtr parseForAssignOrEmpty(); // parse IDENT '=' expr or empty
// expressions
ExprPtr expression();
ExprPtr logicOr();
ExprPtr logicAnd();
ExprPtr equality();
ExprPtr comparison();
ExprPtr term();
ExprPtr factor();
ExprPtr unary();
ExprPtr call();
ExprPtr primary();
};
#endif