Inheritance: TemplateGroup
Example #1
0
        public void TestImportDir()
        {
            /*
            dir1
                g.stg has a() that imports dir2 with absolute path
            dir2
                a.st
                b.st
             */
            string dir1 = Path.Combine(tmpdir, "dir1");
            string dir2 = Path.Combine(tmpdir, "dir2");
            string gstr =
                "import \"" + dir2 + "\"\n" +
                "a() ::= <<dir1 a>>\n";
            writeFile(dir1, "g.stg", gstr);

            string a = "a() ::= <<dir2 a>>\n";
            string b = "b() ::= <<dir2 b>>\n";
            writeFile(dir2, "a.st", a);
            writeFile(dir2, "b.st", b);

            TemplateGroup group = new TemplateGroupFile(Path.Combine(dir1, "g.stg"));
            Template st = group.GetInstanceOf("b"); // visible only if import worked
            string expected = "dir2 b";
            string result = st.Render();
            Assert.AreEqual(expected, result);
        }
Example #2
0
        public static string Render(def.BiTemporal definition, string templateName)
        {
            var templateDI = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory;
            var templateFI = new FileInfo(Path.Combine(templateDI.FullName, templateDirectory, templateFilename));

            var stg      = new st.TemplateGroupFile(templateFI.FullName);
            var template = stg.GetInstanceOf(templateName);

            template.Add("def", definition);

            return(template.Render());
        }
Example #3
0
 public void TestAlias()
 {
     string dir = tmpdir;
     string groupFile =
         "a() ::= \"bar\"\n" +
         "b ::= a\n";
     writeFile(dir, "group.stg", groupFile);
     TemplateGroupFile group = new TemplateGroupFile(Path.Combine(dir, "group.stg"));
     Template st = group.GetInstanceOf("b");
     string expected = "bar";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Example #4
0
        private static void ProcessTemplate([NotNull] string templateFile, Model.Model model)
        {
            if (templateFile == null) throw new ArgumentNullException("templateFile");

            Template template = new TemplateGroupFile(templateFile)
                .GetInstanceOf(MAIN_TEMPLATE_NAME);

            if (template == null)
                throw new ArgumentException(string.Format("Template {0} not found", MAIN_TEMPLATE_NAME));

            template.Add(MODEL_VAR, model);

            File.WriteAllText(BuildTargetFileName(templateFile), template.Render());
        }
Example #5
0
        public void TestDefaultValueBehaviorEmptyTemplate()
        {
            string templates =
                "t(a={}) ::= <<\n" +
                "<a><if(a)>+<else>-<endif>\n" +
                ">>\n";

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(tmpdir + Path.DirectorySeparatorChar + "t.stg");
            Template st = group.GetInstanceOf("t");
            string expected = "+";
            string result = st.Render();
            Assert.AreEqual(expected, result);
        }
Example #6
0
 public void TestAliasWithArgs()
 {
     string dir = tmpdir;
     string groupFile =
         "a(x,y) ::= \"<x><y>\"\n" +
         "b ::= a\n";
     writeFile(dir, "group.stg", groupFile);
     TemplateGroupFile group = new TemplateGroupFile(Path.Combine(dir, "group.stg"));
     Template st = group.GetInstanceOf("b");
     st.Add("x", 1);
     st.Add("y", 2);
     string expected = "12";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Example #7
0
        public void TestAnonymousTemplateInRegion()
        {
            string dir = tmpdir;
            string g = "a() ::= <<[<@r()>]>>\n" +
                       "@a.r() ::= <<\n"+
                       "<[\"foo\"]:{x|<x>}>\n"+
                       ">>\n";
            writeFile(dir, "g.stg", g);

            TemplateGroup group = new TemplateGroupFile(Path.Combine(dir, "g.stg"));
            Template st = group.GetInstanceOf("a");
            string expected = "[foo]";
            string result = st.Render();
            Assert.AreEqual(expected, result);
        }
Example #8
0
        public void TestCantDefineEmbeddedRegionAgain()
        {
            string dir = tmpdir;
            string g = "a() ::= <<[<@r>foo<@end>]>>\n" +
                       "@a.r() ::= <<bar>>\n"; // error; dup
            writeFile(dir, "g.stg", g);

            TemplateGroupFile group = new TemplateGroupFile(Path.Combine(dir, "g.stg"));
            ErrorBuffer errors = new ErrorBuffer();
            group.Listener = errors;
            group.Load();
            string expected = "g.stg 2:3: the explicit definition of region /a.r hides an embedded definition in the same group" + newline;
            string result = errors.ToString();
            Assert.AreEqual(expected, result);
        }
Example #9
0
        public void TestFortranLineWrap()
        {
            string templates =
                    "Function(args) ::= <<       FUNCTION line( <args; wrap=\"\\n      c\", separator=\",\"> )>>" + newline;
            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));

            Template a = group.GetInstanceOf("Function");
            a.Add("args",
                           new string[] { "a", "b", "c", "d", "e", "f" });
            string expecting =
                "       FUNCTION line( a,b,c,d," + Environment.NewLine +
                "      ce,f )";
            Assert.AreEqual(expecting, a.Render(30));
        }
