Exemple #1
0
        public AST child;        // current child to which siblings are added

        /*Make sure that child is the last sibling */
        public void  advanceChildToEnd()
        {
            if (child != null)
            {
                while (child.getNextSibling() != null)
                {
                    child = child.getNextSibling();
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Do a step-first walk, building up a buffer of tokens until
        /// you've reached a particular step and print out any rule subroots
        /// insteads of descending.
        /// </summary>
        /// <param name="buf">derivation buffer</param>
        /// <param name="step">derivation steps</param>
        /// <returns></returns>
        protected internal override int getLeftmostDerivation(StringBuilder buf, int step)
        {
            int numReplacements = 0;

            if (step <= 0)
            {
                buf.Append(' ');
                buf.Append(ToString());
                return(numReplacements);
            }
            AST child = getFirstChild();

            numReplacements = 1;
            // walk child printing them out, descending into at most one
            while (child != null)
            {
                if ((numReplacements >= step) || (child is ParseTreeToken))
                {
                    buf.Append(' ');
                    buf.Append(child.ToString());
                }
                else
                {
                    // descend for at least one more derivation; update count
                    int remainingReplacements = step - numReplacements;
                    int n = ((ParseTree)child).getLeftmostDerivation(buf, remainingReplacements);
                    numReplacements += n;
                }
                child = child.getNextSibling();
            }
            return(numReplacements);
        }
Exemple #3
0
        /// <summary>
        /// Make a tree from a list of nodes.  The first element in the
        /// array is the root.  If the root is null, then the tree is
        /// a simple list not a tree.  Handles null children nodes correctly.
        /// For example, build(a, b, null, c) yields tree (a b c).  build(null,a,b)
        /// yields tree (nil a b).
        /// </summary>
        /// <param name="nodes">List of Nodes.</param>
        /// <returns>AST Node tree.</returns>
        public override AST make(params AST[] nodes)
        {
            if (nodes == null || nodes.Length == 0)
            {
                return(null);
            }
            AST root = nodes[0];
            AST tail = null;

            if (root != null)
            {
                root.setFirstChild(null);                 // don't leave any old pointers set
            }
            // link in children;
            for (int i = 1; i < nodes.Length; i++)
            {
                if (nodes[i] == null)
                {
                    continue;
                }
                // ignore null nodes
                if (root == null)
                {
                    // Set the root and set it up for a flat list
                    root = (tail = nodes[i]);
                }
                else if (tail == null)
                {
                    root.setFirstChild(nodes[i]);
                    ((ASTNode)nodes[i]).setParent((ASTNode)root);
                    tail = root.getFirstChild();
                }
                else
                {
                    ((ASTNode)nodes[i]).setParent((ASTNode)root);
                    tail.setNextSibling(nodes[i]);
                    ((ASTNode)nodes[i]).setPreviousSibling((ASTNode)tail);
                    tail = tail.getNextSibling();
                }
                // Chase tail to last sibling
                while (tail.getNextSibling() != null)
                {
                    tail = tail.getNextSibling();
                }
            }
            return(root);
        }
Exemple #4
0
        public void visit(AST node)
        {
            // Flatten this level of the tree if it has no children
            bool flatten = /*true*/ false;
            AST  node2;

            for (node2 = node; node2 != null; node2 = node2.getNextSibling())
            {
                if (node2.getFirstChild() != null)
                {
                    flatten = false;
                    break;
                }
            }

            for (node2 = node; node2 != null; node2 = node2.getNextSibling())
            {
                if (!flatten || node2 == node)
                {
                    tabs();
                }
                if (node2.getText() == null)
                {
                    Console.Out.Write("nil");
                }
                else
                {
                    Console.Out.Write(node2.getText());
                }

                Console.Out.Write(" [" + node2.Type + "] ");

                if (flatten)
                {
                    Console.Out.Write(" ");
                }
                else
                {
                    Console.Out.WriteLine("");
                }

                if (node2.getFirstChild() != null)
                {
                    level++;
                    visit(node2.getFirstChild());
                    level--;
                }
            }

            if (flatten)
            {
                Console.Out.WriteLine("");
            }
        }
        public void visit(AST node)
        {
            // Flatten this level of the tree if it has no children
            bool flatten = /*true*/ false;
            AST node2;
            for (node2 = node; node2 != null; node2 = node2.getNextSibling())
            {
                if (node2.getFirstChild() != null)
                {
                    flatten = false;
                    break;
                }
            }

            for (node2 = node; node2 != null; node2 = node2.getNextSibling())
            {
                if (!flatten || node2 == node)
                {
                    tabs();
                }
                if (node2.getText() == null)
                {
                    Console.Out.Write("nil");
                }
                else
                {
                    Console.Out.Write(node2.getText());
                }

                Console.Out.Write(" [" + node2.Type + "] ");

                if (flatten)
                {
                    Console.Out.Write(" ");
                }
                else
                {
                    Console.Out.WriteLine("");
                }

                if (node2.getFirstChild() != null)
                {
                    level++;
                    visit(node2.getFirstChild());
                    level--;
                }
            }

            if (flatten)
            {
                Console.Out.WriteLine("");
            }
        }
Exemple #6
0
        /// <summary>
        /// Duplicate AST Node tree rooted at specified AST node and all of it's siblings.
        /// </summary>
        /// <param name="t">Root of AST Node tree.</param>
        /// <returns>Root node of new AST Node tree (or null if <c>t</c> is null).</returns>
        public virtual AST dupList(AST t)
        {
            AST result = dupTree(t);             // if t == null, then result==null
            AST nt     = result;

            while (t != null)
            {
                // for each sibling of the root
                t = t.getNextSibling();
                nt.setNextSibling(dupTree(t));                 // dup each subtree, building new tree
                nt = nt.getNextSibling();
            }
            return(result);
        }
Exemple #7
0
        /*Print out a child-sibling tree in LISP notation */
        public virtual string ToStringList()
        {
            AST    t  = this;
            string ts = "";

            if (t.getFirstChild() != null)
            {
                ts += " (";
            }
            ts += " " + this.ToString();
            if (t.getFirstChild() != null)
            {
                ts += ((BaseAST)t.getFirstChild()).ToStringList();
            }
            if (t.getFirstChild() != null)
            {
                ts += " )";
            }
            if (t.getNextSibling() != null)
            {
                ts += ((BaseAST)t.getNextSibling()).ToStringList();
            }
            return(ts);
        }
Exemple #8
0
        /// <summary>
        /// Duplicate AST Node tree rooted at specified AST node and all of it's siblings.
        /// </summary>
        /// <param name="t">Root of AST Node tree.</param>
        /// <returns>Root node of new AST Node tree (or null if <c>t</c> is null).</returns>
        public override AST dupList(AST t)
        {
            AST result = dupTree(t);             // if t == null, then result==null
            AST nt     = result;

            while (t != null)
            {
                // for each sibling of the root
                t = t.getNextSibling();
                AST d = dupTree(t);
                nt.setNextSibling(d);                 // dup each subtree, building new tree
                ((ASTNode)d).setPreviousSibling((ASTNode)nt);
                nt = nt.getNextSibling();
            }
            return(result);
        }
    /** Is t an exact structural match of this tree with the same node
     *  types?  'self' is considered the start of a sibling list.
     */
    protected bool equalsNodeTypesList(AST self, AST t)
    {
        // Console.Out.WriteLine("self="+self+", t="+t);
        // Console.Out.WriteLine("self.class="+self.getClass()+", t.class="+t.getClass());
        AST sibling;

        // the empty tree is not a match of any non-null tree.
        if (t == null)
        {
            return(false);
        }

        // Otherwise, start walking sibling lists.  First mismatch, return false.
        for (sibling = self;
             sibling != null && t != null;
             sibling = sibling.getNextSibling(), t = t.getNextSibling())
        {
            // Console.Out.WriteLine("sibling="+sibling+", t="+t);
            // as a quick optimization, check root types first.
            if (sibling.GetType() != t.GetType())
            {
                return(false);
            }
            // if roots match, do full list match test on children.
            if (sibling.getFirstChild() != null)
            {
                if (!equalsNodeTypesList(sibling.getFirstChild(),
                                         t.getFirstChild()))
                {
                    return(false);
                }
            }
            // sibling has no kids, make sure t doesn't either
            else if (t.getFirstChild() != null)
            {
                return(false);
            }
        }
        if (sibling == null && t == null)
        {
            return(true);
        }
        // one sibling list has more than the other
        return(false);
    }
Exemple #10
0
        public virtual ASTNode GetFirstChildOfType(int type)
        {
            ASTNode result = null;

            AST sibling = getFirstChild();

            while (sibling != null)
            {
                if (sibling.Type == type)
                {
                    result = (ASTNode)sibling;
                    break;
                }
                sibling = sibling.getNextSibling();
            }

            return(result);
        }
Exemple #11
0
        public virtual void xmlSerialize(TextWriter outWriter)
        {
            // print out this node and all siblings
            for (AST node = this; node != null; node = node.getNextSibling())
            {
                if (node.getFirstChild() == null)
                {
                    // print guts (class name, attributes)
                    ((BaseAST)node).xmlSerializeNode(outWriter);
                }
                else
                {
                    ((BaseAST)node).xmlSerializeRootOpen(outWriter);

                    // print children
                    ((BaseAST)node.getFirstChild()).xmlSerialize(outWriter);

                    // print end tag
                    ((BaseAST)node).xmlSerializeRootClose(outWriter);
                }
            }
        }
        //throws RecognitionException
        public void argumentAssignment(AST _t,
		StringTemplate embedded, IDictionary argumentContext
	)
        {
            antlr.stringtemplate.language.StringTemplateAST argumentAssignment_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t;
            antlr.stringtemplate.language.StringTemplateAST arg = null;

            Object e = null;

            try {      // for error handling
            if (null == _t)
                _t = ASTNULL;
            switch ( _t.Type )
            {
            case ASSIGN:
            {
                AST __t43 = _t;
                antlr.stringtemplate.language.StringTemplateAST tmp20_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,ASSIGN);
                _t = _t.getFirstChild();
                arg = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,ID);
                _t = _t.getNextSibling();
                e=expr(_t);
                _t = retTree_;
                _t = __t43;
                _t = _t.getNextSibling();

                        if ( e!=null ) {
                            self.rawSetArgumentAttribute(embedded,argumentContext,arg.getText(),e);
                        }

                break;
            }
            case DOTDOTDOT:
            {
                antlr.stringtemplate.language.StringTemplateAST tmp21_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,DOTDOTDOT);
                _t = _t.getNextSibling();
                embedded.setPassThroughAttributes(true);
                break;
            }
            default:
            {
                throw new NoViableAltException(_t);
            }
             }
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
        }
        //throws RecognitionException
        public Object attribute(AST _t)
        {
            Object value=null;

            antlr.stringtemplate.language.StringTemplateAST attribute_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t;
            antlr.stringtemplate.language.StringTemplateAST prop = null;
            antlr.stringtemplate.language.StringTemplateAST i3 = null;
            antlr.stringtemplate.language.StringTemplateAST i = null;
            antlr.stringtemplate.language.StringTemplateAST s = null;
            antlr.stringtemplate.language.StringTemplateAST at = null;

            Object obj = null;
            String propName = null;
            Object e = null;

            try {      // for error handling
            if (null == _t)
                _t = ASTNULL;
            switch ( _t.Type )
            {
            case DOT:
            {
                AST __t33 = _t;
                antlr.stringtemplate.language.StringTemplateAST tmp6_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,DOT);
                _t = _t.getFirstChild();
                obj=expr(_t);
                _t = retTree_;
                {
                    if (null == _t)
                        _t = ASTNULL;
                    switch ( _t.Type )
                    {
                    case ID:
                    {
                        prop = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                        match((AST)_t,ID);
                        _t = _t.getNextSibling();
                        propName = prop.getText();
                        break;
                    }
                    case VALUE:
                    {
                        AST __t35 = _t;
                        antlr.stringtemplate.language.StringTemplateAST tmp7_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                        match((AST)_t,VALUE);
                        _t = _t.getFirstChild();
                        e=expr(_t);
                        _t = retTree_;
                        _t = __t35;
                        _t = _t.getNextSibling();
                        if (e!=null) {propName=e.ToString();}
                        break;
                    }
                    default:
                    {
                        throw new NoViableAltException(_t);
                    }
                     }
                }
                _t = __t33;
                _t = _t.getNextSibling();
                value = chunk.getObjectProperty(self,obj,propName);
                break;
            }
            case ID:
            {
                i3 = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,ID);
                _t = _t.getNextSibling();

                value=self.getAttribute(i3.getText());

                break;
            }
            case INT:
            {
                i = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,INT);
                _t = _t.getNextSibling();
                value=Int32.Parse(i.getText());
                break;
            }
            case STRING:
            {
                s = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,STRING);
                _t = _t.getNextSibling();

                    value=s.getText();

                break;
            }
            case ANONYMOUS_TEMPLATE:
            {
                at = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,ANONYMOUS_TEMPLATE);
                _t = _t.getNextSibling();

                    value=at.getText();
                        if ( at.getText()!=null ) {
                            StringTemplate valueST =new StringTemplate(self.getGroup(), at.getText());
                            valueST.setEnclosingInstance(self);
                            valueST.setName("<anonymous template argument>");
                            value = valueST;
                    }

                break;
            }
            default:
            {
                throw new NoViableAltException(_t);
            }
             }
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return value;
        }
