예제 #1
0
 public CodeGenerator(ITreeNodeStream input, TemplateCompiler compiler, string name, string template, IToken templateToken)
     : this(input, new RecognizerSharedState())
 {
     this._compiler = compiler;
     this.outermostTemplateName = name;
     this._template = template;
     this.templateToken = templateToken;
 }
예제 #2
0
 public void TestAnonIncludeArgMismatch3()
 {
     ITemplateErrorListener errors = new ErrorBuffer();
     string template = "<a:{x|foo},{bar}>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()
     {
         ErrorManager = new ErrorManager(errors)
     }).Compile(template);
     string expected = "1:11: anonymous template has 0 arg(s) but mapped across 1 value(s)" + newline;
     Assert.AreEqual(expected, errors.ToString());
 }
예제 #3
0
        public static void main(string[] args)
        {
            TemplateCompiler c = new TemplateCompiler(new TemplateGroup());

            string template         = File.ReadAllText(args[0]);
            List <FormalArgument> a = new List <FormalArgument>();

            a.Add(new FormalArgument("x"));
            string n = Path.GetFileNameWithoutExtension(args[0]);

            c.Compile(args[0], n, a, template, null);
        }
예제 #4
0
        public virtual void DefineArgumentDefaultValueTemplates(TemplateGroup group)
        {
            if (FormalArguments == null)
            {
                return;
            }

            foreach (FormalArgument fa in FormalArguments)
            {
                if (fa.DefaultValueToken != null)
                {
                    switch (fa.DefaultValueToken.Type)
                    {
                    case GroupParser.ANONYMOUS_TEMPLATE:
                        string           argSTname      = fa.Name + "_default_value";
                        TemplateCompiler c2             = new TemplateCompiler(group);
                        string           defArgTemplate = Utility.Strip(fa.DefaultValueToken.Text, 1);
                        fa.CompiledDefaultValue      = c2.Compile(group.FileName, argSTname, null, defArgTemplate, fa.DefaultValueToken);
                        fa.CompiledDefaultValue.Name = argSTname;
                        fa.CompiledDefaultValue.DefineImplicitlyDefinedTemplates(group);
                        break;

                    case GroupParser.STRING:
                        fa.DefaultValue = Utility.Strip(fa.DefaultValueToken.Text, 1);
                        break;

                    case GroupParser.LBRACK:
                        fa.DefaultValue = new object[0];
                        break;

                    case GroupParser.TRUE:
                    case GroupParser.FALSE:
                        fa.DefaultValue = fa.DefaultValueToken.Type == GroupParser.TRUE;
                        break;

                    default:
                        throw new NotSupportedException("Unexpected default value token type.");
                    }
                }
            }
        }
