private Tag CollectForTag(Tag tag, ref int index) { if (tag.IsClosed) { return tag; } if (string.Compare(tag.Name, "if", true) == 0) { tag = new IfStatement(tag.Line, tag.Col, tag.AttributeValue("test")); } Tag collectTag = tag; for (index++; index < elements.Count; index++) { Token elem = elements[index]; if (elem is Text) { collectTag.InnerTokens.Add(elem); } else if (elem is Expression) { collectTag.InnerTokens.Add(elem); } else if (elem is Tag) { Tag innerTag = (Tag) elem; if (string.Compare(innerTag.Name, "else", true) == 0) { if (collectTag is IfStatement) { ((IfStatement) collectTag).FalseBranch = innerTag; collectTag = innerTag; } else { throw new TmplException("else tag has to be positioned inside of if or elseif tag " + innerTag.Line + "," + innerTag.Col, innerTag.Line, innerTag.Col); } } else if (string.Compare(innerTag.Name, "elseif", true) == 0) { if (collectTag is IfStatement) { Tag newTag = new IfStatement(innerTag.Line, innerTag.Col, innerTag.AttributeValue("test")); ((IfStatement) collectTag).FalseBranch = newTag; collectTag = newTag; } else { throw new TmplException("elseif tag is not positioned properly" + innerTag.Line + "," + innerTag.Col, innerTag.Line, innerTag.Col); } } else { collectTag.InnerTokens.Add(CollectForTag(innerTag, ref index)); } } else if (elem is StatementClose) { StatementClose tagClose = (StatementClose) elem; return tag; } else { throw new TmplException("Invalid element: [" + elem.GetType().ToString() + "] " + elem.Line + "," + elem.Col, elem.Line, elem.Col); } } throw new TmplException("Start tag: [" + tag.Name + "] does not have matching end tag." + tag.Line + "," + tag.Col, tag.Line, tag.Col); }
private void ProcessIf(IfStatement tagIf) { bool condition = false; try { condition = Util.ToBoolean(EvalExpression(tagIf.Test)); } catch (Exception ex) { DisplayError("Error evaluating condition for if statement: [" + tagIf.Test + "] " + ex.Message, tagIf.Line, tagIf.Col); return; } if (condition) { ProcessElements(tagIf.InnerTokens); } else { ProcessElement(tagIf.FalseBranch); } }
private void visitTagIf(IfStatement tag) { WriteLine("Tag: " + tag.Name); AddAttribs(tag.Attributes); foreach (Token elem in tag.InnerTokens) { visitElement(elem); } if (tag.FalseBranch != null) { visitElement(tag.FalseBranch); } }