@shapeshift-labs/frontier-lang-parser 0.3.35 → 0.3.36
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/dist/index.js +2 -11
- package/dist/source-syntax-report.js +34 -0
- package/dist/view.js +2 -10
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -16,7 +16,7 @@ import { parseRuntimeCapabilityBlock } from './runtime-capability.js';
|
|
|
16
16
|
import { parseNativeSourceBlock } from './source-evidence.js';
|
|
17
17
|
import { parseTargetProjectionMetadata } from './target-projection.js';
|
|
18
18
|
import { parseViewBlock } from './view.js';
|
|
19
|
-
import { FrontierSourceBlockKinds } from './source-syntax-report.js';
|
|
19
|
+
import { FrontierSourceBlockKinds, readFrontierSourceBlocks } from './source-syntax-report.js';
|
|
20
20
|
export { FrontierSourceBlockKinds, inspectFrontierSourceSyntax } from './source-syntax-report.js';
|
|
21
21
|
|
|
22
22
|
export function parseFrontierSource(source, options = {}) {
|
|
@@ -77,16 +77,7 @@ export function parseFrontierFile(name, source) { return parseFrontierSource(sou
|
|
|
77
77
|
function readName(source) { return /module\s+([A-Za-z_$][\w$]*)/.exec(source)?.[1]; }
|
|
78
78
|
function readId(source) { return /module\s+[A-Za-z_$][\w$]*\s+@id\(\s*["']([^"']+)["']\s*\)/.exec(source)?.[1]; }
|
|
79
79
|
function readBlocks(source) {
|
|
80
|
-
|
|
81
|
-
const header = new RegExp('\\b(' + FrontierSourceBlockKinds.join('|') + ')\\s+([^{}]+)\\{', 'g');
|
|
82
|
-
let match;
|
|
83
|
-
while ((match = header.exec(source))) {
|
|
84
|
-
let depth = 1; let index = header.lastIndex;
|
|
85
|
-
while (index < source.length && depth > 0) { const ch = source[index++]; if (ch === '{') depth++; if (ch === '}') depth--; }
|
|
86
|
-
blocks.push({ kind: match[1], header: match[2].trim(), body: source.slice(header.lastIndex, index - 1) });
|
|
87
|
-
header.lastIndex = index;
|
|
88
|
-
}
|
|
89
|
-
return blocks;
|
|
80
|
+
return readFrontierSourceBlocks(source);
|
|
90
81
|
}
|
|
91
82
|
function idFrom(header, fallback) { return /@id\(\s*["']([^"']+)["']\s*\)/.exec(header)?.[1] ?? fallback; }
|
|
92
83
|
function nameFrom(header) { return /^([A-Za-z_$][\w$]*)/.exec(header)?.[1] ?? 'Unnamed'; }
|
|
@@ -102,6 +102,39 @@ export function inspectFrontierSourceSyntax(source, options = {}) {
|
|
|
102
102
|
};
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
+
export function readFrontierSourceBlocks(source, options = {}) {
|
|
106
|
+
const report = inspectFrontierSourceSyntax(source, options);
|
|
107
|
+
return report.recognizedBlocks.filter((block) => !block.malformed).map((block) => ({
|
|
108
|
+
kind: block.kind,
|
|
109
|
+
header: block.header,
|
|
110
|
+
body: source.slice(block.bodyStartOffset, block.bodyEndOffset),
|
|
111
|
+
syntax: block
|
|
112
|
+
}));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function readFrontierNestedBlocks(kind, source) {
|
|
116
|
+
const structure = scanFrontierStructure(source);
|
|
117
|
+
const blocks = [];
|
|
118
|
+
const header = new RegExp('\\b' + escapeRegExp(kind) + '\\s+([^{}]+?)\\{', 'g');
|
|
119
|
+
let match;
|
|
120
|
+
while ((match = header.exec(source))) {
|
|
121
|
+
const start = match.index;
|
|
122
|
+
const open = header.lastIndex - 1;
|
|
123
|
+
if (!structure.codeOffsets[start] || !structure.codeOffsets[open]) continue;
|
|
124
|
+
const close = findMatchingBrace(structure, open);
|
|
125
|
+
if (close < 0) continue;
|
|
126
|
+
blocks.push({
|
|
127
|
+
kind,
|
|
128
|
+
header: match[1].trim(),
|
|
129
|
+
body: source.slice(header.lastIndex, close),
|
|
130
|
+
start,
|
|
131
|
+
end: close + 1
|
|
132
|
+
});
|
|
133
|
+
header.lastIndex = close + 1;
|
|
134
|
+
}
|
|
135
|
+
return blocks;
|
|
136
|
+
}
|
|
137
|
+
|
|
105
138
|
function readCandidateDeclarationBlocks(source, structure) {
|
|
106
139
|
const moduleRanges = readModuleRanges(source, structure);
|
|
107
140
|
const blocks = [];
|
|
@@ -282,3 +315,4 @@ function readId(source) { return /module\s+[A-Za-z_$][\w$]*\s+@id\(\s*["']([^"']
|
|
|
282
315
|
function idFrom(header) { return /@id\(\s*["']([^"']+)["']\s*\)/.exec(header)?.[1]; }
|
|
283
316
|
function nameFrom(header) { return /^([A-Za-z_$][\w$]*)/.exec(header)?.[1] ?? 'Unnamed'; }
|
|
284
317
|
function unique(values) { return [...new Set(values.filter(Boolean))]; }
|
|
318
|
+
function escapeRegExp(value) { return String(value).replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); }
|
package/dist/view.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { viewNode } from '@shapeshift-labs/frontier-lang-kernel';
|
|
2
|
+
import { readFrontierNestedBlocks } from './source-syntax-report.js';
|
|
2
3
|
|
|
3
4
|
export function parseViewBlock(block) {
|
|
4
5
|
const name = nameFrom(block.header);
|
|
@@ -79,16 +80,7 @@ function readRenderEvents(body) {
|
|
|
79
80
|
}
|
|
80
81
|
|
|
81
82
|
function readNestedBlocks(kind, source) {
|
|
82
|
-
|
|
83
|
-
const header = new RegExp('\\b' + kind + '\\s+([^{}]+)\\{', 'g');
|
|
84
|
-
let match;
|
|
85
|
-
while ((match = header.exec(source))) {
|
|
86
|
-
let depth = 1; let index = header.lastIndex;
|
|
87
|
-
while (index < source.length && depth > 0) { const ch = source[index++]; if (ch === '{') depth++; if (ch === '}') depth--; }
|
|
88
|
-
blocks.push({ kind, header: match[1].trim(), body: source.slice(header.lastIndex, index - 1), start: match.index, end: index });
|
|
89
|
-
header.lastIndex = index;
|
|
90
|
-
}
|
|
91
|
-
return blocks;
|
|
83
|
+
return readFrontierNestedBlocks(kind, source);
|
|
92
84
|
}
|
|
93
85
|
|
|
94
86
|
function stripNestedBlocks(kind, source) {
|