Пример #1
0
        public static TreeEntry ParseYaml(string yamlString)
        {
            var scr  = new StringCollectionReader(yamlString);
            var root = ParseYaml(scr);

            return(root);
        }
Пример #2
0
        private static TreeEntry ParseYaml(StringCollectionReader scr)
        {
            var rootNode = new TreeEntry();

            rootNode.IndentCount = 0;
            rootNode.Children    = new List <TreeEntry>();

            TreeEntry parentNode          = rootNode;
            TreeEntry previousNode        = rootNode;
            int       previousIndentCount = rootNode.IndentCount;
            string    previousLine        = string.Empty;
            bool      isStackTrace        = false;

            while (scr.CanRead)
            {
                var   line     = scr.ReadLine();
                Regex rg_hash  = new Regex(PATTERN_CANDIDATE);
                Regex rg_value = new Regex(PATTERN_VALUE);
                Match mc       = null;
                if (rg_hash.IsMatch(line))
                {
                    mc = rg_hash.Match(line);
                }
                else if (rg_value.IsMatch(line))
                {
                    mc = rg_value.Match(line);
                }

                bool isArray = IsArray(mc);
                int  indent  = GetIndentCount(line);
                if (isStackTrace && parentNode.IndentCount < indent)
                {
                    var currentNode = new TreeEntry(parentNode, null, indent, isArray);
                    currentNode.HeaderVisibility = System.Windows.Visibility.Hidden;
                    currentNode.Value            = line.Trim();
                    parentNode.Children.Add(currentNode);
                    previousNode = currentNode;
                }
                else if (isStackTrace && parentNode.IndentCount == indent)
                {
                    isStackTrace = false;
                    parentNode   = parentNode.Parent;

                    var currentNode = new TreeEntry(parentNode, mc.Groups["key"].Value, indent, isArray);
                    currentNode.Value = mc.Groups["value"].Value.Trim();
                    parentNode.Children.Add(currentNode);
                    previousNode = currentNode;
                }
                else if (previousIndentCount < indent)
                {
                    parentNode = previousNode;
                    var currentNode = new TreeEntry(parentNode, mc != null ? mc.Groups["key"].Value : null, indent, isArray);
                    currentNode.HeaderVisibility = System.Windows.Visibility.Hidden;
                    currentNode.Value            = mc != null ? mc.Groups["value"].Value.Trim() : line.Trim();
                    parentNode.Children.Add(currentNode);
                    previousNode = currentNode;
                    if (line.Contains("StackTrace"))
                    {
                        isStackTrace = true;
                        parentNode   = currentNode;
                    }
                }
                else if (previousIndentCount == indent) //loop
                {
                    var currentNode = new TreeEntry(parentNode, mc != null ? mc.Groups["key"].Value : null, indent, isArray);
                    currentNode.Value            = mc != null ? mc.Groups["value"].Value.Trim() : line.Trim();
                    currentNode.HeaderVisibility = mc != null ? System.Windows.Visibility.Visible : System.Windows.Visibility.Hidden;
                    parentNode.Children.Add(currentNode);
                    previousNode = currentNode;
                    if (line.Contains("StackTrace"))
                    {
                        isStackTrace = true;
                        parentNode   = currentNode;
                    }
                }
                else if (parentNode.IndentCount == indent) //ascending
                {
                    parentNode = parentNode.Parent;

                    var currentNode = new TreeEntry(parentNode, mc.Groups["key"].Value, indent, isArray);
                    currentNode.Value = mc.Groups["value"].Value.Trim();
                    parentNode.Children.Add(currentNode);
                    previousNode = currentNode;
                    if (line.Contains("StackTrace"))
                    {
                        isStackTrace = true;
                        parentNode   = currentNode;
                    }
                }

                previousIndentCount = indent;
            }

            return(rootNode);
        }