* Parse tree walker for hyperscript. * Used by both the JetBrains plugin (GraalVM) and the LSP server (Node VM). * Evaluate this after loading the IIFE. * * Provides __hsParseAndWalk(src) which returns: * { ok: boolean, errors: [...], tree: { type, keyword, start, end, children } } * * The
(node, visited)
| 10 | * which tells you what command/feature was being parsed when it failed. |
| 11 | */ |
| 12 | function __hsWalkTree(node, visited) { |
| 13 | if (!node || typeof node !== 'object') return null; |
| 14 | if (visited.has(node)) return null; |
| 15 | visited.add(node); |
| 16 | var result = { |
| 17 | type: node.type || 'unknown', |
| 18 | keyword: node.keyword || null, |
| 19 | start: (node.startToken && node.startToken.start != null) ? node.startToken.start : null, |
| 20 | end: (node.endToken && node.endToken.end != null) ? node.endToken.end : null, |
| 21 | children: [] |
| 22 | }; |
| 23 | var childKeys = ['features', 'commands', 'start', 'root', 'next', 'body', |
| 24 | 'conditional', 'thenBranch', 'elseBranch', |
| 25 | 'args', 'expr', 'left', 'right', 'target']; |
| 26 | for (var i = 0; i < childKeys.length; i++) { |
| 27 | var key = childKeys[i]; |
| 28 | var val = node[key]; |
| 29 | if (!val) continue; |
| 30 | if (Array.isArray(val)) { |
| 31 | for (var j = 0; j < val.length; j++) { |
| 32 | var child = __hsWalkTree(val[j], visited); |
| 33 | if (child) result.children.push(child); |
| 34 | } |
| 35 | } else if (typeof val === 'object' && val.type) { |
| 36 | var child = __hsWalkTree(val, visited); |
| 37 | if (child) result.children.push(child); |
| 38 | } |
| 39 | } |
| 40 | if (node.next && !visited.has(node.next)) { |
| 41 | var nextChild = __hsWalkTree(node.next, visited); |
| 42 | if (nextChild) result.children.push(nextChild); |
| 43 | } |
| 44 | return result; |
| 45 | } |
| 46 | |
| 47 | function __hsParseAndWalk(src) { |
| 48 | var result = self._hyperscript.parse(src); |