private static IEnumerable <StatementGrammarNode> GetPathFilterDescendants(StatementGrammarNode node, Func <StatementGrammarNode, bool> pathFilter, params string[] descendantNodeIds)
        {
            foreach (var childNode in node.ChildNodes)
            {
                if (pathFilter != null && !pathFilter(childNode))
                {
                    continue;
                }

                if (descendantNodeIds == null || descendantNodeIds.Length == 0 || descendantNodeIds.Contains(childNode.Id))
                {
                    yield return(childNode);
                }

                if (childNode.Type == NodeType.Terminal)
                {
                    continue;
                }

                foreach (var descendant in GetPathFilterDescendants(childNode, pathFilter, descendantNodeIds))
                {
                    yield return(descendant);
                }
            }
        }
 private static void GatherChildNodeTerminals(StatementGrammarNode node, StatementGrammarNode[] targetArray, ref int offset)
 {
     foreach (var childNode in node.ChildNodes)
     {
         GatherNodeTerminals(childNode, targetArray, ref offset);
     }
 }
        private static IEnumerable <StatementGrammarNode> GetChildNodesAtPosition(StatementGrammarNode node, int position)
        {
            var returnedNodes = new List <StatementGrammarNode>();

            if (node == null)
            {
                return(returnedNodes);
            }

            var childNodesAtPosition = node.ChildNodes.Where(n => n.SourcePosition.ContainsIndex(position));

            foreach (var childNodeAtPosition in childNodesAtPosition)
            {
                if (childNodeAtPosition.Type == NodeType.Terminal)
                {
                    returnedNodes.Add(childNodeAtPosition);
                }
                else
                {
                    returnedNodes.AddRange(GetChildNodesAtPosition(childNodeAtPosition, position));
                }
            }

            if (returnedNodes.Count == 0)
            {
                returnedNodes.Add(node);
            }

            return(returnedNodes);
        }
        private static StatementGrammarNode GetNearestTerminalToPosition(StatementGrammarNode node, int position, Func <StatementGrammarNode, bool> filter = null)
        {
            if (node.Type == NodeType.Terminal)
            {
                throw new InvalidOperationException("This operation is available only at non-terminal nodes. ");
            }

            for (var index = node._childNodes.Count - 1; index >= 0; index--)
            {
                var childNode = node._childNodes[index];
                if (childNode.SourcePosition.IndexStart == SourcePosition.Empty.IndexStart || childNode.SourcePosition.IndexStart > position)
                {
                    continue;
                }

                if (filter == null || filter(childNode))
                {
                    return(childNode.Type == NodeType.Terminal
                                                ? childNode
                                                : GetNearestTerminalToPosition(childNode, position, filter));
                }

                var precedingTerminal = childNode;
                do
                {
                    precedingTerminal = precedingTerminal.PrecedingTerminal;
                }while (precedingTerminal != null && !filter(precedingTerminal));

                return(precedingTerminal);
            }

            return(null);
        }
Example #5
0
		public StatementCommentNode(StatementGrammarNode parentNode, IToken token)
			: base(parentNode?.Statement, token)
		{
			if (token == null)
				throw new ArgumentNullException(nameof(token));

			ParentNode = parentNode;
		}
        private IReadOnlyList <StatementGrammarNode> GatherTerminals()
        {
            var statementGrammarNodes = new StatementGrammarNode[_terminalCount];
            var offset = 0;

            GatherNodeTerminals(this, statementGrammarNodes, ref offset);

            return(_terminals = statementGrammarNodes);
        }
        public StatementCommentNode(StatementGrammarNode parentNode, IToken token)
            : base(parentNode?.Statement, token)
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            ParentNode = parentNode;
        }
 private static void GatherNodeTerminals(StatementGrammarNode childNode, StatementGrammarNode[] targetArray, ref int offset)
 {
     if (childNode.Type == NodeType.Terminal)
     {
         targetArray[offset++] = childNode;
     }
     else
     {
         GatherChildNodeTerminals(childNode, targetArray, ref offset);
     }
 }
        private static StatementGrammarNode GetFollowingNode(StatementGrammarNode node)
        {
            if (node.ParentNode == null)
            {
                return(null);
            }

            var index = node.ParentNode._childNodes.IndexOf(node) + 1;

            return(index < node.ParentNode._childNodes.Count
                                ? node.ParentNode._childNodes[index]
                                : GetFollowingNode(node.ParentNode));
        }
Example #10
0
        public static IEnumerable <StatementGrammarNode> GetAllChainedClausesByPath(StatementGrammarNode initialSearchedClause, Func <StatementGrammarNode, StatementGrammarNode> getChainedRootFunction, params string[] chainNonTerminalIds)
        {
            while (initialSearchedClause != null)
            {
                yield return(initialSearchedClause);

                if (getChainedRootFunction != null)
                {
                    initialSearchedClause = getChainedRootFunction(initialSearchedClause);
                }

                initialSearchedClause = initialSearchedClause[chainNonTerminalIds];
            }
        }
Example #11
0
        private static StatementGrammarNode GetPrecedingNode(StatementGrammarNode node)
        {
            var parentNode = node.ParentNode;

            if (parentNode == null)
            {
                return(null);
            }

            var index = parentNode._childNodes.IndexOf(node) - 1;

            return(index >= 0
                                ? parentNode._childNodes[index]
                                : GetPrecedingNode(parentNode));
        }
Example #12
0
        public bool HasAncestor(StatementGrammarNode node, bool includeSelf = false)
        {
            if (includeSelf && this == node)
            {
                return(true);
            }

            var ancestorNode = this;

            do
            {
                ancestorNode = ancestorNode.ParentNode;
            }while (ancestorNode != null && ancestorNode != node);

            return(ancestorNode != null);
        }
Example #13
0
 private static IEnumerable <StatementGrammarNode> GetInvalidGrammerNodes(StatementGrammarNode node)
 {
     foreach (var childNode in node.ChildNodes)
     {
         if (childNode.IsGrammarValid)
         {
             var nestedNodes = GetInvalidGrammerNodes(childNode).Where(n => n.LastTerminalNode != null);
             foreach (var nestedChildMode in nestedNodes)
             {
                 yield return(nestedChildMode.LastTerminalNode.ParentNode);
             }
         }
         else if (childNode.LastTerminalNode != null)
         {
             yield return(childNode.LastTerminalNode.ParentNode);
         }
     }
 }
Example #14
0
        public StatementGrammarNode Clone()
        {
            var clonedNode =
                new StatementGrammarNode(Type, Statement, Token)
            {
                Id             = Id,
                IsRequired     = IsRequired,
                Level          = Level,
                IsGrammarValid = IsGrammarValid,
                IsReservedWord = IsReservedWord
            };

            if (Type == NodeType.NonTerminal)
            {
                clonedNode.AddChildNodes(CreateChildNodeClones());
            }

            return(clonedNode);
        }
Example #15
0
		private IReadOnlyList<StatementGrammarNode> GatherTerminals()
		{
			var statementGrammarNodes = new StatementGrammarNode[_terminalCount];
			var offset = 0;

			GatherNodeTerminals(this, statementGrammarNodes, ref offset);
			
			return _terminals = statementGrammarNodes;
		}
Example #16
0
		private static void GatherChildNodeTerminals(StatementGrammarNode node, StatementGrammarNode[] targetArray, ref int offset)
		{
			foreach (var childNode in node.ChildNodes)
			{
				GatherNodeTerminals(childNode, targetArray, ref offset);
			}
		}
Example #17
0
		public static IEnumerable<StatementGrammarNode> GetAllChainedClausesByPath(StatementGrammarNode initialSearchedClause, Func<StatementGrammarNode, StatementGrammarNode> getChainedRootFunction, params string[] chainNonTerminalIds)
		{
			while (initialSearchedClause != null)
			{
				yield return initialSearchedClause;

				if (getChainedRootFunction != null)
				{
					initialSearchedClause = getChainedRootFunction(initialSearchedClause);
				}

				initialSearchedClause = initialSearchedClause[chainNonTerminalIds];
			}
		}
Example #18
0
		private static IEnumerable<StatementGrammarNode> GetPathFilterDescendants(StatementGrammarNode node, Func<StatementGrammarNode, bool> pathFilter, params string[] descendantNodeIds)
		{
			foreach (var childNode in node.ChildNodes)
			{
				if (pathFilter != null && !pathFilter(childNode))
				{
					continue;
				}

				if (descendantNodeIds == null || descendantNodeIds.Length == 0 || descendantNodeIds.Contains(childNode.Id))
				{
					yield return childNode;
				}

				if (childNode.Type == NodeType.Terminal)
				{
					continue;
				}
				
				foreach (var descendant in GetPathFilterDescendants(childNode, pathFilter, descendantNodeIds))
				{
					yield return descendant;
				}
			}
		}
Example #19
0
		public bool HasAncestor(StatementGrammarNode node, bool includeSelf = false)
		{
			if (includeSelf && this == node)
			{
				return true;
			}

			var ancestorNode = this;
			do
			{
				ancestorNode = ancestorNode.ParentNode;
			}
			while (ancestorNode != null && ancestorNode != node);

			return ancestorNode != null;
		}
Example #20
0
		private static IEnumerable<StatementGrammarNode> GetChildNodesAtPosition(StatementGrammarNode node, int position)
		{
			var returnedNodes = new List<StatementGrammarNode>();
			if (node == null)
			{
				return returnedNodes;
			}

			var childNodesAtPosition = node.ChildNodes.Where(n => n.SourcePosition.ContainsIndex(position));
			foreach (var childNodeAtPosition in childNodesAtPosition)
			{
				if (childNodeAtPosition.Type == NodeType.Terminal)
				{
					returnedNodes.Add(childNodeAtPosition);
				}
				else
				{
					returnedNodes.AddRange(GetChildNodesAtPosition(childNodeAtPosition, position));
				}
			}

			if (returnedNodes.Count == 0)
			{
				returnedNodes.Add(node);
			}

			return returnedNodes;
		}
Example #21
0
 public int IndexOf(StatementGrammarNode childNode)
 {
     return(_childNodes.IndexOf(childNode));
 }
Example #22
0
		public int IndexOf(StatementGrammarNode childNode)
		{
			return _childNodes.IndexOf(childNode);
		}
Example #23
0
		private static StatementGrammarNode GetNearestTerminalToPosition(StatementGrammarNode node, int position, Func<StatementGrammarNode, bool> filter = null)
		{
			if (node.Type == NodeType.Terminal)
			{
				throw new InvalidOperationException("This operation is available only at non-terminal nodes. ");
			}

			for (var index = node._childNodes.Count - 1; index >= 0; index--)
			{
				var childNode = node._childNodes[index];
				if (childNode.SourcePosition.IndexStart == SourcePosition.Empty.IndexStart || childNode.SourcePosition.IndexStart > position)
				{
					continue;
				}

				if (filter == null || filter(childNode))
				{
					return childNode.Type == NodeType.Terminal
						? childNode
						: GetNearestTerminalToPosition(childNode, position, filter);
				}

				var precedingTerminal = childNode;
				do
				{
					precedingTerminal = precedingTerminal.PrecedingTerminal;
				}
				while (precedingTerminal != null && !filter(precedingTerminal));
				
				return precedingTerminal;
			}

			return null;
		}
Example #24
0
		private static StatementGrammarNode GetPrecedingNode(StatementGrammarNode node)
		{
			var parentNode = node.ParentNode;
			if (parentNode == null)
			{
				return null;
			}

			var index = parentNode._childNodes.IndexOf(node) - 1;
			return index >= 0
				? parentNode._childNodes[index]
				: GetPrecedingNode(parentNode);
		}
Example #25
0
		private static void GatherNodeTerminals(StatementGrammarNode childNode, StatementGrammarNode[] targetArray, ref int offset)
		{
			if (childNode.Type == NodeType.Terminal)
			{
				targetArray[offset++] = childNode;
			}
			else
			{
				GatherChildNodeTerminals(childNode, targetArray, ref offset);
			}
		}
Example #26
0
		private static StatementGrammarNode GetFollowingNode(StatementGrammarNode node)
		{
			if (node.ParentNode == null)
			{
				return null;
			}

			var index = node.ParentNode._childNodes.IndexOf(node) + 1;
			return index < node.ParentNode._childNodes.Count
				? node.ParentNode._childNodes[index]
				: GetFollowingNode(node.ParentNode);
		}
Example #27
0
		public StatementGrammarNode Clone()
		{
			var clonedNode =
				new StatementGrammarNode(Type, Statement, Token)
				{
					Id = Id,
					IsRequired = IsRequired,
					Level = Level,
					IsGrammarValid = IsGrammarValid,
					IsReservedWord = IsReservedWord
				};

			if (Type == NodeType.NonTerminal)
			{
				clonedNode.AddChildNodes(CreateChildNodeClones());
			}

			return clonedNode;
		}