/// <summary>Generates a Unique ID for a parse tree node</summary> /// <param name="tree">parse tree to generate the id for</param> /// <returns>ID for the node</returns> /// <remarks> /// This is useful when generating various graph visualization file formats as /// they normally need unique IDs for each node to maintain proper references /// between the nodes. /// </remarks> public static string GetUniqueNodeId(this IParseTree tree) { tree.ValidateNotNull(nameof(tree)); var bldr = new StringBuilder(tree.GetHashCode( ).ToString(CultureInfo.InvariantCulture)); if (tree.Parent != null) { bldr.Append(tree.Parent.GetChildIndex(tree)); bldr.Append(tree.Parent.GetUniqueNodeId( )); } return(bldr.ToString( )); }
/// <summary>Determines the index of a child item in the parent</summary> /// <param name="tree">Parent tree to find the item in</param> /// <param name="item">Item to determine the index of</param> /// <returns>Zero based index in the parent or -1 if the item is not a child of <paramref name="tree"/></returns> public static int GetChildIndex(this IParseTree tree, IParseTree item) { tree.ValidateNotNull(nameof(tree)); for (int i = 0; i < tree.ChildCount; ++i) { if (item == tree.GetChild(i)) { return(i); } } return(-1); }