mirror of
https://github.com/iofq/nvim-treesitter-main.git
synced 2026-01-23 09:25:17 -06:00
86 lines
2.4 KiB
Lua
86 lines
2.4 KiB
Lua
local function table_to_nix(t)
|
|
local entries = {}
|
|
for _, s in ipairs(t) do
|
|
table.insert(entries, string.format('"%s"', s))
|
|
end
|
|
return string.format("[ %s ]", table.concat(entries, " "))
|
|
end
|
|
|
|
local function fmt_grammar(grammar)
|
|
local lines = {
|
|
string.format('%s = buildGrammar {', grammar.language),
|
|
string.format('passthru.name = "%s";', grammar.language),
|
|
string.format('language = "%s";', grammar.language),
|
|
string.format('version = "%s";', grammar.version),
|
|
string.format('src = %s;', grammar.src),
|
|
string.format('meta.homepage = "%s";', grammar.meta.homepage),
|
|
}
|
|
if grammar.generate then table.insert(lines, string.format('generate = %s;', grammar.generate)) end
|
|
if grammar.location then table.insert(lines, string.format('location = "%s";', grammar.location)) end
|
|
if grammar.requires then table.insert(
|
|
lines,
|
|
string.format('requires = %s;', table_to_nix(grammar.requires))
|
|
) end
|
|
|
|
table.insert(lines, "};")
|
|
return table.concat(lines, "\n")
|
|
end
|
|
|
|
local function generate_grammars(g)
|
|
local output = {}
|
|
for name, p in pairs(g) do
|
|
if p.install_info then
|
|
local out = assert(
|
|
io.popen(
|
|
string.format(
|
|
"nurl %s %s",
|
|
p.install_info.url,
|
|
p.install_info.revision
|
|
)
|
|
)
|
|
)
|
|
local grammar = {
|
|
version = "0.0.0+rev=" .. p.install_info.revision,
|
|
language = name,
|
|
src = out:read("*a"),
|
|
meta = {
|
|
homepage = p.install_info.url,
|
|
},
|
|
}
|
|
out:close()
|
|
|
|
if p.install_info.generate then
|
|
grammar.generate = p.install_info.generate
|
|
end
|
|
if p.install_info.location then
|
|
grammar.location = p.install_info.location
|
|
end
|
|
if p.requires then
|
|
grammar.requires = p.requires
|
|
end
|
|
table.insert(output, fmt_grammar(grammar))
|
|
end
|
|
end
|
|
table.sort(output)
|
|
return table.concat(output)
|
|
end
|
|
|
|
local function header()
|
|
local out = assert(io.popen("nurl -Ls ','"):read("*a"))
|
|
return string.format([[
|
|
# This file was generated by generate-parsers.lua for nvim-treesitter-main
|
|
{ buildGrammar, %s }: {
|
|
]], out)
|
|
end
|
|
|
|
local parsers = dofile(arg[1])
|
|
local output_path = arg[2]
|
|
local file = assert(io.open(output_path, "w"))
|
|
|
|
file:write(header())
|
|
file:write(generate_grammars(parsers))
|
|
file:write("}") -- footer
|
|
|
|
file:close()
|
|
|
|
assert(os.execute("nixfmt " .. output_path))
|