Extending the Token class in JavaCC

Viewed 126

I'm working on a JavaCC project and I'm having some trouble extending the Token class. I want to create a subclass classed IDToken and override its getValue() method. Below is my subclass code

public class IDToken extends Token implements java.io.Serializable {
  private static final long serialVersionUID = 1L;
  public int kind;
  public int beginLine;
  public int beginColumn;
  public int endLine;
  public int endColumn;
  public String image;
  public Token next;
  public Token specialToken;
  public IDToken(){}
  public IDToken(int kind)
  {
    this(kind,null);
  }
  public IDToken(int kind, String image){
    this.kind=kind;
    this.image=image;

  }
  public String toString(){
    return this.image;
  }
  public static Token newToken(int ofKind, String image){
    switch (ofKind)
    {
      default: return new Token(ofKind, image);
    }
  }
  public static Token newToken(int ofKind){
    return newToken(ofKind, null);
  }
  @Override
  public Object getValue(){
    return "abcdefg";
  }
}

However the override doesn't seem to work, getValue() returns null. I think I'm supposed to use newToken() in Token.java to create my IDToken but I haven't been able to figure out how to do that just yet.

1 Answers

In order to be able to extend the Token class and re-define the getValue() method you need to set the TOKEN_FACTORY option to a custom class that has static method newToken(int, String) returning an instance of Token:

// Parser.jj
options {
    STATIC = false;
    JAVA_UNICODE_ESCAPE = true;
    UNICODE_INPUT = true;
    JDK_VERSION = "1.8";

    TOKEN_FACTORY = "FooBar";
}
// FooBar.java
class MyCustomToken extends Token {
    @Override
    public Object getValue() {
        return 42;
    }
}

public class FooBar {
    public static Token newToken(int kind, String image) {
        switch (kind) {
            case 42:
                return new MyCustomToken();
            default:
                return new Token(kind, image);
        }
    }
}
Related