From 5f2ce89ea309df1574eeb780945e492c4065caf0 Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Thu, 18 Jun 2015 10:34:03 -0700 Subject: [PATCH] improve robustness --- runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java b/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java index cae1db694..aee54eea7 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java @@ -107,6 +107,7 @@ public class Trees { * @since 4.5.1 */ public static String toStringTree(Tree t, TreeTextProvider nodeTextProvider) { + if ( t==null ) return "null"; String s = Utils.escapeWhitespace(nodeTextProvider.getText(t), false); if ( t.getChildCount()==0 ) return s; StringBuilder buf = new StringBuilder(); @@ -265,8 +266,9 @@ public class Trees { if ( t instanceof ParserRuleContext ) { ParserRuleContext r = (ParserRuleContext) t; if ( startTokenIndex>=r.getStart().getTokenIndex() && // is range fully contained in t? - stopTokenIndex<=r.getStop().getTokenIndex() ) + (r.getStop()==null || stopTokenIndex<=r.getStop().getTokenIndex()) ) { + // note: r.getStop()==null likely implies that we bailed out of parser and there's nothing to the right return r; } }