From c9d5d97e4d487b92a52d765dd9c467423ddd1de1 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 28 Sep 2026 16:45:54 -0300 Subject: A REPL's language is named and found as the syntax table finds one: aliases, any case Repl took only the grammar's exact name, and a file's language was found by a case-sensitive suffix of its path, apart from the aliases a code fence already had. The aliases move to grammar_manifest with byName and byPath, which syntax, Repl and langOf share, so `Repl py` and a .PY file work. The line-by-line warning is for the python language, not an id that starts with python. Joincol's doc line was sitting on Repl. Co-Authored-By: Claude Opus 5.5 --- src/grammar_manifest.zig | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'src/grammar_manifest.zig') diff --git a/src/grammar_manifest.zig b/src/grammar_manifest.zig index 0303af17..d6666729 100644 --- a/src/grammar_manifest.zig +++ b/src/grammar_manifest.zig @@ -2,6 +2,8 @@ //! runtime syntax registry. Keep target symbols and query values out of this //! file: the build runner imports it as host code. +const std = @import("std"); + pub const Tier = enum { zig, minimal, full }; pub const Grammar = struct { @@ -49,3 +51,44 @@ pub const all = [_]Grammar{ .{ .name = "typst", .dep = "ts_typst", .exts = &.{ ".typ", ".typst" }, .tier = .full, .scanner = true, .query = "queries/typst/highlights.scm" }, .{ .name = "zig", .dep = "ts_zig", .exts = &.{ ".zig", ".zon" }, .tier = .zig }, }; + +/// The names a language goes by besides its grammar's: a fence's `py`, +/// a `Repl sh`. +pub const aliases = [_]struct { []const u8, []const u8 }{ + .{ "js", "javascript" }, + .{ "jsx", "javascript" }, + .{ "mjs", "javascript" }, + .{ "py", "python" }, + .{ "sh", "bash" }, + .{ "shell", "bash" }, + .{ "zsh", "bash" }, + .{ "bash", "bash" }, + .{ "rs", "rust" }, + .{ "c++", "cpp" }, + .{ "cxx", "cpp" }, + .{ "cc", "cpp" }, + .{ "cs", "c_sharp" }, + .{ "kt", "kotlin" }, + .{ "rb", "ruby" }, + .{ "ml", "ocaml" }, + .{ "hs", "haskell" }, +}; + +/// The grammar a language name means: its own name or an alias, in any case. +pub fn byName(name: []const u8) ?usize { + var canonical = name; + for (aliases) |a| if (std.ascii.eqlIgnoreCase(name, a[0])) { + canonical = a[1]; + break; + }; + for (all, 0..) |g, i| if (std.ascii.eqlIgnoreCase(canonical, g.name)) return i; + return null; +} + +/// The grammar a path's extension selects, in any case. +pub fn byPath(path: []const u8) ?usize { + const ext = std.fs.path.extension(path); + if (ext.len == 0) return null; + for (all, 0..) |g, i| for (g.exts) |e| if (std.ascii.eqlIgnoreCase(ext, e)) return i; + return null; +} -- cgit v1.3