[stringtemplate-interest] Bug about map inheritance?
Terence Parr
parrt at cs.usfca.edu
Sat Jan 16 13:46:24 PST 2010
thanks. adding to bug list. i'll check for v4.
Ter
On Jan 16, 2010, at 4:23 AM, Minglei.Lee wrote:
> 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
>
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
More information about the stringtemplate-interest
mailing list