Skip to content

Refactor grammar build process #480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -593,14 +593,15 @@
},
"scripts": {
"release-elixir-ls": "cd elixir-ls && mix elixir_ls.release2 -o ../elixir-ls-release",
"vscode:prepublish": "npm-run-all release-elixir-ls esbuild-release",
"vscode:prepublish": "npm-run-all build-grammar release-elixir-ls esbuild-release",
"mixcompile": "mix compile",
"mixcompile-esbuild": "npm-run-all mixcompile esbuild",
"clean": "rimraf ./out",
"compile": "tsc -b",
"watch": "tsc -b -w",
"update-vscode": "node ./node_modules/vscode/bin/install",
"pretest": "npm-run-all clean compile",
"pretest": "npm-run-all build-grammar clean compile",
"build-grammar": "node ./scripts/buildGrammar.js",
"test": "node ./out/test/runTest.js",
"lint": "biome check",
"fix-formatting": "biome format --write",
Expand All @@ -618,6 +619,7 @@
"@vscode/test-electron": "^2.5.2",
"esbuild": "^0.25.4",
"glob": "^11.0.2",
"js-yaml": "^4.1.0",
"mocha": "^11.5.0",
"npm-run-all": "^4.1.5",
"rimraf": "^6.0.1",
Expand Down
103 changes: 103 additions & 0 deletions scripts/buildGrammar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/usr/bin/env node
const fs = require("node:fs");
const path = require("node:path");
const yaml = require("js-yaml");

function loadYaml(relPath) {
return yaml.load(fs.readFileSync(path.join(__dirname, relPath), "utf8"));
}

function escapeChar(ch) {
const specials = [
"{",
"}",
"[",
"]",
"<",
">",
"(",
")",
"/",
"\\",
'"',
"'",
"|",
];
return specials.includes(ch) ? `\\${ch}` : ch;
}

function buildSigils(conf) {
const patterns = [];
for (const h of conf.interpolated.heredocs) {
patterns.push({
begin: `~[a-z](?>${h.delim})`,
beginCaptures: {
0: { name: "punctuation.definition.string.begin.elixir" },
},
comment: "Double-quoted heredocs sigils",
end: `^\\s*${h.delim}`,
endCaptures: { 0: { name: "punctuation.definition.string.end.elixir" } },
name: h.name,
patterns: [
{ include: "#interpolated_elixir" },
{ include: "#escaped_char" },
],
});
}
for (const d of conf.interpolated.delimiters) {
patterns.push({
begin: `~[a-z]\\${escapeChar(d.open)}`,
beginCaptures: {
0: { name: "punctuation.definition.string.begin.elixir" },
},
comment: "sigil (allow for interpolation)",
end: `\\${escapeChar(d.close)}[a-z]*`,
endCaptures: { 0: { name: "punctuation.definition.string.end.elixir" } },
name: "string.quoted.double.interpolated.elixir",
patterns: [
{ include: "#interpolated_elixir" },
{ include: "#escaped_char" },
...(d.nest ? [{ include: d.nest }] : []),
],
});
}
for (const h of conf.literal.heredocs) {
patterns.push({
begin: `~[A-Z][A-Z0-9]*(?>${h.delim})`,
beginCaptures: {
0: { name: "punctuation.definition.string.begin.elixir" },
},
comment: "Double-quoted heredocs sigils",
end: `^\\s*${h.delim}`,
endCaptures: { 0: { name: "punctuation.definition.string.end.elixir" } },
name: h.name,
});
}
for (const d of conf.literal.delimiters) {
const pat = {
begin: `~[A-Z][A-Z0-9]*\\${escapeChar(d.open)}`,
beginCaptures: {
0: { name: "punctuation.definition.string.begin.elixir" },
},
comment: "sigil (without interpolation)",
end: `\\${escapeChar(d.close)}[a-z]*`,
endCaptures: { 0: { name: "punctuation.definition.string.end.elixir" } },
name: "string.quoted.double.literal.elixir",
};
if (d.nest) pat.patterns = [{ include: d.nest }];
patterns.push(pat);
}
return { patterns };
}

const base = loadYaml("../syntaxes/yaml/elixir.yml");
const sigils = loadYaml("../syntaxes/yaml/sigils.yml");
const modules = loadYaml("../syntaxes/yaml/modules.yml");

base.patterns = modules.concat(base.patterns);
base.repository.sigils = buildSigils(sigils);

fs.writeFileSync(
path.join(__dirname, "../syntaxes/elixir.json"),
JSON.stringify(base, null, 2),
);
Loading
Loading