summaryrefslogtreecommitdiff
path: root/src/Syntax.zig
blob: 18ed477da20118866309f40087bc3813990b28fa (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
pub const makefile = @import("Syntax/makefile.zig");
pub const zig = @import("Syntax/zig.zig");

const es = @import("root");
const std = @import("std");

const ComptimeStringMap = std.ComptimeStringMap;
const Highlight = es.Highlight;
const Syntax = @This();

pub const chooseSyntax = ComptimeStringMap(
    Syntax,
    pairWith(&makefile.filematch, makefile.syntax) ++ pairWith(&zig.filematch, zig.syntax),
).get;

pub const Flags = struct {
    hl_numbers: bool = false,
    hl_strings: bool = false,
};

name: []const u8,
keyword_classifier: *const fn ([]const u8) ?Highlight,
singleline_comment_start: ?[]const u8,
multiline_comment_start: ?[]const u8,
multiline_comment_end: ?[]const u8,
separators: []const u8,
flags: Flags,

pub fn isSeparator(self: Syntax, char: u8) bool {
    return std.ascii.isWhitespace(char) or std.mem.indexOfScalar(u8, self.separators, char) != null;
}

pub fn pairWith(
    comptime keys: []const []const u8,
    comptime value: anytype,
) [keys.len]KeyValue(@TypeOf(value)) {
    @setEvalBranchQuota(20000);

    var pairs = [_]KeyValue(@TypeOf(value)){undefined} ** keys.len;
    for (keys, 0..) |key, i| {
        pairs[i] = .{ .@"0" = key, .@"1" = value };
    }

    return pairs;
}

fn KeyValue(comptime V: type) type {
    return struct {
        @"0": []const u8,
        @"1": V,
    };
}