copyFrom copies error nodes over. Fixes #1298

This commit is contained in:
parrt 2016-12-09 11:10:26 -08:00
parent 53918ab41e
commit eb8e2890c3
3 changed files with 23 additions and 2 deletions

View File

@ -110,6 +110,7 @@ public abstract class BaseRuntimeTest {
@Test
public void testOne() throws Exception {
// System.out.println(delegate.getTmpDir());
if ( descriptor.ignore(descriptor.getTarget()) ) {
System.out.printf("Ignore "+descriptor);
return;

View File

@ -74,9 +74,9 @@ public class ParseTreesDescriptors {
}
public static class ExtraTokensShouldBeInTree extends BaseParserTestDescriptor {
public static class ExtraTokensAndAltLabels extends BaseParserTestDescriptor {
public String input = "${ ? a ?}";
public String output = "(s ${ ? (v a) ? })\n";
public String output = "(s ${ (v ? a) ? })\n";
public String errors =
"line 1:3 extraneous input '?' expecting {'a', 'b'}\n"+
"line 1:7 extraneous input '?' expecting '}'\n";

View File

@ -80,6 +80,14 @@ public class ParserRuleContext extends RuleContext {
/** COPY a ctx (I'm deliberately not using copy constructor) to avoid
* confusion with creating node with parent. Does not copy children.
*
* This is used in the generated parser code to flip a generic XContext
* node for rule X to a YContext for alt label Y. In that sense, it is
* not really a generic copy function.
*
* If we do an error sync() at start of a rule, we might add error nodes
* to the generic XContext so this function must copy those nodes to
* the YContext as well else they are lost!
*/
public void copyFrom(ParserRuleContext ctx) {
this.parent = ctx.parent;
@ -87,6 +95,18 @@ public class ParserRuleContext extends RuleContext {
this.start = ctx.start;
this.stop = ctx.stop;
// copy any error nodes to alt label node
if ( ctx.children!=null ) {
this.children = new ArrayList<>();
// reset parent pointer for any error nodes
for (ParseTree child : ctx.children) {
if ( child instanceof ErrorNodeImpl ) {
this.children.add(child);
((ErrorNodeImpl) child).parent = this;
}
}
}
}
public ParserRuleContext(ParserRuleContext parent, int invokingStateNumber) {