Example #10
0
        public void TestArg()
        {
            string templates =
                "foo(a,) ::= << >>\n";
            writeFile(tmpdir, "t.stg", templates);

            TemplateGroupFile group;
            ITemplateErrorListener errors = new ErrorBuffer();
            group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
            group.Listener = errors;
            group.Load(); // force load
            string expected = "t.stg 1:6: missing ID at ')'" + newline;
            string result = errors.ToString();
            Assert.AreEqual(expected, result);
        }
Example #11
0
 public void TestInstanceofRenderer()
 {
     string templates =
             "numberThing(x,y,z) ::= \"numbers: <x>, <y>; <z>\"\n";
     writeFile(tmpdir, "t.stg", templates);
     TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
     group.RegisterRenderer(typeof(int), new NumberRenderer());
     group.RegisterRenderer(typeof(double), new NumberRenderer());
     Template st = group.GetInstanceOf("numberThing");
     st.Add("x", -2100);
     st.Add("y", 3.14159);
     st.Add("z", "hi");
     string expecting = "numbers: -2100, 3.14159; hi";
     string result = st.Render();
     Assert.AreEqual(expecting, result);
 }
Example #12
0
 public void TestDefaultArgument()
 {
     string templates =
             "method(name) ::= <<" + newline +
             "$stat(name)$" + newline +
             ">>" + newline +
             "stat(name,value=\"99\") ::= \"x=$value$; // $name$\"" + newline
             ;
     writeFile(tmpdir, "group.stg", templates);
     TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "group.stg"), '$', '$');
     Template b = group.GetInstanceOf("method");
     b.Add("name", "foo");
     string expecting = "x=99; // foo";
     string result = b.Render();
     Assert.AreEqual(expecting, result);
 }
Example #13
0
        public void TestEarlyEvalInIfExpr()
        {
            string templates = "main(x) ::= << <if((x))>foo<else>bar<endif> >>";
            writeFile(tmpdir, "t.stg", templates);

            TemplateGroup group = new TemplateGroupFile(tmpdir + "/t.stg");

            Template st = group.GetInstanceOf("main");

            string s = st.Render();
            Assert.AreEqual(" bar ", s);

            st.Add("x", "true");
            s = st.Render();
            Assert.AreEqual(" foo ", s);
        }
 public void TestIndirectCallWithPassThru()
 {
     // pass-through for dynamic template invocation is not supported by the
     // bytecode representation
     writeFile(tmpdir, "t.stg",
         "t1(x) ::= \"<x>\"\n" +
         "main(x=\"hello\",t=\"t1\") ::= <<\n" +
         "<(t)(...)>\n" +
         ">>");
     TemplateGroup group = new TemplateGroupFile(tmpdir + "/t.stg");
     ErrorBuffer errors = new ErrorBuffer();
     group.Listener = errors;
     Template st = group.GetInstanceOf("main");
     Assert.AreEqual("t.stg 2:34: mismatched input '...' expecting RPAREN" + newline, errors.ToString());
     Assert.IsNull(st);
 }
