private void EvaluateDefineNodes(SymNode aNode, SymTokenDocument aDocument, SymParserDocumentContext aContext)
        {
            foreach (SymNode n in aNode)
            {
                if (n is SymTokenBalancerMarkerLevelNode)
                {
                    bool added = false;
                    //
                    SymTokenBalancerMarkerLevelNode levelNode = (SymTokenBalancerMarkerLevelNode)n;
                    if (levelNode.IsFunction)
                    {
                        SymNodeToken functionNameNode = levelNode.FunctionName;
                        //
                        if (functionNameNode.Token.Equals(iDefinedNodeToken))
                        {
                            SymTokenContainer defineName = levelNode.ChildTokens;
                            string            flattened  = defineName.CoalescedTokenValue;

                            // Get definition result
                            bool     isDefined      = aContext.DefineDirectory.IsDefined(flattened);
                            SymToken isDefinedToken = new SymToken(isDefined.ToString().ToLower(), SymToken.TClass.EClassAlphaNumeric, SymToken.TType.ETypeAlphaNumericNormal);

                            // Remove already added "defined" text node from output document
                            if (aDocument.CurrentNode.LastChild is SymNodeToken)
                            {
                                SymNodeToken last = (SymNodeToken)aDocument.CurrentNode.LastChild;
                                if (last.Token.Equals(iDefinedNodeToken))
                                {
                                    last.Remove();
                                }
                            }

                            // Add result
                            aDocument.CurrentNode.Add(new SymNodeToken(isDefinedToken));
                            added = true;
                        }
                    }

                    if (added == false)
                    {
                        if (levelNode.HasPrevious && levelNode.Previous is SymTokenBalancerNodeEmittedElement)
                        {
                            levelNode.EmittedElementPrevious.AddToDocumentIfEmittable(aDocument);
                        }

                        SymNode newLevelNode = new SymNodeAddAsChild();
                        aDocument.CurrentNode.Add(newLevelNode);
                        aDocument.CurrentNode = newLevelNode;
                        EvaluateDefineNodes(n, aDocument, aContext);
                        aDocument.MakeParentCurrent();

                        if (levelNode.HasNext && levelNode.Next is SymTokenBalancerNodeEmittedElement)
                        {
                            levelNode.EmittedElementNext.AddToDocumentIfEmittable(aDocument);
                        }
                    }
                }
                else if (n is SymNodeToken)
                {
                    SymNodeToken node = (SymNodeToken)n;
                    SymNodeToken copy = new SymNodeToken(node.Token);
                    aDocument.CurrentNode.Add(copy);
                }
                else if (n is SymTokenBalancerNodeEmittedElement)
                {
                    // Handled when the level marker is reached
                }
            }
        }
Esempio n. 2
0
        public static SymNode Trim(SymNode aNode, bool aRecurse)
        {
            // Forward pass
            while (aNode.HasChildren)
            {
                SymNode n = aNode.FirstChild;

                if (n is SymNodeToken)
                {
                    SymNodeToken nodeToken    = (SymNodeToken)n;
                    bool         isWhiteSpace = (nodeToken.Token.Class == SymToken.TClass.EClassWhiteSpace);
                    //
                    if (isWhiteSpace)
                    {
                        System.Diagnostics.Debug.Assert(n.HasChildren == false);
                        nodeToken.Remove();
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            // Backward pass
            while (aNode.HasChildren)
            {
                SymNode n = aNode.LastChild;

                if (n is SymNodeToken)
                {
                    SymNodeToken nodeToken    = (SymNodeToken)n;
                    bool         isWhiteSpace = (nodeToken.Token.Class == SymToken.TClass.EClassWhiteSpace);
                    //
                    if (isWhiteSpace)
                    {
                        System.Diagnostics.Debug.Assert(n.HasChildren == false);
                        nodeToken.Remove();
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            // Child pass
            if (aRecurse)
            {
                foreach (SymNode n in aNode)
                {
                    if (n.HasChildren)
                    {
                        Trim(n, aRecurse);
                    }
                }
            }

            return(aNode);
        }