Class: Trees
Overview
A set of utility routines useful for all kinds of ANTLR trees.# from io import StringIO from antlr4.Token import Token from antlr4.Utils import escapeWhitespace from antlr4.tree.Tree import RuleNode, ErrorNode, TerminalNode, Tree, ParseTree
Class Method Summary collapse
- ._findAllNodes(t, index, findTokens, nodes) ⇒ Object
- .descendants(t) ⇒ Object
- .findAllNodes(cls, t, index, findTokens) ⇒ Object
- .findAllRuleNodes(t, ruleIndex) ⇒ Object
- .findAllTokenNodes(t, ttype) ⇒ Object
-
.getAncestors(t) ⇒ Object
Return a list of all ancestors of this node.
-
.getChildren(t) ⇒ Object
Return ordered list of all children of this node.
- .getNodeText(t, ruleNames = nil, recog = nil) ⇒ Object
-
.toStringTree(t, ruleNames = nil, recog = nil) ⇒ Object
node payloads to get the text for the nodes.
Class Method Details
._findAllNodes(t, index, findTokens, nodes) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/antlr4/tree/Trees.rb', line 89 def self._findAllNodes(t, index, findTokens, nodes) #from antlr4.ParserRuleContext import ParserRuleContext # check this node (the root) first if findTokens and t.kind_of? TerminalNode then nodes.push(t) if t.symbol.type==index elsif not findTokens and t.kind_of? ParserRuleContext then nodes.push(t) if t.ruleIndex == index end # check children for i in 0 .. t.getChildCount()-1 self._findAllNodes(t.getChild(i), index, findTokens, nodes) end end |
.descendants(t) ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/antlr4/tree/Trees.rb', line 103 def self.descendants(t) nodes = Array.new nodes.push(t) for i in 0..t.getChildCount()-1 nodes.concat(self.descendants(t.getChild(i))) end return nodes end |
.findAllNodes(cls, t, index, findTokens) ⇒ Object
83 84 85 86 87 |
# File 'lib/antlr4/tree/Trees.rb', line 83 def self.findAllNodes(cls, t, index, findTokens) nodes = Array.new _findAllNodes(t, index, findTokens, nodes) return nodes end |
.findAllRuleNodes(t, ruleIndex) ⇒ Object
79 80 81 |
# File 'lib/antlr4/tree/Trees.rb', line 79 def self.findAllRuleNodes(t, ruleIndex) return findAllNodes(t, ruleIndex, false) end |
.findAllTokenNodes(t, ttype) ⇒ Object
75 76 77 |
# File 'lib/antlr4/tree/Trees.rb', line 75 def self.findAllTokenNodes(t, ttype) return findAllNodes(t, ttype, true) end |
.getAncestors(t) ⇒ Object
Return a list of all ancestors of this node. The first node of
list is the root and the last is the parent of this node.
65 66 67 68 69 70 71 72 73 |
# File 'lib/antlr4/tree/Trees.rb', line 65 def self.getAncestors(t) ancestors = [] t = t.getParent() while not t.nil? do ancestors.unshift(t) # insert at start t = t.getParent() end return ancestors end |
.getChildren(t) ⇒ Object
Return ordered list of all children of this node
58 59 60 |
# File 'lib/antlr4/tree/Trees.rb', line 58 def self.getChildren(t) return (0 .. t.getChildCount()-1).map{|i| t.getChild(i) } end |
.getNodeText(t, ruleNames = nil, recog = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/antlr4/tree/Trees.rb', line 34 def self.getNodeText(t, ruleNames=nil, recog=nil) if not recog.nil? then ruleNames = recog.ruleNames end if not ruleNames.nil? then if t.kind_of? RuleNode then return ruleNames[t.getRuleContext().getRuleIndex()] elsif t.kind_of? ErrorNode then return t.to_s elsif t.kind_of? TerminalNode then if not t.symbol.nil? then return t.symbol.text end end end # no recog for rule names payload = t.getPayload() if payload.kind_of? Token then return payload.text end return t.getPayload().to_s end |
.toStringTree(t, ruleNames = nil, recog = nil) ⇒ Object
node payloads to get the text for the nodes. Detect
parse trees and extract data appropriately.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/antlr4/tree/Trees.rb', line 12 def self.toStringTree(t, ruleNames=nil, recog=nil) if not recog.nil? then ruleNames = recog.ruleNames end s = getNodeText(t, ruleNames).escapeWhitespace(false) if t.getChildCount()==0 then return s end StringIO.open do |buf| buf.write("(") buf.write(s) buf.write(' ') for i in 0..t.getChildCount()-1 do if i > 0 then buf.write(' ') end buf.write(toStringTree(t.getChild(i), ruleNames)) end buf.write(")") return buf.string() end end |