toStringTree static method

String toStringTree(
  1. Tree? t, {
  2. Parser? recog,
  3. List<String>? ruleNames,
})

Print out a whole tree in LISP form. {@link #getNodeText} is used on the node payloads to get the text for the nodes. Detect parse trees and extract data appropriately.

Implementation

static String toStringTree(
  Tree? t, {
  Parser? recog,
  List<String>? ruleNames,
}) {
  if (t == null) return 'null';
  ruleNames ??= recog?.ruleNames;
  var s = escapeWhitespace(getNodeText(t, ruleNames: ruleNames), false);
  if (t.childCount == 0) return s;
  final buf = StringBuffer();
  buf.write('(');
  s = escapeWhitespace(getNodeText(t, ruleNames: ruleNames), false);
  buf.write(s);
  buf.write(' ');
  for (var i = 0; i < t.childCount; i++) {
    if (i > 0) buf.write(' ');
    buf.write(toStringTree(t.getChild(i), ruleNames: ruleNames));
  }
  buf.write(')');
  return buf.toString();
}