Example #1
0
        protected virtual int WriteSubTemplate(StringTemplate self,
                                               IStringTemplateWriter @out,
                                               StringTemplate subtemplate)
        {
            /* To evaluate the IF chunk, make a new instance whose enclosingInstance
             * points at 'self' so get attribute works.  Otherwise, enclosingInstance
             * points at the template used to make the precompiled code.  We need a
             * new template instance every time we exec this chunk to get the new
             * "enclosing instance" pointer.
             */
            StringTemplate s = subtemplate.GetInstanceOf();

            s.EnclosingInstance = self;
            // make sure we evaluate in context of enclosing template's
            // group so polymorphism works. :)
            s.Group       = self.Group;
            s.NativeGroup = self.NativeGroup;
            return(s.Write(@out));
        }
Example #2
0
        /** <summary>
         *  To write out the value of a condition expr, invoke the evaluator in eval.g
         *  to walk the condition tree computing the boolean value.  If result
         *  is true, then write subtemplate.
         *  </summary>
         */
        public override int Write(StringTemplate self, IStringTemplateWriter @out)
        {
            if (AST == null || self == null || @out == null)
            {
                return(0);
            }
            //System.Console.Out.WriteLine( "evaluating conditional tree: " + AST.ToStringTree() );
#if !COMPILE_EXPRESSIONS
            ActionEvaluator eval = null;
#endif
            int n = 0;
            try
            {
                bool testedTrue = false;
                // get conditional from tree and compute result
#if COMPILE_EXPRESSIONS
                if (EvaluateCondition == null)
                {
                    EvaluateCondition = GetEvaluator(this, AST.GetChild(0));
                }
                bool includeSubtemplate = EvaluateCondition(self, @out);   // eval and write out tree
#else
                ITree cond = AST.GetChild(0);
                eval = new ActionEvaluator(self, this, @out, cond);
                // eval and write out trees
                bool includeSubtemplate = eval.ifCondition();
#endif
                //System.Console.Out.WriteLine( "subtemplate " + _subtemplate );
                // IF
                if (includeSubtemplate)
                {
                    n          = WriteSubTemplate(self, @out, _subtemplate);
                    testedTrue = true;
                }
                // ELSEIF
                else if (_elseIfSubtemplates != null && _elseIfSubtemplates.Count > 0)
                {
                    for (int i = 0; i < _elseIfSubtemplates.Count; i++)
                    {
                        ElseIfClauseData elseIfClause = _elseIfSubtemplates[i];
#if COMPILE_EXPRESSIONS
                        if (elseIfClause.EvaluateCondition == null)
                        {
                            elseIfClause.EvaluateCondition = GetEvaluator(this, elseIfClause.expr.AST);
                        }
                        includeSubtemplate = elseIfClause.EvaluateCondition(self, @out);
#else
                        eval = new ActionEvaluator(self, this, @out, elseIfClause.expr.AST);
                        includeSubtemplate = eval.ifCondition();
#endif
                        if (includeSubtemplate)
                        {
                            WriteSubTemplate(self, @out, elseIfClause.st);
                            testedTrue = true;
                            break;
                        }
                    }
                }
                // ELSE
                if (!testedTrue && _elseSubtemplate != null)
                {
                    // evaluate ELSE clause if present and IF condition failed
                    StringTemplate s = _elseSubtemplate.GetInstanceOf();
                    s.EnclosingInstance = self;
                    s.Group             = self.Group;
                    s.NativeGroup       = self.NativeGroup;
                    n = s.Write(@out);
                }
                // cond==false and no else => Missing output not empty
                if (!testedTrue && _elseSubtemplate == null)
                {
                    n = Missing;
                }
            }
            catch (RecognitionException re)
            {
                self.Error("can't evaluate tree: " + AST.ToStringTree(), re);
            }
            return(n);
        }