Example #15
0
        public void TestLocaleWithNumberRenderer()
        {
            //string templates = "foo(x,y) ::= << <x; format=\"%,d\"> <y; format=\"%,2.3f\"> >>\n";
            string templates = "foo(x,y) ::= << <x; format=\"{0:#,#}\"> <y; format=\"{0:0.000}\"> >>\n";

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
            group.RegisterRenderer(typeof(int), new NumberRenderer());
            group.RegisterRenderer(typeof(double), new NumberRenderer());
            Template st = group.GetInstanceOf("foo");
            st.Add("x", -2100);
            st.Add("y", 3.14159);
            // Polish uses ' ' (ASCII 160) for ',' and ',' for '.'
            string expecting = " -2 100 3,142 "; // Ê
            string result = st.Render(new CultureInfo("pl"));
            Assert.AreEqual(expecting, result);
        }
Example #16
0
        public void TestAccessDictionaryFromAnonymousTemplate()
        {
            string dir = tmpdir;
            string g =
                "a() ::= <<[<[\"foo\",\"a\"]:{x|<if(values.(x))><x><endif>}>]>>\n" +
                "values ::= [\n" +
                "    \"a\":false,\n" +
                "    default:true\n" +
                "]\n";
            writeFile(dir, "g.stg", g);

            TemplateGroup group = new TemplateGroupFile(Path.Combine(dir, "g.stg"));
            Template st = group.GetInstanceOf("a");
            string expected = "[foo]";
            string result = st.Render();
            Assert.AreEqual(expected, result);
        }
Example #17
0
        public void TestAttribute()
        {
            string templates =
                "t(x) ::= << <x> >>" + Environment.NewLine;

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
            Template st = group.GetInstanceOf("t");
            List<InterpEvent> events = st.GetEvents();
            string expected =
                "[IndentEvent{self=/t(x), expr=' <x>', source=[0..4), output=[0..1)}," +
                " EvalExprEvent{self=/t(x), expr='<x>', source=[1..4), output=[0..0)}," +
                " EvalExprEvent{self=/t(x), expr=' ', source=[4..5), output=[0..1)}," +
                " EvalTemplateEvent{self=/t(x), output=[0..1)}]";
            string result = events.ToListString();
            Assert.AreEqual(expected, result);
        }
        public void TestHiddenPropertyNotError()
        {
            ErrorBuffer errors = new ErrorBuffer();

            string templates =
                "t(u) ::= \"<u.name>\"" + Environment.NewLine;

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
            group.Listener = errors;
            Template st = group.GetInstanceOf("t");
            st.Add("u", new UserHiddenName("parrt"));
            st.Render();
            string expected = "";
            string result = errors.ToString();
            Assert.AreEqual(expected, result);
        }
Example #19
0
        public void TestIndentBeyondLineWidth()
        {
            string templates =
                    "duh(chars) ::= <<    <chars; wrap=\"\\n\"\\>>>" + newline;
            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));

            Template a = group.GetInstanceOf("duh");
            a.Add("chars", new string[] { "a", "b", "c", "d", "e" });
            //
            string expecting =
                "    a" + Environment.NewLine +
                "    b" + Environment.NewLine +
                "    c" + Environment.NewLine +
                "    d" + Environment.NewLine +
                "    e";
            Assert.AreEqual(expecting, a.Render(2));
        }
        public void TestDefineRegionInSubgroup()
        {
            string dir = tmpdir;
            string g1 = "a() ::= <<[<@r()>]>>\n";
            writeFile(dir, "g1.stg", g1);
            string g2 = "@a.r() ::= <%\n" +
            "	foo\n\n\n" +
            "%>\n";
            writeFile(dir, "g2.stg", g2);

            TemplateGroup group1 = new TemplateGroupFile(Path.Combine(dir, "g1.stg"));
            TemplateGroup group2 = new TemplateGroupFile(Path.Combine(dir, "g2.stg"));
            group2.ImportTemplates(group1); // define r in g2
            Template st = group2.GetInstanceOf("a");
            string expected = "[foo]";
            string result = st.Render();
            Assert.AreEqual(expected, result);
        }
