[stringtemplate-interest] trouble wth regions

Terence Parr parrt at cs.usfca.edu
Fri Jan 12 19:45:34 PST 2007


A subtle error.

StringTemplateGroup(new StringReader(subText),  
DefaultTemplateLexer.class);

loads the templates before the supergroup is set.  Try creating sub  
gropu, set super, then loadTemplate or whatever it is.  Or I think  
you can say

group sub : superGroup;
...

Ter

On Jan 12, 2007, at 8:42 PM, Matthew Foemmel wrote:

> Hi all,
>
> I'm having a bit of trouble getting regions to work, and was  
> wondering if someone could give me a hand. I've created two groups:  
> a parent, which contains the layout for a web page, and a child,  
> which contains the actual content. I'd like use regions to create  
> "holes" in the layout page where the content page can insert values.
>
> For example, the parent group looks like this:
>
> -----
> group Super;
>
> main() ::= <<
> <html><body>$@body()$</body></html>
>>>
> -----
>
> and the child group looks like this:
>
> -----
> group Sub;
>
> @main.body() ::= "Hello, World!"
> -----
>
> However, when I try to get an instance of the "main" template from  
> the child group, I get an exception that says it can't find  
> template "main.st". If I remove the "@main.body()" template from  
> the child group, I don't get that error (but then the content from  
> the child group isn't inserted, obviously).
>
> Here's my JUnit test:
>
> -----
> import static org.junit.Assert.*;
>
> import java.io.StringReader;
>
> import org.antlr.stringtemplate.StringTemplateGroup;
> import org.antlr.stringtemplate.StringTemplateGroupInterface;
> import org.antlr.stringtemplate.StringTemplateGroupLoader;
> import org.antlr.stringtemplate.language.DefaultTemplateLexer;
> import org.junit.Test;
>
>
> public class StringTemplateTest {
> @Test
> public void regionInhertiance() {
> String superText = "group Super;\n\nmain() ::= <<\n<html><body> 
> $@body()$</body></html>\n>>";
> StringTemplateGroup superGroup = new StringTemplateGroup(new  
> StringReader(superText), DefaultTemplateLexer.class);
>
> String subText = "group Sub;\n\n at main.body() ::= \"Hello, World!\"";
> StringTemplateGroup subGroup = new StringTemplateGroup(new  
> StringReader(subText), DefaultTemplateLexer.class);
> subGroup.setSuperGroup(superGroup);
>
> String expected = "<html><body>Hello, World!</body></html>";
> String actual = subGroup.getInstanceOf("main").toString();
>
> assertEquals(expected, actual);
> }
>
> }
> -----
>
> Any help would be appreciated (I'm using version 3.0 of  
> stringtemplate). Thanks in advance.
>
> Cheers,
> Foemmel
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest



More information about the stringtemplate-interest mailing list