antlr4ts icon indicating copy to clipboard operation
antlr4ts copied to clipboard

Question: Visitor context typings?

Open GordonSmith opened this issue 5 years ago • 2 comments

I ported over an existing antlr (JavaScript) project to use antlr4ts, with the expectation that the visitor callbacks would have strong typing for both the context and the return value from "visitChildren". I see neither?

Am I doing something wrong, or was my expectation wrong?

GordonSmith avatar Jan 16 '21 12:01 GordonSmith

Here is what I do:

import { AbstractParseTreeVisitor } from "antlr4ts/tree/AbstractParseTreeVisitor";

class MyVisitor extends AbstractParseTreeVisitor<ReturnType> implements ProjectVisitor<ReturnType>{

    visitRule(ctx: RuleContext): ReturnType {
        // ....
        return new ReturnType();
    }
}

Where ProjectVisitor is the Visitor interface that was generated for your grammar. The ReturnType is a generic type that you specify for this specific visitor implementation. You only have to implement the visitXxxx functions you actually need.

HTH [2c]

obiwanjacobi avatar Jan 20 '21 19:01 obiwanjacobi

That is similar to what I had, but I wasn't specifically using RuleContext, if that is typed correctly (with alias support) that is probably 80% of what I am looking for - thx.

GordonSmith avatar Jan 21 '21 04:01 GordonSmith