Example #21
0
        private static void ProcessTemplate(string templateFile, Model.Model model)
        {
            string targetFile = BuildTargetFileName(templateFile);

            Console.WriteLine("\ttemplate: " + templateFile);
            try
            {
                Template template = new TemplateGroupFile(templateFile)
                    .GetInstanceOf(MAIN_TEMPLATE_NAME);

                template.Add(MODEL_VAR, model);

                File.WriteAllText(targetFile, template.Render());
            }
            finally
            {
                Console.WriteLine("\toutput:   " + targetFile);
            }
        }
Example #22
0
        internal StringTemplate(FileInfo fileInfo)
        {
            this.fileInfo = fileInfo;

            this.fileInfo.Refresh();
            if (!this.fileInfo.Exists)
            {
                throw new Exception("Template file not found: " + fileInfo.FullName);
            }

            TemplateGroup templateGroup = new TemplateGroupFile(this.fileInfo.FullName, '$', '$');

            this.id = Render(templateGroup, TemplateId) != null ? new Guid(Render(templateGroup, TemplateId)) : Guid.Empty;
            this.name = Render(templateGroup, TemplateName);
            this.version = Render(templateGroup, TemplateVersion);

            if (this.id == Guid.Empty)
            {
                throw new Exception("Template has no id");
            }
        }
Example #23
0
        public void TestIndexAttrVisibleLocallyOnly()
        {
            string templates =
                "t(names) ::= \"<names:{n | <u(n)>}>\"\n" +
                "u(x) ::= \"<i>:<x>\"";
            ErrorBuffer errors = new ErrorBuffer();
            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
            group.Listener = errors;
            Template st = group.GetInstanceOf("t");
            st.Add("names", "Ter");
            string result = st.Render();
            group.GetInstanceOf("u").impl.Dump();

            string expectedError = "t.stg 2:11: implicitly-defined attribute i not visible" + newline;
            Assert.AreEqual(expectedError, errors.ToString());

            string expected = ":Ter";
            Assert.AreEqual(expected, result);
            group.Listener = ErrorManager.DefaultErrorListener;
        }
Example #24
0
        public void TestArgWithSameNameAsEnclosing()
        {
            string templates =
                "t(x,y) ::= \"<u(x)>\"\n" +
                "u(y) ::= \"<x><y>\"";
            ErrorBuffer errors = new ErrorBuffer();
            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
            group.Listener = errors;
            Template st = group.GetInstanceOf("t");
            st.Add("x", "x");
            st.Add("y", "y");
            string result = st.Render();

            string expectedError = "";
            Assert.AreEqual(expectedError, errors.ToString());

            string expected = "xx";
            Assert.AreEqual(expected, result);
            group.Listener = ErrorManager.DefaultErrorListener;
        }
Example #25
0
        public void TestEarlyEvalOfMapInIfExpr()
        {
            string templates =
                    "m ::= [\n" +
                    "	\"parrt\": \"value\",\n" +
                    "	default: \"other\"\n" +
                    "]\n" +
                    "main(x) ::= << p<x>t: <m.({p<x>t})>, <if(m.({p<x>t}))>if<else>else<endif> >>\n";
            writeFile(tmpdir, "t.stg", templates);

            TemplateGroup group = new TemplateGroupFile(tmpdir + "/t.stg");

            Template st = group.GetInstanceOf("main");

            st.Add("x", null);
            string s = st.Render();
            Assert.AreEqual(" pt: other, if ", s);

            st.Add("x", "arr");
            s = st.Render();
            Assert.AreEqual(" parrt: value, if ", s);
        }
        public static string tryMyTemplate()
        {
            TemplateGroup stg = new TemplateGroupFile(@"C:\Users\Paulo\SkyDrive\Visual Studio 2013\Projects\DecafIde\DecafIde\CIL.stg");

            Template fileTemplate = stg.GetInstanceOf("file");
            Template structTemplate = stg.GetInstanceOf("struct");

            structTemplate.Add("name", "name1");
            structTemplate.Add("fields", "field1");
            structTemplate.Add("fields", "field2");

            fileTemplate.Add("defs", structTemplate.Render());

            structTemplate = stg.GetInstanceOf("struct");
            structTemplate.Add("name", "name2");
            structTemplate.Add("fields", "field21");
            structTemplate.Add("fields", "field22");

            fileTemplate.Add("defs", structTemplate.Render());

            return fileTemplate.Render();
        }
