Sources

Visitor vs Listener

Previously I used listener pattern to implement Enkel parser.
There is hovewer another way to do that - Visitor. To enabled it specify -visitor on the commnad line.

I was kind of curious which one would be more suitable for Enkel so I created a small project that exposes the differences.
Check out this blog post where you can read full comparison and get sources.

The main benefit is that Visitor returns value where Listener does not:

There is less code to write.

Less bug prone. No need to store parsing result in the field and rely on getter.

//ListenerclassClassListenerextendsEnkelBaseListener<ClassDeclaration>{privateClassparsedClass;@OverridepublicvoidenterClassDeclaration(@NotNullEnkelParser.ClassDeclarationContextctx){StringclassName=ctx.className().getText();//do some other stuffparsedClass=newClass(className,methods);}publicClassgetParsedClass(){returnparsedClass;}}

//VisitorpublicclassClassVisitorextendsEnkelBaseVisitor<ClassDeclaration>{@OverridepublicClassDeclarationvisitClassDeclaration(@NotNullEnkelParser.ClassDeclarationContextctx){Stringname=ctx.className().getText();//do some other stuffreturnnewClassDeclaration(name,methods);}}

The decision to switch to Visitor pattern was therefore quite obvious.