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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
const std = @import("std");
const jv = @import("../jv.zig");
const Token = @import("./tokenize.zig").Token;
const TokenKind = @import("./tokenize.zig").TokenKind;
pub const ParseError = error{
UnexpectedEnd,
InvalidQuery,
};
pub const AstKind = enum {
identity,
array_index,
object_key,
literal,
binary_expr,
pipe,
comma,
};
pub const BinaryOp = enum {
alt,
assign,
update,
alt_assign,
add_assign,
sub_assign,
mul_assign,
div_assign,
mod_assign,
@"or",
@"and",
eq,
ne,
lt,
gt,
le,
ge,
add,
sub,
mul,
div,
mod,
};
pub const Ast = union(AstKind) {
identity,
array_index: struct { base: *Ast, index: *Ast },
object_key: []const u8,
literal: *jv.Value,
binary_expr: struct { op: BinaryOp, lhs: *Ast, rhs: *Ast },
pipe: struct { lhs: *Ast, rhs: *Ast },
comma: struct { lhs: *Ast, rhs: *Ast },
pub fn kind(self: @This()) AstKind {
return self;
}
};
pub const TokenStream = struct {
const Self = @This();
tokens: []const Token,
current_position: usize,
pub fn init(tokens: []const Token) Self {
return .{
.tokens = tokens,
.current_position = 0,
};
}
pub fn next(self: *Self) ParseError!Token {
if (self.current_position >= self.tokens.len) {
return error.UnexpectedEnd;
}
const token = self.tokens[self.current_position];
self.current_position += 1;
return token;
}
pub fn peek(self: *Self) ParseError!Token {
if (self.current_position >= self.tokens.len) {
return error.UnexpectedEnd;
}
return self.tokens[self.current_position];
}
pub fn expect(self: *Self, expected: TokenKind) ParseError!Token {
const token = try self.next();
if (token.kind() != expected) {
return error.InvalidQuery;
}
return token;
}
};
pub fn parse(allocator: std.mem.Allocator, parse_allocator: std.mem.Allocator, tokens: []const Token) !*Ast {
var token_stream = TokenStream.init(tokens);
return parseQuery(allocator, parse_allocator, &token_stream);
}
fn parseProgram(allocator: std.mem.Allocator, parse_allocator: std.mem.Allocator, tokens: *TokenStream) !*Ast {
return parseBody(allocator, parse_allocator, tokens);
}
fn parseBody(allocator: std.mem.Allocator, parse_allocator: std.mem.Allocator, tokens: *TokenStream) !*Ast {
return parseQuery(allocator, parse_allocator, tokens);
}
fn parseQuery(allocator: std.mem.Allocator, parse_allocator: std.mem.Allocator, tokens: *TokenStream) !*Ast {
return parseQuery2(allocator, parse_allocator, tokens);
}
fn parseQuery2(allocator: std.mem.Allocator, parse_allocator: std.mem.Allocator, tokens: *TokenStream) !*Ast {
var lhs = try parseQuery3(allocator, parse_allocator, tokens);
while (true) {
const token = tokens.peek() catch break;
if (token.kind() == .pipe) {
_ = try tokens.next();
const rhs = try parseQuery3(allocator, parse_allocator, tokens);
const ast = try parse_allocator.create(Ast);
ast.* = .{ .pipe = .{
.lhs = lhs,
.rhs = rhs,
} };
lhs = ast;
} else {
break;
}
}
_ = try tokens.expect(.end);
return lhs;
}
fn parseQuery3(allocator: std.mem.Allocator, parse_allocator: std.mem.Allocator, tokens: *TokenStream) !*Ast {
var lhs = try parseExpr(allocator, parse_allocator, tokens);
while (true) {
const token = tokens.peek() catch return lhs;
if (token.kind() == .comma) {
_ = try tokens.next();
const rhs = try parseExpr(allocator, parse_allocator, tokens);
const ast = try parse_allocator.create(Ast);
ast.* = .{ .comma = .{
.lhs = lhs,
.rhs = rhs,
} };
lhs = ast;
} else {
break;
}
}
return lhs;
}
fn parseExpr(allocator: std.mem.Allocator, parse_allocator: std.mem.Allocator, tokens: *TokenStream) !*Ast {
var lhs = try parseExpr2(allocator, parse_allocator, tokens);
while (true) {
const token = try tokens.peek();
if (token.kind() == .slash_slash) {
_ = try tokens.next();
const rhs = try parseExpr2(allocator, parse_allocator, tokens);
const ast = try parse_allocator.create(Ast);
ast.* = .{ .binary_expr = .{
.op = .alt,
.lhs = lhs,
.rhs = rhs,
} };
lhs = ast;
} else {
break;
}
}
return lhs;
}
fn parseExpr2(allocator: std.mem.Allocator, parse_allocator: std.mem.Allocator, tokens: *TokenStream) !*Ast {
const lhs = try parseExpr3(allocator, parse_allocator, tokens);
const token = tokens.peek() catch return lhs;
const op: BinaryOp = switch (token.kind()) {
.equal => .assign,
.pipe_equal => .update,
.slash_slash_equal => .alt_assign,
.plus_equal => .add_assign,
.minus_equal => .sub_assign,
.asterisk_equal => .mul_assign,
.slash_equal => .div_assign,
.percent_equal => .mod_assign,
else => return lhs,
};
_ = try tokens.next();
const rhs = try parseExpr3(allocator, parse_allocator, tokens);
const ast = try parse_allocator.create(Ast);
ast.* = .{ .binary_expr = .{
.op = op,
.lhs = lhs,
.rhs = rhs,
} };
return ast;
}
fn parseExpr3(allocator: std.mem.Allocator, parse_allocator: std.mem.Allocator, tokens: *TokenStream) !*Ast {
const lhs = try parseExpr4(allocator, parse_allocator, tokens);
const token = tokens.peek() catch return lhs;
if (token.kind() != .keyword_or) {
return lhs;
}
_ = try tokens.next();
const rhs = try parseExpr4(allocator, parse_allocator, tokens);
const ast = try parse_allocator.create(Ast);
ast.* = .{ .binary_expr = .{
.op = .@"or",
.lhs = lhs,
.rhs = rhs,
} };
return ast;
}
fn parseExpr4(allocator: std.mem.Allocator, parse_allocator: std.mem.Allocator, tokens: *TokenStream) !*Ast {
const lhs = try parseExpr5(allocator, parse_allocator, tokens);
const token = tokens.peek() catch return lhs;
if (token.kind() != .keyword_and) {
return lhs;
}
_ = try tokens.next();
const rhs = try parseExpr5(allocator, parse_allocator, tokens);
const ast = try parse_allocator.create(Ast);
ast.* = .{ .binary_expr = .{
.op = .@"and",
.lhs = lhs,
.rhs = rhs,
} };
return ast;
}
fn parseExpr5(allocator: std.mem.Allocator, parse_allocator: std.mem.Allocator, tokens: *TokenStream) !*Ast {
const lhs = try parseExpr6(allocator, parse_allocator, tokens);
const token = tokens.peek() catch return lhs;
const op: BinaryOp = switch (token.kind()) {
.equal_equal => .eq,
.not_equal => .ne,
.less_than => .lt,
.greater_than => .gt,
.less_than_equal => .le,
.greater_than_equal => .ge,
else => return lhs,
};
_ = try tokens.next();
const rhs = try parseExpr6(allocator, parse_allocator, tokens);
const ast = try parse_allocator.create(Ast);
ast.* = .{ .binary_expr = .{
.op = op,
.lhs = lhs,
.rhs = rhs,
} };
return ast;
}
fn parseExpr6(allocator: std.mem.Allocator, parse_allocator: std.mem.Allocator, tokens: *TokenStream) !*Ast {
var lhs = try parseExpr7(allocator, parse_allocator, tokens);
while (true) {
const token = tokens.peek() catch return lhs;
const op: BinaryOp = switch (token.kind()) {
.plus => .add,
.minus => .sub,
else => return lhs,
};
_ = try tokens.next();
const rhs = try parseExpr7(allocator, parse_allocator, tokens);
const ast = try parse_allocator.create(Ast);
ast.* = .{ .binary_expr = .{
.op = op,
.lhs = lhs,
.rhs = rhs,
} };
lhs = ast;
}
}
fn parseExpr7(allocator: std.mem.Allocator, parse_allocator: std.mem.Allocator, tokens: *TokenStream) !*Ast {
var lhs = try parseTerm(allocator, parse_allocator, tokens);
while (true) {
const token = tokens.peek() catch return lhs;
const op: BinaryOp = switch (token.kind()) {
.asterisk => .mul,
.slash => .div,
.percent => .mod,
else => return lhs,
};
_ = try tokens.next();
const rhs = try parseTerm(allocator, parse_allocator, tokens);
const ast = try parse_allocator.create(Ast);
ast.* = .{ .binary_expr = .{
.op = op,
.lhs = lhs,
.rhs = rhs,
} };
lhs = ast;
}
}
fn parseTerm(allocator: std.mem.Allocator, parse_allocator: std.mem.Allocator, tokens: *TokenStream) !*Ast {
var result = try parsePrimary(allocator, parse_allocator, tokens);
while (true) {
const token = tokens.peek() catch return result;
if (token.kind() == .bracket_left) {
result = try parseSuffix(allocator, parse_allocator, tokens, result);
} else {
break;
}
}
return result;
}
fn parsePrimary(allocator: std.mem.Allocator, parse_allocator: std.mem.Allocator, tokens: *TokenStream) !*Ast {
const first_token = try tokens.peek();
switch (first_token) {
.keyword_null => {
_ = try tokens.next();
const null_value = try allocator.create(jv.Value);
null_value.* = .null;
const null_node = try parse_allocator.create(Ast);
null_node.* = .{ .literal = null_value };
return null_node;
},
.keyword_true => {
_ = try tokens.next();
const true_value = try allocator.create(jv.Value);
true_value.* = .{ .bool = true };
const true_node = try parse_allocator.create(Ast);
true_node.* = .{ .literal = true_value };
return true_node;
},
.keyword_false => {
_ = try tokens.next();
const false_value = try allocator.create(jv.Value);
false_value.* = .{ .bool = false };
const false_node = try parse_allocator.create(Ast);
false_node.* = .{ .literal = false_value };
return false_node;
},
.number => |f| {
_ = try tokens.next();
const number_value = try allocator.create(jv.Value);
const i: i64 = @intFromFloat(f);
if (@as(f64, @floatFromInt(i)) == f) {
number_value.* = .{ .integer = i };
} else {
number_value.* = .{ .float = f };
}
const number_node = try parse_allocator.create(Ast);
number_node.* = .{ .literal = number_value };
return number_node;
},
.string => |s| {
_ = try tokens.next();
const string_value = try allocator.create(jv.Value);
string_value.* = .{ .string = try allocator.dupe(u8, s) };
const string_node = try parse_allocator.create(Ast);
string_node.* = .{ .literal = string_value };
return string_node;
},
.dot => {
_ = try tokens.next();
const ast = try parse_allocator.create(Ast);
ast.* = .identity;
return ast;
},
.field => |name| {
_ = try tokens.next();
const ast = try parse_allocator.create(Ast);
ast.* = .{ .object_key = try allocator.dupe(u8, name) };
return ast;
},
else => return error.InvalidQuery,
}
}
fn parseSuffix(allocator: std.mem.Allocator, parse_allocator: std.mem.Allocator, tokens: *TokenStream, base: *Ast) !*Ast {
_ = try tokens.expect(.bracket_left);
const index_token = try tokens.expect(.number);
_ = try tokens.expect(.bracket_right);
const index_value = try allocator.create(jv.Value);
index_value.* = .{ .integer = @intFromFloat(index_token.number) };
const index_node = try parse_allocator.create(Ast);
index_node.* = .{ .literal = index_value };
const ast = try parse_allocator.create(Ast);
ast.* = .{ .array_index = .{ .base = base, .index = index_node } };
return ast;
}
|