motoko
Version:
Compile and run Motoko smart contracts in Node.js or the browser.
88 lines • 2.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.simplifyAST = exports.asNode = void 0;
function asNode(ast) {
if (ast && typeof ast === 'object' && !Array.isArray(ast)) {
return ast;
}
}
exports.asNode = asNode;
function simplifyAST(ast, parent) {
if (Array.isArray(ast)) {
return ast.map((a) => simplifyAST(a, parent));
}
if (typeof ast !== 'object') {
return ast;
}
if (ast.name === '@') {
const [start, end, subAst] = ast.args;
const node = typeof subAst === 'string'
? { name: subAst, parent }
: simplifyAST(subAst, parent);
node.start = [+start.args[1], +start.args[2]];
node.end = [+end.args[1], +end.args[2]];
const file = start.args[0];
if (file) {
node.file = file;
}
return node;
}
if (ast.name === '@@') {
const [file, start, end] = ast.args;
return {
name: 'Region',
file,
start: [+start.args[0], +start.args[1]],
end: [+end.args[0], +end.args[1]],
};
}
if (ast.name === ':') {
const [typeAst, type, typeRep] = ast.args;
const node = typeof typeAst === 'string'
? { name: typeAst, parent }
: simplifyAST(typeAst, parent);
node.type = type;
node.typeRep = simplifyAST(typeRep, parent);
return node;
}
if (ast.name === '*') {
const [doc, docAst] = ast.args;
const node = typeof docAst === 'string'
? { name: docAst, parent }
: simplifyAST(docAst, parent);
node.doc = doc;
return node;
}
const node = {
name: ast.name,
};
Object.defineProperty(node, 'parent', {
value: parent,
enumerable: false,
});
node.args = simplifyAST(ast.args, node);
if (parent && ast.name === 'ID') {
// Inherit properties from parent for convenience.
Object.defineProperty(node, 'type', {
get: () => parent.type,
set: (newType) => parent.type = newType,
enumerable: true,
configurable: true,
});
Object.defineProperty(node, 'typeRep', {
get: () => parent.typeRep,
set: (newTypeRep) => parent.typeRep = newTypeRep,
enumerable: true,
configurable: true,
});
Object.defineProperty(node, 'doc', {
get: () => parent.doc,
set: (newDoc) => parent.doc = newDoc,
enumerable: true,
configurable: true,
});
}
return node;
}
exports.simplifyAST = simplifyAST;
//# sourceMappingURL=ast.js.map