예제 #1
0
        public static TamlDocument Parse(StreamReader reader)
        {
            //return ParseNonRecursive(reader);
            //var (_, parsedDocument) = ParseRecursive(0, reader); // We start parsing at the root (=0) level
            //return (TamlDocument)parsedDocument!;

            var lines = ReadLines(reader);

            var document = new TamlDocument();

            int currentLevel = 0;

            TamlKeyValuePair?parent = null;

            for (var i = 0; i < lines.Count; i++)
            {
                var(indent, tamlValue) = lines[i];

                var tamlKeyValuePair = tamlValue as TamlKeyValuePair;



                if (indent == 0)
                {
                    // line is root element
                    document.KeyValuePairs.Add(tamlKeyValuePair !);
                    currentLevel = 0;
                }
                else if (indent > currentLevel)
                {
                    // belongs to the parrent

                    parent = lines[i - 1].value as TamlKeyValuePair;

                    if (parent !.Value is null)
                    {
                        var newValue = new TamlArray();
                        newValue.AppendValue(new TamlValue(tamlKeyValuePair !.Key));
                        parent.Value = newValue;
                        currentLevel = indent;
                    }
                }
                else if (indent < currentLevel)
                {
                    // we have to go one indent level up
                    currentLevel = indent;
                    i--;
                }
                else if (indent == currentLevel && parent != null && parent.Value is TamlArray parrentArray)
                {
                    parrentArray.AppendValue(new TamlValue(tamlKeyValuePair !.Key));
                }
            }
            return(document);
        }
예제 #2
0
파일: Parser.cs 프로젝트: TGrannen/TAML
        public static TamlDocument Parse(StreamReader reader)
        {
            var parsedDoc = ReadLines(reader);
            var lines     = parsedDoc.Lines;

            var document = new TamlDocument();

            document.ProcessorDirectives = parsedDoc.ProcessorDirectives;

            int currentLevel = 0;

            var stack = new Stack <TamlKeyValuePair>();

            for (var i = 0; i < lines.Count; i++)
            {
                var(indent, currentPair) = lines[i];

                if (indent == 0)
                {
                    // line is root element
                    document.KeyValuePairs.Add(currentPair !);
                    currentLevel = 0;
                    continue;
                }

                if (indent > currentLevel)
                {
                    stack.Push(lines[i - 1].value);
                }
                else if (indent < currentLevel)
                {
                    stack.Pop();
                }

                TamlKeyValuePair?parent = stack.Count > 0 ? stack.Peek() : null;

                if (indent > currentLevel)
                {
                    if (parent !.Value is null)
                    {
                        var newArray = new TamlArray();
                        newArray.AppendValue(currentPair);
                        parent.Value = newArray;
                    }
                }
                else if (parent != null && parent.Value is TamlArray parentArray)
                {
                    parentArray.AppendValue(currentPair);
                }

                currentLevel = indent;
            }
            return(document);
        }