Exemple #14
0
        //throws RecognitionException
        public boolean expression(AST _t)
        {
            boolean e ;

            AST expression_AST_in = (AST)_t;

            boolean a, b;
            String l, r;

            e = false;

            try {      // for error handling
            if (null == _t)
                _t = ASTNULL;
            switch ( _t.Type )
            {
            case AND:
            {
                AST __t43 = _t;
                AST tmp21_AST_in = _t;
                match(_t,AND);
                _t = _t.getFirstChild();
                a=expression(_t);
                _t = retTree_;
                b=expression(_t);
                _t = retTree_;
                e = a && b;
                _t = __t43;
                _t = _t.getNextSibling();
                break;
            }
            case OR:
            {
                AST __t44 = _t;
                AST tmp22_AST_in = _t;
                match(_t,OR);
                _t = _t.getFirstChild();
                a=expression(_t);
                _t = retTree_;
                b=expression(_t);
                _t = retTree_;
                e = a || b;
                _t = __t44;
                _t = _t.getNextSibling();
                break;
            }
            case EQUAL:
            {
                AST __t45 = _t;
                AST tmp23_AST_in = _t;
                match(_t,EQUAL);
                _t = _t.getFirstChild();
                l=value(_t);
                _t = retTree_;
                r=value(_t);
                _t = retTree_;

                e = l.equals(r);

                _t = __t45;
                _t = _t.getNextSibling();
                break;
            }
            default:
            {
                throw new NoViableAltException(_t);
            }
             }
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return e ;
        }
        //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;

            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 ID:
            case DOT:
            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();
                            IStringTemplateWriter sw = self.Group.CreateInstanceOfTemplateWriter(buf);
                            int n = chunk.WriteAttribute(self,e,sw);
                            if (n > 0)
                            {
                                value = buf.ToString();

                            }

                break;
            }
            default:
            {
                throw new NoViableAltException(_t);
            }
             }
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return value;
        }
 /// <summary>
 /// Duplicate AST Node tree rooted at specified AST node and all of it's siblings.
 /// </summary>
 /// <param name="t">Root of AST Node tree.</param>
 /// <returns>Root node of new AST Node tree (or null if <c>t</c> is null).</returns>
 public virtual AST dupList(AST t)
 {
     AST result = dupTree(t); // if t == null, then result==null
     AST nt = result;
     while (t != null)
     {
         // for each sibling of the root
         t = t.getNextSibling();
         nt.setNextSibling(dupTree(t)); // dup each subtree, building new tree
         nt = nt.getNextSibling();
     }
     return result;
 }