Example #3
0
 protected virtual int WriteSubTemplate( StringTemplate self,
     IStringTemplateWriter @out,
     StringTemplate subtemplate)
 {
     /* To evaluate the IF chunk, make a new instance whose enclosingInstance
      * points at 'self' so get attribute works.  Otherwise, enclosingInstance
      * points at the template used to make the precompiled code.  We need a
      * new template instance every time we exec this chunk to get the new
      * "enclosing instance" pointer.
      */
     StringTemplate s = subtemplate.GetInstanceOf();
     s.EnclosingInstance = self;
     // make sure we evaluate in context of enclosing template's
     // group so polymorphism works. :)
     s.Group = self.Group;
     s.NativeGroup = self.NativeGroup;
     return s.Write( @out );
 }
 public void TestCatWithTemplateApplicationAsElement()
 {
     StringTemplate e = new StringTemplate(
             "$[names:{$it$!},phones]; separator=\", \"$"
         );
     e = e.GetInstanceOf();
     e.SetAttribute( "names", "Ter" );
     e.SetAttribute( "names", "Tom" );
     e.SetAttribute( "phones", "1" );
     e.SetAttribute( "phones", "2" );
     string expecting = "Ter!, Tom!, 1, 2";
     Assert.AreEqual( expecting, e.ToString() );
 }
 public void TestCatWithIFAsElement()
 {
     StringTemplate e = new StringTemplate(
             "$[{$if(names)$doh$endif$},phones]; separator=\", \"$"
         );
     e = e.GetInstanceOf();
     e.SetAttribute( "names", "Ter" );
     e.SetAttribute( "names", "Tom" );
     e.SetAttribute( "phones", "1" );
     e.SetAttribute( "phones", "2" );
     string expecting = "doh, 1, 2";
     Assert.AreEqual( expecting, e.ToString() );
 }
 public void TestCatListAndEmptyAttributes()
 {
     // + is overloaded to be cat strings and cat lists so the
     // two operands (from left to right) determine which way it
     // goes.  In this case, x+mine is a list so everything from their
     // to the right becomes list cat.
     StringTemplate e = new StringTemplate(
             "$[x,mine,y,yours,z]; separator=\", \"$"
         );
     e = e.GetInstanceOf();
     e.SetAttribute( "mine", "1" );
     e.SetAttribute( "mine", "2" );
     e.SetAttribute( "mine", "3" );
     e.SetAttribute( "yours", "a" );
     string expecting = "1, 2, 3, a";
     Assert.AreEqual( expecting, e.ToString() );
 }
 public void TestCat2AttributesWithApply()
 {
     StringTemplate e = new StringTemplate(
             "$[names,phones]:{a|$a$.}$"
         );
     e = e.GetInstanceOf();
     e.SetAttribute( "names", "Ter" );
     e.SetAttribute( "names", "Tom" );
     e.SetAttribute( "phones", "1" );
     e.SetAttribute( "phones", "2" );
     string expecting = "Ter.Tom.1.2.";
     Assert.AreEqual( expecting, e.ToString() );
 }
 public void TestTruncOp()
 {
     StringTemplate e = new StringTemplate(
             "$trunc(names); separator=\", \"$"
         );
     e = e.GetInstanceOf();
     e.SetAttribute( "names", "Ter" );
     e.SetAttribute( "names", "Tom" );
     e.SetAttribute( "names", "Sriram" );
     string expecting = "Ter, Tom";
     Assert.AreEqual( expecting, e.ToString() );
 }
 public void TestParallelAttributeIterationWithNullValue()
 {
     StringTemplate e = new StringTemplate(
             "$names,phones,salaries:{n,p,s | $n$@$p$: $s$\n}$"
         );
     e = e.GetInstanceOf();
     e.SetAttribute( "names", "Ter" );
     e.SetAttribute( "names", "Tom" );
     e.SetAttribute( "names", "Sriram" );
     e.SetAttribute( "phones", new List<object>( new object[] { "1", null, "3" } ) );
     e.SetAttribute( "salaries", "big" );
     e.SetAttribute( "salaries", "huge" );
     e.SetAttribute( "salaries", "enormous" );
     string expecting = "Ter@1: big" + newline +
                        "Tom@: huge" + newline +
                        "Sriram@3: enormous" + newline;
     Assert.AreEqual( expecting, e.ToString() );
 }
Example #10
0
 public void TestParallelAttributeIterationWithMissingArgs()
 {
     IStringTemplateErrorListener errors = new ErrorBuffer();
     StringTemplate e = new StringTemplate(
             "$names,phones,salaries:{$n$@$p$}; separator=\", \"$"
         );
     e.ErrorListener = errors;
     e = e.GetInstanceOf();
     e.SetAttribute( "names", "Tom" );
     e.SetAttribute( "phones", "2" );
     e.SetAttribute( "salaries", "big" );
     e.ToString(); // generate the error
     string errorExpecting = "missing arguments in anonymous template in context [anonymous]";
     Assert.AreEqual( errorExpecting, errors.ToString() );
 }
