[antlr-interest] Access to delegates for imported grammars

Bill Andersen bill.andersen at mac.com
Tue Feb 1 16:03:47 PST 2011


Folks

I have a grammar A that imports B.  One thing I'd like to do is to provide a mechanism for non-default error reporting in A and B and I do this in the way described here

	http://www.antlr.org/wiki/display/ANTLR3/Error+reporting+and+recovery

Thing is, I have to plug this code 

  private ErrorReporter errorReporter = null;
  
  public void setErrorReporter(ErrorReporter errorReporter) {
    this.errorReporter = errorReporter;
    <delegate>.setErrorReporter(errorReporter);
  }

  // Overrides parser/lexer emitErrorMsg   
  public void emitErrorMessage(String msg) {
    if (errorReporter == null) {
      super.emitErrorMessage(msg);
    } else {
      errorReporter.reportError(msg);
    }
  }

into all the grammars where I want this functionality, including the imported grammars.

What I'd like to do is to define subclasses of Parser and TreeParser that include this behavior already.  I need a way for the method setErrorReporter() in A to also work on B.  Where I have <delegate> above is where I do this.  

The issue is that, if I provide these methods in defined subclasses of Parser and TreeParser, there seems to be no way in that code that  can get to the delegates without knowing their names.  What I'd like for setErrorReporter() is something like this (call my defined Parser subclass MyParser):

  public void setErrorReporter(ErrorReporter errorReporter) {
    this.errorReporter = errorReporter;
    for (MyParser delegate : getDelegates()) {
      delegate.setErrorReporter(errorReporter);
    }
  }

First, is there any way to do what I'm trying to do here?  Second, if there's not, is there a good workaround (I have a feeling that I might have to change ANTLR's processing of imports to generate that getDelegates() method)?

Any help appreciated

  .bill


More information about the antlr-interest mailing list