/** Create an evaluator using attributes from self */
 public ActionEvaluator(StringTemplate self, ASTExpr chunk, StringTemplateWriter @out)
 {
     this.self = self;
     this.chunk = chunk;
     this.@out = @out;
 }
Beispiel #2
0
        /// <summary>A separator is normally just a string literal, but is still an AST that
        /// we must evaluate.  The separator can be any expression such as a template
        /// include or string cat expression etc...
        /// </summary>
        protected internal virtual String computeSeparator(StringTemplate self, StringTemplateWriter outWriter, Object separator)
        {
            if (separator == null)
            {
                return null;
            }
            if (separator is StringTemplateAST)
            {
                StringTemplateAST separatorTree = (StringTemplateAST) separator;
                // must evaluate, writing to a string so we can hand on to it
                ASTExpr e = new ASTExpr(getEnclosingTemplate(), separatorTree, null);
                System.IO.StringWriter buf = new System.IO.StringWriter();
                // create a new instance of whatever StringTemplateWriter
                // implementation they are using.  Default is AutoIndentWriter.
                // Defalut behavior is to indent but without
                // any prior indents surrounding this attribute expression
                StringTemplateWriter sw = null;
                System.Type writerClass = outWriter.GetType();
                try
                {
                    System.Reflection.ConstructorInfo ctor = writerClass.GetConstructor(new System.Type[]{typeof(System.IO.TextWriter)});
                    sw = (StringTemplateWriter) ctor.Invoke(new Object[]{buf});
                }
                catch (System.Exception exc)
                {
                    // default new AutoIndentWriter(buf)
                    self.error("cannot make implementation of StringTemplateWriter", exc);
                    sw = new AutoIndentWriter(buf);
                }

                try
                {
                    e.write(self, sw);
                }
                catch (System.IO.IOException ioe)
                {
                    self.error("can't evaluate separator expression", ioe);
                }
                return buf.ToString();
            }
            else
            {
                // just in case we expand in the future and it's something else
                return separator.ToString();
            }
        }
 public virtual ASTExpr parseAction(String action)
 {
     ActionLexer lexer = new ActionLexer(new System.IO.StringReader(action.ToString()));
     ActionParser parser = new ActionParser(lexer, this);
     parser.setASTNodeClass("antlr.stringtemplate.language.StringTemplateAST");
     ASTExpr a = null;
     try
     {
         IDictionary options = parser.action();
         AST tree = parser.getAST();
         if (tree != null)
         {
             if (tree.Type == ActionParser.CONDITIONAL)
             {
                 a = new ConditionalExpr(this, tree);
             }
             else
             {
                 a = new ASTExpr(this, tree, options);
             }
         }
     }
     catch (RecognitionException re)
     {
         error("Can't parse chunk: " + action.ToString(), re);
     }
     catch (TokenStreamException tse)
     {
         error("Can't parse chunk: " + action.ToString(), tse);
     }
     return a;
 }