[antlr-interest] Get code in constructor (Java target)

David-Sarah Hopwood david-sarah at jacaranda.org
Fri Jul 10 22:11:52 PDT 2009


Gustaf Johansson wrote:
> Hello,
> 
> I have a global hashmap that i want to initialize in the constructor.

If you declare an instance field with an initializer, or use a
'static {...}' block, Java will effectively copy that initializer or
block into all constructors.

For example:

@parser::members {
  private Map<KeyType, ValueType> map = initMap();
}

or

@parser::members {
  private Map<KeyType, ValueType> map;

  static {
    map = new HashMap<KeyType, ValueType>();
    ...
  }
}

-- 
David-Sarah Hopwood  ⚥  http://davidsarah.livejournal.com



More information about the antlr-interest mailing list