aboutsummaryrefslogtreecommitdiff
path: root/src/z/parser/js/ast/lit.zig
blob: 3a5149f053810be7dad15299370bcc6ac864dcc1 (plain)
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
const std = @import("std");
const Loc = @import("loc.zig").Loc;
const Formatter = @import("../formatter.zig").Formatter;

pub const LitType = union(enum) {
    null: void,
    bool: bool,
    number: f64,
    string: []const u8,
    regex: Regex,
    bigint: []const u8,
};

pub const Lit = struct {
    loc: Loc,
    data: LitType,

    pub fn fmt(self: @This(), f: *Formatter) void {
        switch (self.data) {
            .null => f.write("null"),
            .bool => |b| f.write(if (b) "true" else "false"),
            .number => |n| {
                var buf: [64]u8 = undefined;
                f.write(std.fmt.bufPrint(&buf, "{d}", .{n}) catch "0");
            },
            .string => |s| {
                f.writeChar('"');
                f.write(s);
                f.writeChar('"');
            },
            .regex => |r| {
                f.writeChar('/');
                f.write(r.pattern);
                f.writeChar('/');
                f.write(r.flags);
            },
            .bigint => |s| {
                f.write(s);
                f.writeChar('n');
            },
        }
    }
};

pub const Regex = struct {
    pattern: []const u8,
    flags: []const u8,
};