[stringtemplate-interest] Bug about map inheritance?

Minglei.Lee minglei.lee at gmail.com
Sat Jan 16 04:23:11 PST 2010


Hi, everyone

 

I think I found a bug about map inheritance in StringTemplate.

 

The official page
(http://www.antlr.org/wiki/display/ST/Template+inheritance) says that map
can be inheritance and overrided:

 

"The descendant template group can then add more templates and maps,
override inherited templates and maps or modify inherited templates in a
finer-grained manner using the template regions feature."

 

But when I defining a map in supergroup, and defining a map with the same
name in subgroup, StringTemplate complains "redefinition of map" and reports
an error. I found the following code in GroupParser.java:

 

if (g.getMap(name.getText())!=null) {

g.error("redefinition of map: "+name.getText());

}

 

Where g is an instance of StringTemplateGroup. The method 'getMap' is
defined as following:

 

public Map getMap(String name) {

if ( maps==null ) {

              if ( superGroup==null ) {

                        return null;

              }

              return superGroup.getMap(name);

     }

     Map m = (Map)maps.get(name);

     if ( m==null && superGroup!=null ) {

              m = superGroup.getMap(name);

     }

     return m;

}

 

This method lookups map not only in subgroup, but also in supergroup. So
when defining a map in subgroup with a same name in supergroup, an error
occurs.

 

I added a new method in StringTemplateGroup:

 

public boolean isMapDefinedInThisGroup(String text) {

return maps != null && maps.containsKey(text); 

}

 

And modified GroupParser as following:

 

if (g. isMapDefinedInThisGroup(name.getText())) {

g.error("redefinition of map: "+name.getText());

}

 

Then the error disappears, and the map inheritance functionality works as
expected. J

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20100116/713af953/attachment.html 


More information about the stringtemplate-interest mailing list