Пример #1
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;
        }
        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);
        }
Пример #3
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);
        }
Пример #4
0
        public static IList<TomlComment> TryParsePreExpressionCommenst(TokenBuffer tokens)
        {
            var comments = new List<TomlComment>();
            while (tokens.TryExpect(TokenType.Comment))
            {
                comments.Add(new TomlComment(tokens.Consume().value, CommentLocation.Prepend));
                tokens.ConsumeAllNewlines();
            }

            tokens.ConsumeAllNewlines();

            return comments;
        }
Пример #5
0
        public static IList<TomlComment> TryParseComments(TokenBuffer tokens, CommentLocation location)
        {
            var comments = new List<TomlComment>();
            while (tokens.TryExpect(TokenType.Comment))
            {
                comments.Add(new TomlComment(tokens.Consume().value, location));
                tokens.ConsumeAllNewlines();
            }

            tokens.ConsumeAllNewlines();

            return comments;
        }
Пример #6
0
        public static IList <TomlComment> TryParsePreExpressionCommenst(TokenBuffer tokens)
        {
            var comments = new List <TomlComment>();

            while (tokens.TryExpect(TokenType.Comment))
            {
                comments.Add(new TomlComment(tokens.Consume().value, CommentLocation.Prepend));
                tokens.ConsumeAllNewlines();
            }

            tokens.ConsumeAllNewlines();

            return(comments);
        }
Пример #7
0
        public static IList <TomlComment> TryParseComments(TokenBuffer tokens, CommentLocation location)
        {
            var comments = new List <TomlComment>();

            while (tokens.TryExpect(TokenType.Comment))
            {
                comments.Add(new TomlComment(tokens.Consume().value, location));
                tokens.ConsumeAllNewlines();
            }

            tokens.ConsumeAllNewlines();

            return(comments);
        }
Пример #8
0
        public static TomlTable TryApply(TomlTable current, TomlTable.RootTable root, TokenBuffer tokens)
        {
            tokens.ConsumeAllNewlines();

            var preComments = CommentProduction.TryParsePreExpressionCommenst(tokens);
            var expressionToken = tokens.Peek();

            tokens.ConsumeAllNewlines();

            var arrayKeyChain = TomlArrayTableProduction.TryApply(tokens);
            if (arrayKeyChain != null)
            {
                var addTo = GetTargetTable(root, arrayKeyChain, CreateImplicitelyType.Table);
                var arr = GetExistingOrCreateAndAdd(addTo, arrayKeyChain.Last(), errorPosition: expressionToken);

                arr.Comments.AddRange(preComments);
                arr.Comments.AddRange(CommentProduction.TryParseAppendExpressionComments(expressionToken, tokens));

                var newArrayEntry = new TomlTable(root);
                arr.Add(newArrayEntry);
                return newArrayEntry;
            }

            var tableKeyChain = TomlTableProduction.TryApply(tokens);
            if (tableKeyChain != null)
            {
                var newTable = new TomlTable(root) { IsDefined = true };
                newTable.Comments.AddRange(preComments);
                newTable.Comments.AddRange(CommentProduction.TryParseAppendExpressionComments(expressionToken, tokens));

                var addTo = GetTargetTable(root, tableKeyChain, CreateImplicitelyType.Table);

                string name = tableKeyChain.Last();
                var existingRow = addTo.TryGetValue(name);
                if (existingRow == null)
                {
                    addTo.Add(name, newTable);
                }
                else
                {
                    var tbl = existingRow as TomlTable;
                    if (tbl.IsDefined)
                    {
                        throw new Exception($"Failed to add new table because the target table already contains a row with the key '{name}' of type '{existingRow.ReadableTypeName}'.");
                    }
                    else
                    {
                        tbl.IsDefined = true;
                        return tbl;
                    }
                }

                return newTable;
            }

            if (!tokens.End)
            {
                var kvp = KeyValuePairProduction.Apply(root, tokens);
                if (kvp != null)
                {
                    kvp.Item2.Comments.AddRange(preComments);
                    kvp.Item2.Comments.AddRange(CommentProduction.TryParseAppendExpressionComments(expressionToken, tokens));

                    current.Add(kvp.Item1, kvp.Item2);
                    return current;
                }
            }

            root.Comments.AddRange(preComments);

            return null;
        }
