|
| 1 | +#!/usr/bin/env node |
| 2 | +const fs = require("node:fs"); |
| 3 | +const path = require("node:path"); |
| 4 | +const yaml = require("js-yaml"); |
| 5 | + |
| 6 | +function loadYaml(relPath) { |
| 7 | + return yaml.load(fs.readFileSync(path.join(__dirname, relPath), "utf8")); |
| 8 | +} |
| 9 | + |
| 10 | +function escapeChar(ch) { |
| 11 | + const specials = [ |
| 12 | + "{", |
| 13 | + "}", |
| 14 | + "[", |
| 15 | + "]", |
| 16 | + "<", |
| 17 | + ">", |
| 18 | + "(", |
| 19 | + ")", |
| 20 | + "/", |
| 21 | + "\\", |
| 22 | + '"', |
| 23 | + "'", |
| 24 | + "|", |
| 25 | + ]; |
| 26 | + return specials.includes(ch) ? `\\${ch}` : ch; |
| 27 | +} |
| 28 | + |
| 29 | +function buildSigils(conf) { |
| 30 | + const patterns = []; |
| 31 | + for (const h of conf.interpolated.heredocs) { |
| 32 | + patterns.push({ |
| 33 | + begin: `~[a-z](?>${h.delim})`, |
| 34 | + beginCaptures: { |
| 35 | + 0: { name: "punctuation.definition.string.begin.elixir" }, |
| 36 | + }, |
| 37 | + comment: "Double-quoted heredocs sigils", |
| 38 | + end: `^\\s*${h.delim}`, |
| 39 | + endCaptures: { 0: { name: "punctuation.definition.string.end.elixir" } }, |
| 40 | + name: h.name, |
| 41 | + patterns: [ |
| 42 | + { include: "#interpolated_elixir" }, |
| 43 | + { include: "#escaped_char" }, |
| 44 | + ], |
| 45 | + }); |
| 46 | + } |
| 47 | + for (const d of conf.interpolated.delimiters) { |
| 48 | + patterns.push({ |
| 49 | + begin: `~[a-z]\\${escapeChar(d.open)}`, |
| 50 | + beginCaptures: { |
| 51 | + 0: { name: "punctuation.definition.string.begin.elixir" }, |
| 52 | + }, |
| 53 | + comment: "sigil (allow for interpolation)", |
| 54 | + end: `\\${escapeChar(d.close)}[a-z]*`, |
| 55 | + endCaptures: { 0: { name: "punctuation.definition.string.end.elixir" } }, |
| 56 | + name: "string.quoted.double.interpolated.elixir", |
| 57 | + patterns: [ |
| 58 | + { include: "#interpolated_elixir" }, |
| 59 | + { include: "#escaped_char" }, |
| 60 | + ...(d.nest ? [{ include: d.nest }] : []), |
| 61 | + ], |
| 62 | + }); |
| 63 | + } |
| 64 | + for (const h of conf.literal.heredocs) { |
| 65 | + patterns.push({ |
| 66 | + begin: `~[A-Z][A-Z0-9]*(?>${h.delim})`, |
| 67 | + beginCaptures: { |
| 68 | + 0: { name: "punctuation.definition.string.begin.elixir" }, |
| 69 | + }, |
| 70 | + comment: "Double-quoted heredocs sigils", |
| 71 | + end: `^\\s*${h.delim}`, |
| 72 | + endCaptures: { 0: { name: "punctuation.definition.string.end.elixir" } }, |
| 73 | + name: h.name, |
| 74 | + }); |
| 75 | + } |
| 76 | + for (const d of conf.literal.delimiters) { |
| 77 | + const pat = { |
| 78 | + begin: `~[A-Z][A-Z0-9]*\\${escapeChar(d.open)}`, |
| 79 | + beginCaptures: { |
| 80 | + 0: { name: "punctuation.definition.string.begin.elixir" }, |
| 81 | + }, |
| 82 | + comment: "sigil (without interpolation)", |
| 83 | + end: `\\${escapeChar(d.close)}[a-z]*`, |
| 84 | + endCaptures: { 0: { name: "punctuation.definition.string.end.elixir" } }, |
| 85 | + name: "string.quoted.double.literal.elixir", |
| 86 | + }; |
| 87 | + if (d.nest) pat.patterns = [{ include: d.nest }]; |
| 88 | + patterns.push(pat); |
| 89 | + } |
| 90 | + return { patterns }; |
| 91 | +} |
| 92 | + |
| 93 | +const base = loadYaml("../syntaxes/yaml/elixir.yml"); |
| 94 | +const sigils = loadYaml("../syntaxes/yaml/sigils.yml"); |
| 95 | +const modules = loadYaml("../syntaxes/yaml/modules.yml"); |
| 96 | + |
| 97 | +base.patterns = modules.concat(base.patterns); |
| 98 | +base.repository.sigils = buildSigils(sigils); |
| 99 | + |
| 100 | +fs.writeFileSync( |
| 101 | + path.join(__dirname, "../syntaxes/elixir.json"), |
| 102 | + JSON.stringify(base, null, 2), |
| 103 | +); |
0 commit comments