@shapeshift-labs/frontier-lang-parser 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 +15 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +64 -0
- package/examples/todo.frontier +5 -0
- package/examples/todo.mjs +3 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ShapeShift Labs
|
|
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,15 @@
|
|
|
1
|
+
# @shapeshift-labs/frontier-lang-parser
|
|
2
|
+
|
|
3
|
+
Parser for the first Frontier Lang `.frontier` syntax slice.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @shapeshift-labs/frontier-lang-parser
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The parser projects text into `@shapeshift-labs/frontier-lang-kernel` documents. The syntax is intentionally small and experimental.
|
|
12
|
+
|
|
13
|
+
## License
|
|
14
|
+
|
|
15
|
+
MIT.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { FrontierLangDocument } from '@shapeshift-labs/frontier-lang-kernel';
|
|
2
|
+
export interface ParseFrontierOptions { readonly id?: string; readonly name?: string; }
|
|
3
|
+
export declare function parseFrontierSource(source: string, options?: ParseFrontierOptions): FrontierLangDocument;
|
|
4
|
+
export declare function parseFrontierFile(name: string, source: string): FrontierLangDocument;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { actionNode, createDocument, effectNode, entityNode, stateNode } from '@shapeshift-labs/frontier-lang-kernel';
|
|
2
|
+
|
|
3
|
+
export function parseFrontierSource(source, options = {}) {
|
|
4
|
+
const nodes = [];
|
|
5
|
+
const documentId = options.id ?? readId(source) ?? 'mod_frontier';
|
|
6
|
+
const documentName = options.name ?? readName(source) ?? 'FrontierModule';
|
|
7
|
+
for (const block of readBlocks(source)) {
|
|
8
|
+
if (block.kind === 'entity') nodes.push(parseEntity(block));
|
|
9
|
+
if (block.kind === 'state') nodes.push(parseState(block));
|
|
10
|
+
if (block.kind === 'action') nodes.push(parseAction(block));
|
|
11
|
+
if (block.kind === 'effect') nodes.push(parseEffect(block));
|
|
12
|
+
}
|
|
13
|
+
return createDocument({ id: documentId, name: documentName, nodes });
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function parseFrontierFile(name, source) {
|
|
17
|
+
return parseFrontierSource(source, { name: name.replace(/\.frontier$/, '') });
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function readName(source) { return /module\s+([A-Za-z_$][\w$]*)/.exec(source)?.[1]; }
|
|
21
|
+
function readId(source) { return /module\s+[A-Za-z_$][\w$]*\s+@id\(\s*["']([^"']+)["']\s*\)/.exec(source)?.[1]; }
|
|
22
|
+
function readBlocks(source) {
|
|
23
|
+
const blocks = [];
|
|
24
|
+
const header = /\b(entity|state|action|effect)\s+([^{}]+)\{/g;
|
|
25
|
+
let match;
|
|
26
|
+
while ((match = header.exec(source))) {
|
|
27
|
+
let depth = 1; let index = header.lastIndex;
|
|
28
|
+
while (index < source.length && depth > 0) { const ch = source[index++]; if (ch === '{') depth++; if (ch === '}') depth--; }
|
|
29
|
+
blocks.push({ kind: match[1], header: match[2].trim(), body: source.slice(header.lastIndex, index - 1) });
|
|
30
|
+
header.lastIndex = index;
|
|
31
|
+
}
|
|
32
|
+
return blocks;
|
|
33
|
+
}
|
|
34
|
+
function idFrom(header, fallback) { return /@id\(\s*["']([^"']+)["']\s*\)/.exec(header)?.[1] ?? fallback; }
|
|
35
|
+
function nameFrom(header) { return /^([A-Za-z_$][\w$]*)/.exec(header)?.[1] ?? 'Unnamed'; }
|
|
36
|
+
function parseEntity(block) {
|
|
37
|
+
const name = nameFrom(block.header);
|
|
38
|
+
const fields = [];
|
|
39
|
+
const fieldRe = /^\s*([A-Za-z_$][\w$]*)(?:\s+@id\(\s*["']([^"']+)["']\s*\))?\s*:\s*([^@{\n]+)([^\n{]*)(?:\{([^}]*)\})?/gm;
|
|
40
|
+
let m;
|
|
41
|
+
while ((m = fieldRe.exec(block.body))) {
|
|
42
|
+
const mergeText = (m[4] ?? '') + ' ' + (m[5] ?? '');
|
|
43
|
+
fields.push({ id: m[2] ?? `field_${name}_${m[1]}`, name: m[1], type: m[3].trim(), key: /@key/.test(m[4] ?? ''), merge: parseMerge(mergeText) });
|
|
44
|
+
}
|
|
45
|
+
return entityNode({ id: idFrom(block.header, `ent_${name}`), name, fields });
|
|
46
|
+
}
|
|
47
|
+
function parseState(block) {
|
|
48
|
+
const name = nameFrom(block.header);
|
|
49
|
+
const collections = [];
|
|
50
|
+
const re = /^\s*([A-Za-z_$][\w$]*)(?:\s+@id\(\s*["']([^"']+)["']\s*\))?\s*:\s*([^@{\n]+)(?:\{([^}]*)\})?/gm;
|
|
51
|
+
let m;
|
|
52
|
+
while ((m = re.exec(block.body))) collections.push({ id: m[2] ?? `collection_${name}_${m[1]}`, name: m[1], type: m[3].trim(), merge: parseMerge(m[4] ?? '') });
|
|
53
|
+
return stateNode({ id: idFrom(block.header, `state_${name}`), name, collections });
|
|
54
|
+
}
|
|
55
|
+
function parseAction(block) {
|
|
56
|
+
const name = nameFrom(block.header);
|
|
57
|
+
return actionNode({ id: idFrom(block.header, `action_${name}`), name, input: /input\s*:\s*([^\n]+)/.exec(block.body)?.[1]?.trim(), returns: /returns\s+([^\n]+)/.exec(block.body)?.[1]?.trim(), reads: readList('reads', block.body), writes: readList('writes', block.body), uses: readList('uses', block.body) });
|
|
58
|
+
}
|
|
59
|
+
function parseEffect(block) {
|
|
60
|
+
const name = nameFrom(block.header);
|
|
61
|
+
return effectNode({ id: idFrom(block.header, `effect_${name}`), name, capability: /capability\s+([^\n]+)/.exec(block.body)?.[1]?.trim() ?? name, resources: readList('resources', block.body) });
|
|
62
|
+
}
|
|
63
|
+
function readList(label, body) { const line = new RegExp('^\\s*' + label + '\\s+([^\\n]+)', 'm').exec(body)?.[1]; return line ? line.split(',').map((item) => item.trim()).filter(Boolean) : undefined; }
|
|
64
|
+
function parseMerge(text) { const kind = /merge\s+([A-Za-z][\w-]*)/.exec(text)?.[1]; if (!kind) return undefined; const law = /law\s+([A-Za-z][\w-]*)/.exec(text)?.[1]; return { kind, law }; }
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shapeshift-labs/frontier-lang-parser",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Parser for the first Frontier Lang .frontier syntax slice.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"examples",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "node scripts/build.mjs",
|
|
23
|
+
"test": "npm run build && node test/smoke.mjs",
|
|
24
|
+
"prepare": "npm run build",
|
|
25
|
+
"prepack": "npm test"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"frontier",
|
|
29
|
+
"semantic-merge",
|
|
30
|
+
"patch",
|
|
31
|
+
"replay",
|
|
32
|
+
"compiler"
|
|
33
|
+
],
|
|
34
|
+
"author": "ShapeShift Labs",
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/siliconjungle/-shapeshift-labs-frontier-lang-parser.git"
|
|
39
|
+
},
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/siliconjungle/-shapeshift-labs-frontier-lang-parser/issues"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/siliconjungle/-shapeshift-labs-frontier-lang-parser#readme",
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@shapeshift-labs/frontier-lang-kernel": "^0.1.0"
|
|
49
|
+
}
|
|
50
|
+
}
|