Exemple #17
0
        //throws RecognitionException
        public String literal(AST _t)
        {
            String s ;

            AST literal_AST_in = (AST)_t;
            AST l = null;
            AST ql = null;

            s = null;

            try {      // for error handling
            if (null == _t)
                _t = ASTNULL;
            switch ( _t.Type )
            {
            case LITERAL:
            {
                l = _t;
                match(_t,LITERAL);
                _t = _t.getNextSibling();

                s = l.getText();

                break;
            }
            case QLITERAL:
            {
                ql = _t;
                match(_t,QLITERAL);
                _t = _t.getNextSibling();

                s = ql.getText();

                break;
            }
            default:
            {
                throw new NoViableAltException(_t);
            }
             }
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return s ;
        }
        //throws RecognitionException
        /** create a new list of expressions as a new multi-value attribute */
        public Object list(AST _t)
        {
            Object value=null;

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

            Object e = null;
            IList elements = new ArrayList();
            value = new CatIterator(elements);

            try {      // for error handling
            AST __t6 = _t;
            antlr.stringtemplate.language.StringTemplateAST tmp14_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
            match((AST)_t,LIST);
            _t = _t.getFirstChild();
            { // ( ... )+
                int _cnt8=0;
                for (;;)
                {
                    if (_t == null)
                        _t = ASTNULL;
                    if ((tokenSet_0_.member(_t.Type)))
                    {
                        e=expr(_t);
                        _t = retTree_;

                                      	if ( e!=null ) {
                                            e = ASTExpr.convertAnythingToIterator(e);
                                      		elements.Add(e);
                                      	}

                    }
                    else
                    {
                        if (_cnt8 >= 1) { goto _loop8_breakloop; } else { throw new NoViableAltException(_t);; }
                    }

                    _cnt8++;
                }
            _loop8_breakloop:				;
            }    // ( ... )+
            _t = __t6;
            _t = _t.getNextSibling();
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return value;
        }
        //throws RecognitionException
        public void singleTemplateArg(AST _t,
            StringTemplate embedded, IDictionary argumentContext
            )
        {
            Antlr.StringTemplate.Language.StringTemplateAST singleTemplateArg_AST_in = (Antlr.StringTemplate.Language.StringTemplateAST)_t;

            object e = null;

            try {      // for error handling
            AST __t40 = _t;
            Antlr.StringTemplate.Language.StringTemplateAST tmp25_AST_in = (_t==ASTNULL) ? null : (Antlr.StringTemplate.Language.StringTemplateAST)_t;
            match((AST)_t,SINGLEVALUEARG);
            _t = _t.getFirstChild();
            e=expr(_t);
            _t = retTree_;
            _t = __t40;
            _t = _t.getNextSibling();

                    if ( e!=null ) {
                        string soleArgName = null;
                        // find the sole defined formal argument for embedded
                        bool error = false;
                        HashList formalArgs = (HashList) embedded.FormalArguments;
                        if ( formalArgs!=null ) {
                            ICollection argNames = formalArgs.Keys;
                            if ( argNames.Count==1 ) {
                                IEnumerator iter = argNames.GetEnumerator();
                                iter.MoveNext();
                                soleArgName = (string) iter.Current;
                                //Console.WriteLine("sole formal arg of "+embedded.Name+" is "+soleArgName);
                            }
                            else {
                                error=true;
                            }
                        }
                        else {
                            error=true;
                        }
                        if ( error ) {
                            self.Error("template "+embedded.Name+
                                       " must have exactly one formal arg in template context "+
                                       self.GetEnclosingInstanceStackString());
                       	}
                       	else {
                       		self.RawSetArgumentAttribute(embedded,argumentContext,soleArgName,e);
                       	}
                    }

            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
        }
        //throws RecognitionException
        public object function(AST _t)
        {
            object value=null;

            Antlr.StringTemplate.Language.StringTemplateAST function_AST_in = (Antlr.StringTemplate.Language.StringTemplateAST)_t;

            object a;

            try {      // for error handling
            AST __t21 = _t;
            Antlr.StringTemplate.Language.StringTemplateAST tmp10_AST_in = (_t==ASTNULL) ? null : (Antlr.StringTemplate.Language.StringTemplateAST)_t;
            match((AST)_t,FUNCTION);
            _t = _t.getFirstChild();
            {
                if (null == _t)
                    _t = ASTNULL;
                switch ( _t.Type )
                {
                case LITERAL_first:
                {
                    Antlr.StringTemplate.Language.StringTemplateAST tmp11_AST_in = (_t==ASTNULL) ? null : (Antlr.StringTemplate.Language.StringTemplateAST)_t;
                    match((AST)_t,LITERAL_first);
                    _t = _t.getNextSibling();
                    a=singleFunctionArg(_t);
                    _t = retTree_;
                    value=chunk.First(a);
                    break;
                }
                case LITERAL_rest:
                {
                    Antlr.StringTemplate.Language.StringTemplateAST tmp12_AST_in = (_t==ASTNULL) ? null : (Antlr.StringTemplate.Language.StringTemplateAST)_t;
                    match((AST)_t,LITERAL_rest);
                    _t = _t.getNextSibling();
                    a=singleFunctionArg(_t);
                    _t = retTree_;
                    value=chunk.Rest(a);
                    break;
                }
                case LITERAL_last:
                {
                    Antlr.StringTemplate.Language.StringTemplateAST tmp13_AST_in = (_t==ASTNULL) ? null : (Antlr.StringTemplate.Language.StringTemplateAST)_t;
                    match((AST)_t,LITERAL_last);
                    _t = _t.getNextSibling();
                    a=singleFunctionArg(_t);
                    _t = retTree_;
                    value=chunk.Last(a);
                    break;
                }
                case LITERAL_length:
                {
                    Antlr.StringTemplate.Language.StringTemplateAST tmp14_AST_in = (_t==ASTNULL) ? null : (Antlr.StringTemplate.Language.StringTemplateAST)_t;
                    match((AST)_t,LITERAL_length);
                    _t = _t.getNextSibling();
                    a=singleFunctionArg(_t);
                    _t = retTree_;
                    value=chunk.Length(a);
                    break;
                }
                case LITERAL_strip:
                {
                    Antlr.StringTemplate.Language.StringTemplateAST tmp15_AST_in = (_t==ASTNULL) ? null : (Antlr.StringTemplate.Language.StringTemplateAST)_t;
                    match((AST)_t,LITERAL_strip);
                    _t = _t.getNextSibling();
                    a=singleFunctionArg(_t);
                    _t = retTree_;
                    value=chunk.Strip(a);
                    break;
                }
                case LITERAL_trunc:
                {
                    Antlr.StringTemplate.Language.StringTemplateAST tmp16_AST_in = (_t==ASTNULL) ? null : (Antlr.StringTemplate.Language.StringTemplateAST)_t;
                    match((AST)_t,LITERAL_trunc);
                    _t = _t.getNextSibling();
                    a=singleFunctionArg(_t);
                    _t = retTree_;
                    value=chunk.Trunc(a);
                    break;
                }
                default:
                {
                    throw new NoViableAltException(_t);
                }
                 }
            }
            _t = __t21;
            _t = _t.getNextSibling();
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return value;
        }
        //throws RecognitionException
        public Object ifAtom(AST _t)
        {
            Object value=null;

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

            try {      // for error handling
            value=expr(_t);
            _t = retTree_;
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return value;
        }
Exemple #22
0
        /*Is 'sub' a subtree of this list?
         *  The siblings of the root are NOT ignored.
         */
        public virtual bool EqualsListPartial(AST sub)
        {
            AST sibling;

            // the empty tree is always a subset of any tree.
            if (sub == null)
            {
                return(true);
            }

            // Otherwise, start walking sibling lists.  First mismatch, return false.
            for (sibling = this; sibling != null && sub != null; sibling = sibling.getNextSibling(), sub = sub.getNextSibling())
            {
                // as a quick optimization, check roots first.
                if (!sibling.Equals(sub))
                {
                    return(false);
                }
                // if roots match, do partial list match test on children.
                if (sibling.getFirstChild() != null)
                {
                    if (!sibling.getFirstChild().EqualsListPartial(sub.getFirstChild()))
                    {
                        return(false);
                    }
                }
            }
            if (sibling == null && sub != null)
            {
                // nothing left to match in this tree, but subtree has more
                return(false);
            }
            // either both are null or sibling has more, but subtree doesn't
            return(true);
        }
Exemple #23
0
        /*Is t an exact structural and equals() match of this tree.  The
         *  'this' reference is considered the start of a sibling list.
         */
        public virtual bool EqualsList(AST t)
        {
            AST sibling;

            // the empty tree is not a match of any non-null tree.
            if (t == null)
            {
                return(false);
            }

            // Otherwise, start walking sibling lists.  First mismatch, return false.
            for (sibling = this; sibling != null && t != null; sibling = sibling.getNextSibling(), t = t.getNextSibling())
            {
                // as a quick optimization, check roots first.
                if (!sibling.Equals(t))
                {
                    return(false);
                }
                // if roots match, do full list match test on children.
                if (sibling.getFirstChild() != null)
                {
                    if (!sibling.getFirstChild().EqualsList(t.getFirstChild()))
                    {
                        return(false);
                    }
                }
                else if (t.getFirstChild() != null)
                {
                    return(false);
                }
            }
            if (sibling == null && t == null)
            {
                return(true);
            }
            // one sibling list has more than the other
            return(false);
        }
Exemple #24
0
		/*Is t an exact structural and equals() match of this tree.  The
		*  'this' reference is considered the start of a sibling list.
		*/
		public virtual bool EqualsList(AST t)
		{
			AST sibling;
			
			// the empty tree is not a match of any non-null tree.
			if (t == null)
			{
				return false;
			}
			
			// Otherwise, start walking sibling lists.  First mismatch, return false.
			 for (sibling = this; sibling != null && t != null; sibling = sibling.getNextSibling(), t = t.getNextSibling())
			{
				// as a quick optimization, check roots first.
				if (!sibling.Equals(t))
				{
					return false;
				}
				// if roots match, do full list match test on children.
				if (sibling.getFirstChild() != null)
				{
					if (!sibling.getFirstChild().EqualsList(t.getFirstChild()))
					{
						return false;
					}
				}
				else if (t.getFirstChild() != null)
				{
					return false;
				}
			}
			if (sibling == null && t == null)
			{
				return true;
			}
			// one sibling list has more than the other
			return false;
		}
        //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;
        }
        //throws RecognitionException
        public void singleTemplateArg(AST _t,
		StringTemplate embedded, IDictionary argumentContext
	)
        {
            antlr.stringtemplate.language.StringTemplateAST singleTemplateArg_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t;

            Object e = null;

            try {      // for error handling
            AST __t41 = _t;
            antlr.stringtemplate.language.StringTemplateAST tmp22_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
            match((AST)_t,SINGLEVALUEARG);
            _t = _t.getFirstChild();
            e=expr(_t);
            _t = retTree_;
            _t = __t41;
            _t = _t.getNextSibling();

                    if ( e!=null ) {
                        String soleArgName = null;
                        // find the sole defined formal argument for embedded
                        bool error = false;
                        IDictionary formalArgs = embedded.getFormalArguments();
                        if ( formalArgs!=null )
                        {
                            ICollection argNames = formalArgs.Keys;
                            if ( argNames.Count==1 )
                            {
                                string[] argNamesArray = new string[argNames.Count];
                                argNames.CopyTo(argNamesArray,0);
                                soleArgName = argNamesArray[0];
                                //System.out.println("sole formal arg of "+embedded.getName()+" is "+soleArgName);
                            }
                            else
                            {
                                error=true;
                            }
                        }
                        else
                        {
                            error=true;
                        }
                        if ( error )
                        {
                            self.error("template "+embedded.getName()+
                                       " must have exactly one formal arg in template context "+
                                       self.getEnclosingInstanceStackString());
                       	}
                       	else
                       	{
                       		self.rawSetArgumentAttribute(embedded,argumentContext,soleArgName,e);
                       	}
                    }

            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
        }
        //throws RecognitionException
        public Object function(AST _t)
        {
            Object value=null;

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

            Object a;

            try {      // for error handling
            AST __t21 = _t;
            antlr.stringtemplate.language.StringTemplateAST tmp10_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
            match((AST)_t,FUNCTION);
            _t = _t.getFirstChild();
            {
                if (null == _t)
                    _t = ASTNULL;
                switch ( _t.Type )
                {
                case LITERAL_first:
                {
                    antlr.stringtemplate.language.StringTemplateAST tmp11_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                    match((AST)_t,LITERAL_first);
                    _t = _t.getNextSibling();
                    a=singleFunctionArg(_t);
                    _t = retTree_;
                    value=chunk.first(a);
                    break;
                }
                case LITERAL_rest:
                {
                    antlr.stringtemplate.language.StringTemplateAST tmp12_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                    match((AST)_t,LITERAL_rest);
                    _t = _t.getNextSibling();
                    a=singleFunctionArg(_t);
                    _t = retTree_;
                    value=chunk.rest(a);
                    break;
                }
                case LITERAL_last:
                {
                    antlr.stringtemplate.language.StringTemplateAST tmp13_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                    match((AST)_t,LITERAL_last);
                    _t = _t.getNextSibling();
                    a=singleFunctionArg(_t);
                    _t = retTree_;
                    value=chunk.last(a);
                    break;
                }
                default:
                {
                    throw new NoViableAltException(_t);
                }
                 }
            }
            _t = __t21;
            _t = _t.getNextSibling();
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return value;
        }
        //throws RecognitionException
        public void template(AST _t,
            ArrayList templatesToApply
            )
        {
            Antlr.StringTemplate.Language.StringTemplateAST template_AST_in = (Antlr.StringTemplate.Language.StringTemplateAST)_t;
            Antlr.StringTemplate.Language.StringTemplateAST t = null;
            Antlr.StringTemplate.Language.StringTemplateAST args = null;
            Antlr.StringTemplate.Language.StringTemplateAST anon = null;
            Antlr.StringTemplate.Language.StringTemplateAST args2 = null;

            object n = null;

            try {      // for error handling
            AST __t26 = _t;
            Antlr.StringTemplate.Language.StringTemplateAST tmp18_AST_in = (_t==ASTNULL) ? null : (Antlr.StringTemplate.Language.StringTemplateAST)_t;
            match((AST)_t,TEMPLATE);
            _t = _t.getFirstChild();
            {
                if (null == _t)
                    _t = ASTNULL;
                switch ( _t.Type )
                {
                case ID:
                {
                    t = (_t==ASTNULL) ? null : (Antlr.StringTemplate.Language.StringTemplateAST)_t;
                    match((AST)_t,ID);
                    _t = _t.getNextSibling();
                    args = (_t==ASTNULL) ? null : (Antlr.StringTemplate.Language.StringTemplateAST)_t;
                    if (null == _t) throw new MismatchedTokenException();
                    _t = _t.getNextSibling();

                    string templateName = t.getText();
                    StringTemplateGroup group = self.Group;
                    StringTemplate embedded = group.GetEmbeddedInstanceOf(self, templateName);
                    if ( embedded!=null ) {
                    embedded.ArgumentsAST = args;
                    templatesToApply.Add(embedded);
                    }

                    break;
                }
                case ANONYMOUS_TEMPLATE:
                {
                    anon = (_t==ASTNULL) ? null : (Antlr.StringTemplate.Language.StringTemplateAST)_t;
                    match((AST)_t,ANONYMOUS_TEMPLATE);
                    _t = _t.getNextSibling();

                    StringTemplate anonymous = anon.StringTemplate;
                    // to properly see overridden templates, always set
                    // anonymous' group to be self's group
                                    anonymous.Group = self.Group;
                    templatesToApply.Add(anonymous);

                    break;
                }
                case VALUE:
                {
                    AST __t28 = _t;
                    Antlr.StringTemplate.Language.StringTemplateAST tmp19_AST_in = (_t==ASTNULL) ? null : (Antlr.StringTemplate.Language.StringTemplateAST)_t;
                    match((AST)_t,VALUE);
                    _t = _t.getFirstChild();
                    n=expr(_t);
                    _t = retTree_;
                    args2 = (_t==ASTNULL) ? null : (Antlr.StringTemplate.Language.StringTemplateAST)_t;
                    if (null == _t) throw new MismatchedTokenException();
                    _t = _t.getNextSibling();

                                            StringTemplate embedded = null;
                                            if ( n!=null ) {
                                String templateName = n.ToString();
                                                StringTemplateGroup group = self.Group;
                                                embedded = group.GetEmbeddedInstanceOf(self, templateName);
                                                if ( embedded!=null ) {
                                                    embedded.ArgumentsAST = args2;
                                                    templatesToApply.Add(embedded);
                                                }
                                            }

                    _t = __t28;
                    _t = _t.getNextSibling();
                    break;
                }
                default:
                {
                    throw new NoViableAltException(_t);
                }
                 }
            }
            _t = __t26;
            _t = _t.getNextSibling();
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
        }
        //throws RecognitionException
        public bool ifCondition(AST _t)
        {
            bool value=false;

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

            Object a=null, b=null;

            try {      // for error handling
            if (null == _t)
                _t = ASTNULL;
            switch ( _t.Type )
            {
            case APPLY:
            case MULTI_APPLY:
            case INCLUDE:
            case VALUE:
            case FUNCTION:
            case LIST:
            case PLUS:
            case DOT:
            case ID:
            case ANONYMOUS_TEMPLATE:
            case STRING:
            case INT:
            {
                a=ifAtom(_t);
                _t = retTree_;
                value = chunk.testAttributeTrue(a);
                break;
            }
            case NOT:
            {
                AST __t30 = _t;
                antlr.stringtemplate.language.StringTemplateAST tmp18_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,NOT);
                _t = _t.getFirstChild();
                a=ifAtom(_t);
                _t = retTree_;
                _t = __t30;
                _t = _t.getNextSibling();
                value = !chunk.testAttributeTrue(a);
                break;
            }
            default:
            {
                throw new NoViableAltException(_t);
            }
             }
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return value;
        }
    /** Is t an exact structural match of this tree with the same node
     *  types?  'self' is considered the start of a sibling list.
     */
    protected bool equalsNodeTypesList(AST self, AST t)
    {
        // Console.Out.WriteLine("self="+self+", t="+t);
        // Console.Out.WriteLine("self.class="+self.getClass()+", t.class="+t.getClass());
        AST sibling;

        // the empty tree is not a match of any non-null tree.
        if (t == null) {
            return false;
        }

        // Otherwise, start walking sibling lists.  First mismatch, return false.
        for (sibling = self;
             sibling != null && t != null;
             sibling = sibling.getNextSibling(), t = t.getNextSibling())
        {
            // Console.Out.WriteLine("sibling="+sibling+", t="+t);
            // as a quick optimization, check root types first.
            if ( sibling.GetType()!=t.GetType() ) {
                return false;
            }
            // if roots match, do full list match test on children.
            if (sibling.getFirstChild() != null) {
                if (!equalsNodeTypesList(sibling.getFirstChild(),
                                         t.getFirstChild()))
                {
                    return false;
                }
            }
            // sibling has no kids, make sure t doesn't either
            else if (t.getFirstChild() != null) {
                return false;
            }
        }
        if (sibling == null && t == null) {
            return true;
        }
        // one sibling list has more than the other
        return false;
    }
        //throws RecognitionException
        public Object singleFunctionArg(AST _t)
        {
            Object value=null;

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

            try {      // for error handling
            AST __t24 = _t;
            antlr.stringtemplate.language.StringTemplateAST tmp17_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
            match((AST)_t,SINGLEVALUEARG);
            _t = _t.getFirstChild();
            value=expr(_t);
            _t = retTree_;
            _t = __t24;
            _t = _t.getNextSibling();
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return value;
        }
        //throws RecognitionException
        public object value(AST _t)
        {
            object obj;

            AST value_AST_in = (AST)_t;
            AST l = null;
            AST ql = null;
            AST v = null;

            obj = null;

            try {      // for error handling
            if (null == _t)
                _t = ASTNULL;
            switch ( _t.Type )
            {
            case LITERAL:
            {
                l = _t;
                match(_t,LITERAL);
                _t = _t.getNextSibling();

                obj = Convert.ToInt32(l.getText());

                break;
            }
            case QLITERAL:
            {
                ql = _t;
                match(_t,QLITERAL);
                _t = _t.getNextSibling();

                obj = ql.getText();

                break;
            }
            case VAR:
            {
                AST tmp18_AST_in = _t;
                match(_t,VAR);
                _t = _t.getNextSibling();
                AST tmp19_AST_in = _t;
                match(_t,LPAREN);
                _t = _t.getNextSibling();
                v = _t;
                match(_t,LITERAL);
                _t = _t.getNextSibling();
                AST tmp20_AST_in = _t;
                match(_t,RPAREN);
                _t = _t.getNextSibling();

                string var = v.getText();

                obj = _inst.Eval(var);

                if (obj == null) {
                throw new RecognitionException("unrecognized variable " + var);
                }

                break;
            }
            default:
            {
                throw new NoViableAltException(_t);
            }
             }
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return obj;
        }
        //throws RecognitionException
        public void template(AST _t,
		ArrayList templatesToApply
	)
        {
            antlr.stringtemplate.language.StringTemplateAST template_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t;
            antlr.stringtemplate.language.StringTemplateAST t = null;
            antlr.stringtemplate.language.StringTemplateAST args = null;
            antlr.stringtemplate.language.StringTemplateAST anon = null;
            antlr.stringtemplate.language.StringTemplateAST args2 = null;

            IDictionary argumentContext = null;
            Object n = null;

            try {      // for error handling
            AST __t26 = _t;
            antlr.stringtemplate.language.StringTemplateAST tmp15_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
            match((AST)_t,TEMPLATE);
            _t = _t.getFirstChild();
            {
                if (null == _t)
                    _t = ASTNULL;
                switch ( _t.Type )
                {
                case ID:
                {
                    t = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                    match((AST)_t,ID);
                    _t = _t.getNextSibling();
                    args = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                    if (null == _t) throw new MismatchedTokenException();
                    _t = _t.getNextSibling();

                    String templateName = t.getText();
                    StringTemplateGroup group = self.getGroup();
                    StringTemplate embedded = group.getEmbeddedInstanceOf(self, templateName);
                    if ( embedded!=null ) {
                    embedded.setArgumentsAST(args);
                    templatesToApply.Add(embedded);
                    }

                    break;
                }
                case ANONYMOUS_TEMPLATE:
                {
                    anon = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                    match((AST)_t,ANONYMOUS_TEMPLATE);
                    _t = _t.getNextSibling();

                    StringTemplate anonymous = anon.getStringTemplate();
                    templatesToApply.Add(anonymous);

                    break;
                }
                case VALUE:
                {
                    AST __t28 = _t;
                    antlr.stringtemplate.language.StringTemplateAST tmp16_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                    match((AST)_t,VALUE);
                    _t = _t.getFirstChild();
                    n=expr(_t);
                    _t = retTree_;
                    args2 = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                    if (null == _t) throw new MismatchedTokenException();
                    _t = _t.getNextSibling();

                                            StringTemplate embedded = null;
                                            if ( n!=null )
                                            {
                                String templateName = n.ToString();
                                                StringTemplateGroup group = self.getGroup();
                                                embedded = group.getEmbeddedInstanceOf(self, templateName);
                                                if ( embedded!=null )
                                                {
                                                    embedded.setArgumentsAST(args2);
                                                    templatesToApply.Add(embedded);
                                                }
                                            }

                    _t = __t28;
                    _t = _t.getNextSibling();
                    break;
                }
                default:
                {
                    throw new NoViableAltException(_t);
                }
                 }
            }
            _t = __t26;
            _t = _t.getNextSibling();
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
        }
        //throws RecognitionException
        /** Apply template(s) to an attribute; can be applied to another apply
         *  result.
         */
        public Object templateApplication(AST _t)
        {
            Object value=null;

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

            Object a=null;
            ArrayList templatesToApply=new ArrayList();

            try {      // for error handling
            AST __t10 = _t;
            antlr.stringtemplate.language.StringTemplateAST tmp3_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
            match((AST)_t,APPLY);
            _t = _t.getFirstChild();
            a=expr(_t);
            _t = retTree_;
            { // ( ... )+
                int _cnt12=0;
                for (;;)
                {
                    if (_t == null)
                        _t = ASTNULL;
                    if ((_t.Type==TEMPLATE))
                    {
                        template(_t,templatesToApply);
                        _t = retTree_;
                    }
                    else
                    {
                        if (_cnt12 >= 1) { goto _loop12_breakloop; } else { throw new NoViableAltException(_t);; }
                    }

                    _cnt12++;
                }
            _loop12_breakloop:				;
            }    // ( ... )+
            value = chunk.applyListOfAlternatingTemplates(self,a,templatesToApply);
            _t = __t10;
            _t = _t.getNextSibling();
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return value;
        }
        //throws RecognitionException
        public bool expression(AST _t)
        {
            bool e ;

            AST expression_AST_in = (AST)_t;

            bool a, b;
            object l, r;

            e = false;

            try {      // for error handling
            if (null == _t)
                _t = ASTNULL;
            switch ( _t.Type )
            {
            case AND:
            {
                AST __t44 = _t;
                AST tmp21_AST_in = _t;
                match(_t,AND);
                _t = _t.getFirstChild();
                a=expression(_t);
                _t = retTree_;
                b=expression(_t);
                _t = retTree_;
                e = a && b;
                _t = __t44;
                _t = _t.getNextSibling();
                break;
            }
            case OR:
            {
                AST __t45 = _t;
                AST tmp22_AST_in = _t;
                match(_t,OR);
                _t = _t.getFirstChild();
                a=expression(_t);
                _t = retTree_;
                b=expression(_t);
                _t = retTree_;
                e = a || b;
                _t = __t45;
                _t = _t.getNextSibling();
                break;
            }
            case EQUAL:
            {
                AST __t46 = _t;
                AST tmp23_AST_in = _t;
                match(_t,EQUAL);
                _t = _t.getFirstChild();
                l=value(_t);
                _t = retTree_;
                r=value(_t);
                _t = retTree_;

                e = ((l as IComparable).CompareTo(r) == 0);

                _t = __t46;
                _t = _t.getNextSibling();
                break;
            }
            case NOT_EQUAL:
            {
                AST __t47 = _t;
                AST tmp24_AST_in = _t;
                match(_t,NOT_EQUAL);
                _t = _t.getFirstChild();
                l=value(_t);
                _t = retTree_;
                r=value(_t);
                _t = retTree_;

                e = ((l as IComparable).CompareTo(r) != 0);

                _t = __t47;
                _t = _t.getNextSibling();
                break;
            }
            case LESSTHAN:
            {
                AST __t48 = _t;
                AST tmp25_AST_in = _t;
                match(_t,LESSTHAN);
                _t = _t.getFirstChild();
                l=value(_t);
                _t = retTree_;
                r=value(_t);
                _t = retTree_;

                e = ((l as IComparable).CompareTo(r) < 0);

                _t = __t48;
                _t = _t.getNextSibling();
                break;
            }
            case LESSEQUAL:
            {
                AST __t49 = _t;
                AST tmp26_AST_in = _t;
                match(_t,LESSEQUAL);
                _t = _t.getFirstChild();
                l=value(_t);
                _t = retTree_;
                r=value(_t);
                _t = retTree_;

                e = ((l as IComparable).CompareTo(r) <= 0);

                _t = __t49;
                _t = _t.getNextSibling();
                break;
            }
            case GE:
            {
                AST __t50 = _t;
                AST tmp27_AST_in = _t;
                match(_t,GE);
                _t = _t.getFirstChild();
                l=value(_t);
                _t = retTree_;
                r=value(_t);
                _t = retTree_;

                e = ((l as IComparable).CompareTo(r) >= 0);

                _t = __t50;
                _t = _t.getNextSibling();
                break;
            }
            case GT:
            {
                AST __t51 = _t;
                AST tmp28_AST_in = _t;
                match(_t,GT);
                _t = _t.getFirstChild();
                l=value(_t);
                _t = retTree_;
                r=value(_t);
                _t = retTree_;

                e = ((l as IComparable).CompareTo(r) > 0);

                _t = __t51;
                _t = _t.getNextSibling();
                break;
            }
            default:
            {
                throw new NoViableAltException(_t);
            }
             }
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return e ;
        }
        //throws RecognitionException
        public int action(AST _t)
        {
            int numCharsWritten=0;

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

            Object e=null;

            try {      // for error handling
            e=expr(_t);
            _t = retTree_;
            numCharsWritten = chunk.writeAttribute(self,e,@out);
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return numCharsWritten;
        }
Exemple #37
0
        //throws RecognitionException
        public String value(AST _t)
        {
            String s ;

            AST value_AST_in = (AST)_t;
            AST l = null;
            AST ql = null;
            AST v = null;

            String variable = null;
            s = null;

            try {      // for error handling
            if (null == _t)
                _t = ASTNULL;
            switch ( _t.Type )
            {
            case LITERAL:
            {
                l = _t;
                match(_t,LITERAL);
                _t = _t.getNextSibling();

                s = l.getText();

                break;
            }
            case QLITERAL:
            {
                ql = _t;
                match(_t,QLITERAL);
                _t = _t.getNextSibling();

                s = ql.getText();

                break;
            }
            case VAR:
            {
                AST tmp18_AST_in = _t;
                match(_t,VAR);
                _t = _t.getNextSibling();
                AST tmp19_AST_in = _t;
                match(_t,LPAREN);
                _t = _t.getNextSibling();
                v = _t;
                match(_t,LITERAL);
                _t = _t.getNextSibling();
                AST tmp20_AST_in = _t;
                match(_t,RPAREN);
                _t = _t.getNextSibling();

                variable = v.getText();

                s = (String) environment.get(variable);

                if (s == null) {
                throw new RecognitionException("unrecognized variable " + variable);
                }

                break;
            }
            default:
            {
                throw new NoViableAltException(_t);
            }
             }
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return s ;
        }
        //throws RecognitionException
        /** self is assumed to be the enclosing context as foo(x=y) must find y in
         *  the template that encloses the ref to foo(x=y).  We must pass in
         *  the embedded template (the one invoked) so we can check formal args
         *  in rawSetArgumentAttribute.
         */
        public IDictionary argList(AST _t,
		StringTemplate embedded, IDictionary initialContext
	)
        {
            IDictionary argumentContext=null;

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

            argumentContext = initialContext;
            if ( argumentContext==null ) {
            argumentContext=new Hashtable();
            }

            try {      // for error handling
            if (null == _t)
                _t = ASTNULL;
            switch ( _t.Type )
            {
            case ARGS:
            {
                AST __t37 = _t;
                antlr.stringtemplate.language.StringTemplateAST tmp19_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,ARGS);
                _t = _t.getFirstChild();
                {    // ( ... )*
                    for (;;)
                    {
                        if (_t == null)
                            _t = ASTNULL;
                        if ((_t.Type==ASSIGN||_t.Type==DOTDOTDOT))
                        {
                            argumentAssignment(_t,embedded,argumentContext);
                            _t = retTree_;
                        }
                        else
                        {
                            goto _loop39_breakloop;
                        }

                    }
            _loop39_breakloop:					;
                }    // ( ... )*
                _t = __t37;
                _t = _t.getNextSibling();
                break;
            }
            case SINGLEVALUEARG:
            {
                singleTemplateArg(_t,embedded,argumentContext);
                _t = retTree_;
                break;
            }
            default:
            {
                throw new NoViableAltException(_t);
            }
             }
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return argumentContext;
        }
        //throws RecognitionException
        /** Apply template(s) to an attribute; can be applied to another apply
         *  result.
         */
        public Object templateApplication(AST _t)
        {
            Object value=null;

            antlr.stringtemplate.language.StringTemplateAST templateApplication_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t;
            antlr.stringtemplate.language.StringTemplateAST anon = null;

            Object a=null;
            ArrayList templatesToApply=new ArrayList();
            ArrayList attributes=new ArrayList();

            try {      // for error handling
            if (null == _t)
                _t = ASTNULL;
            switch ( _t.Type )
            {
            case APPLY:
            {
                AST __t14 = _t;
                antlr.stringtemplate.language.StringTemplateAST tmp3_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,APPLY);
                _t = _t.getFirstChild();
                a=expr(_t);
                _t = retTree_;
                { // ( ... )+
                    int _cnt16=0;
                    for (;;)
                    {
                        if (_t == null)
                            _t = ASTNULL;
                        if ((_t.Type==TEMPLATE))
                        {
                            template(_t,templatesToApply);
                            _t = retTree_;
                        }
                        else
                        {
                            if (_cnt16 >= 1) { goto _loop16_breakloop; } else { throw new NoViableAltException(_t);; }
                        }

                        _cnt16++;
                    }
            _loop16_breakloop:					;
                }    // ( ... )+
                value = chunk.applyListOfAlternatingTemplates(self,a,templatesToApply);
                _t = __t14;
                _t = _t.getNextSibling();
                break;
            }
            case MULTI_APPLY:
            {
                AST __t17 = _t;
                antlr.stringtemplate.language.StringTemplateAST tmp4_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,MULTI_APPLY);
                _t = _t.getFirstChild();
                { // ( ... )+
                    int _cnt19=0;
                    for (;;)
                    {
                        if (_t == null)
                            _t = ASTNULL;
                        if ((tokenSet_0_.member(_t.Type)))
                        {
                            a=expr(_t);
                            _t = retTree_;
                            attributes.Add(a);
                        }
                        else
                        {
                            if (_cnt19 >= 1) { goto _loop19_breakloop; } else { throw new NoViableAltException(_t);; }
                        }

                        _cnt19++;
                    }
            _loop19_breakloop:					;
                }    // ( ... )+
                antlr.stringtemplate.language.StringTemplateAST tmp5_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,COLON);
                _t = _t.getNextSibling();
                anon = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,ANONYMOUS_TEMPLATE);
                _t = _t.getNextSibling();

                            StringTemplate anonymous = anon.getStringTemplate();
                            templatesToApply.Add(anonymous);
                            value = chunk.applyTemplateToListOfAttributes(self,
                                                                          attributes,
                                                                          anon.getStringTemplate());

                _t = __t17;
                _t = _t.getNextSibling();
                break;
            }
            default:
            {
                throw new NoViableAltException(_t);
            }
             }
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return value;
        }
Exemple #40
0
		/*Is 'sub' a subtree of this list?
		*  The siblings of the root are NOT ignored.
		*/
		public virtual bool EqualsListPartial(AST sub)
		{
			AST sibling;
			
			// the empty tree is always a subset of any tree.
			if (sub == null)
			{
				return true;
			}
			
			// Otherwise, start walking sibling lists.  First mismatch, return false.
			 for (sibling = this; sibling != null && sub != null; sibling = sibling.getNextSibling(), sub = sub.getNextSibling())
			{
				// as a quick optimization, check roots first.
				if (!sibling.Equals(sub))
					return false;
				// if roots match, do partial list match test on children.
				if (sibling.getFirstChild() != null)
				{
					if (!sibling.getFirstChild().EqualsListPartial(sub.getFirstChild()))
						return false;
				}
			}
			if (sibling == null && sub != null)
			{
				// nothing left to match in this tree, but subtree has more
				return false;
			}
			// either both are null or sibling has more, but subtree doesn't
			return true;
		}
        //throws RecognitionException
        public Object templateInclude(AST _t)
        {
            Object value=null;

            antlr.stringtemplate.language.StringTemplateAST templateInclude_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t;
            antlr.stringtemplate.language.StringTemplateAST id = null;
            antlr.stringtemplate.language.StringTemplateAST a1 = null;
            antlr.stringtemplate.language.StringTemplateAST a2 = null;

            StringTemplateAST args = null;
            String name = null;
            Object n = null;

            try {      // for error handling
            AST __t10 = _t;
            antlr.stringtemplate.language.StringTemplateAST tmp8_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
            match((AST)_t,INCLUDE);
            _t = _t.getFirstChild();
            {
                if (null == _t)
                    _t = ASTNULL;
                switch ( _t.Type )
                {
                case ID:
                {
                    id = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                    match((AST)_t,ID);
                    _t = _t.getNextSibling();
                    a1 = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                    if (null == _t) throw new MismatchedTokenException();
                    _t = _t.getNextSibling();
                    name=id.getText(); args=a1;
                    break;
                }
                case VALUE:
                {
                    AST __t12 = _t;
                    antlr.stringtemplate.language.StringTemplateAST tmp9_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                    match((AST)_t,VALUE);
                    _t = _t.getFirstChild();
                    n=expr(_t);
                    _t = retTree_;
                    a2 = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                    if (null == _t) throw new MismatchedTokenException();
                    _t = _t.getNextSibling();
                    _t = __t12;
                    _t = _t.getNextSibling();
                    if (n!=null) {name=n.ToString();} args=a2;
                    break;
                }
                default:
                {
                    throw new NoViableAltException(_t);
                }
                 }
            }
            _t = __t10;
            _t = _t.getNextSibling();

            if ( name!=null ) {
                value = chunk.getTemplateInclude(self, name, args);
            }

            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return value;
        }
        //throws RecognitionException
        public Object attribute(AST _t)
        {
            Object value=null;

            antlr.stringtemplate.language.StringTemplateAST attribute_AST_in = (antlr.stringtemplate.language.StringTemplateAST)_t;
            antlr.stringtemplate.language.StringTemplateAST prop = null;
            antlr.stringtemplate.language.StringTemplateAST i3 = null;
            antlr.stringtemplate.language.StringTemplateAST i = null;
            antlr.stringtemplate.language.StringTemplateAST s = null;

            Object obj = null;

            try {      // for error handling
            if (null == _t)
                _t = ASTNULL;
            switch ( _t.Type )
            {
            case DOT:
            {
                AST __t21 = _t;
                antlr.stringtemplate.language.StringTemplateAST tmp4_AST_in = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,DOT);
                _t = _t.getFirstChild();
                obj=attribute(_t);
                _t = retTree_;
                prop = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,ID);
                _t = _t.getNextSibling();
                _t = __t21;
                _t = _t.getNextSibling();
                value = chunk.getObjectProperty(self,obj,prop.getText());
                break;
            }
            case ID:
            {
                i3 = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,ID);
                _t = _t.getNextSibling();

                try {
                value=self.getAttribute(i3.getText());
                }
                catch (ArgumentOutOfRangeException nse) {
                // rethrow with more precise error message
                throw new ArgumentOutOfRangeException(nse.ParamName, String.Format(
                    "no such attribute: {0} in template {1}", i3.getText(), self.getName()));
                }

                break;
            }
            case INT:
            {
                i = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,INT);
                _t = _t.getNextSibling();
                value=Int32.Parse(i.getText());
                break;
            }
            case STRING:
            {
                s = (_t==ASTNULL) ? null : (antlr.stringtemplate.language.StringTemplateAST)_t;
                match((AST)_t,STRING);
                _t = _t.getNextSibling();
                value=s.getText();
                break;
            }
            default:
            {
                throw new NoViableAltException(_t);
            }
             }
            }
            catch (RecognitionException ex)
            {
            reportError(ex);
            if (null != _t)
            {
                _t = _t.getNextSibling();
            }
            }
            retTree_ = _t;
            return value;
        }