-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
254 lines (242 loc) · 7.29 KB
/
Copy pathparser.js
File metadata and controls
254 lines (242 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* eslint-disable func-style,unicorn/consistent-function-scoping */
const { CadenceParser } = require('@onflow/cadence-parser');
const { inspect } = require('node:util');
const { isObject } = require('node:util');
const { isPlainObject } = require('lodash');
const { isPrimitive } = require('node:util');
const { join } = require('node:path');
const { readFile } = require('node:fs/promises');
const { isArray } = require('node:util');
const resolvedParser = require.resolve('@onflow/cadence-parser');
const wasmPath = join(resolvedParser, '../../cadence-parser.wasm').replaceAll(
/\\/g,
'/',
);
const debugParser = false;
function log(...args) {
if (!debugParser) return;
console.log(...args);
}
function err(...args) {
if (!debugParser) return;
console.error(...args);
}
/**
* @typedef {Object} VarWithScope
* @property {string} identifier The name of the variable.
* @property {number} blockOffset The offset of the variable.
* @property {number} blockLength The length of the variable.
* @property {string} [type] The type of the variable.
*/
/**
* @param {number} pos
* @param {VarWithScope[]} vars
* @returns {VarWithScope[]}
*/
function filterVarsWithScope(pos, vars) {
return vars.filter(
(v) => v.blockOffset <= pos && pos <= v.blockOffset + v.blockLength,
);
}
log(wasmPath);
const parser = readFile(wasmPath).then((wasm) => CadenceParser.create(wasm));
/**
* @typedef {Object} BlockDecl
* @property {number} blockOffset
* @property {number} blockLength
*/
/**
* @typedef {Object} AccDecl
* @property {number} blockOffset
* @property {number} blockLength
* @property {string} identifier
*/
/**
* @typedef {Object} RestrictedDef
* @property {string} def,
* @property {string} parentDef
* @property {number} outerOffset
* @property {number} outerLength
* @property {number} nominalOffset
* @property {number} nominalLength
*/
/**
* ParserContext
* @global
* @typedef {Object} ParserContext
* @property {string} path
* @property {number} blockOffset
* @property {number} blockLength
* @property {string} typeParentDef
* @property {string[]} typeDeclPath
* @property {boolean} root
* @property {BlockDecl[]} blocks
* @property {VarWithScope[]} accDecls
* @property {RestrictedDef[]} restricted
*/
/**
* @param {string} data
* @returns {Promise<ParserContext>}
*/
async function parseCadence(data) {
const ast = (await parser).parse(data);
log(inspect(ast, true, 4));
function checkType(data, node, context) {
const interest = ['AnnotatedType', 'ReferencedType', 'RestrictedType'];
const ftype = interest.find((type) => node.Type === type || node[type]);
const def = data.substring(node.StartPos.Offset, node.EndPos.Offset + 1);
if (ftype && node[ftype])
checkType(data, node[ftype], {
...context,
typeDeclPath: [...context.typeDeclPath, ftype],
typeParentDef: def,
});
if (node.Type === 'RestrictedType' && node.RestrictedType) {
log(
'@@@@@@@@@!!!!!!@@@@@@@@@@',
def,
node,
node.RestrictedType.Identifier,
node.Restrictions,
);
context.restricted.push({
def,
parentDef: context.typeParentDef,
outerOffset: node.StartPos.Offset,
outerLength: node.EndPos.Offset - node.StartPos.Offset,
nominalOffset: node.RestrictedType.StartPos.Offset,
nominalLength:
node.RestrictedType.EndPos.Offset -
node.RestrictedType.StartPos.Offset,
});
}
log('@@@@@@@@@@@@@@@@', node);
}
function visit(
data,
node,
context = {
path: '',
blockOffset: 0,
blockLength: 0,
blocks: [],
accDecls: [],
restricted: [],
typeDeclPath: [],
root: true,
},
) {
if (context.blockLength === 0) context.blockLength = data.length;
let { blockOffset: newBlockOffset, blockLength: newBlockLength } = context;
const interest = [
// 'Prepare',
// 'Contract',
// 'FunctionDeclaration',
// 'VariableDeclaration',
// 'Block',
// 'InvocationExpression',
// 'InvokedExpression',
// 'MemberExpression',
];
let offset = context.offset || 0;
let lenght = context.lenght || 0;
if (node.StartPos && node.EndPos) {
offset = node.StartPos.Offset;
lenght = node.EndPos.Offset + 1;
if (node.Type === 'Block') {
newBlockOffset = offset;
newBlockLength = lenght;
context.blocks.push({
blockOffset: offset,
blockLength: lenght,
});
}
}
if (interest.includes(node.Type)) {
log(node, context);
log(context.path, node.Type);
log(data.substring(offset, lenght));
}
if (node.TypeArguments) {
for (const typeArgument of node.TypeArguments) {
checkType(data, typeArgument, context);
}
}
if (node.Type === 'VariableDeclaration') {
let isAccDecl = false;
if (node.Value && node.Value.InvokedExpression) {
const expr = node.Value.InvokedExpression;
if (expr.Identifier && expr.Identifier.Identifier === 'getAccount') {
isAccDecl = true;
}
log('=========', node.Value.InvokedExpression);
} else if (
node.Transfer?.Operation === 'TransferOperationCopy' &&
node.Value &&
node.Value.Identifier?.Identifier
) {
isAccDecl = true;
log('=========', node.Value.InvokedExpression);
}
if (isAccDecl) {
log('!!!!!!!!!!!!!!!!!', node.Identifier.Identifier);
if (
!context.accDecls.some(
(acc) =>
acc.identifier === node.Identifier.Identifier &&
acc.blockOffset === context.blockOffset &&
acc.blockLength === context.blockLength,
)
)
context.accDecls.push({
blockOffset: context.blockOffset,
blockLength: context.blockLength,
identifier: node.Identifier.Identifier,
});
}
}
function revisit(sub) {
return visit(data, sub, {
...context,
offset,
lenght,
path: `${context.path}/${node.Type ?? ''}`,
blockOffset: newBlockOffset,
blockLength: newBlockLength,
root: false,
});
}
const ignoreType = new Set(['NilExpression']);
for (const check in node) {
let checked = node[check];
checked = isArray(checked) ? checked : [checked];
for (const subChecked of checked) {
if (!subChecked) continue;
if (
isPlainObject(subChecked) &&
!isPrimitive(subChecked) &&
subChecked.Type
) {
if (ignoreType.has(subChecked.Type)) continue;
revisit(subChecked);
}
}
}
// if (node.Statements) {
// for (const sub of node.Statements) data = revisit(sub);
// }
const declDeep = new Set(['Program']);
if (node.Declarations) {
console.log('decl', node.Type, node.Type && declDeep.has(node.Type));
if (node.Type && declDeep.has(node.Type))
for (const sub of node.Declarations) data = revisit(sub);
}
if (node.Type === 'CompositeDeclaration') {
for (const sub of node.Members.Declarations) data = revisit(sub);
}
if (context.root) return context;
return data;
}
return visit(data, ast.program);
}
module.exports = { parseCadence, filterVarsWithScope };