예제 #5
0
 public void TestProp2()
 {
     string template = "<u.id>: <u.name>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected =
         "load_attr 0, load_prop 1, write, write_str 2, " +
         "load_attr 0, load_prop 3, write";
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[u, id, : , name]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
예제 #6
0
 public void TestRegion()
 {
     string template = "x:<@r()>";
     // compile as if in root dir and in template 'a'
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup('<', '>')).Compile("a", template);
     string asmExpected =
         "write_str 0, new 1 0, write";
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[x:, /region__/a__r]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
예제 #7
0
 // for error location
 /** Compile a template */
 public virtual CompiledTemplate Compile(string srcName,
                           string name,
                           List<FormalArgument> args,
                           string template,
                           IToken templateToken)
 {
     //System.out.println("TemplateGroup.Compile: "+enclosingTemplateName);
     TemplateCompiler c = new TemplateCompiler(this);
     return c.Compile(srcName, name, args, template, templateToken);
 }
예제 #8
0
 public void TestAnonMap()
 {
     string template = "<name:{n | <n>}>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected =
         "load_attr 0, null, new 1 1, map, write";
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[name, _sub1]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
예제 #9
0
 public void TestSuperIncludeWithNamedArgs()
 {
     string template = "<super.foo(x=a,y={b})>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected =
         "args, load_attr 0, store_arg 1, new 2 0, store_arg 3, super_new_box_args 4, write";
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[a, x, _sub1, y, foo]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
예제 #10
0
        public virtual void DefineArgumentDefaultValueTemplates(TemplateGroup group)
        {
            if (FormalArguments == null)
                return;

            foreach (FormalArgument fa in FormalArguments)
            {
                if (fa.DefaultValueToken != null)
                {
                    switch (fa.DefaultValueToken.Type)
                    {
                    case GroupParser.ANONYMOUS_TEMPLATE:
                        string argSTname = fa.Name + "_default_value";
                        TemplateCompiler c2 = new TemplateCompiler(group);
                        string defArgTemplate = Utility.Strip(fa.DefaultValueToken.Text, 1);
                        fa.CompiledDefaultValue = c2.Compile(group.FileName, argSTname, null, defArgTemplate, fa.DefaultValueToken);
                        fa.CompiledDefaultValue.Name = argSTname;
                        fa.CompiledDefaultValue.DefineImplicitlyDefinedTemplates(group);
                        break;

                    case GroupParser.STRING:
                        fa.DefaultValue = Utility.Strip(fa.DefaultValueToken.Text, 1);
                        break;

                    case GroupParser.LBRACK:
                        fa.DefaultValue = new object[0];
                        break;

                    case GroupParser.TRUE:
                    case GroupParser.FALSE:
                        fa.DefaultValue = fa.DefaultValueToken.Type == GroupParser.TRUE;
                        break;

                    default:
                        throw new NotSupportedException("Unexpected default value token type.");
                    }
                }
            }
        }
예제 #11
0
 public void TestIndirectMapArg()
 {
     string template = "<name:(t)(x)>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected =
         "load_attr 0, load_attr 1, tostr, null, load_attr 2, new_ind 2, map, write";
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[name, t, x]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
예제 #12
0
 public void TestSuperInclude()
 {
     string template = "<super.foo()>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected =
         "super_new 0 0, write";
     code.Dump();
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[foo]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
예제 #13
0
 public void TestIncludeWithPassThrough()
 {
     string template = "hi <foo(...)>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected =
         "write_str 0, args, passthru 1, new_box_args 1, write";
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[hi , foo]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
예제 #14
0
 public void TestIndirectIncludeWitArgs()
 {
     string template = "hi <(foo)(a,b)>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected =
         "write_str 0, load_attr 1, tostr, load_attr 2, load_attr 3, new_ind 2, write";
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[hi , foo, a, b]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
예제 #15
0
 public void TestInclude()
 {
     string template = "hi <foo()>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected =
         "write_str 0, new 1 0, write";
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[hi , foo]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
예제 #16
0
 public void TestIfElse()
 {
     string template = "go: <if(name)>hi, foo<else>bye<endif>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected =
         "write_str 0, " +
         "load_attr 1, " +
         "brf 15, " +
         "write_str 2, " +
         "br 18, " +
         "write_str 3";
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[go: , name, hi, foo, bye]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
예제 #17
0
 public void TestAnonZipMap()
 {
     string template = "<a,b:{x,y | <x><y>}>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected =
         "load_attr 0, load_attr 1, null, null, new 2 2, zip_map 2, write";
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[a, b, _sub1]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
예제 #18
0
 public void TestRepeatedMap()
 {
     string template = "<name:bold():italics()>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected =
         "load_attr 0, null, new 1 1, map, null, new 2 1, map, write";
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[name, bold, italics]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
예제 #19
0
 public void TestList()
 {
     string template = "<[a,b]>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected = "list, load_attr 0, add, load_attr 1, add, write";
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[a, b]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
예제 #20
0
 public void TestRotMapArg()
 {
     string template = "<name:bold(x),italics()>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected =
         "load_attr 0, null, load_attr 1, new 2 2, null, new 3 1, rot_map 2, write";
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[name, x, bold, italics]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
예제 #21
0
 public void TestMapAsOption()
 {
     string template = "<a; wrap=name:bold()>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected =
         "load_attr 0, options, load_attr 1, null, new 2 1, map, " +
         "store_option 4, write_opt";
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[a, name, bold]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
예제 #22
0
 public void TestSuperIncludeWithArgs()
 {
     string template = "<super.foo(a,{b})>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected =
         "load_attr 0, new 1 0, super_new 2 2, write";
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[a, _sub1, foo]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
 public override CompiledTemplate LoadTemplateFile(string prefix, string unqualifiedFileName,
                                    ICharStream templateStream)
 {
     string template = templateStream.Substring(0, templateStream.Count);
     string templateName = Path.GetFileNameWithoutExtension(unqualifiedFileName);
     string fullyQualifiedTemplateName = prefix + templateName;
     CompiledTemplate impl = new TemplateCompiler(this).Compile(fullyQualifiedTemplateName, template);
     CommonToken nameT = new CommonToken(TemplateLexer.SEMI); // Seems like a hack, best I could come up with.
     nameT.InputStream = templateStream;
     RawDefineTemplate(fullyQualifiedTemplateName, impl, nameT);
     impl.DefineImplicitlyDefinedTemplates(this);
     return impl;
 }
예제 #24
0
 public void TestZipMapArg()
 {
     string template = "<names,phones:bold(x)>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected =
         "load_attr 0, load_attr 1, null, null, load_attr 2, new 3 3, zip_map 2, write";
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[names, phones, x, bold]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
예제 #25
0
 public void TestOptions()
 {
     string template = "hi <name; anchor, wrap=foo(), separator=\", \">";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected =
         "write_str 0, " +
         "load_attr 1, " +
         "options, " +
         "load_str 2, " +
         "store_option 0, " +
         "new 3 0, " +
         "store_option 4, " +
         "load_str 4, " +
         "store_option 3, " +
         "write_opt";
     string stringsExpected = // the ", , ," is the ", " separator string
         "[hi , name, true, foo, , ]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
 }
예제 #26
0
 public void TestAnonIncludeArgs()
 {
     string template = "<({ a, b | <a><b>})>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected =
         "new 0 0, tostr, write";
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[_sub1]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
예제 #27
0
 public void TestProp()
 {
     string template = "hi <a.b>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected =
         "write_str 0, load_attr 1, load_prop 2, write";
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[hi , a, b]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
예제 #28
0
 public void TestOptionAsTemplate()
 {
     string template = "hi <name; separator={, }>";
     CompiledTemplate code = new TemplateCompiler(new TemplateGroup()).Compile(template);
     string asmExpected =
         "write_str 0, load_attr 1, options, new 2 0, store_option 3, write_opt";
     string asmResult = code.GetInstructions();
     Assert.AreEqual(asmExpected, asmResult);
     string stringsExpected = "[hi , name, _sub1]";
     string stringsResult = code.strings.ToListString();
     Assert.AreEqual(stringsExpected, stringsResult);
 }
예제 #29
0
        public virtual void DefineArgumentDefaultValueTemplates(TemplateGroup group)
        {
            if (FormalArguments == null)
                return;

            foreach (FormalArgument fa in FormalArguments)
            {
                if (fa.DefaultValueToken != null)
                {
                    if (fa.DefaultValueToken.Type == GroupParser.ANONYMOUS_TEMPLATE)
                    {
                        string argSTname = fa.Name + "_default_value";
                        TemplateCompiler c2 = new TemplateCompiler(group);
                        string defArgTemplate = Utility.Strip(fa.DefaultValueToken.Text, 1);
                        fa.CompiledDefaultValue = c2.Compile(group.FileName, argSTname, null, defArgTemplate, fa.DefaultValueToken);
                        fa.CompiledDefaultValue.Name = argSTname;
                        fa.CompiledDefaultValue.DefineImplicitlyDefinedTemplates(group);
                    }
                    else if (fa.DefaultValueToken.Type == GroupParser.STRING)
                    {
                        fa.DefaultValue = Utility.Strip(fa.DefaultValueToken.Text, 1);
                    }
                    else
                    {
                        // true or false
                        fa.DefaultValue = fa.DefaultValueToken.Type == GroupParser.TRUE;
                    }
                }
            }
        }