コード例 #1
0
ファイル: IfNode.cs プロジェクト: asaa62820/Project
 public IfNode(TokenMatch tokenMatch)
     : base(tokenMatch)
 {
     _TrueNode  = new TokenNode();
     _FalseNode = new TokenNode();
 }
コード例 #2
0
        private TokenNode InternalLexer(string template, List <TokenMatch> tokenMatches)
        {
            Stack <TokenNode> nodeStack   = new Stack <TokenNode>();
            TokenNode         currentNode = new TokenNode();
            int      lastIndex            = 0;
            TextNode lastTextNode         = null;
            bool     checkEmptyLine       = false;

            foreach (TokenMatch tokenMatch in tokenMatches)
            {
                Match match = tokenMatch.Match;

                #region For TextNode

                if (match.Index > lastIndex)
                {
                    string text = template.Substring(lastIndex, match.Index - lastIndex);
                    if (checkEmptyLine)
                    {
                        text = CheckEmptyLine(lastTextNode, text);
                    }
                    lastTextNode   = (TextNode)currentNode.ChildNodes.Add((TextNode)text);
                    checkEmptyLine = false;
                }
                lastIndex      = match.Index + match.Length;
                checkEmptyLine = tokenMatch.RemoveEmptyLine && !checkEmptyLine;

                #endregion

                switch (tokenMatch.TokenType)
                {
                    #region switch TemplateTokenType

                case TemplateTokenType.Set:
                    currentNode.ChildNodes.Add((SetNode)tokenMatch);
                    break;

                case TemplateTokenType.Expression:
                    currentNode.ChildNodes.Add((ExpressionNode)tokenMatch);
                    break;

                case TemplateTokenType.ForEach:
                    nodeStack.Push(currentNode);
                    currentNode = currentNode.ChildNodes.Add((ForeachNode)tokenMatch);
                    break;

                case TemplateTokenType.While:
                    nodeStack.Push(currentNode);
                    currentNode = currentNode.ChildNodes.Add((WhileNode)tokenMatch);
                    break;

                case TemplateTokenType.If:
                    nodeStack.Push(currentNode);
                    IfNode ifNode = currentNode.ChildNodes.Add((IfNode)tokenMatch);
                    currentNode = ifNode.TrueNode;
                    nodeStack.Push(ifNode);
                    break;

                case TemplateTokenType.ElseIf:
                    IfNode last = nodeStack.Peek() as IfNode;
                    if (last == null)
                    {
                        throw new Exception("error syntax: #elseif can't use without the #if");
                    }
                    currentNode = last.FalseNode;
                    last        = currentNode.ChildNodes.Add((IfNode)tokenMatch);
                    currentNode = last.TrueNode;
                    nodeStack.Push(last);
                    break;

                case TemplateTokenType.Else:
                    IfNode ifNode2 = nodeStack.Peek() as IfNode;
                    if (ifNode2 == null)
                    {
                        throw new Exception("error syntax: #else can't use without the #if");
                    }
                    currentNode = ifNode2.FalseNode;
                    break;

                case TemplateTokenType.EndBlock:
                    while (nodeStack.Peek() is IfNode)
                    {
                        nodeStack.Pop();
                    }
                    currentNode = nodeStack.Pop();
                    break;

                    #endregion
                }
            }

            #region For TextNode

            if (lastIndex < template.Length - 1)
            {
                string text = template.Substring(lastIndex);
                if (checkEmptyLine)
                {
                    text = CheckEmptyLine(lastTextNode, text);
                }
                currentNode.ChildNodes.Add((TextNode)text);
            }

            #endregion

            return(currentNode);
        }