コード例 #1
0
ファイル: TableBuilder.cs プロジェクト: firleju/Nett
        private static TomlTable FindOrCreateOwnerTableForTableArray(TomlTable current, KeyChain keys, out TomlKey last)
        {
            if (keys.IsLastSegment)
            {
                last = keys.Key;
                return(current);
            }

            TomlObject row = current.TryGetValue(keys.Key);

            if (row != null)
            {
                if (row is TomlTable tbl)
                {
                    return(FindOrCreateOwnerTableForTable(tbl, keys.Next, out last));
                }
                else if (row is TomlTableArray ta)
                {
                    return(FindOrCreateOwnerTableForTableArray(ta.Items.Last(), keys.Next, out last));
                }
                else
                {
                    throw new InvalidOperationException(
                              $"Key '{keys}' corresponds to a TOML object hat is not of type TOML table or TOML table array.");
                }
            }
            else
            {
                TomlTableArray a = new TomlTableArray(current.Root);
                TomlTable      t = new TomlTable(current.Root);
                a.Add(t);
                current.AddRow(keys.Key, a);
                return(FindOrCreateOwnerTableForTableArray(t, keys.Next, out last));
            }
        }
コード例 #2
0
ファイル: TableBuilder.cs プロジェクト: firleju/Nett
        private static TomlTable CreateTableArray(TomlTable.RootTable root, TableArrayNode tableArray, IHasComments comments)
        {
            System.Collections.Generic.IEnumerable <TerminalNode> keySegments = tableArray.Key.SyntaxNode().GetSegments();
            KeyChain chain = KeyChain.FromSegments(keySegments);

            if (chain.IsEmpty)
            {
                throw new InvalidOperationException("Empty TOML key is not allowed.");
            }

            TomlTable  owner    = FindOrCreateOwnerTableForTableArray(root, chain, out TomlKey last);
            TomlObject existing = owner.TryGetValue(last);

            if (existing != null && existing is TomlTableArray existingArray)
            {
                TomlTable newTable = new TomlTable(root);
                existingArray.Add(newTable);
                newTable.AddComments(comments);
                return(newTable);
            }
            else if (existing == null)
            {
                TomlTableArray newTableArray = new TomlTableArray(root);
                owner.AddRow(last, newTableArray);
                TomlTable newTable = new TomlTable(root);
                newTable.AddComments(comments);
                newTableArray.Add(newTable);
                return(newTable);
            }
            else
            {
                throw new InvalidOperationException($"Cannot define table array '{last}' as an object of type "
                                                    + $"'{existing.ReadableTypeName}' exists already.");
            }
        }
コード例 #3
0
        private static TomlTableArray Apply(ITomlRoot root, TokenBuffer tokens)
        {
            tokens.ExpectAndConsume(TokenType.LBrac);
            tokens.ConsumeAllNewlines();

            var arr = new TomlTableArray(root);
            TomlTable tbl = null;
            while ((tbl = InlineTableProduction.TryApply(root, tokens)) != null)
            {
                arr.Add(tbl);

                if (tokens.TryExpect(TokenType.Comma))
                {
                    tokens.Consume();
                    tokens.ConsumeAllNewlines();
                }
                else
                {
                    tokens.ConsumeAllNewlines();
                    tokens.Expect(TokenType.RBrac);
                }
            }

            tokens.ConsumeAllNewlines();
            tokens.ExpectAndConsume(TokenType.RBrac);

            return arr;
        }
コード例 #4
0
        private static TomlTableArray Apply(ITomlRoot root, TokenBuffer tokens)
        {
            tokens.ExpectAndConsume(TokenType.LBrac);
            tokens.ConsumeAllNewlines();

            var       arr = new TomlTableArray(root);
            TomlTable tbl = null;

            while ((tbl = InlineTableProduction.TryApply(root, tokens)) != null)
            {
                arr.Add(tbl);

                if (tokens.TryExpect(TokenType.Comma))
                {
                    tokens.Consume();
                    tokens.ConsumeAllNewlines();
                }
                else
                {
                    tokens.ConsumeAllNewlines();
                    tokens.Expect(TokenType.RBrac);
                }
            }

            tokens.ConsumeAllNewlines();
            tokens.ExpectAndConsume(TokenType.RBrac);

            return(arr);
        }
コード例 #5
0
        private static TomlTableArray Apply(ITomlRoot root, TokenBuffer tokens)
        {
            tokens.ExpectAndConsume(TokenType.LBrac);
            tokens.ConsumeAllNewlines();

            var prep = CommentProduction.TryParseComments(tokens, CommentLocation.Prepend);

            var arr = new TomlTableArray(root);

            while (true)
            {
                var tbl = InlineTableProduction.TryApply(root, tokens);
                if (tbl == null)
                {
                    break;
                }

                if (prep != null)
                {
                    tbl.AddComments(prep);
                    prep = null;
                }

                arr.Add(tbl);

                if (tokens.TryExpect(TokenType.Comma))
                {
                    tokens.Consume();
                    tokens.ConsumeAllNewlines();
                    tbl.AddComments(CommentProduction.TryParseComments(tokens, CommentLocation.Append));
                }
                else
                {
                    break;
                }
            }

            tokens.ConsumeAllNewlines();

            if (arr.Count > 0)
            {
                arr.Last().AddComments(CommentProduction.TryParseComments(tokens, CommentLocation.Append));
            }
            else
            {
                arr.AddComments(prep);
            }

            tokens.ExpectAndConsume(TokenType.RBrac);
            arr.AddComments(CommentProduction.TryParseComments(tokens, CommentLocation.Append));

            return(arr);
        }
コード例 #6
0
        private static TomlTable GetExistingOrCreateAndAddTableArray(TomlTable tbl, TomlKey key)
        {
            Func <TomlTable, TomlTable> createNew = (e) =>
            {
                var array    = new TomlTableArray(tbl.Root);
                var newTable = new TomlTable(tbl.Root);
                array.Add(newTable);
                tbl.AddRow(key, array);
                return(newTable);
            };

            return(GetExistinOrCreateAndAdd(tbl, key, createNew));
        }
コード例 #7
0
ファイル: ExpressionProduction.cs プロジェクト: paiden/Nett
        private static TomlTable GetExistingOrCreateAndAddTableArray(TomlTable tbl, string key)
        {
            Func<TomlTable, TomlTable> createNew = (e) =>
            {
                var array = new TomlTableArray(tbl.Root);
                var newTable = new TomlTable(tbl.Root);
                array.Add(newTable);
                tbl.Add(key, array);
                return newTable;
            };

            return GetExistinOrCreateAndAdd(tbl, key, createNew);
        }