Пример #9
0
        public static TomlTable TryApply(TomlTable current, TomlTable.RootTable root, TokenBuffer tokens)
        {
            tokens.ConsumeAllNewlines();

            var preComments     = CommentProduction.TryParsePreExpressionCommenst(tokens);
            var expressionToken = tokens.Peek();

            tokens.ConsumeAllNewlines();

            var arrayKeyChain = TomlArrayTableProduction.TryApply(tokens);

            if (arrayKeyChain != null)
            {
                var addTo = GetTargetTable(root, arrayKeyChain, CreateImplicitelyType.Table);
                var arr   = GetExistingOrCreateAndAdd(addTo, arrayKeyChain.Last(), errorPosition: expressionToken);

                arr.Comments.AddRange(preComments);
                arr.Comments.AddRange(CommentProduction.TryParseAppendExpressionComments(expressionToken, tokens));

                var newArrayEntry = new TomlTable(root);
                arr.Add(newArrayEntry);
                return(newArrayEntry);
            }

            var tableKeyChain = TomlTableProduction.TryApply(tokens);

            if (tableKeyChain != null)
            {
                var newTable = new TomlTable(root)
                {
                    IsDefined = true
                };
                newTable.Comments.AddRange(preComments);
                newTable.Comments.AddRange(CommentProduction.TryParseAppendExpressionComments(expressionToken, tokens));

                var addTo = GetTargetTable(root, tableKeyChain, CreateImplicitelyType.Table);

                var key         = tableKeyChain.Last();
                var existingRow = addTo.TryGetValue(key);
                if (existingRow == null)
                {
                    addTo.AddRow(key, newTable);
                }
                else
                {
                    var tbl = existingRow as TomlTable;
                    if (tbl.IsDefined)
                    {
                        throw new Exception($"Failed to add new table because the target table already contains a row with the key '{key}' of type '{existingRow.ReadableTypeName}'.");
                    }
                    else
                    {
                        tbl.IsDefined = true;
                        return(tbl);
                    }
                }

                return(newTable);
            }

            if (!tokens.End)
            {
                var kvp = KeyValuePairProduction.Apply(root, tokens);
                if (kvp != null)
                {
                    kvp.Item2.Comments.AddRange(preComments);
                    kvp.Item2.Comments.AddRange(CommentProduction.TryParseAppendExpressionComments(expressionToken, tokens));

                    current.AddRow(kvp.Item1, kvp.Item2);
                    return(current);
                }
            }

            root.Comments.AddRange(preComments);

            return(null);
        }
Пример #10
0
        private static TomlArray ParseTomlArray(ITomlRoot root, TokenBuffer tokens)
        {
            TomlArray a;

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

            if (tokens.TryExpect(TokenType.RBrac))
            {
                // Empty array handled inside this if, else part can assume the array has values
                tokens.Consume();
                return new TomlArray(root);
            }
            else
            {
                List<TomlValue> values = new List<TomlValue>();
                var errPos = tokens.Peek();
                var v = ParseTomlValue(root, tokens);

                if (v == null)
                {
                    throw Parser.CreateParseError(errPos, $"Array value is missing.");
                }

                values.Add(v);

                while (!tokens.TryExpect(TokenType.RBrac))
                {
                    if (!tokens.TryExpectAndConsume(TokenType.Comma))
                    {
                        throw Parser.CreateParseError(tokens.Peek(), "Array not closed.");
                    }

                    tokens.ConsumeAllNewlines();
                    values.Last().Comments.AddRange(CommentProduction.TryParseComments(tokens, CommentLocation.Append));

                    if (!tokens.TryExpect(TokenType.RBrac))
                    {
                        var et = tokens.Peek();
                        v = ParseTomlValue(root, tokens);

                        if (v == null)
                        {
                            throw Parser.CreateParseError(et, $"Array value is missing.");
                        }

                        if (v.GetType() != values[0].GetType())
                        {
                            throw Parser.CreateParseError(et, $"Expected value of type '{values[0].ReadableTypeName}' but value of type '{v.ReadableTypeName}' was found.");
                        }

                        values.Add(v);
                        tokens.ConsumeAllNewlines();
                    }
                }

                a = new TomlArray(root, values.ToArray());
            }

            a.Last().Comments.AddRange(CommentProduction.TryParseComments(tokens, CommentLocation.Append));
            tokens.ExpectAndConsume(TokenType.RBrac);

            return a;
        }