Beispiel #1
0
        private TagNode CollectForTag(TagNode tag, ref int index)
        {
            if (tag.Closed) // if self-closing tag, do not collect inner elements
                return tag;

            if (String.Compare(tag.TagName, "if", true) == 0) {
                tag = new IfTag(tag.Line, tag.Column, tag.GetAttributeValue("test"));
            }

            TagNode collectTag = tag;

            for (index++; index < nodes.Count; index++) {
                Node node = nodes[index];

                if (node is TextNode)
                    collectTag.ChildNodes.Add(node);
                else if (node is Expression)
                    collectTag.ChildNodes.Add(node);
                else if (node is TagNode) {
                    TagNode innerTag = (TagNode)node;
                    if (string.Compare(innerTag.TagName, "else", true) == 0) {
                        if (collectTag is IfTag) {
                            ((IfTag)collectTag).FalseBranch = innerTag;
                            collectTag = innerTag;
                        } else
                            throw new ParseException("else tag has to be positioned inside of if or elseif tag", innerTag.Line, innerTag.Column);

                    } else if (string.Compare(innerTag.TagName, "elif", true) == 0) {
                        if (collectTag is IfTag) {
                            TagNode newTag = new IfTag(innerTag.Line, innerTag.Column, innerTag.GetAttributeValue("test"));
                            ((IfTag)collectTag).FalseBranch = newTag;
                            collectTag = newTag;
                        } else
                            throw new ParseException("elseif tag is not positioned properly", innerTag.Line, innerTag.Column);
                    } else
                        collectTag.ChildNodes.Add(CollectForTag(innerTag, ref index));
                } else if (node is TagEnd) {
                    TagEnd tagClose = (TagEnd)node;
                    if (string.Compare(tag.TagName, tagClose.TagName, true) == 0)
                        return tag;

                    throw new ParseException("Close tag for " + tagClose.TagName + " doesn't have matching start tag.", node.Line, node.Column);
                } else
                    throw new ParseException("Invalid node: " + node.GetType(), node.Line, node.Column);

            }

            throw new ParseException("Start tag: " + tag.TagName + " does not have matching end tag.", tag.Line, tag.Column);
        }
        private void ProcessIf(IfTag tagIf)
        {
            bool condition;

            try {
                object value = EvaluateExpression(tagIf.TestExpression);

                condition = Util.ToBoolean(value);
            } catch (Exception ex) {
                OnError("Error evaluating condition for if statement: " + ex.Message, tagIf.Line, tagIf.Column);
                return;
            }

            if (condition)
                ProcessNodes(tagIf.ChildNodes);
            else
                ProcessNode(tagIf.FalseBranch);
        }