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
|
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,
};
pub const BinaryOp = enum {
add,
};
pub const Ast = union(AstKind) {
identity,
array_index: *Ast,
object_key: []const u8,
literal: *jv.Value,
binary_expr: struct { op: BinaryOp, 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, tokens: []const Token) !*Ast {
var token_stream = TokenStream.init(tokens);
return parseQuery(allocator, &token_stream);
}
// GRAMMAR
// query := expr
fn parseQuery(allocator: std.mem.Allocator, tokens: *TokenStream) !*Ast {
const result = try parseExpr(allocator, tokens);
_ = try tokens.expect(.end);
return result;
}
// GRAMMAR
// expr := term
// | term + term
fn parseExpr(allocator: std.mem.Allocator, tokens: *TokenStream) !*Ast {
var lhs = try parseTerm(allocator, tokens);
const token = try tokens.peek();
if (token.kind() == .plus) {
_ = try tokens.next();
const rhs = try parseTerm(allocator, tokens);
const ast = try allocator.create(Ast);
ast.* = .{ .binary_expr = .{
.op = .add,
.lhs = lhs,
.rhs = rhs,
} };
lhs = ast;
}
return lhs;
}
// GRAMMAR
// term := "."
// | "." field_access
// | "." index_access
// | NUMBER
fn parseTerm(allocator: std.mem.Allocator, tokens: *TokenStream) !*Ast {
const first_token = try tokens.peek();
if (first_token.kind() == .number) {
_ = try tokens.next();
const number_value = try allocator.create(jv.Value);
number_value.* = .{ .integer = first_token.number };
const number_node = try allocator.create(Ast);
number_node.* = .{ .literal = number_value };
return number_node;
}
_ = try tokens.expect(.dot);
const next_token = try tokens.peek();
switch (next_token.kind()) {
.end => {
const ast = try allocator.create(Ast);
ast.* = .identity;
return ast;
},
.identifier => {
return parseFieldAccess(allocator, tokens);
},
.bracket_left => {
return parseIndexAccess(allocator, tokens);
},
else => return error.InvalidQuery,
}
}
// GRAMMAR
// field_access := IDENTIFIER
fn parseFieldAccess(allocator: std.mem.Allocator, tokens: *TokenStream) !*Ast {
const token = try tokens.expect(.identifier);
const ast = try allocator.create(Ast);
ast.* = .{ .object_key = token.identifier };
return ast;
}
// GRAMMAR
// index_access := "[" NUMBER "]"
fn parseIndexAccess(allocator: std.mem.Allocator, tokens: *TokenStream) !*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 = index_token.number };
const index_node = try allocator.create(Ast);
index_node.* = .{ .literal = index_value };
const ast = try allocator.create(Ast);
ast.* = .{ .array_index = index_node };
return ast;
}
|