@creationix/rex 0.1.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 +50 -0
- package/package.json +56 -0
- package/rex-cli.ts +3 -0
- package/rex-compile.ts +196 -0
- package/rex.ohm +408 -0
- package/rex.ohm-bundle.d.ts +195 -0
- package/rex.ohm-bundle.js +1 -0
- package/rex.ts +2418 -0
- package/rexc-interpreter.ts +848 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tim Caswell
|
|
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,50 @@
|
|
|
1
|
+
# @creationix/rex
|
|
2
|
+
|
|
3
|
+
Rex compiler and parser package.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
bun add -g @creationix/rex
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Publish to npm
|
|
12
|
+
|
|
13
|
+
From `packages/rex-lang`:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm whoami
|
|
17
|
+
bun run prepublishOnly
|
|
18
|
+
npm publish --access public
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Or one-off dry run:
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
bun run pack:dry-run
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## CLI
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
rex --help
|
|
31
|
+
rex --expr "when x do y end"
|
|
32
|
+
rex --file input.rex
|
|
33
|
+
cat input.rex | rex
|
|
34
|
+
rex --expr "a and b" --ir
|
|
35
|
+
rex --expr "x = method + path x" --minify-names --dedupe-values
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Programmatic API
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { compile, parseToIR, optimizeIR, encodeIR } from "@creationix/rex";
|
|
42
|
+
|
|
43
|
+
const source = "when x do y else z end";
|
|
44
|
+
const encoded = compile(source);
|
|
45
|
+
const optimized = compile(source, { optimize: true, minifyNames: true, dedupeValues: true });
|
|
46
|
+
|
|
47
|
+
const ir = parseToIR(source);
|
|
48
|
+
const optimizedIR = optimizeIR(ir);
|
|
49
|
+
const reEncoded = encodeIR(optimizedIR);
|
|
50
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@creationix/rex",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Compiler and parser for the Rex language",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"rex",
|
|
7
|
+
"dsl",
|
|
8
|
+
"compiler",
|
|
9
|
+
"parser",
|
|
10
|
+
"configuration"
|
|
11
|
+
],
|
|
12
|
+
"type": "module",
|
|
13
|
+
"bin": {
|
|
14
|
+
"rex": "./rex-cli.ts"
|
|
15
|
+
},
|
|
16
|
+
"main": "rex.ts",
|
|
17
|
+
"types": "./rex.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": "./rex.ts"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"README.md",
|
|
23
|
+
"LICENSE",
|
|
24
|
+
"rex.ts",
|
|
25
|
+
"rex-compile.ts",
|
|
26
|
+
"rex-cli.ts",
|
|
27
|
+
"rex.ohm",
|
|
28
|
+
"rex.ohm-bundle.js",
|
|
29
|
+
"rex.ohm-bundle.d.ts",
|
|
30
|
+
"rexc-interpreter.ts"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"test": "bun test",
|
|
34
|
+
"build:grammar": "ohm generateBundles --withTypes rex.ohm",
|
|
35
|
+
"compile": "bun run rex-compile.ts",
|
|
36
|
+
"verify:docs": "bun run verify-doc-examples.ts",
|
|
37
|
+
"pack:dry-run": "npm pack --dry-run",
|
|
38
|
+
"prepublishOnly": "bun run build:grammar && bun test && npm run pack:dry-run"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "https://github.com/creationix/rex",
|
|
46
|
+
"directory": "packages/rex-lang"
|
|
47
|
+
},
|
|
48
|
+
"license": "MIT",
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"ohm-js": "^17.1.0"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@ohm-js/cli": "^2.0.1",
|
|
54
|
+
"@types/bun": "latest"
|
|
55
|
+
}
|
|
56
|
+
}
|
package/rex-cli.ts
ADDED
package/rex-compile.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { compile, parseToIR } from "./rex.ts";
|
|
2
|
+
import { dirname, resolve } from "node:path";
|
|
3
|
+
|
|
4
|
+
type CliOptions = {
|
|
5
|
+
expr?: string;
|
|
6
|
+
file?: string;
|
|
7
|
+
out?: string;
|
|
8
|
+
ir: boolean;
|
|
9
|
+
minifyNames: boolean;
|
|
10
|
+
dedupeValues: boolean;
|
|
11
|
+
dedupeMinBytes?: number;
|
|
12
|
+
domainRefs: Record<string, number>;
|
|
13
|
+
help: boolean;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
type DomainSchema = {
|
|
17
|
+
globals?: Record<string, unknown>;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function parseArgs(argv: string[]): CliOptions {
|
|
21
|
+
const options: CliOptions = {
|
|
22
|
+
ir: false,
|
|
23
|
+
minifyNames: false,
|
|
24
|
+
dedupeValues: false,
|
|
25
|
+
domainRefs: {},
|
|
26
|
+
help: false,
|
|
27
|
+
};
|
|
28
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
29
|
+
const arg = argv[index];
|
|
30
|
+
if (!arg) continue;
|
|
31
|
+
if (arg === "--help" || arg === "-h") {
|
|
32
|
+
options.help = true;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (arg === "--ir") {
|
|
36
|
+
options.ir = true;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (arg === "--minify-names" || arg === "-m") {
|
|
40
|
+
options.minifyNames = true;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (arg === "--dedupe-values") {
|
|
44
|
+
options.dedupeValues = true;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (arg === "--dedupe-min-bytes") {
|
|
48
|
+
const value = argv[index + 1];
|
|
49
|
+
if (!value) throw new Error("Missing value for --dedupe-min-bytes");
|
|
50
|
+
const parsed = Number(value);
|
|
51
|
+
if (!Number.isInteger(parsed) || parsed < 1) throw new Error("--dedupe-min-bytes must be a positive integer");
|
|
52
|
+
options.dedupeMinBytes = parsed;
|
|
53
|
+
index += 1;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (arg === "--domain-extension") {
|
|
57
|
+
const value = argv[index + 1];
|
|
58
|
+
if (!value) throw new Error("Missing value for --domain-extension");
|
|
59
|
+
options.domainRefs[value] = 0;
|
|
60
|
+
index += 1;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (arg === "--domain-ref") {
|
|
64
|
+
const value = argv[index + 1];
|
|
65
|
+
if (!value) throw new Error("Missing value for --domain-ref");
|
|
66
|
+
const separator = value.indexOf("=");
|
|
67
|
+
if (separator < 1 || separator === value.length - 1) {
|
|
68
|
+
throw new Error("--domain-ref expects NAME=ID (for example: headers=0)");
|
|
69
|
+
}
|
|
70
|
+
const name = value.slice(0, separator);
|
|
71
|
+
const idText = value.slice(separator + 1);
|
|
72
|
+
const id = Number(idText);
|
|
73
|
+
if (!Number.isInteger(id) || id < 0) throw new Error(`Invalid domain ref id in --domain-ref '${value}'`);
|
|
74
|
+
options.domainRefs[name] = id;
|
|
75
|
+
index += 1;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (arg === "--expr" || arg === "-e") {
|
|
79
|
+
const value = argv[index + 1];
|
|
80
|
+
if (!value) throw new Error("Missing value for --expr");
|
|
81
|
+
options.expr = value;
|
|
82
|
+
index += 1;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (arg === "--file" || arg === "-f") {
|
|
86
|
+
const value = argv[index + 1];
|
|
87
|
+
if (!value) throw new Error("Missing value for --file");
|
|
88
|
+
options.file = value;
|
|
89
|
+
index += 1;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (arg === "--out" || arg === "-o") {
|
|
93
|
+
const value = argv[index + 1];
|
|
94
|
+
if (!value) throw new Error("Missing value for --out");
|
|
95
|
+
options.out = value;
|
|
96
|
+
index += 1;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
throw new Error(`Unknown option: ${arg}`);
|
|
100
|
+
}
|
|
101
|
+
return options;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function usage() {
|
|
105
|
+
return [
|
|
106
|
+
"Compile high-level Rex to compact encoding (rexc).",
|
|
107
|
+
"",
|
|
108
|
+
"Usage:",
|
|
109
|
+
" bun run rex:compile --expr \"when x do y end\"",
|
|
110
|
+
" bun run rex:compile --file input.rex",
|
|
111
|
+
" cat input.rex | bun run rex:compile",
|
|
112
|
+
"",
|
|
113
|
+
"Options:",
|
|
114
|
+
" -e, --expr <source> Compile an inline expression/program",
|
|
115
|
+
" -f, --file <path> Compile source from a file",
|
|
116
|
+
" -o, --out <path> Write output to file instead of stdout",
|
|
117
|
+
" --ir Output lowered IR JSON instead of compact encoding",
|
|
118
|
+
" -m, --minify-names Minify local variable names in compiled output",
|
|
119
|
+
" --dedupe-values Deduplicate large repeated values using forward pointers",
|
|
120
|
+
" --dedupe-min-bytes <n> Minimum encoded value bytes for pointer dedupe (default: 4)",
|
|
121
|
+
" --domain-extension <name> Map domain symbol name to ref 0 (apostrophe)",
|
|
122
|
+
" --domain-ref <name=id> Map domain symbol name to a specific ref id",
|
|
123
|
+
" -h, --help Show this message",
|
|
124
|
+
].join("\n");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async function readStdin(): Promise<string> {
|
|
128
|
+
return Bun.stdin.text();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async function resolveSource(options: CliOptions): Promise<string> {
|
|
132
|
+
if (options.expr && options.file) throw new Error("Use only one of --expr or --file");
|
|
133
|
+
if (options.expr) return options.expr;
|
|
134
|
+
if (options.file) return Bun.file(options.file).text();
|
|
135
|
+
if (!process.stdin.isTTY) {
|
|
136
|
+
const piped = await readStdin();
|
|
137
|
+
if (piped.trim().length > 0) return piped;
|
|
138
|
+
}
|
|
139
|
+
throw new Error("No input provided. Use --expr, --file, or pipe source via stdin.");
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async function loadDomainRefsFromFolder(folderPath: string): Promise<Record<string, number>> {
|
|
143
|
+
const schemaPath = resolve(folderPath, "rex-domain.json");
|
|
144
|
+
const file = Bun.file(schemaPath);
|
|
145
|
+
if (!(await file.exists())) return {};
|
|
146
|
+
|
|
147
|
+
const parsed = JSON.parse(await file.text()) as DomainSchema;
|
|
148
|
+
if (!parsed || typeof parsed !== "object" || !parsed.globals || typeof parsed.globals !== "object") {
|
|
149
|
+
throw new Error(`Invalid rex-domain.json at ${schemaPath}: expected { globals: { ... } }`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const refs: Record<string, number> = {};
|
|
153
|
+
let nextRef = 0;
|
|
154
|
+
for (const name of Object.keys(parsed.globals)) {
|
|
155
|
+
refs[name] = nextRef;
|
|
156
|
+
nextRef += 1;
|
|
157
|
+
}
|
|
158
|
+
return refs;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async function resolveDomainRefs(options: CliOptions): Promise<Record<string, number>> {
|
|
162
|
+
const baseFolder = options.file ? dirname(resolve(options.file)) : process.cwd();
|
|
163
|
+
const autoRefs = await loadDomainRefsFromFolder(baseFolder);
|
|
164
|
+
return { ...autoRefs, ...options.domainRefs };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async function main() {
|
|
168
|
+
const options = parseArgs(Bun.argv.slice(2));
|
|
169
|
+
if (options.help) {
|
|
170
|
+
console.log(usage());
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const source = await resolveSource(options);
|
|
175
|
+
const domainRefs = await resolveDomainRefs(options);
|
|
176
|
+
const output = options.ir
|
|
177
|
+
? JSON.stringify(parseToIR(source), null, 2)
|
|
178
|
+
: compile(source, {
|
|
179
|
+
minifyNames: options.minifyNames,
|
|
180
|
+
dedupeValues: options.dedupeValues,
|
|
181
|
+
dedupeMinBytes: options.dedupeMinBytes,
|
|
182
|
+
domainRefs,
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
if (options.out) {
|
|
186
|
+
await Bun.write(options.out, `${output}\n`);
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
console.log(output);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
await main().catch((error) => {
|
|
193
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
194
|
+
console.error(`rex:compile error: ${message}`);
|
|
195
|
+
process.exit(1);
|
|
196
|
+
});
|