Example #11
0
 public void TestParallelAttributeIterationWithMismatchArgListSizes()
 {
     IStringTemplateErrorListener errors = new ErrorBuffer();
     StringTemplate e = new StringTemplate(
             "$names,phones,salaries:{n,p | $n$@$p$}; separator=\", \"$"
         );
     e.ErrorListener = errors;
     e = e.GetInstanceOf();
     e.SetAttribute( "names", "Ter" );
     e.SetAttribute( "names", "Tom" );
     e.SetAttribute( "phones", "1" );
     e.SetAttribute( "phones", "2" );
     e.SetAttribute( "salaries", "big" );
     string expecting = "Ter@1, Tom@2";
     Assert.AreEqual( expecting, e.ToString() );
     string errorExpecting = "number of arguments [n, p] mismatch between attribute list and anonymous template in context [anonymous]";
     Assert.AreEqual( errorExpecting, errors.ToString() );
 }
Example #12
0
 public void TestParallelAttributeIterationHasI()
 {
     StringTemplate e = new StringTemplate(
             "$names,phones,salaries:{n,p,s | $i0$. $n$@$p$: $s$\n}$"
         );
     e = e.GetInstanceOf();
     e.SetAttribute( "names", "Ter" );
     e.SetAttribute( "names", "Tom" );
     e.SetAttribute( "phones", "1" );
     e.SetAttribute( "phones", "2" );
     e.SetAttribute( "salaries", "big" );
     e.SetAttribute( "salaries", "huge" );
     string expecting = "0. Ter@1: big" + newline + "1. Tom@2: huge" + newline;
     Assert.AreEqual( expecting, e.ToString() );
 }
Example #13
0
 public void TestNestedOp()
 {
     StringTemplate e = new StringTemplate(
             "$first(rest(names))$" // gets 2nd element
         );
     e = e.GetInstanceOf();
     e.SetAttribute( "names", "Ter" );
     e.SetAttribute( "names", "Tom" );
     e.SetAttribute( "names", "Sriram" );
     string expecting = "Tom";
     Assert.AreEqual( expecting, e.ToString() );
 }
Example #14
0
        public void TestNestedIF()
        {
            StringTemplate e = new StringTemplate(
                    "$if(title)$" + newline +
                    "foo" + newline +
                    "$else$" + newline +
                    "$if(header)$" + newline +
                    "bar" + newline +
                    "$else$" + newline +
                    "blort" + newline +
                    "$endif$" + newline +
                    "$endif$"
                );
            e.SetAttribute( "title", "sample" );
            string expecting = "foo";
            Assert.AreEqual( expecting, e.ToString() );

            e = e.GetInstanceOf();
            e.SetAttribute( "header", "more" );
            expecting = "bar";
            Assert.AreEqual( expecting, e.ToString() );

            e = e.GetInstanceOf();
            expecting = "blort";
            Assert.AreEqual( expecting, e.ToString() );
        }
Example #15
0
        public void TestApplyAnonymousTemplateToMapAndSet()
        {
            StringTemplate st =
                    new StringTemplate( "$items:{<li>$it$</li>}$" );
            IDictionary m = new SortedList<object, object>();
            m["a"] = "1";
            m["b"] = "2";
            m["c"] = "3";
            st.SetAttribute( "items", m );
            string expecting = "<li>1</li><li>2</li><li>3</li>";
            Assert.AreEqual( expecting, st.ToString() );

            st = st.GetInstanceOf();
            HashSet<object> s = new HashSet<object>();
            s.Add( "1" );
            s.Add( "2" );
            s.Add( "3" );
            st.SetAttribute( "items", s );
            //expecting = "<li>3</li><li>2</li><li>1</li>";
            expecting = "<li>1</li><li>2</li><li>3</li>";
            Assert.AreEqual( expecting, st.ToString() );
        }
Example #16
0
 public void TestListLiteralWithEmptyElements()
 {
     StringTemplate e = new StringTemplate(
             "$[\"Ter\",,\"Jesse\"]:{n | $i$:$n$}; separator=\", \", null=\"\"$"
         );
     e = e.GetInstanceOf();
     e.SetAttribute( "names", "Ter" );
     e.SetAttribute( "phones", "1" );
     e.SetAttribute( "salaries", "big" );
     string expecting = "1:Ter, 2:, 3:Jesse";
     Assert.AreEqual( expecting, e.ToString() );
 }
