コード例 #1
0
        //throws RecognitionException
        public Object expr(AST _t)
        {
            Object value=null;

            antlr.stringtemplate.language.StringTemplateAST expr_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t;

            Object a=null, b=null, e=null;
            IDictionary argumentContext=null;

            try {      // for error handling
            if (null == _t)
                _t = ASTNULL;
            switch ( _t.Type )
            {
            case PLUS:
            {
                AST __t3 = _t;
                antlr.stringtemplate.language.StringTemplateAST tmp1_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,PLUS);
                _t = _t.getFirstChild();
                a=expr(_t);
                _t = retTree_;
                b=expr(_t);
                _t = retTree_;
                value = chunk.add(a,b);
                _t = __t3;
                _t = _t.getNextSibling();
                break;
            }
            case APPLY:
            case MULTI_APPLY:
            {
                value=templateApplication(_t);
                _t = retTree_;
                break;
            }
            case DOT:
            case ID:
            case ANONYMOUS_TEMPLATE:
            case STRING:
            case INT:
            {
                value=attribute(_t);
                _t = retTree_;
                break;
            }
            case INCLUDE:
            {
                value=templateInclude(_t);
                _t = retTree_;
                break;
            }
            case FUNCTION:
            {
                value=function(_t);
                _t = retTree_;
                break;
            }
            case LIST:
            {
                value=list(_t);
                _t = retTree_;
                break;
            }
            case VALUE:
            {
                AST __t4 = _t;
                antlr.stringtemplate.language.StringTemplateAST tmp2_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,VALUE);
                _t = _t.getFirstChild();
                e=expr(_t);
                _t = retTree_;
                _t = __t4;
                _t = _t.getNextSibling();

                StringWriter buf = new StringWriter();
                Type writerClass = @out.GetType();
                StringTemplateWriter sw = null;
                try {
                ConstructorInfo ctor =
                    writerClass.GetConstructor(new Type[] {typeof(TextWriter)});
                sw = (StringTemplateWriter)ctor.Invoke(new Object[] {buf});
                }
                catch (Exception exc) {
                    // default new AutoIndentWriter(buf)
                    self.error("cannot make implementation of StringTemplateWriter",exc);
                    sw = new AutoIndentWriter(buf);
                    }
                chunk.writeAttribute(self,e,sw);
                value = buf.ToString();

                break;
            }
            default:
            {
                throw new NoViableAltException(_t);
            }
             }
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return value;
        }
コード例 #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();
            }
        }
コード例 #3
0
 /// <summary>return an instance of a StringTemplateWriter that spits output to w.
 /// If a writer is specified, use it instead of the default.
 /// </summary>
 public virtual StringTemplateWriter getStringTemplateWriter(System.IO.TextWriter w)
 {
     StringTemplateWriter stw = null;
     if (userSpecifiedWriter != null)
     {
         try
         {
             System.Reflection.ConstructorInfo ctor = userSpecifiedWriter.GetConstructor(new System.Type[]{typeof(System.IO.TextWriter)});
             stw = (StringTemplateWriter) ctor.Invoke(new Object[]{w});
         }
         catch (System.Exception e)
         {
             error("problems getting StringTemplateWriter", e);
         }
     }
     if (stw == null)
     {
         stw = new AutoIndentWriter(w);
     }
     return stw;
 }