-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.cs
228 lines (219 loc) · 5.9 KB
/
Parser.cs
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Collections.Generic;
namespace JSONParser
{
public class Parser
{
private List<Token> _tokens;
private int index;
public List<Token> Tokens { get => _tokens; set => _tokens = value; }
private List<string> _errors;
public List<string> Errors => _errors;
public Parser(Lexer lexer)
{
_tokens = lexer.Tokens;
_errors = lexer.Errors;
index = 0;
}
private Token next()
{
if (index >= _tokens.Count)
{
return new Token(TokenType.EOF, "\0");
}
return _tokens[index++];
}
private Token peek(int a = 1)
{
if (index + a >= _tokens.Count)
{
return new Token(TokenType.EOF, "\0");
}
return _tokens[index + a];
}
private Token Current => peek(0);
private Token match(params TokenType[] types)
{
foreach (TokenType type in types)
{
if (Current.Type == type)
{
return next();
}
}
throw new Exception($"Unexpected Token {Current.Type} '{Current.Value}'. Expected {types[0]}");
}
private Dictionary<string, dynamic> parse_object()
{
var dict = new Dictionary<string, dynamic>();
match(TokenType.OPENING_CURLY_BRACE);
while (Current.Type != TokenType.CLOSING_CURLY_BRACE && Current.Type != TokenType.EOF)
{
var string_val = parse_string();
match(TokenType.COLON);
dict.Add(string_val, parse());
if (Current.Type == TokenType.COMMA)
{
next();
}
}
match(TokenType.CLOSING_CURLY_BRACE);
return dict;
}
private List<dynamic> parse_list()
{
var list = new List<dynamic>();
match(TokenType.OPENING_BRACKET);
while (Current.Type != TokenType.CLOSING_BRACKET)
{
list.Add(parse());
if (Current.Type == TokenType.COMMA)
{
next();
if (Current.Type == TokenType.CLOSING_BRACKET)
{
throw new Exception("Unexpected comma at end of list");
}
}
else
{
break;
}
}
match(TokenType.CLOSING_BRACKET);
return list;
}
private string parse_string()
{
match(TokenType.QUOTE);
var stringToken = match(TokenType.STRING);
match(TokenType.QUOTE);
return stringToken.Value;
}
private dynamic parse()
{
if (_errors.Count > 0)
{
throw new Exception("Invalid JSON");
}
switch (Current.Type)
{
case TokenType.OPENING_CURLY_BRACE:
return parse_object();
case TokenType.NUMBER:
return float.Parse(next().Value);
case TokenType.QUOTE:
return parse_string();
case TokenType.OPENING_BRACKET:
return parse_list();
case TokenType.TRUE:
next();
return true;
case TokenType.FALSE:
next();
return false;
case TokenType.NULL:
next();
return null!;
default:
throw new Exception($"Expected list, object, integer or string after \":\", got \"{Current.Value}\"");
};
}
public dynamic ParseJson()
{
if (_errors.Count > 0)
{
throw new Exception("Invalid JSON");
}
dynamic result;
switch (Current.Type)
{
case TokenType.OPENING_CURLY_BRACE:
result = parse_object();
match(TokenType.EOF);
return result;
case TokenType.OPENING_BRACKET:
result = parse_list();
match(TokenType.EOF);
return result;
default:
throw new Exception("Expected list or object");
};
}
public static void PrettyPrint(dynamic arg, int indent = 1)
{
if (arg is float)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write(arg);
Console.ResetColor();
return;
}
if (arg is string)
{
Console.ForegroundColor = ConsoleColor.Black;
Console.Write($"\"");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(arg);
Console.ForegroundColor = ConsoleColor.Black;
Console.Write($"\"");
Console.ResetColor();
return;
}
if (arg is bool)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write(arg ? "true" : "false");
Console.ResetColor();
return;
}
if (arg is null)
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write("null");
Console.ResetColor();
return;
}
var padding = new string(' ', indent * 2);
if (arg is List<dynamic> list)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("[");
Console.ResetColor();
for (int i = 0; i < list.Count; i++)
{
PrettyPrint(list[i]);
if (i != list.Count - 1)
Console.Write(", ");
}
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("]");
Console.ResetColor();
}
if (arg is Dictionary<string, dynamic> dict)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write($"{{\n");
Console.ResetColor();
foreach (var pair in dict)
{
Console.ForegroundColor = ConsoleColor.Black;
Console.Write($"{padding}\"");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write(pair.Key);
Console.ForegroundColor = ConsoleColor.Black;
Console.Write("\"");
Console.ResetColor();
Console.Write(" -> ");
PrettyPrint(pair.Value, indent + 1);
Console.Write("\n");
}
padding = new string(' ', (indent - 1) * 2);
Console.ForegroundColor = ConsoleColor.Green;
Console.Write($"{padding}}}");
Console.ResetColor();
}
Console.Write("");
}
}
}