aboutsummaryrefslogtreecommitdiff
path: root/src/json_parser.zig
blob: 8a025864ffab68e41160d358d760b880c90233b4 (plain) (blame)
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
const std = @import("std");
const logger = @import("logger.zig");
const js_lexer = @import("js_lexer.zig");
const importRecord = @import("import_record.zig");
const js_ast = @import("js_ast.zig");
const options = @import("options.zig");
const alloc = @import("alloc.zig");

const fs = @import("fs.zig");
usingnamespace @import("strings.zig");
usingnamespace @import("ast/base.zig");
usingnamespace js_ast.G;

const ImportKind = importRecord.ImportKind;
const BindingNodeIndex = js_ast.BindingNodeIndex;

const StmtNodeIndex = js_ast.StmtNodeIndex;
const ExprNodeIndex = js_ast.ExprNodeIndex;
const ExprNodeList = js_ast.ExprNodeList;
const StmtNodeList = js_ast.StmtNodeList;
const BindingNodeList = js_ast.BindingNodeList;
const assert = std.debug.assert;

const Ref = js_ast.Ref;
const LocRef = js_ast.LocRef;
const S = js_ast.S;
const B = js_ast.B;
const G = js_ast.G;
const T = js_lexer.T;
const E = js_ast.E;
const Stmt = js_ast.Stmt;
const Expr = js_ast.Expr;
const Binding = js_ast.Binding;
const Symbol = js_ast.Symbol;
const Level = js_ast.Op.Level;
const Op = js_ast.Op;
const Scope = js_ast.Scope;
const locModuleScope = logger.Loc.Empty;

fn JSONLikeParser(opts: js_lexer.JSONOptions) type {
    const Lexer = if (opts.allow_comments) js_lexer.TSConfigJSONLexer else js_lexer.JSONLexer;
    return struct {
        lexer: Lexer,
        source: logger.Source,
        log: logger.Log,
        allocator: *std.mem.Allocator,

        pub fn init(allocator: *std.mem.Allocator, source: logger.Source, log: logger.Log) Parser {
            return Parser{
                .lexer = Lexer.init(log, source, allocator),
                .allocator = allocator,
                .log = log,
                .source = source,
            };
        }

        const Parser = @This();

        pub fn e(p: *Parser, t: anytype, loc: logger.Loc) Expr {
            if (@typeInfo(@TypeOf(t)) == .Pointer) {
                return Expr.init(t, loc);
            } else {
                return Expr.alloc(p.allocator, t, loc);
            }
        }
        pub fn parseExpr(p: *Parser) ?Expr {
            const loc = p.lexer.loc();

            switch (p.lexer.token) {
                .t_false => {
                    p.lexer.next();
                    return p.e(E.Boolean{
                        .value = false,
                    }, loc);
                },
                .t_true => {
                    p.lexer.next();
                    return p.e(E.Boolean{
                        .value = true,
                    }, loc);
                },
                .t_null => {
                    p.lexer.next();
                    return p.e(E.Null{}, loc);
                },
                .t_string_literal => {
                    const value = p.lexer.string_literal;
                    p.lexer.next();
                    return p.e(E.String{
                        .value = value,
                    }, loc);
                },
                .t_numeric_literal => {
                    const value = p.lexer.number;
                    p.lexer.next();
                    return p.e(E.Number{ .value = value }, loc);
                },
                .t_minus => {
                    p.lexer.next();
                    const value = p.lexer.number;
                    p.lexer.expect(.t_numeric_literal);
                    return p.e(E.Number{ .value = -value }, loc);
                },
                .t_open_bracket => {
                    p.lexer.next();
                    var is_single_line = !p.lexer.has_newline_before;
                    var exprs = List(Expr).init(p.allocator);

                    while (p.lexer.token != .t_close_bracket) {
                        if (exprs.items.len > 0) {
                            if (p.lexer.has_newline_before) {
                                is_single_line = false;
                            }

                            if (!p.parseMaybeTrailingComma(.t_close_bracket)) {
                                break;
                            }

                            if (p.lexer.has_newline_before) {
                                is_single_line = false;
                            }
                        }

                        if (p.parseExpr()) |expr| {
                            try exprs.append(expr);
                        } else {
                            break;
                        }
                    }

                    if (p.lexer.has_newline_before) {
                        is_single_line = false;
                    }
                    p.lexer.expect(.t_close_bracket);
                    return p.e(E.Array{ .items = exprs.toOwnedSlice() }, loc);
                },
                .t_open_brace => {
                    p.lexer.next();
                    var is_single_line = !p.lexer.has_newline_before;
                    var properties = List(G.Property).init(p.allocator);
                    var duplicates = std.StringHashMap(u0).init(p.allocator);

                    while (p.lexer.token != .t_close_brace) {
                        if (properties.items.len > 0) {
                            is_single_line = if (p.lexer.has_newline_before) false else is_single_line;
                            if (!p.parseMaybeTrailingComma(.t_close_brace)) {
                                break;
                            }
                            is_single_line = if (p.lexer.has_newline_before) false else is_single_line;
                        }

                        var key_string = p.lexer.string_literal;
                        var key_range = p.lexer.range();
                        var key = p.e(E.String{ .value = key_string }, key_range.loc);
                        p.lexer.expect(.t_string_literal);
                        var key_text = p.lexer.utf16ToString();
                        // Warn about duplicate keys
                        if (duplicates.contains(key_text)) {
                            p.log.addRangeWarningFmt(p.source, r, "Duplicate key \"{s}\" in object literal", .{key_text}) catch unreachable;
                        } else {
                            duplicates.put(key_text, 0) catch unreachable;
                        }

                        p.lexer.expect(.t_colon);
                        var value = p.parseExpr() orelse return null;
                        try properties.append(G.Property{ .key = key, .value = value });
                    }

                    is_single_line = if (p.lexer.has_newline_before) false else is_single_line;
                    p.lexer.expect(.t_close_brace);
                    return p.e(E.Object{
                        .properties = properties.toOwnedSlice(),
                        .is_single_line = is_single_line,
                    }, loc);
                },
                else => {
                    p.lexer.unexpected();
                    return null;
                },
            }
        }

        pub fn parseMaybeTrailingComma(p: *Parser, closer: T) bool {
            const comma_range = p.lexer.range();
            p.lexer.expect(.t_comma);

            if (p.lexer.token == closer) {
                if (!opts.allow_trailing_commas) {
                    p.log.addRangeError(p.source, comma_range, "JSON does not support trailing commas") catch unreachable;
                }
                return false;
            }

            return true;
        }
    };
}

const JSONParser = JSONLikeParser(js_lexer.JSONOptions{});
const TSConfigParser = JSONLikeParser(js_lexer.JSONOptions{ .allow_comments = true, .allow_trailing_commas = true });

pub fn ParseJSON(log: logger.Log, source: logger.Source) !?Expr {
    var parser = JSONParser.init(allocator, log, source);

    return try parser.parseExpr();
}

pub fn ParseTSConfig(log: logger.Loc, source: logger.Source) !?Expr {
    var parser = TSConfigParser.init(allocator, log, source);

    return try parser.parseExpr();
}