Example #27
0
        public void TestEmptyGroupImportGroupFileSameDir()
        {
            /*
            dir
                group1.stg		that imports group2.stg in same dir with just filename
                group2.stg		has c()
             */
            string dir = tmpdir;
            string groupFile =
                "import \"group2.stg\"\n";
            writeFile(dir, "group1.stg", groupFile);

            groupFile =
                "c() ::= \"g2 c\"\n";
            writeFile(dir, "group2.stg", groupFile);

            TemplateGroup group1 = new TemplateGroupFile(Path.Combine(dir, "group1.stg"));
            Template st = group1.GetInstanceOf("c"); // should see c()
            string expected = "g2 c";
            string result = st?.Render();
            Assert.AreEqual(expected, result);
        }
Example #28
0
        public void SimpleVisualizerTest()
        {
            string templates =
                "method(type,name,locals,args,stats) ::= <<\n" +
                "public <type> <ick()> <name>(<args:{a| int <a>}; separator=\", \">) {\n" +
                "    <if(locals)>int locals[<locals>];<endif>\n" +
                "    <stats;separator=\"\\n\">\n" +
                "}\n" +
                ">>\n" +
                "assign(a,b) ::= \"<a> = <b>;\"\n" +
                "return(x) ::= <<return <x>;>>\n" +
                "paren(x) ::= \"(<x>)\"\n";

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
            group.TrackCreationEvents = true;
            Template st = group.GetInstanceOf("method");
            st.impl.Dump();
            st.Add("type", "float");
            st.Add("name", "foo");
            st.Add("locals", 3);
            st.Add("args", new string[] { "x", "y", "z" });
            Template s1 = group.GetInstanceOf("assign");
            Template paren = group.GetInstanceOf("paren");
            paren.Add("x", "x");
            s1.Add("a", paren);
            s1.Add("b", "y");
            Template s2 = group.GetInstanceOf("assign");
            s2.Add("a", "y");
            s2.Add("b", "z");
            Template s3 = group.GetInstanceOf("return");
            s3.Add("x", "3.14159");
            st.Add("stats", s1);
            st.Add("stats", s2);
            st.Add("stats", s3);

            st.Visualize();
        }
Example #29
0
        public void TestGroupFileInDirImportsAGroupDir()
        {
            /*
            dir
                g.stg has a() that imports subdir with relative path
                subdir
                    b.st
                    c.st
             */
            string dir = tmpdir;
            string gstr =
                "import \"subdir\"\n" + // finds subdir in dir
                "a() ::= \"a: <b()>\"\n";
            writeFile(dir, "g.stg", gstr);

            writeFile(dir, "subdir/b.st", "b() ::= \"b: <c()>\"\n");
            writeFile(dir, "subdir/c.st", "c() ::= <<subdir c>>\n");

            TemplateGroup group = new TemplateGroupFile(dir + "/g.stg");
            Template st = group.GetInstanceOf("a");
            string expected = "a: b: subdir c";
            string result = st.Render();
            Assert.AreEqual(expected, result);
        }
Example #30
0
        public void TestArg3()
        {
            string templates =
                "foo(a b) ::= << >>\n";
            writeFile(tmpdir, "t.stg", templates);

            TemplateGroupFile group;
            ErrorBuffer errors = new ErrorBuffer();
            group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
            group.Listener = errors;
            group.Load(); // force load
            string expected =
                "[t.stg 1:6: no viable alternative at input 'b']";
            string result = errors.Errors.ToListString();
            Assert.AreEqual(expected, result);
        }
Example #31
0
        public void TestUnterminatedString()
        {
            string templates =
                "f() ::= \""; // extra }
            writeFile(tmpdir, "t.stg", templates);

            TemplateGroupFile group;
            ErrorBuffer errors = new ErrorBuffer();
            group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
            group.Listener = errors;
            group.Load(); // force load
            string expected = "[t.stg 1:9: unterminated string, t.stg 1:9: missing template at '<EOF>']";
            string result = errors.Errors.ToListString();
            Assert.AreEqual(expected, result);
        }