tunnelvisionlabs / antlr4ts

Optimized TypeScript target for ANTLR 4
Other
634 stars 109 forks source link

Question: Visitor context typings? #498

Open GordonSmith opened 3 years ago

GordonSmith commented 3 years ago

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?

obiwanjacobi commented 3 years ago

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]

GordonSmith commented 3 years ago

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.