I'm trying to have my parser rule select an enum value based on my DIR
token. Is there a way I can do this without creating separate, full-fledged tokens for each direction? Or generally a cleaner approach?
DIR : (NORTH|SOUTH) (EAST|WEST)?
| EAST
| WEST;
fragment NORTH: N '.'? | N O R T H;
fragment SOUTH: S '.'? | S O U T H;
fragment EAST : E '.'? | E A S T;
fragment WEST : W '.'? | W E S T;
(there are token fragments for each letter to facilitate case-insensitivity)
The enum is public enum Direction { NORTH, SOUTH, EAST, WEST, NORTHEAST, NORTHWEST, SOUTHEAST, SOUTHWEST }
Right now the only solution I see is to convert DIR
to a parser rule and make the directions separate tokens:
NORTH: N '.'? | N O R T H;
SOUTH: S '.'? | S O U T H;
dir returns [Direction dir]
: NORTH { dir = Direction.NORTH; }
| SOUTH { dir = Direction.SOUTH; }
This isn't terrible for this scenario, but I've got some other enums that will have lots more options so I'm looking for any ways to simplify this.