-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathTokenHandler.h
More file actions
68 lines (59 loc) · 1.75 KB
/
TokenHandler.h
File metadata and controls
68 lines (59 loc) · 1.75 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
59
60
61
62
63
64
65
66
67
68
#ifndef _TOKEN_HANDLER_GUARD
#define _TOKEN_HANDLER_GUARD
#include "TokenStream.h"
#include "DataHandler.h"
#include <vector>
#include <functional>
#include "Ast.h"
/**
* Creates an AST from a ::TokenStream.
*/
class Parser {
typedef std::function<void(Parser*)> Handler;
typedef std::map<TokenType, Handler> HandlerMap;
TokenStream& ts;
TokenStream::iterator current;
DataHandler& data_handler;
HandlerMap handlers;
Ast::Block* program;
/**
* Gets a ::Token from the ::TokenStream but skips one optional token of
* a given type.
* @param type the type of ::Token to skip
*/
void skipOptional(TokenType type);
void readBlock(std::vector<Token>& tokens, TokenType begin = TokenType::BlockBegin);
/**
* Insert a ::Token before current + offset.
*/
void insert(const Token& t, int offset = 1);
/**
* Handles an unexpected token in Parser::handleToken.
* @see Parser::handleToken
*/
void handleUnexpectedToken();
bool handleToken();
void handleIdentifier();
void handleFuncImpl();
void handle_declaration();
void handle_setvar();
void handle_if();
void handle_while();
void handle_while_run();
/**
* Parses the next tokens as expected for an Ast::FunctionCall.
* @param in_expr determines whether this function call should be seen
* as a separate statement (if so, false)
*/
Ast::FunctionCall* handleFunctionCall(bool in_expr = true);
Ast::Expression* expression();
Ast::Expression* term();
Ast::UnaryOp* primary();
Ast::Condition* condition_term();
Ast::Condition* condition();
void setupHandlers();
public:
Parser(TokenStream& tokens, DataHandler& data);
Ast::Block* run();
};
#endif