Exemple #1
0
        public static IList<string> Apply(TokenBuffer tokens)
        {
            tokens.ExpectAndConsume(TokenType.LBrac);
            IList<string> tableKeyChain = TableKeyProduction.Apply(tokens);
            tokens.ExpectAndConsume(TokenType.RBrac);

            if (!tokens.TryExpectAndConsume(TokenType.NewLine) && !tokens.TryExpectAndConsume(TokenType.Comment) && !tokens.End)
            {
                var msg = $"Expected newline after table specifier. "
                    + $"Token of type '{tokens.Peek().type}' with value '{tokens.Peek().value}' on same line.";
                throw Parser.CreateParseError(tokens.Peek(), msg);
            }

            return tableKeyChain;
        }
Exemple #2
0
        public static IList <TomlKey> Apply(TokenBuffer tokens)
        {
            tokens.ExpectAndConsume(TokenType.LBrac);
            IList <TomlKey> tableKeyChain = TableKeyProduction.Apply(tokens);

            tokens.ExpectAndConsume(TokenType.RBrac);

            if (!tokens.TryExpectAndConsume(TokenType.NewLine) && !tokens.TryExpectAndConsume(TokenType.Comment) && !tokens.End)
            {
                var msg = $"Expected newline after table specifier. "
                          + $"Token of type '{tokens.Peek().type}' with value '{tokens.Peek().value}' on same line.";
                throw Parser.CreateParseError(tokens.Peek(), msg);
            }

            return(tableKeyChain);
        }
Exemple #3
0
 private static TomlKey ApplyInternal(TokenBuffer tokens, bool required)
 {
     if (tokens.TryExpect(TokenType.BareKey) || tokens.TryExpect(TokenType.Integer))
     {
         return(new TomlKey(tokens.Consume().value, TomlKey.KeyType.Bare));
     }
     else if (tokens.TryExpect(TokenType.String))
     {
         return(new TomlKey(tokens.Consume().value, TomlKey.KeyType.Basic));
     }
     else if (tokens.TryExpect(TokenType.LiteralString))
     {
         return(new TomlKey(tokens.Consume().value, TomlKey.KeyType.Literal));
     }
     else if (required)
     {
         var t = tokens.Peek();
         if (t.value == "=")
         {
             throw Parser.CreateParseError(t, "Key is missing.");
         }
         else
         {
             throw Parser.CreateParseError(t, $"Failed to parse key because unexpected token '{t.value}' was found.");
         }
     }
     else
     {
         return(new TomlKey(string.Empty));
     }
 }
Exemple #4
0
        public static IList<TomlComment> TryParseAppendExpressionComments(Token lastExpressionToken, TokenBuffer tokens)
        {
            var comments = new List<TomlComment>();
            while (tokens.TryExpect(TokenType.Comment) && tokens.Peek().line == lastExpressionToken.line)
            {
                comments.Add(new TomlComment(tokens.Consume().value, CommentLocation.Append));
            }

            return comments;
        }
Exemple #5
0
        public static IList <TomlComment> TryParseAppendExpressionComments(Token lastExpressionToken, TokenBuffer tokens)
        {
            var comments = new List <TomlComment>();

            while (tokens.TryExpect(TokenType.Comment) && tokens.Peek().line == lastExpressionToken.line)
            {
                comments.Add(new TomlComment(tokens.Consume().value, CommentLocation.Append));
            }

            return(comments);
        }
Exemple #6
0
        public static TomlObject Apply(ITomlRoot root, TokenBuffer tokens)
        {
            var value = ParseTomlValue(root, tokens);

            if (value == null)
            {
                var t = tokens.Peek();
                if (t.IsEmpty || t.IsEof || t.IsNewLine)
                {
                    throw Parser.CreateParseError(t, "Value is missing.");
                }
                else
                {
                    string msg = $"Expected a TOML value while parsing key value pair."
                        + $" Token of type '{t.type}' with value '{t.value}' is invalid.";
                    throw Parser.CreateParseError(t, msg);
                }
            }

            return value;
        }
        public static TomlObject Apply(ITomlRoot root, TokenBuffer tokens)
        {
            var value = ParseTomlValue(root, tokens);

            if (value == null)
            {
                var t = tokens.Peek();
                if (t.IsEmpty || t.IsEof || t.IsNewLine)
                {
                    throw Parser.CreateParseError(t, "Value is missing.");
                }
                else
                {
                    string msg = $"Expected a TOML value while parsing key value pair."
                                 + $" Token of type '{t.type}' with value '{t.value}' is invalid.";
                    throw Parser.CreateParseError(t, msg);
                }
            }

            return(value);
        }
Exemple #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;
        }
        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);
        }
Exemple #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;
        }
Exemple #11
0
        private static TomlArray ParseTomlArray(ITomlRoot root, TokenBuffer tokens)
        {
            TomlArray a;
            var       prep = CommentProduction.TryParseComments(tokens, CommentLocation.Prepend).ToList();

            tokens.ExpectAndConsume(TokenType.LBrac);
            using (tokens.UseIgnoreNewlinesContext())
            {
                prep.AddRange(CommentProduction.TryParseComments(tokens, CommentLocation.Prepend));
                if (tokens.TryExpect(TokenType.RBrac))
                {
                    // Empty array handled inside this if, else part can assume the array has values
                    // Comments in an empty array are moved before the array at the moment.
                    // There currently does not exist a comment  location that will allow to write this comments correctly
                    // => Parse the real items correctly and do not create a parse error, but the comment will get lost
                    // on the next write.
                    tokens.Consume();
                    a = new TomlArray(root);
                    a.Comments.AddRange(prep);
                    return(a);
                }
                else
                {
                    List <TomlValue> values = new List <TomlValue>();

                    // Parse first !required! array value
                    var v = ParseArrayValue();
                    v.Comments.AddRange(prep);
                    values.Add(v);

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

                        // This comment is misplaced as we simply append it to the last value, but it does not belong to it
                        // Comments processing needs some tweaking/redesign in the future.
                        v.Comments.AddRange(CommentProduction.TryParseComments(tokens, CommentLocation.Append));

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

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

                            values.Add(v);
                        }
                    }

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

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

                TomlValue ParseArrayValue()
                {
                    var prepComments       = CommentProduction.TryParseComments(tokens, CommentLocation.Prepend);
                    var valueParseErrorPos = tokens.Peek();
                    var value = ParseTomlValue(root, tokens);

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

                    value.Comments.AddRange(prepComments);
                    value.Comments.AddRange(CommentProduction.TryParseComments(tokens, CommentLocation.Append));

                    return(value);
                }
            }

            return(a);
        }