@shapeshift-labs/frontier-lang-parser 0.3.13 → 0.3.14

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.
Files changed (2) hide show
  1. package/dist/conversion.js +112 -1
  2. package/package.json +1 -1
@@ -1,10 +1,22 @@
1
1
  const FAMILIES = {
2
2
  type: { field: 'typeConstraints', sourceKey: 'sourceTypes', targetKey: 'targetTypes' },
3
3
  typeConstraint: { field: 'typeConstraints', sourceKey: 'sourceTypes', targetKey: 'targetTypes' },
4
+ resourceTransfer: { field: 'resourceTransfers', sourceKey: 'sourceGraphs', targetKey: 'targetGraphs', graph: true },
5
+ resourceTransferConstraint: { field: 'resourceTransfers', sourceKey: 'sourceGraphs', targetKey: 'targetGraphs', graph: true },
6
+ 'resource-transfer': { field: 'resourceTransfers', sourceKey: 'sourceGraphs', targetKey: 'targetGraphs', graph: true },
7
+ ownership: { field: 'resourceTransfers', sourceKey: 'sourceGraphs', targetKey: 'targetGraphs', graph: true },
4
8
  controlFlow: { field: 'controlFlowConstraints', sourceKey: 'sourceControlFlows', targetKey: 'targetControlFlows' },
5
9
  controlFlowConstraint: { field: 'controlFlowConstraints', sourceKey: 'sourceControlFlows', targetKey: 'targetControlFlows' },
6
10
  lifetime: { field: 'lifetimeConstraints', sourceKey: 'sourceLifetimeConstraints', targetKey: 'targetLifetimeConstraints' },
7
11
  lifetimeConstraint: { field: 'lifetimeConstraints', sourceKey: 'sourceLifetimeConstraints', targetKey: 'targetLifetimeConstraints' },
12
+ borrowScope: { field: 'borrowScopeConstraints', sourceKey: 'sourceBorrowScopes', targetKey: 'targetBorrowScopes' },
13
+ borrowScopeConstraint: { field: 'borrowScopeConstraints', sourceKey: 'sourceBorrowScopes', targetKey: 'targetBorrowScopes' },
14
+ 'borrow-scope': { field: 'borrowScopeConstraints', sourceKey: 'sourceBorrowScopes', targetKey: 'targetBorrowScopes' },
15
+ 'borrow-scope-constraint': { field: 'borrowScopeConstraints', sourceKey: 'sourceBorrowScopes', targetKey: 'targetBorrowScopes' },
16
+ borrowChecker: { field: 'borrowCheckerConstraints', sourceKey: 'sourceBorrowScopes', targetKey: 'targetBorrowScopes' },
17
+ borrowCheckerConstraint: { field: 'borrowCheckerConstraints', sourceKey: 'sourceBorrowScopes', targetKey: 'targetBorrowScopes' },
18
+ 'borrow-checker': { field: 'borrowCheckerConstraints', sourceKey: 'sourceBorrowScopes', targetKey: 'targetBorrowScopes' },
19
+ 'borrow-checker-constraint': { field: 'borrowCheckerConstraints', sourceKey: 'sourceBorrowScopes', targetKey: 'targetBorrowScopes' },
8
20
  callableBoundary: { field: 'callableBoundaryConstraints', sourceKey: 'sourceCallables', targetKey: 'targetCallables' },
9
21
  adtPattern: { field: 'adtPatternConstraints', sourceKey: 'sourcePatterns', targetKey: 'targetPatterns' },
10
22
  dataLayout: { field: 'dataLayoutConstraints', sourceKey: 'sourceLayouts', targetKey: 'targetLayouts' },
@@ -58,7 +70,7 @@ function addConstraint(plan, family, name, text) {
58
70
  metadata: { name, family, authoredConversionBlockId: plan.id }
59
71
  });
60
72
  const recordKey = role === 'target' ? config.targetKey : config.sourceKey;
61
- entry[recordKey] = [record];
73
+ entry[recordKey] = [config.graph ? resourceGraphFromRecord(record, entry) : record];
62
74
  plan[config.field] = [...(plan[config.field] ?? []), entry];
63
75
  }
64
76
 
