In alot of the example in the book specifically in chapter 7 there are a bunch of tree walker and listeners which are defined by using this definition:
public static class PropertyFileLoader extends PropretyFileBaseListener{
...
public static class PropertyFileVisitor extends PropertyFileBaseVisitor<Void>
...
Why is the static there? What does it do?
The link above is talking about nested inner classes, I don't have any nested inner classes... I cant see a difference on using the static keyword and not using the static keyword.
An example of the auto generated classes produced by antlr are as follows:
public class CymbolBaseListener implements CymbolListener {
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterVarDecl(CymbolParser.VarDeclContext ctx){ }
public interface CymbolListener extends ParseTreeListener {
/**
* Enter a parse tree produced by {@link CymbolParser#rules}.
* @param ctx the parse tree
*/
void enterVarDecl(CymbolParser.VarDeclContext ctx);
where ParseTreeListener comes with the antlr runtime ParseTreeListener
If I wanted to use that these classes what I would do is the following:
public class MyListener extends CymbolBaseListener {
public void enterVarDecl(CymbolParser.VarDeclContext ctx)
{
System.out.println("I have found a variable declaration!");
}
}
and the way to get the listener going would be to write a static main which does the following:
public static void main(String[] args)
{
ANTLRFileStream input = new ANTLRFileStream(args[0]);
CymbolLexer mylexer = new CymbolLexer(input);
CommonTokenStream tokens = new CommonTokenStream(mylexer);
CymbolParser myparser = new CymbolParser(tokens);
MyListener listener = new MyListener();
ParseTreeWalker walker = new ParseTreeWalker();
ParseTree tree = myparser.rules();
walker.walk(listener, tree);
}
Why has the author made most of the examples use the static keyword:
public static class MyListener extends CymbolBaseListener
as a non-static class could also be used. I have just realized that some examples in the book have the static keyword whereas others do not but I don't understand what the intention behind it is.