Be Kind To New Users
Be Kind To New Users

Reputation: 10063

Setting types of parsed values in Antlr

I have a rule that looks like this:

INTEGER : [0-9]+;
myFields : uno=INTEGER COMMA dos=INTEGER

Right now to access uno I need to code:

Integer i = Integer.parseInt(myFields.uno.getText())

It would be much cleaner if I could tell antler to do that conversion for me; then I would just need to code:

Integer i = myFields.uno

What are my options?

Upvotes: 1

Views: 553

Answers (2)

GRosenberg
GRosenberg

Reputation: 6001

A standard way of handling custom operations on tokens is to embed them in a custom token class:

public class MyToken extends CommonToken {
    ....
    public Integer getInt() {
        return Integer.parseInt(getText()); // TODO: error handling
    }
}

Also create

public class MyTokenFactory extends TokenFactory { .... }

to source the custom tokens. Add the factory to the lexer using Lexer#setTokenFactory().

Within the custom TokenFactory, override the method

Symbol create(int type, String text); // (typically override both factory methods)

to construct and return a new MyToken.

Given that the signature includes the target token type type, custom type-specific token subclasses could be returned, each with their own custom methods.

Couple of issues with this, though. First, in practice, it is not typically needed: the assignment var is statically typed, so as in the the OP example,

options { TokenLabelType = "MyToken"; }

Integer i = myFields.uno.getInt(); // no cast required

If Integer is desired & expected, use getInt(). If Boolean ....

Second, ANTLR options allows setting a TokenLabelType to preclude the requirement to manually cast custom tokens. Use of only one token label type is supported. So, to use multiple token types, manual casting is required.

Upvotes: 0

cantSleepNow
cantSleepNow

Reputation: 10192

You could write the code as action, but it would still be explicit conversion (eventually). The parser (like every parser) parses the text and then it's up to "parsing events" (achieved by listener or visitor or actions in ANTLR4) to create meaningful structures/objects.
Of course you could extend some of the generated or built-in classes and then get the type directly, but as mentioned before, at some point you'll always need to convert text to some type needed.

Upvotes: 1

Related Questions