-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
176 lines (134 loc) · 4.06 KB
/
parser.cpp
File metadata and controls
176 lines (134 loc) · 4.06 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
Grammar:
----------------------------------------------
JSON -> null
or, Object
Object -> Map
or, List
or, Integer
or, String
or, Boolean
or, NULL
Integer -> (Digit)+
String -> '"' (Char)* '"'
Boolean -> 'true'
or, 'false'
Null -> 'null'
Map -> '{' PairList '}'
PairList -> null
or, Pair (',' Pair)*
Pair -> String ':' Object
List -> '[' ValueList ']'
ValueList -> null
or, Object (',' Object)*
*/
#include "parse_tree_nodes.h"
#include "lexer.cpp"
class Parser {
queue<Token> q;
Object *root;
public:
Parser(const string &input) {
q = Lexer(input).tonenize();
root = nullptr;
}
Object *parse() {
if (root != nullptr) return root;
root = parse_json();
if (!q.empty())
throw runtime_error("Trailing characters!");
return root;
}
private:
bool match(TokenType type) {
return q.size() && q.front().type == type;
}
bool token_is_object(Token type) {
return !match(TokenType::COMMA)
&& !match(TokenType::COLON);
}
Object *parse_json() {
if (q.empty()) return new Null();
else return parse_object();
}
Object *parse_object() {
Object *x = nullptr;
if (match(TokenType::OPENING_CURLY_BRACKET)) {
x = parse_map();
} else if (match(TokenType::OPENING_SQUARE_BRACKET)) {
x = parse_list();
} else if (match(TokenType::INTEGER)) {
x = new Integer(q.front().val);
q.pop();
} else if (match(TokenType::STRING)) {
x = new String(q.front().val);
q.pop();
} else if (match(TokenType::BOOLEAN_FALSE) || match(TokenType::BOOLEAN_TRUE)) {
x = new Boolean(q.front().val);
q.pop();
} else if (match(TokenType::NULL_TYPE)) {
x = new Null();
q.pop();
} else {
throw runtime_error("Invalid token!");
}
return x;
}
List *parse_list() {
if (!match(TokenType::OPENING_SQUARE_BRACKET))
throw runtime_error("'[' missing for list!");
//* pop the opening bracket
q.pop();
List *x = parse_value_list();
//* pop the closing bracket
if (!match(TokenType::CLOSING_SQUARE_BRACKET))
throw runtime_error("Missing ']'!");
q.pop();
return x;
}
List *parse_value_list() {
List *x = new List();
while (!q.empty() && !match(TokenType::CLOSING_SQUARE_BRACKET)) {
x->add_element(parse_object());
if (!match(TokenType::COMMA))
break;
//* pop the comma
q.pop();
}
return x;
}
Map *parse_map() {
if (!match(TokenType::OPENING_CURLY_BRACKET))
throw runtime_error("'{' missing for map!");
//* pop the opening bracket
q.pop();
Map *x = parse_pair_list();
//* pop the closing bracket
if (!match(TokenType::CLOSING_CURLY_BRACKET))
throw runtime_error("Missing '}'!");
q.pop();
return x;
}
Map *parse_pair_list() {
Map *x = new Map();
while (!q.empty() && !match(TokenType::CLOSING_CURLY_BRACKET)) {
if (!match(TokenType::STRING))
throw runtime_error("Map key must be string!");
string key = q.front().val;
q.pop();
if (!match(TokenType::COLON))
throw runtime_error("Colon missing for map key:value pair!");
//* pop the colon
q.pop();
if (!token_is_object(q.front()))
throw runtime_error("Value missing for map key!");
Object *val = parse_object();
x->add_element(key, val);
if (!match(TokenType::COMMA))
break;
//* pop comma
q.pop();
}
return x;
}
};