Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make generic visitor with parameterized return values: XVisitor<T> upon -visitor option #9

Closed
parrt opened this issue Feb 11, 2012 · 3 comments

Comments

@parrt
Copy link
Member

parrt commented Feb 11, 2012

add the following double dispatch:

public void accept(XVisitor visitor) { visitor.visit(this); }

and generate XVisitor. Make a BaseXVisitor that has a

void visitChildren(ParserRuleContext ctx) {
  for each child c: visit(c);
}

And generates the following default implementations:

void visit(slistContext ctx) { visitChildren(p); }

I guess we also need a generic method in ParserRuleContext:

public abstract void accept(XVisitor visitor);

People can do things like this:

int visit(NodeType n) { return n.left.accept(this) + n.right.accept(this); }
@DJCordhose
Copy link

Cool stuff. Also BaseVisitor is a good thing.

For generic return types this would be in each context class

    public <R> R accept(Visitor<R> visitor) {
        return visitor.visit(this);
    }

and something like this in a visitor implementation provided by the user

public Integer visit(startContext ctx) {
    return ctx.e.accept(this);
}

Am I right?

Visitor interface and blank default implementation would be generated by ANTLR I guess.

@DJCordhose
Copy link

The generated visitor interface might look like this

public interface Visitor {
R visit(rule1Context ctx);
R visit(rule2Context ctx);
...
}

parrt added a commit that referenced this issue Feb 15, 2012
@parrt parrt closed this as completed Feb 17, 2012
@parrt
Copy link
Member Author

parrt commented Feb 17, 2012

Fixed with parrt/antlr4@725b105. Merged into master in #16.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants