2020-05-10 11:58:07 +02:00
|
|
|
|
|
|
|
const exported = {};
|
|
|
|
|
2020-05-26 16:55:41 +02:00
|
|
|
class NodeVisitor {
|
|
|
|
visit(node) {
|
|
|
|
for (const child of node.preorder()) {
|
|
|
|
const key = `visit${kindToString(child.kind)}`;
|
|
|
|
if (this[key] !== undefined) {
|
|
|
|
this[key](child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exported.NodeVisitor = NodeVisitor;
|
|
|
|
|
2020-05-10 11:58:07 +02:00
|
|
|
const nodeProto = {
|
|
|
|
|
2020-05-23 14:18:20 +02:00
|
|
|
*getChildNodes() {
|
|
|
|
for (const key of Object.keys(this)) {
|
2020-05-29 18:44:58 +02:00
|
|
|
if (key === 'span' || key === 'parentNode' || key === 'type') {
|
2020-05-23 14:18:20 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
const value = this[key];
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
for (const element of value) {
|
|
|
|
if (isSyntax(element)) {
|
|
|
|
yield element;
|
|
|
|
}
|
|
|
|
}
|
2020-05-27 09:11:59 +02:00
|
|
|
} else {
|
2020-05-23 14:18:20 +02:00
|
|
|
if (isSyntax(value)) {
|
|
|
|
yield value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-05-26 20:59:44 +02:00
|
|
|
visit(visitors) {
|
|
|
|
const stack = [this];
|
|
|
|
while (stack.length > 0) {
|
|
|
|
const node = stack.pop();
|
2020-05-28 14:08:49 +02:00
|
|
|
const kindName = kindToString(node.kind);
|
|
|
|
const kindNamesToVisit = [kindName, ...NODE_TYPES[kindName].parents];
|
2020-05-26 20:59:44 +02:00
|
|
|
for (const visitor of visitors) {
|
2020-05-28 14:08:49 +02:00
|
|
|
for (const kindName of kindNamesToVisit) {
|
|
|
|
const key = `visit${kindName}`
|
|
|
|
if (visitor[key] !== undefined) {
|
|
|
|
visitor[key](node);
|
|
|
|
}
|
|
|
|
}
|
2020-05-26 20:59:44 +02:00
|
|
|
}
|
|
|
|
for (const childNode of node.getChildNodes()) {
|
|
|
|
stack.push(childNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-05-23 14:18:20 +02:00
|
|
|
*preorder() {
|
|
|
|
const stack = [this];
|
|
|
|
while (stack.length > 0) {
|
|
|
|
const node = stack.pop();
|
|
|
|
yield node
|
|
|
|
for (const childNode of node.getChildNodes()) {
|
|
|
|
stack.push(childNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
mayContainKind(kind) {
|
|
|
|
// TODO
|
|
|
|
return true;
|
2020-05-26 20:59:44 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
getParentOfKind(kind) {
|
|
|
|
let currNode = this.parentNode;
|
|
|
|
while (currNode !== null) {
|
|
|
|
if (currNode.kind === kind) {
|
|
|
|
return currNode;
|
|
|
|
}
|
|
|
|
currNode = currNode.parentNode;
|
|
|
|
}
|
|
|
|
return null;
|
2020-05-23 14:18:20 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
*findAllChildrenOfKind(kind) {
|
|
|
|
for (const node of this.preorder()) {
|
|
|
|
if (!node.mayContainKind(kind)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (node.kind === kind) {
|
|
|
|
yield node
|
|
|
|
}
|
|
|
|
}
|
2020-05-10 11:58:07 +02:00
|
|
|
}
|
2020-05-23 14:18:20 +02:00
|
|
|
|
2020-05-10 11:58:07 +02:00
|
|
|
}
|
|
|
|
|
2020-05-10 15:56:34 +02:00
|
|
|
function isSyntax(value) {
|
|
|
|
return typeof value === 'object'
|
|
|
|
&& value !== null
|
|
|
|
&& value.__NODE_TYPE !== undefined;
|
|
|
|
}
|
|
|
|
|
2020-05-25 11:29:19 +02:00
|
|
|
exported.isSyntax = isSyntax;
|
|
|
|
|
2020-05-23 21:15:20 +02:00
|
|
|
let nextNodeId = 1;
|
|
|
|
|
2020-05-10 11:58:07 +02:00
|
|
|
function createNode(nodeType) {
|
|
|
|
const obj = Object.create(nodeProto);
|
|
|
|
Object.defineProperty(obj, '__NODE_TYPE', {
|
|
|
|
enumerable: false,
|
|
|
|
writable: false,
|
|
|
|
configurable: true,
|
|
|
|
value: nodeType,
|
|
|
|
});
|
|
|
|
Object.defineProperty(obj, 'kind', {
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
2020-05-10 15:56:34 +02:00
|
|
|
get() {
|
2020-05-10 11:58:07 +02:00
|
|
|
return this.__NODE_TYPE.index;
|
|
|
|
}
|
|
|
|
});
|
2020-05-27 19:57:15 +02:00
|
|
|
Object.defineProperty(obj, 'errors', {
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
value: [],
|
|
|
|
})
|
2020-05-23 21:15:20 +02:00
|
|
|
Object.defineProperty(obj, 'id', {
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
value: nextNodeId++,
|
|
|
|
})
|
2020-05-10 11:58:07 +02:00
|
|
|
obj.span = null;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const nodeName of Object.keys(NODE_TYPES)) {
|
2020-05-10 15:56:34 +02:00
|
|
|
|
2020-05-10 11:58:07 +02:00
|
|
|
exported[`create${nodeName}`] = function (...args) {
|
|
|
|
const nodeType = NODE_TYPES[nodeName];
|
|
|
|
const node = createNode(nodeType);
|
|
|
|
let i = 0;
|
|
|
|
const iter = nodeType.fields[Symbol.iterator]();
|
|
|
|
for (; i < args.length; i++) {
|
|
|
|
const { done, value } = iter.next();
|
|
|
|
if (done) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const [fieldName, fieldType] = value;
|
|
|
|
node[fieldName] = args[i];
|
|
|
|
}
|
|
|
|
while (true) {
|
|
|
|
const { done, value } = iter.next();
|
|
|
|
if (done) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const [fieldName, fieldType] = value;
|
|
|
|
throw new Error(`No argument provided for field '${fieldName}'`);
|
|
|
|
}
|
|
|
|
if (i < args.length) {
|
|
|
|
node.span = args[i++];
|
|
|
|
}
|
|
|
|
if (i < args.length) {
|
|
|
|
throw new Error(`Too many arguments provided to function create${nodeName}`);
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
2020-05-10 15:56:34 +02:00
|
|
|
|
2020-05-10 11:58:07 +02:00
|
|
|
}
|
|
|
|
|
2020-05-10 23:50:42 +02:00
|
|
|
exported.setParents = function setParents(node, parentNode = null) {
|
|
|
|
node.parentNode = parentNode;
|
2020-05-23 21:15:20 +02:00
|
|
|
for (const child of node.getChildNodes()) {
|
2020-05-10 23:50:42 +02:00
|
|
|
setParents(child, node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 11:58:07 +02:00
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = exports;
|
|
|
|
}
|
|
|
|
|