You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by "ocs@ocs.cz" <oc...@ocs.cz> on 2020/05/19 15:57:20 UTC

Another ASTT question: own error collector, best way?

Hi there,

well, another thing I need to tweak in the compiler is to change the way the errors and warnings are reported.

I actually have a trick which so far seems to work and which I bumped into years ago. It looks like this (again removed the self-evident parts); as you can see, it is far from elegant:

===
@GroovyASTTransformation(phase = CompilePhase.INITIALIZATION)
class ErrorReporterOverrideAST extends ASTT {
    void visitNodes(ASTNode[] nodes, SourceUnit su) {
        ErrorCollectorProxy ecproxy=ErrorCollectorProxy.newInstance(su.configuration)
        ecproxy.su=su
        su.errorCollector=ecproxy
   }
}
@InheritConstructors class ErrorCollectorProxy extends ErrorCollector {
    SourceUnit su
    void _processSyntaxException(SyntaxException exc,SourceUnit ovrdsu=nil) { ... own processing and report ... }
    void _processMessage(Message message) {
        if (message in SyntaxErrorMessage) _processSyntaxException(message.cause)
        else { ... own processing and report ... }
    }
    void addError(Message message) {  _processMessage(message) }
    void addError(Message message, boolean fatal) { _processMessage(message) }
    void addErrorAndContinue(Message message) { _processMessage(message) }
    void addErrorAndContinue(SyntaxException error, SourceUnit source) { _processSyntaxException(error,source) }
    void addException(java.lang.Exception cause, SourceUnit source)  { ... own processing and report ... }
    void addFatalError(Message message) { _processMessage(message) }
    void addWarning(WarningMessage message) { ... own processing and report ... }
}
===

The question is, can't it be done in some more elegant and robust way? E.g., not through and ASTT but simply using a compilation customizer (I have checked, found none, alas). Or at least, with just one override point general enough to be sure I won't miss errors in future Groovy releases, or something like that?

Sufficient if it works Groovy 3+ only -- I won't change the ASTTs in my old projects anymore, but would like to clean up the newly refactored ones far as reasonably possible.

Thanks!
OC


Re: Another ASTT question: own error collector, best way?

Posted by Paul King <pa...@asert.com.au>.
What you have is probably fairly reasonable at this time.

There are ways to hook into the lifecycle, e.g.  invokeMethod, which I
assume you have looked at. You could remove some of the duplication around
calling _processMessage that way.

Cheers, Paul.


On Wed, May 20, 2020 at 1:57 AM ocs@ocs.cz <oc...@ocs.cz> wrote:

> Hi there,
>
> well, another thing I need to tweak in the compiler is to change the way
> the errors and warnings are reported.
>
> I actually have a trick which so far seems to work and which I bumped into
> years ago. It looks like this (again removed the self-evident parts); as
> you can see, it is far from elegant:
>
> ===
> @GroovyASTTransformation(phase = CompilePhase.INITIALIZATION)
> class ErrorReporterOverrideAST extends ASTT {
>     void visitNodes(ASTNode[] nodes, SourceUnit su) {
>         ErrorCollectorProxy
> ecproxy=ErrorCollectorProxy.newInstance(su.configuration)
>         ecproxy.su=su
>         su.errorCollector=ecproxy
>    }
> }
> @InheritConstructors class ErrorCollectorProxy extends ErrorCollector {
>     SourceUnit su
>     void _processSyntaxException(SyntaxException exc,SourceUnit
> ovrdsu=nil) { ... own processing and report ... }
>     void _processMessage(Message message) {
>         if (message in SyntaxErrorMessage)
> _processSyntaxException(message.cause)
>         else { ... own processing and report ... }
>     }
>     void addError(Message message) {  _processMessage(message) }
>     void addError(Message message, boolean fatal) {
> _processMessage(message) }
>     void addErrorAndContinue(Message message) { _processMessage(message) }
>     void addErrorAndContinue(SyntaxException error, SourceUnit source) {
> _processSyntaxException(error,source) }
>     void addException(java.lang.Exception cause, SourceUnit source)  { ...
> own processing and report ... }
>     void addFatalError(Message message) { _processMessage(message) }
>     void addWarning(WarningMessage message) { ... own processing and
> report ... }
> }
> ===
>
> The question is, can't it be done in some more elegant and robust way?
> E.g., not through and ASTT but simply using a compilation customizer (I
> have checked, found none, alas). Or at least, with just one override point
> general enough to be sure I won't miss errors in future Groovy releases, or
> something like that?
>
> Sufficient if it works Groovy 3+ only -- I won't change the ASTTs in my
> old projects anymore, but would like to clean up the newly refactored ones
> far as reasonably possible.
>
> Thanks!
> OC
>
>