|
| 1 | +const assert = (cond, msg = 'Assertion failed') => { |
| 2 | + throw new Error(msg); |
| 3 | +}; |
| 4 | + |
| 5 | +function handleListOf(child) { |
| 6 | + return child.toAST(this.args.mapping); |
| 7 | +} |
| 8 | + |
| 9 | +function handleEmptyListOf() { |
| 10 | + return []; |
| 11 | +} |
| 12 | + |
| 13 | +function handleNonemptyListOf(first, sep, rest) { |
| 14 | + return [first.toAST(this.args.mapping)].concat(rest.toAST(this.args.mapping)); |
| 15 | +} |
| 16 | + |
| 17 | +const defaultMapping = { |
| 18 | + listOf: handleListOf, |
| 19 | + ListOf: handleListOf, |
| 20 | + |
| 21 | + emptyListOf: handleEmptyListOf, |
| 22 | + EmptyListOf: handleEmptyListOf, |
| 23 | + |
| 24 | + nonemptyListOf: handleNonemptyListOf, |
| 25 | + NonemptyListOf: handleNonemptyListOf, |
| 26 | +}; |
| 27 | + |
| 28 | +class Visitor { |
| 29 | + constructor(mapping) { |
| 30 | + this.mapping = mapping; |
| 31 | + } |
| 32 | + |
| 33 | + visit(node, offset) { |
| 34 | + if (node.isTerminal()) { |
| 35 | + return this.visitTerminal(node, offset); |
| 36 | + } else if (node.isNonterminal()) { |
| 37 | + return this.visitNonterminal(node, offset); |
| 38 | + } else if (node.isIter()) { |
| 39 | + return this.visitIter(node, offset); |
| 40 | + } else { |
| 41 | + throw new Error(`Unknown node type: ${node._type}`); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + visitTerminal(node, offset) { |
| 46 | + return node.sourceString(offset); |
| 47 | + } |
| 48 | + |
| 49 | + visitNonterminal(node, offset) { |
| 50 | + const {ruleName} = node; |
| 51 | + const children = node.childrenNoSpaces; |
| 52 | + const {mapping} = this; |
| 53 | + |
| 54 | + let currOffset = offset; |
| 55 | + const childOffsets = node.children.flatMap((c, i) => { |
| 56 | + const origOffset = currOffset; |
| 57 | + currOffset += c.matchLength; |
| 58 | + return c.isNonterminal() && c.ruleName === '$spaces' ? [] : origOffset; |
| 59 | + }); |
| 60 | + |
| 61 | + // without customization |
| 62 | + if (!Object.hasOwn(mapping, ruleName)) { |
| 63 | + // lexical rule |
| 64 | + if (node.isLexical()) { |
| 65 | + return node.sourceString(offset); |
| 66 | + } |
| 67 | + |
| 68 | + // singular node (e.g. only surrounded by literals or lookaheads) |
| 69 | + const realChildren = children.filter(c => !c.isTerminal()); |
| 70 | + if (realChildren.length === 1) { |
| 71 | + const idx = children.indexOf(realChildren[0]); |
| 72 | + return this.visit(realChildren[0], childOffsets[idx]); |
| 73 | + } |
| 74 | + |
| 75 | + // rest: terms with multiple children |
| 76 | + } |
| 77 | + // direct forward |
| 78 | + if (typeof mapping[ruleName] === 'number') { |
| 79 | + assert(false, 'not handled: direct forward'); |
| 80 | + return this.visit(children[mapping[ruleName]]); |
| 81 | + } |
| 82 | + |
| 83 | + // named/mapped children or unnamed children ('0', '1', '2', ...) |
| 84 | + const propMap = mapping[ruleName] || children; |
| 85 | + const ans = { |
| 86 | + type: ruleName, |
| 87 | + }; |
| 88 | + // eslint-disable-next-line guard-for-in |
| 89 | + for (const prop in propMap) { |
| 90 | + const mappedProp = mapping[ruleName] && mapping[ruleName][prop]; |
| 91 | + if (typeof mappedProp === 'number') { |
| 92 | + // direct forward |
| 93 | + ans[prop] = this.visit(children[mappedProp], childOffsets[mappedProp]); |
| 94 | + } else if ( |
| 95 | + typeof mappedProp === 'string' || |
| 96 | + typeof mappedProp === 'boolean' || |
| 97 | + mappedProp === null |
| 98 | + ) { |
| 99 | + // primitive value |
| 100 | + ans[prop] = mappedProp; |
| 101 | + } else if (typeof mappedProp === 'object' && mappedProp instanceof Number) { |
| 102 | + // primitive number (must be unboxed) |
| 103 | + ans[prop] = Number(mappedProp); |
| 104 | + } else if (typeof mappedProp === 'function') { |
| 105 | + // computed value |
| 106 | + ans[prop] = mappedProp.call(node, children); |
| 107 | + } else if (mappedProp === undefined) { |
| 108 | + if (children[prop] && !children[prop].isTerminal()) { |
| 109 | + ans[prop] = this.visit(children[prop]); |
| 110 | + } else { |
| 111 | + // delete predefined 'type' properties, like 'type', if explicitely removed |
| 112 | + delete ans[prop]; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + return ans; |
| 117 | + } |
| 118 | + |
| 119 | + visitIter(node) { |
| 120 | + const children = node.childrenNoSpaces; |
| 121 | + if (node.isOptional()) { |
| 122 | + if (children.length === 0) { |
| 123 | + return null; |
| 124 | + } else { |
| 125 | + return this.visit(children[0]); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return children.map(c => this.visit(c)); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// Returns a plain JavaScript object that includes an abstract syntax tree (AST) |
| 134 | +// for the given match result `res` containg a concrete syntax tree (CST) and grammar. |
| 135 | +// The optional `mapping` parameter can be used to customize how the nodes of the CST |
| 136 | +// are mapped to the AST (see /doc/extras.md#toastmatchresult-mapping). |
| 137 | +export function toAST(matcher, mapping) { |
| 138 | + mapping = Object.assign({}, defaultMapping, mapping); |
| 139 | + // pd: Unclear if/how this is actually being used? |
| 140 | + // const operation = Object.assign({}, defaultOperation); |
| 141 | + // for (const termName in mapping) { |
| 142 | + // if (typeof mapping[termName] === 'function') { |
| 143 | + // operation[termName] = mapping[termName]; |
| 144 | + // delete mapping[termName]; |
| 145 | + // } |
| 146 | + // } |
| 147 | + return new Visitor(mapping).visit(matcher.getCstRoot(), 0); |
| 148 | +} |
0 commit comments