@@ -75,17 +87,28 @@ function parseConstraintRecord(name, text, role) {
75
87
  symbolId: readInlineWord('symbol', text) ?? readInlineWord('symbolId', text),
76
88
  symbolName: readInlineWord('symbolName', text),
77
89
  localName: readInlineWord('localName', text),
90
+ ownerId: readInlineWord('owner', text) ?? readInlineWord('ownerId', text),
91
+ ownerKind: readInlineWord('ownerKind', text),
92
+ mode: readInlineWord('mode', text) ?? readInlineWord('loanMode', text),
93
+ aliasKind: readInlineWord('aliasKind', text),
94
+ moveKind: readInlineWord('moveKind', text),
95
+ dropKind: readInlineWord('dropKind', text),
96
+ resourceKind: readInlineWord('resourceKind', text),
97
+ scopeKind: readInlineWord('scopeKind', text),
78
98
  typeKind: readInlineWord('typeKind', text),
79
99
  signatureHash: readInlineWord('signatureHash', text),
80
100
  contractHash: readInlineWord('contractHash', text),
81
101
  typeHash: readInlineWord('typeHash', text),
82
102
  flowKind: readInlineWord('flowKind', text),
103
+ controlFlowKind: readInlineWord('controlFlowKind', text),
104
+ sourceControlFlowId: readInlineWord('sourceControlFlowId', text),
83
105
  sourceId: readInlineWord('from', text) ?? readInlineWord('sourceId', text),
84
106
  targetId: readInlineWord('to', text) ?? readInlineWord('targetId', text),
85
107
  label: readInlineWord('label', text),
86
108
  conditionHash: readInlineWord('conditionHash', text),
87
109
  orderingKey: readInlineWord('orderingKey', text) ?? readInlineWord('orderKey', text),
88
110
  lifetimeKind: readInlineWord('lifetimeKind', text),
111
+ lifetimeRegionId: readInlineWord('lifetimeRegion', text) ?? readInlineWord('lifetimeRegionId', text),
89
112
  regionKind: readInlineWord('regionKind', text),
90
113
  predicate: readInlineQuoted('predicate', text) ?? readInlineWord('predicate', text),
91
114
  resourceId: readInlineWord('resource', text) ?? readInlineWord('resourceId', text),
@@ -103,6 +126,94 @@ function parseConstraintRecord(name, text, role) {
103
126
  });
104
127
  }
105
128
 
