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

AST visitor pattern? #8

Closed
pcj opened this issue Aug 6, 2015 · 3 comments
Closed

AST visitor pattern? #8

pcj opened this issue Aug 6, 2015 · 3 comments

Comments

@pcj
Copy link
Contributor

pcj commented Aug 6, 2015

Great that you are implementing the commonmark spec in java.

I like the way pegdown implements the Visitor Pattern to be able to modify or interpret the AST once constructed. Is there a similar or planned facility for commonmark-java? For example, in pegdown:

import org.pegdown.PegDownProcessor;
import org.pegdown.Extensions;
import org.pegdown.ast.RootNode;
import org.pegdown.ast.Visitor;

...

int exts
    = Extensions.DEFINITIONS
    | Extensions.AUTOLINKS
    | Extensions.HARDWRAPS
    | Extensions.TABLES
    | Extensions.STRIKETHROUGH
    | Extensions.SUPPRESS_ALL_HTML
    ;

PegDownProcessor processor = new PegDownProcessor(exts);
RootNode root = processor.parseMarkdown(s.toCharArray());

MyVisitor visitor = new MyVisitor();
root.accept(visitor);

...

class MyVisitor implements Visitor {

    @Override
    public void visit(org.pegdown.ast.Node node) {
        log.debug("visit {}", node);
    }

   ...
}

Thanks,
Paul

@robinst
Copy link
Collaborator

robinst commented Aug 8, 2015

Hey!

In org.commonmark.node, there is both a Visitor interface and an AbstractVisitor class already. An example use:

Node node = parser.parse("...");
MyVisitor visitor = new MyVisitor();
node.accept(visitor);

class MyVisitor extends AbstractVisitor {
    @Override
    public void visit(Paragraph paragraph) {
        // Do something with paragraph (override other methods for other nodes):
        log.debug("visit {}", paragraph);
        // Descend into children:
        visitChildren(paragraph);
    }
}

Does that help? I guess this should be documented with an example in the README (and also do #4).

@pcj
Copy link
Contributor Author

pcj commented Aug 8, 2015

Yes that's it. Looks pretty obvious from the source now. Example in the README a good idea.

Thanks!

@robinst
Copy link
Collaborator

robinst commented Aug 17, 2015

Done now.

@robinst robinst closed this as completed Aug 17, 2015
@silenuz silenuz mentioned this issue Sep 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants