|
| 1 | +/** |
| 2 | + * @name Pretty print AST as graph |
| 3 | + * @description Debug the AST by printing it as a graph. |
| 4 | + * @id unified/debug-ast |
| 5 | + * @kind graph |
| 6 | + */ |
| 7 | + |
| 8 | +import unified |
| 9 | + |
| 10 | +private predicate skip(AstNode e) { e instanceof Modifier } |
| 11 | + |
| 12 | +AstNode getChild(AstNode n, int index) { |
| 13 | + result.getParent() = n and |
| 14 | + result.getParentIndex() = index and |
| 15 | + not skip(n) and |
| 16 | + not skip(result) |
| 17 | +} |
| 18 | + |
| 19 | +predicate relevant(AstNode n) { |
| 20 | + n.getLocation().getFile().getBaseName() = "CLI.swift" and |
| 21 | + n.getLocation().getStartLine() = [98 .. 120] and |
| 22 | + not skip(n) |
| 23 | +} |
| 24 | + |
| 25 | +// Add additional node info here |
| 26 | +string tag(AstNode n) { none() } |
| 27 | + |
| 28 | +query predicate nodes(AstNode n, string key, string val) { |
| 29 | + key = "semmle.label" and |
| 30 | + relevant(n) and |
| 31 | + val = concat(string t | t = tag(n)) + " " + n.toString() |
| 32 | +} |
| 33 | + |
| 34 | +// Add additional edge info here |
| 35 | +string etag(AstNode child, AstNode parent) { |
| 36 | + exists(string name, int i, string arg | |
| 37 | + PrintAst::getChild(parent, name, i) = child and |
| 38 | + result = name + arg and |
| 39 | + if i = -1 then arg = "()" else arg = "(" + i.toString() + ")" |
| 40 | + ) |
| 41 | +} |
| 42 | + |
| 43 | +query predicate edges(AstNode pred, AstNode succ, string attr, string val) { |
| 44 | + attr = "semmle.label" and |
| 45 | + relevant(succ) and |
| 46 | + exists(int i | |
| 47 | + pred = getChild(succ, i) and |
| 48 | + val = i + " " + concat(string t | t = etag(pred, succ) | t, ", ") |
| 49 | + ) |
| 50 | +} |
0 commit comments