recipe-tmlanguage 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +142 -0
- package/bin/recipe-tmlang.ts +107 -0
- package/package.json +82 -0
- package/recipe.tmLanguage.json +604 -0
- package/recipe.tmLanguage.ts +10 -0
- package/src/grammar.ts +332 -0
- package/src/verifier.ts +168 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kaj Kowalski
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# recipe-tmlanguage
|
|
2
|
+
|
|
3
|
+
[][npm]
|
|
4
|
+
[][jsr]
|
|
5
|
+
|
|
6
|
+
TextMate grammar for the [recipe] pharmacological notation language,
|
|
7
|
+
**generated** from the same JavaScript vocabulary modules that drive
|
|
8
|
+
`tree-sitter-recipe`'s parser.
|
|
9
|
+
|
|
10
|
+
Ships as `dist/recipe.tmLanguage.json` for consumption by Shiki, VS Code,
|
|
11
|
+
Sublime Text, Atom, `vscode-textmate`, and anything else that speaks
|
|
12
|
+
TextMate.
|
|
13
|
+
|
|
14
|
+
## Why generate, not author
|
|
15
|
+
|
|
16
|
+
The parser's vocabulary (`FREQUENCY`, `TIMING`, `ROUTE`, …, `UNITS`) lives in
|
|
17
|
+
plain `readonly string[]` modules under
|
|
18
|
+
`tree-sitter-recipe/grammar/`. Authoring a parallel TextMate grammar by hand
|
|
19
|
+
would mean copying those lists into regex alternations and re-copying them
|
|
20
|
+
every time the parser is touched. Drift guaranteed.
|
|
21
|
+
|
|
22
|
+
Instead, `src/grammar.ts` imports those exact modules through
|
|
23
|
+
`tree-sitter-recipe`'s package `exports` map, escapes metachars, sorts
|
|
24
|
+
alternatives longest-first (TextMate is first-match, not longest-match like
|
|
25
|
+
tree-sitter), and emits a ready-to-ship JSON grammar. One source of truth,
|
|
26
|
+
no sync burden.
|
|
27
|
+
|
|
28
|
+
## Scope map
|
|
29
|
+
|
|
30
|
+
Captures from `tree-sitter-recipe/queries/highlights.scm` are mapped to
|
|
31
|
+
**standard** TextMate scopes (`keyword.control.…`, `support.function.…`,
|
|
32
|
+
`invalid.illegal.…`, …) with a trailing `.recipe` namespace. Every mainstream
|
|
33
|
+
theme already paints these — no custom theme needs to be shipped with the
|
|
34
|
+
grammar.
|
|
35
|
+
|
|
36
|
+
| highlights.scm capture | TextMate scope |
|
|
37
|
+
| ------------------------ | ------------------------------------------------------ |
|
|
38
|
+
| `@keyword.directive` | `keyword.control.directive.{rx,dispense,signa}.recipe` |
|
|
39
|
+
| `@keyword.repeat` | `keyword.other.frequency.recipe` |
|
|
40
|
+
| `@keyword` (timing) | `keyword.other.timing.recipe` |
|
|
41
|
+
| `@function.macro` | `support.function.route.recipe` |
|
|
42
|
+
| `@attribute` | `entity.other.attribute-name.recipe` |
|
|
43
|
+
| `@keyword.error` | `invalid.illegal.warning.recipe` |
|
|
44
|
+
| `@type` (form) | `storage.type.form.recipe` |
|
|
45
|
+
| `@keyword.operator` | `keyword.operator.{compounding,fill,dtd}.recipe` |
|
|
46
|
+
| `@keyword.conditional` | `keyword.control.conditional.recipe` |
|
|
47
|
+
| `@number` | `constant.numeric.recipe` |
|
|
48
|
+
| `@type.builtin` (unit) | `support.type.unit.recipe` |
|
|
49
|
+
| `@variable` (ingredient) | `variable.other.ingredient.recipe` |
|
|
50
|
+
| `@string` (signa) | `string.unquoted.signa.recipe` |
|
|
51
|
+
| `@comment` | `comment.{line.number-sign,block}.recipe` |
|
|
52
|
+
| `@comment.documentation` | `comment.{line,block}.documentation.recipe` |
|
|
53
|
+
| `@punctuation.delimiter` | `punctuation.separator.recipe` |
|
|
54
|
+
|
|
55
|
+
## Layout
|
|
56
|
+
|
|
57
|
+
```text
|
|
58
|
+
recipe-tmlanguage/
|
|
59
|
+
├── cli.ts # DreamCLI entry — `recipe-tmlang generate|verify`
|
|
60
|
+
├── src/
|
|
61
|
+
│ ├── grammar.ts # pure: vocab → TextMate grammar object
|
|
62
|
+
│ └── verifier.ts # pure: tokenize fixtures, check scope assertions
|
|
63
|
+
└── dist/
|
|
64
|
+
└── recipe.tmLanguage.json
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Setup
|
|
68
|
+
|
|
69
|
+
`tree-sitter-recipe` is pulled in via Bun's link mechanism. Run once in the
|
|
70
|
+
sibling repo, then link here:
|
|
71
|
+
|
|
72
|
+
```sh
|
|
73
|
+
cd ../tree-sitter-recipe && bun link
|
|
74
|
+
cd ../recipe-tmlanguage && bun link tree-sitter-recipe && bun install
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## CLI
|
|
78
|
+
|
|
79
|
+
Powered by [DreamCLI][dreamcli] — no hand-rolled argparse.
|
|
80
|
+
|
|
81
|
+
```sh
|
|
82
|
+
bun cli.ts --help
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### `generate`
|
|
86
|
+
|
|
87
|
+
```sh
|
|
88
|
+
bun cli.ts generate # → dist/recipe.tmLanguage.json
|
|
89
|
+
bun cli.ts generate --out foo.json # custom path
|
|
90
|
+
bun cli.ts generate --indent 2 --quiet # 2-space indent, silent
|
|
91
|
+
bun cli.ts generate --json # machine-readable result on stdout
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### `verify`
|
|
95
|
+
|
|
96
|
+
Tokenizes every fixture in `tree-sitter-recipe/test/highlight/` with
|
|
97
|
+
`vscode-textmate` + `vscode-oniguruma` (the same engine Shiki wraps) and
|
|
98
|
+
asserts the scope at each caret marker matches the tree-sitter capture.
|
|
99
|
+
Fixtures and the Oniguruma WASM are located via package `exports` — no
|
|
100
|
+
relative paths to maintain.
|
|
101
|
+
|
|
102
|
+
```sh
|
|
103
|
+
bun cli.ts verify
|
|
104
|
+
bun cli.ts verify --json # { pass, total, failures[] }
|
|
105
|
+
bun cli.ts verify --max-failures 0 # print every failure
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Current status: **148 / 149** fixture assertions pass. The single failing
|
|
109
|
+
assertion (`expanded.recipe:22:23`) points one column past the end of a
|
|
110
|
+
23-character source line — a fixture off-by-one, not a grammar defect.
|
|
111
|
+
|
|
112
|
+
### Drop into Shiki
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
import grammar from "recipe-tmlanguage/dist/recipe.tmLanguage.json";
|
|
116
|
+
import { createHighlighter } from "shiki";
|
|
117
|
+
|
|
118
|
+
const shiki = await createHighlighter({
|
|
119
|
+
themes: ["github-dark"],
|
|
120
|
+
langs: [grammar],
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const html = shiki.codeToHtml(source, {
|
|
124
|
+
lang: "recipe",
|
|
125
|
+
theme: "github-dark",
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
The scope names are standard TextMate, so any Shiki theme paints recipe
|
|
130
|
+
blocks immediately.
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
[MIT][License] © Kaj Kowalski
|
|
135
|
+
|
|
136
|
+
[License]: LICENSE
|
|
137
|
+
[dreamcli]: https://npm.im/@kjanat/dreamcli
|
|
138
|
+
[jsr]: https://jsr.io/@kjanat/recipe-tmlanguage
|
|
139
|
+
[npm]: https://npm.im/recipe-tmlanguage
|
|
140
|
+
[recipe]: https://github.com/kjanat/tree-sitter-recipe
|
|
141
|
+
|
|
142
|
+
<!-- markdownlint-disable-file no-hard-tabs -->
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* recipe-tmlang — TextMate grammar generator & verifier for recipe-tmlanguage.
|
|
4
|
+
*
|
|
5
|
+
* Subcommands
|
|
6
|
+
* - generate: Build dist/recipe.tmLanguage.json from the tree-sitter-recipe vocab.
|
|
7
|
+
* - verify: Tokenize tree-sitter-recipe's highlight fixtures and assert scopes.
|
|
8
|
+
*
|
|
9
|
+
* Zero manual argparse — argument parsing, help, and completions all come from
|
|
10
|
+
* {@link https://github.com/kjanat/dreamcli | DreamCLI}. `--json` is a DreamCLI built-in;
|
|
11
|
+
* we branch on {@linkcode Out.jsonMode}.
|
|
12
|
+
*/
|
|
13
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
14
|
+
import { dirname, resolve } from "node:path";
|
|
15
|
+
import { cwd, exit } from "node:process";
|
|
16
|
+
import { fileURLToPath } from "node:url";
|
|
17
|
+
|
|
18
|
+
import { cli, command, flag } from "dreamcli";
|
|
19
|
+
import type { Out } from "dreamcli";
|
|
20
|
+
|
|
21
|
+
import { buildGrammar, serializeGrammar } from "#grammar";
|
|
22
|
+
import { verify } from "#verifier";
|
|
23
|
+
|
|
24
|
+
import pkg from "#pkg" with { type: "json" };
|
|
25
|
+
|
|
26
|
+
const { homepage, repository, version } = pkg;
|
|
27
|
+
const DEFAULT_OUT = `${resolve(import.meta.dirname, "..")}/recipe.tmLanguage.json`;
|
|
28
|
+
|
|
29
|
+
const TS_RX_DIR = resolve(dirname(fileURLToPath(import.meta.resolve("tree-sitter-recipe/package.json"))));
|
|
30
|
+
const DEFAULT_FIXTURES_DIR = resolve(TS_RX_DIR, "test/highlight");
|
|
31
|
+
const DEFAULT_ONIG_WASM = fileURLToPath(import.meta.resolve("vscode-oniguruma/release/onig.wasm"));
|
|
32
|
+
|
|
33
|
+
const indentOf = (raw: string): "tab" | number => (raw === "tab" ? "tab" : Number(raw));
|
|
34
|
+
|
|
35
|
+
const generate = command("generate")
|
|
36
|
+
.description("Build the TextMate grammar from the tree-sitter-recipe vocabulary")
|
|
37
|
+
.flag("out", flag.string().alias("o").default(DEFAULT_OUT).describe("Output JSON path"))
|
|
38
|
+
.flag("indent", flag.enum(["tab", "2", "4"]).default("tab").describe("JSON indent"))
|
|
39
|
+
.flag("quiet", flag.boolean().alias("q").default(false).describe("Suppress stats on success"))
|
|
40
|
+
.action(({ flags, out }) => {
|
|
41
|
+
const { grammar, stats } = buildGrammar();
|
|
42
|
+
const serialized = serializeGrammar(grammar, indentOf(flags.indent));
|
|
43
|
+
const outAbs = resolve(cwd(), flags.out);
|
|
44
|
+
mkdirSync(dirname(outAbs), { recursive: true });
|
|
45
|
+
writeFileSync(outAbs, serialized);
|
|
46
|
+
if (out.jsonMode) {
|
|
47
|
+
out.json({ ok: true, outPath: outAbs, bytes: serialized.length, stats });
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (flags.quiet) return;
|
|
51
|
+
|
|
52
|
+
out.log(`wrote ${outAbs}`);
|
|
53
|
+
out.log(` ${stats.topLevelPatterns} top-level patterns · ${serialized.length} bytes`);
|
|
54
|
+
const v = stats.vocab;
|
|
55
|
+
out.log(
|
|
56
|
+
` vocab: ${v.frequency} frequency · ${v.timing.single}+${v.timing.multi} timing · ${v.route.single}+${v.route.multi} route · ${v.dispensing.single}+${v.dispensing.multi} dispensing · ${v.forms.single}+${v.forms.multi} forms · ${v.compounding.single}+${v.compounding.multi} compounding · ${v.conditional.single}+${v.conditional.multi} conditional · ${v.warning} warning · ${v.units} units`,
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const verifyCmd = command("verify")
|
|
61
|
+
.description("Tokenize tree-sitter-recipe highlight fixtures and assert scope matches")
|
|
62
|
+
.flag("grammar", flag.string().alias("g").default(DEFAULT_OUT).describe("Path to .tmLanguage.json"))
|
|
63
|
+
.flag("fixtures", flag.string().alias("f").default(DEFAULT_FIXTURES_DIR).describe("Directory of .recipe fixtures"))
|
|
64
|
+
.flag("onig-wasm", flag.string().default(DEFAULT_ONIG_WASM).describe("Path to oniguruma WASM"))
|
|
65
|
+
.flag("max-failures", flag.number().default(40).describe("Max failures to print (0 = all)"))
|
|
66
|
+
.action(async ({ flags, out }) => {
|
|
67
|
+
const result = await verify({
|
|
68
|
+
grammarPath: resolve(cwd(), flags.grammar),
|
|
69
|
+
fixturesDir: resolve(cwd(), flags.fixtures),
|
|
70
|
+
onigWasmPath: resolve(cwd(), flags["onig-wasm"]),
|
|
71
|
+
});
|
|
72
|
+
const failuresLen = result.failures.length;
|
|
73
|
+
|
|
74
|
+
if (out.jsonMode) {
|
|
75
|
+
out.json(result);
|
|
76
|
+
if (failuresLen > 0) {
|
|
77
|
+
out.setExitCode(1);
|
|
78
|
+
exit();
|
|
79
|
+
}
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
out.log(`${result.pass} / ${result.total} assertions pass`);
|
|
84
|
+
if (failuresLen === 0) return;
|
|
85
|
+
|
|
86
|
+
out.log("");
|
|
87
|
+
out.log("── failures ──");
|
|
88
|
+
const limit = flags["max-failures"] === 0 ? failuresLen : flags["max-failures"];
|
|
89
|
+
for (const f of result.failures.slice(0, limit)) {
|
|
90
|
+
const gotStr = f.got
|
|
91
|
+
? f.got.filter((s) => s !== "source.recipe").join(" · ") || "(root only)"
|
|
92
|
+
: "(no token)";
|
|
93
|
+
out.log(` ${f.fixture}:${f.line}:${f.col} expected ${f.capture} got [${gotStr}]`);
|
|
94
|
+
}
|
|
95
|
+
if (failuresLen > limit) {
|
|
96
|
+
out.log(` … +${failuresLen - limit} more`);
|
|
97
|
+
}
|
|
98
|
+
out.setExitCode(1);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
export const app = cli("recipe-tmlang").packageJson({ repository, homepage, version }).links()
|
|
102
|
+
.description("TextMate grammar generator & verifier for the recipe DSL")
|
|
103
|
+
.command(generate)
|
|
104
|
+
.command(verifyCmd)
|
|
105
|
+
.completions();
|
|
106
|
+
|
|
107
|
+
if (import.meta.main) app.run();
|
package/package.json
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "recipe-tmlanguage",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "TextMate grammar for the recipe (.recipe) pharmacological notation language.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"dreamcli",
|
|
7
|
+
"recipe",
|
|
8
|
+
"textmate",
|
|
9
|
+
"grammar"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/kjanat/recipe-tmlanguage#recipe-tmlanguage",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/kjanat/recipe-tmlanguage.git"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"author": {
|
|
18
|
+
"name": "Kaj Kowalski",
|
|
19
|
+
"email": "info@kajkowalski.nl",
|
|
20
|
+
"url": "https://github.com/kjanat"
|
|
21
|
+
},
|
|
22
|
+
"type": "module",
|
|
23
|
+
"imports": {
|
|
24
|
+
"#pkg": "./package.json",
|
|
25
|
+
"#grammar": "./src/grammar.ts",
|
|
26
|
+
"#verifier": "./src/verifier.ts"
|
|
27
|
+
},
|
|
28
|
+
"exports": {
|
|
29
|
+
".": "./recipe.tmLanguage.json",
|
|
30
|
+
"./dist/*": "./dist/*",
|
|
31
|
+
"./package.json": "./package.json"
|
|
32
|
+
},
|
|
33
|
+
"main": "./recipe.tmLanguage.json",
|
|
34
|
+
"module": "./recipe.tmLanguage.json",
|
|
35
|
+
"bin": {
|
|
36
|
+
"recipe-tmlang": "bin/recipe-tmlang.ts"
|
|
37
|
+
},
|
|
38
|
+
"directories": {
|
|
39
|
+
"lib": "/src",
|
|
40
|
+
"bin": "/bin"
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"/bin/",
|
|
44
|
+
"/src/",
|
|
45
|
+
"/recipe.tmLanguage.json",
|
|
46
|
+
"/recipe.tmLanguage.ts",
|
|
47
|
+
"/README.md",
|
|
48
|
+
"/LICENSE"
|
|
49
|
+
],
|
|
50
|
+
"scripts": {
|
|
51
|
+
"check": "biome check",
|
|
52
|
+
"cli": "run recipe-tmlang",
|
|
53
|
+
"fmt": "dprint fmt",
|
|
54
|
+
"generate": "run recipe-tmlang generate",
|
|
55
|
+
"lint": "biome lint",
|
|
56
|
+
"prepack": "bun generate",
|
|
57
|
+
"recipe-tmlang": "bun bin/recipe-tmlang.ts",
|
|
58
|
+
"typecheck": "tsc --noEmit",
|
|
59
|
+
"verify": "run recipe-tmlang verify"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"dreamcli": "npm:@kjanat/dreamcli@^2.4.0",
|
|
63
|
+
"tree-sitter-recipe": "0.3.0",
|
|
64
|
+
"vscode-oniguruma": "^2.0.1",
|
|
65
|
+
"vscode-textmate": "^9.2.0"
|
|
66
|
+
},
|
|
67
|
+
"devDependencies": {
|
|
68
|
+
"@biomejs/biome": "^2.5.1",
|
|
69
|
+
"@types/bun": "^1.3.14",
|
|
70
|
+
"dprint": "^0.54.0",
|
|
71
|
+
"runner-run": "^0.14.3",
|
|
72
|
+
"typescript": "^6.0.3"
|
|
73
|
+
},
|
|
74
|
+
"packageManager": "bun@1.3.14",
|
|
75
|
+
"engines": {
|
|
76
|
+
"bun": ">=1.3",
|
|
77
|
+
"node": ">=22.22.2"
|
|
78
|
+
},
|
|
79
|
+
"trustedDependencies": [
|
|
80
|
+
"tree-sitter-recipe"
|
|
81
|
+
]
|
|
82
|
+
}
|