Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make the auto-generated parser class implement an interface in ANTLR4?

I am using ANTLR 4 to create a parser, and I have completed my grammar. I need to inject some Java code into the resulting parser file that ANTLR auto-generates for me.

If I want to include a method in the resulting parser, I can add this to the ANTLR grammar:

@parser::members
{
  @Override
  public CGrammarParser.CSnippetContext call()
  {
    return cSnippet();
  }
}

If I want to include some import statements, I can add this to the grammar:

@header
{
  import java.lang.Thread;
  import java.lang.InterruptedException;
  import java.util.concurrent.Callable;
}

If I want to modify the class declaration so that it implements an interface, how do I do that? In other words, this is what ANTLR auto-generates:

public class CGrammarParser extends Parser 
{
  ...
}

But this is what I want it to generate:

public class CGrammarParser extends Parser implements Callable<CGrammarParser.CSnippetContext> 
{
  ...
}
like image 327
james.garriss Avatar asked Oct 19 '22 18:10

james.garriss


1 Answers

No, not like you describe (via interface(s)). However, you can define a super class your parser should extend from. This super class should, of course, extend ANTLR's Parser class. In your own (abstract) parser class, you then define the interfaces you want to implement.

Here's how that could work:

CallableParser

import java.util.concurrent.Callable;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.TokenStream;

public abstract class CallableParser extends Parser implements Callable<CGrammarParser.CSnippetContext>
{
    public CallableParser(TokenStream input)
    {
        super(input);
    }
}

CGrammar.g4

grammar CGrammar;

options
{
  superClass = CallableParser;
}

@header
{
  import java.lang.Thread;
  import java.lang.InterruptedException;
  import java.util.concurrent.Callable;
}

@parser::members
{
  @Override
  public CGrammarParser.CSnippetContext call()
  {
    return cSnippet();
  }
}

cSnippet
 : ANY*? EOF
 ;

ANY
 : .
 ;
like image 175
Bart Kiers Avatar answered Oct 21 '22 17:10

Bart Kiers