-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
94 lines (72 loc) · 2.38 KB
/
parser.cpp
File metadata and controls
94 lines (72 loc) · 2.38 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
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
#include "parser.h"
#include "lexer.h"
#include "number.h"
#include "binaryoperator.h"
Parser::Parser(Lexer &lexer)
: _lexer(lexer), _currentToken(lexer.getNextToken())
{
}
const AstNode *Parser::parse()
{
auto rootNode = this->expression();
// Check if there is unconsumed token, if so than it's a syntax error.
if (this->_currentToken.type() != Token::TT_EOF) {
throw "Invalid syntax";
}
return rootNode;
}
const AstNode *Parser::expression()
{
// From grammar:
// <expression> := <term> {(PLUS|MINUS) <term>}
auto node = this->term();
while (this->_currentToken.type() == Token::TT_PLUS ||
this->_currentToken.type() == Token::TT_MINUS) {
const Token token = this->_currentToken;
this->eat(this->_currentToken.type()); // Eat '+' or '-' token.
node = new BinaryOperator(node, token, this->term());
}
return node;
}
const AstNode *Parser::term()
{
// From grammar:
// <term> := <factor> {(MULTIPLY|DIVIDE) <factor>}
auto node = this->factor();
while (this->_currentToken.type() == Token::TT_MULTIPLY ||
this->_currentToken.type() == Token::TT_DIVIDE) {
const Token token = this->_currentToken;
this->eat(this->_currentToken.type()); // Eat '*' or '/' token.
node = new BinaryOperator(node, token, this->factor());
}
return node;
}
const AstNode *Parser::factor()
{
// From grammar:
// <factor> := (NUMBER|LEFT_PARENTHESIS <expression> RIGHT_PARENTHESIS)
if (this->_currentToken.type() == Token::TT_NUMBER) {
const Token token = this->_currentToken;
this->eat(Token::TT_NUMBER);
if (this->_currentToken.type() == Token::TT_NUMBER) {
// We found multidigit number in input.
throw "Invalid syntax";
}
return new Number(token);
} else if (this->_currentToken.type() == Token::TT_LEFT_PARENTHESIS) {
this->eat(Token::TT_LEFT_PARENTHESIS);
auto node = this->expression();
this->eat(Token::TT_RIGHT_PARENTHESIS);
return node;
} else {
throw "Something went wrong!"; // Unexpected situation. API misuse.
}
}
void Parser::eat(Token::TokenType type)
{
if (_currentToken.type() == type) {
this->_currentToken = this->_lexer.getNextToken();
} else {
throw "Invalid syntax."; // TODO: Add more details about the error.
}
}