-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
136 lines (113 loc) · 3.31 KB
/
parser.cpp
File metadata and controls
136 lines (113 loc) · 3.31 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "parser.h"
#include <stdexcept>
Parser::Parser(Lexer & lexer, Database & database) : lexer(lexer), database(database) {
currentToken = lexer.getNextToken();
}
void Parser::match(TokenType expected) {
if (currentToken.type == expected) {
currentToken = lexer.getNextToken();
} else {
throw runtime_error("Syntax Error: Unexpected token " + currentToken.value);
}
}
void Parser::parseCreateStatement() {
match(CREATE);
match(TABLE);
string tableName = currentToken.value;
match(IDENTIFIER);
database.createTable(tableName);
match(LPAREN);
do {
string columnName = currentToken.value;
match(IDENTIFIER);
string dataType;
if (currentToken.type == INTEGER) {
dataType = "INTEGER";
match(INTEGER);
} else if (currentToken.type == VARCHAR) {
dataType = "VARCHAR";
match(VARCHAR);
match(LPAREN);
match(NUMBER);
match(RPAREN);
} else if (currentToken.type == DATE) {
dataType = "DATE";
match(DATE);
} else {
throw runtime_error("Syntax Error: Invalid data type.");
}
Column column(columnName, dataType);
database.addColumnToTable(tableName, column);
if (currentToken.type == COMMA) {
match(COMMA);
} else {
break;
}
} while (true);
match(RPAREN);
match(SEMICOLON);
}
void Parser::parseInsertStatement() {
match(INSERT);
match(INTO);
string tableName = currentToken.value;
match(IDENTIFIER);
match(VALUES);
match(LPAREN);
vector<string> values;
do {
if (currentToken.type == NUMBER || currentToken.type == STRING) {
values.push_back(currentToken.value);
match(currentToken.type);
} else {
throw runtime_error("Syntax Error: Invalid value.");
}
if (currentToken.type == COMMA) {
match(COMMA);
} else {
break;
}
} while (true);
match(RPAREN);
match(SEMICOLON);
Row row(values);
database.addRowToTable(tableName, row);
}
void Parser::parseSelectStatement() {
match(SELECT);
if (currentToken.type == STAR) {
match(STAR);
} else {
do {
match(IDENTIFIER);
if (currentToken.type == COMMA) {
match(COMMA);
} else {
break;
}
} while (true);
}
match(FROM);
string tableName = currentToken.value;
match(IDENTIFIER);
if (currentToken.type == WHERE) {
match(WHERE);
match(IDENTIFIER); // Column name
match(EQUALS); // Operator
match(NUMBER); // Value
}
match(SEMICOLON);
Table * table = database.findTable(tableName);
//table->print();
}
void Parser::parseStatement() {
if (currentToken.type == CREATE) {
parseCreateStatement();
} else if (currentToken.type == INSERT) {
parseInsertStatement();
} else if (currentToken.type == SELECT) {
parseSelectStatement();
} else {
throw runtime_error("Syntax Error: Invalid statement.");
}
}