129
+ function resourceGraphFromRecord(record, entry) {
130
+ const tokens = lowerTokens(record);
131
+ const resourceId = record.resourceId ?? record.symbolId ?? `${record.id}_resource`;
132
+ const evidenceIds = record.evidenceIds ?? entry.evidenceIds;
133
+ return cleanRecord({
134
+ id: `${record.id}_resource_graph`,
135
+ sourceLanguage: entry.sourceLanguage,
136
+ target: entry.target,
137
+ sourcePath: record.sourcePath,
138
+ sourceHash: record.sourceHash,
139
+ evidenceIds,
140
+ resources: [{
141
+ id: resourceId,
142
+ resourceKind: record.resourceKind ?? record.kind ?? record.constraintKind,
143
+ sourcePath: record.sourcePath,
144
+ sourceHash: record.sourceHash,
145
+ evidenceIds,
146
+ metadata: { factKinds: record.factKinds, constraintKinds: record.constraintKinds }
147
+ }],
148
+ owners: needsOwner(tokens, record) ? [{
149
+ id: `${record.id}_owner`,
150
+ resourceId,
151
+ ownerId: record.ownerId ?? record.symbolId ?? `${resourceId}:owner`,
152
+ ownerKind: record.ownerKind ?? tokenKind(tokens, /owner|single-owner/) ?? 'owner',
153
+ evidenceIds
154
+ }] : undefined,
155
+ loans: needsLoan(tokens, record) ? [{
156
+ id: `${record.id}_loan`,
157
+ resourceId,
158
+ mode: record.mode ?? loanMode(tokens),
159
+ lifetimeRegionId: record.lifetimeRegionId,
160
+ evidenceIds
161
+ }] : undefined,
162
+ aliases: tokenMatches(tokens, /alias/) ? [{
163
+ id: `${record.id}_alias`,
164
+ resourceId,
165
+ aliasKind: record.aliasKind ?? tokenKind(tokens, /alias/) ?? 'alias',
166
+ evidenceIds
167
+ }] : undefined,
168
+ moves: tokenMatches(tokens, /move|transfer/) ? [{
169
+ id: `${record.id}_move`,
170
+ resourceId,
171
+ moveKind: record.moveKind ?? tokenKind(tokens, /move|transfer/) ?? 'move',
172
+ evidenceIds
173
+ }] : undefined,
174
+ drops: tokenMatches(tokens, /drop/) ? [{
175
+ id: `${record.id}_drop`,
176
+ resourceId,
177
+ dropKind: record.dropKind ?? tokenKind(tokens, /drop/) ?? 'drop',
178
+ evidenceIds
179
+ }] : undefined,
180
+ lifetimeRegions: record.lifetimeKind || record.lifetimeRegionId || tokenMatches(tokens, /lifetime/) ? [{
181
+ id: record.lifetimeRegionId ?? `${record.id}_lifetime`,
182
+ resourceId,
183
+ lifetimeKind: record.lifetimeKind ?? tokenKind(tokens, /lifetime/) ?? record.regionKind,
184
+ evidenceIds
185
+ }] : undefined,
186
+ borrowScopes: needsLoan(tokens, record) || record.scopeKind ? [{
187
+ id: `${record.id}_borrow_scope`,
188
+ resourceId,
189
+ scopeKind: record.scopeKind ?? record.kind,
190
+ lifetimeRegionId: record.lifetimeRegionId,
191
+ constraintKinds: record.constraintKinds,
192
+ evidenceIds
193
+ }] : undefined,
194
+ unsafeBoundaries: tokenMatches(tokens, /unsafe|raw/) ? [{
195
+ id: `${record.id}_unsafe`,
196
+ resourceId,
197
+ kind: tokenKind(tokens, /unsafe|raw/) ?? 'unsafe-boundary',
198
+ evidenceIds
199
+ }] : undefined
200
+ });
201
+ }
202
+
203
+ function lowerTokens(record) {
204
+ return unique([record.kind, record.constraintKind, record.scopeKind, record.resourceKind, record.ownerKind, record.mode, record.aliasKind, record.moveKind, record.dropKind, record.lifetimeKind, record.regionKind, record.flowKind, record.controlFlowKind, ...(record.constraintKinds ?? []), ...(record.factKinds ?? [])].filter(Boolean).map((value) => String(value).toLowerCase()));
205
+ }
206
+ function needsOwner(tokens, record) { return record.ownerId || record.ownerKind || tokenMatches(tokens, /owner|single-owner/); }
207
+ function needsLoan(tokens, record) { return record.mode || tokenMatches(tokens, /borrow|loan|raw/); }
208
+ function loanMode(tokens) {
209
+ if (tokens.includes('shared-borrow') || tokens.includes('shared')) return 'shared';
210
+ if (tokens.includes('exclusive-borrow') || tokens.includes('mutable') || tokens.includes('exclusive')) return 'exclusive';
211
+ if (tokens.includes('raw-access-boundary') || tokens.includes('raw')) return 'raw';
212
+ if (tokens.includes('move')) return 'move';
213
+ return 'unknown';
214
+ }
215
+ function tokenMatches(tokens, pattern) { return tokens.some((token) => pattern.test(token)); }
216
+ function tokenKind(tokens, pattern) { return tokens.find((token) => pattern.test(token)); }
106
217
  function idFrom(text, fallback) { return /@id\(\s*["']([^"']+)["']\s*\)/.exec(text)?.[1] ?? fallback; }
107
218
  function nameFrom(header) { return /^([A-Za-z_$][\w$]*)/.exec(header)?.[1] ?? 'Conversion'; }
108
219
  function readInlineWord(label, text) { return new RegExp('(?:^|\\s)' + label + '\\s+([^\\s,]+)').exec(text)?.[1]?.trim(); }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshift-labs/frontier-lang-parser",
3
- "version": "0.3.13",
3
+ "version": "0.3.14",
4
4
  "description": "Parser for the first Frontier Lang .frontier syntax slice.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",