Example #17
0
 public void TestStripOpOfSingleAlt()
 {
     StringTemplate e = new StringTemplate(
             "$strip(data)$"
         );
     e = e.GetInstanceOf();
     e.SetAttribute( "data", "hi" );
     string expecting = "hi"; // nulls are skipped
     Assert.AreEqual( expecting, e.ToString() );
 }
Example #18
0
 public void TestParallelAttributeIterationWithSingletons()
 {
     StringTemplate e = new StringTemplate(
             "$names,phones,salaries:{n,p,s | $n$@$p$: $s$}; separator=\", \"$"
         );
     e = e.GetInstanceOf();
     e.SetAttribute( "names", "Ter" );
     e.SetAttribute( "phones", "1" );
     e.SetAttribute( "salaries", "big" );
     string expecting = "Ter@1: big";
     Assert.AreEqual( expecting, e.ToString() );
 }
Example #19
0
        public void TestFirstWithListOfMaps2()
        {
            StringTemplate e = new StringTemplate(
                    "$first(maps):{ m | $m.Ter$ }$"
                );
            IDictionary m1 = new Dictionary<object, object>();
            IDictionary m2 = new Dictionary<object, object>();
            m1.Add( "Ter", "x5707" );
            e.SetAttribute( "maps", m1 );
            m2.Add( "Tom", "x5332" );
            e.SetAttribute( "maps", m2 );
            string expecting = "x5707";
            Assert.AreEqual( expecting, e.ToString() );

            e = e.GetInstanceOf();
            IList list = new List<object>() { m1, m2 };
            e.SetAttribute( "maps", list );
            expecting = "x5707";
            Assert.AreEqual( expecting, e.ToString() );
        }
Example #20
0
 public void TestRepeatedRestOp()
 {
     StringTemplate e = new StringTemplate(
             "$rest(names)$, $rest(names)$" // gets 2nd element
         );
     e = e.GetInstanceOf();
     e.SetAttribute( "names", "Ter" );
     e.SetAttribute( "names", "Tom" );
     string expecting = "Tom, Tom";
     Assert.AreEqual( expecting, e.ToString() );
 }
Example #21
0
 public void TestCat3Attributes()
 {
     StringTemplate e = new StringTemplate(
             "$[names,phones,salaries]; separator=\", \"$"
         );
     e = e.GetInstanceOf();
     e.SetAttribute( "names", "Ter" );
     e.SetAttribute( "names", "Tom" );
     e.SetAttribute( "phones", "1" );
     e.SetAttribute( "phones", "2" );
     e.SetAttribute( "salaries", "big" );
     e.SetAttribute( "salaries", "huge" );
     string expecting = "Ter, Tom, 1, 2, big, huge";
     Assert.AreEqual( expecting, e.ToString() );
 }
Example #22
0
 public void TestRestOpEmptyList()
 {
     StringTemplate e = new StringTemplate(
             "$rest(names); separator=\", \"$"
         );
     e = e.GetInstanceOf();
     e.SetAttribute( "names", new List<object>() );
     string expecting = "";
     Assert.AreEqual( expecting, e.ToString() );
 }
Example #23
0
 public void TestCatListAndSingleAttribute()
 {
     // replace first of yours with first of mine
     StringTemplate e = new StringTemplate(
             "$[mine,yours]; separator=\", \"$"
         );
     e = e.GetInstanceOf();
     e.SetAttribute( "mine", "1" );
     e.SetAttribute( "mine", "2" );
     e.SetAttribute( "mine", "3" );
     e.SetAttribute( "yours", "a" );
     string expecting = "1, 2, 3, a";
     Assert.AreEqual( expecting, e.ToString() );
 }
Example #24
0
 public void TestRestWithLengthOneListAttributeOp()
 {
     StringTemplate e = new StringTemplate(
             "$rest(names)$"
         );
     e = e.GetInstanceOf();
     e.SetAttribute( "names", new List<object>( new object[] { "Ter" } ) );
     string expecting = "";
     Assert.AreEqual( expecting, e.ToString() );
 }
