-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
118 lines (95 loc) · 2.9 KB
/
Copy pathparser.cpp
File metadata and controls
118 lines (95 loc) · 2.9 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* @author Thadeu <thadeutucci@gmail.com>
* @date 2019
* Parser.
* expr : term ((PLUS | MINUS) term)*
* term : factor ((MUL | DIV) factor)*
* factor : INTEGER | LPAREN expr RPAREN
*/
#include "parser.h"
using namespace std;
Parser::Parser(const string& _expression) : expression(_expression)
{
this->currentTokenIdx = -1;
}
unique_ptr<ASTNode> Parser::parse()
{
nextToken();
unique_ptr<ASTNode> node = expr();
// Error if parser could not read to the end of expression
if (static_cast<size_t>(currentTokenIdx) < this->expression.size()) {
throw invalid_argument("Could not parse expression to the end.");
}
return node;
}
void Parser::nextToken()
{
this->currentTokenIdx++;
if (static_cast<size_t>(currentTokenIdx) < this->expression.size())
this->currentToken = this->expression.at(this->currentTokenIdx);
// Ignore spaces and tabs.
while (this->currentToken == ' '
|| this->currentToken == '\t') {
this->currentTokenIdx++;
if (static_cast<size_t>(currentTokenIdx) >= this->expression.size())
break;
this->currentToken = this->expression.at(this->currentTokenIdx);
}
}
TokenType Parser::getCurrentTokenType()
{
if (this->currentToken == '(')
return TokenType::leftParen;
if (this->currentToken == ')')
return TokenType::rightParen;
if (this->currentToken >= '0' && this->currentToken <= '9')
return TokenType::number;
if (this->currentToken == '+' || this->currentToken == '-')
return TokenType::plusMinusOperator;
if (this->currentToken == '*' || this->currentToken == '/')
return TokenType::multDivOperator;
return TokenType::undefined;
}
void Parser::assertTokenType(TokenType tokenType)
{
if (this->getCurrentTokenType() != tokenType)
{
throw invalid_argument("unexpected token.");
}
}
unique_ptr<ASTNode> Parser::expr()
{
unique_ptr<ASTNode> node = term();
while (getCurrentTokenType() == TokenType::plusMinusOperator)
{
char value = this->currentToken;
nextToken();
node = unique_ptr<InternalNode>(new InternalNode(std::move(node), term(), value));
}
return node;
}
unique_ptr<ASTNode> Parser::term()
{
unique_ptr<ASTNode> node = factor();
while (getCurrentTokenType() == TokenType::multDivOperator)
{
char value = this->currentToken;
nextToken();
node = unique_ptr<InternalNode>(new InternalNode(move(node), factor(), value));
}
return node;
}
unique_ptr<ASTNode> Parser::factor()
{
if (getCurrentTokenType() == TokenType::number) {
char value = this->currentToken;
nextToken();
return unique_ptr<LeafNode>(new LeafNode(value));
}
assertTokenType(TokenType::leftParen);
nextToken();
unique_ptr<ASTNode> node = expr();
assertTokenType(TokenType::rightParen);
nextToken();
return node;
}