Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contributors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,4 @@ YYYY/MM/DD, github id, Full name, email
2021/05/06, canastro, Ricardo Canastro, ricardocanastro@users.noreply.github.com
2021/07/01, marcauberer, Marc Auberer, marc.auberer@chillibits.com
2021/07/14, renzhentaxibaerde, Renzhentaxi Baerde, renzhentaxibaerde@gmail.com
2021/07/19, tom-james-watson, Thomas Watson, antlr.contributors@tomjwatson.com
36 changes: 31 additions & 5 deletions runtime/JavaScript/src/antlr4/tree/Trees.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,52 @@ const Trees = {
* node payloads to get the text for the nodes. Detect
* parse trees and extract data appropriately.
*/
toStringTree: function(tree, ruleNames, recog) {
toStringTree: function(tree, ruleNames, recog, prettyPrint, indentLvl) {
ruleNames = ruleNames || null;
recog = recog || null;
prettyPrint = prettyPrint || false;
indentLvl = indentLvl || 0;

if(recog!==null) {
ruleNames = recog.ruleNames;
}

let s = Trees.getNodeText(tree, ruleNames);
s = Utils.escapeWhitespace(s, false);
const c = tree.getChildCount();

if(c===0) {
if (prettyPrint) {
return `\n${" ".repeat(indentLvl)}${s}`;
}
return s;
}
let res = "(" + s + ' ';

let res = "(" + s + " ";
if (prettyPrint) {
res = `\n${" ".repeat(indentLvl)}${res}`;
}

if(c>0) {
s = Trees.toStringTree(tree.getChild(0), ruleNames);
s = Trees.toStringTree(
tree.getChild(0), ruleNames, recog, prettyPrint, indentLvl + 1
);
res = res.concat(s);
}

for(let i=1;i<c;i++) {
s = Trees.toStringTree(tree.getChild(i), ruleNames);
res = res.concat(' ' + s);
s = Trees.toStringTree(
tree.getChild(i), ruleNames, recog, prettyPrint, indentLvl + 1
);
if (prettyPrint) {
res = `${res}${" ".repeat(indentLvl)}${s}`;
} else {
res = res.concat(' ' + s);
}
}

if (prettyPrint) {
res = `${res}\n${" ".repeat(indentLvl)}`;
}
res = res.concat(")");
return res;
Expand Down