From d13e89cbbdda9722fc636b0666adce58501d1c9b Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Sun, 2 Jan 2022 08:38:11 +0200 Subject: Some changes in Syntax --- src/Syntax.zig | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'src/Syntax.zig') diff --git a/src/Syntax.zig b/src/Syntax.zig index f29c7fc..6858027 100644 --- a/src/Syntax.zig +++ b/src/Syntax.zig @@ -1,14 +1,18 @@ 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 database = [_]Syntax{ - makefile.syntax, - zig.syntax, -}; +pub const chooseSyntax = ComptimeStringMap( + Syntax, + pairWith(&makefile.filematch, makefile.syntax) + ++ pairWith(&zig.filematch, zig.syntax), +).get; pub const Flags = struct { hl_numbers: bool = false, @@ -16,10 +20,7 @@ pub const Flags = struct { }; name: []const u8, -// TODO: Make these into comptime StringSets, see std.ComptimeStringMap -filematch: []const []const u8, -keywords1: []const []const u8, -keywords2: []const []const u8, +keyword_classifier: fn([]const u8) ?Highlight, singleline_comment_start: ?[]const u8, multiline_comment_start: ?[]const u8, multiline_comment_end: ?[]const u8, @@ -29,3 +30,25 @@ flags: Flags, pub fn isSeparator(self: Syntax, char: u8) bool { return std.ascii.isSpace(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) |key, i| { + pairs[i] = .{ .@"0" = key, .@"1" = value }; + } + + return pairs; +} + +fn KeyValue(comptime V: type) type { + return struct { + @"0": []const u8, + @"1": V, + }; +} + -- cgit v1.2.3