Example #25
0
 public void TestCatWithNullTemplateApplicationAsElement()
 {
     StringTemplate e = new StringTemplate(
             "$[names:{$it$!},\"foo\"]:{x}; separator=\", \"$"
         );
     e = e.GetInstanceOf();
     e.SetAttribute( "phones", "1" );
     e.SetAttribute( "phones", "2" );
     string expecting = "x";  // only one since template application gives nothing
     Assert.AreEqual( expecting, e.ToString() );
 }
Example #26
0
 public void TestRestWithOneAttributeOp()
 {
     StringTemplate e = new StringTemplate(
             "$rest(names)$"
         );
     e = e.GetInstanceOf();
     e.SetAttribute( "names", "Ter" );
     string expecting = "";
     Assert.AreEqual( expecting, e.ToString() );
 }
Example #27
0
 public void TestCombinedOp()
 {
     // replace first of yours with first of mine
     StringTemplate e = new StringTemplate(
             "$[first(mine),rest(yours)]; separator=\", \"$"
         );
     e = e.GetInstanceOf();
     e.SetAttribute( "mine", "1" );
     e.SetAttribute( "mine", "2" );
     e.SetAttribute( "mine", "3" );
     e.SetAttribute( "yours", "a" );
     e.SetAttribute( "yours", "b" );
     string expecting = "1, b";
     Assert.AreEqual( expecting, e.ToString() );
 }
Example #28
0
 public void TestStripOpOfListOfListsWithNulls()
 {
     StringTemplate e = new StringTemplate(
             "$strip(data):{list | $strip(list)$}; separator=\",\"$"
         );
     e = e.GetInstanceOf();
     IList data = new List<object>();
     IList dataOne = new List<object>();
     dataOne.Add( "Hi" );
     dataOne.Add( "mom" );
     data.Add( dataOne );
     data.Add( null );
     IList dataTwo = new List<object>();
     dataTwo.Add( "Hi" );
     dataTwo.Add( null );
     dataTwo.Add( "dad" );
     dataTwo.Add( null );
     data.Add( dataTwo );
     e.SetAttribute( "data", data );
     string expecting = "Himom,Hidad"; // nulls are skipped
     Assert.AreEqual( expecting, e.ToString() );
 }
 public virtual string ToString( bool showTemplatePatterns )
 {
     StringBuilder buf = new StringBuilder();
     buf.Append( "group " + Name + ";" + _newline );
     StringTemplate formalArgs = new StringTemplate( "$args;separator=\",\"$" );
     foreach ( var template in _templates.OrderBy( item => (string)item.Key ) )
     {
         string tname = template.Key;
         StringTemplate st = template.Value;
         if ( st != NOT_FOUND_ST )
         {
             formalArgs = formalArgs.GetInstanceOf();
             formalArgs.SetAttribute( "args", st.FormalArguments );
             buf.Append( tname + "(" + formalArgs + ")" );
             if ( showTemplatePatterns )
             {
                 buf.Append( " ::= <<" );
                 buf.Append( st.Template );
                 buf.Append( ">>" + _newline );
             }
             else
             {
                 buf.Append( _newline );
             }
         }
     }
     return buf.ToString();
 }
Example #30
0
 public void TestStripOpOfListWithNulls()
 {
     StringTemplate e = new StringTemplate(
             "$strip(data)$"
         );
     e = e.GetInstanceOf();
     IList data = new List<object>();
     data.Add( "Hi" );
     data.Add( null );
     data.Add( "mom" );
     data.Add( null );
     e.SetAttribute( "data", data );
     string expecting = "Himom"; // nulls are skipped
     Assert.AreEqual( expecting, e.ToString() );
 }
Example #31
0
 public void TestStripOpOfNull()
 {
     StringTemplate e = new StringTemplate(
             "$strip(data)$"
         );
     e = e.GetInstanceOf();
     string expecting = ""; // nulls are skipped
     Assert.AreEqual( expecting, e.ToString() );
 }
Example #32
0
 public void TestLengthOpWithSet()
 {
     StringTemplate e = new StringTemplate(
             "$length(names)$"
         );
     e = e.GetInstanceOf();
     HashSet<object> m = new HashSet<object>();
     m.Add( "Tom" );
     m.Add( "Sriram" );
     m.Add( "Doug" );
     e.SetAttribute( "names", m );
     string expecting = "3";
     Assert.AreEqual( expecting, e.ToString() );
 }