コード例 #1
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));
     }
 }
コード例 #2
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);
        }
コード例 #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
ファイル: ValueProduction.cs プロジェクト: mister-walter/Nett
        private static TomlValue ParseTomlValue(ITomlRoot root, TokenBuffer tokens)
        {
            if (tokens.TryExpect(TokenType.Integer))
            {
                return(ParseTomlInt(root, tokens));
            }
            else if (tokens.TryExpect(TokenType.Float))
            {
                return(ParseTomlFloat(root, tokens));
            }
            else if (tokens.TryExpect(TokenType.DateTime))
            {
                return(TomlDateTime.Parse(root, tokens.Consume().value));
            }
            else if (tokens.TryExpect(TokenType.Timespan))
            {
                return(new TomlTimeSpan(root, TimeSpan.Parse(tokens.Consume().value, CultureInfo.InvariantCulture)));
            }
            else if (tokens.TryExpect(TokenType.String))
            {
                return(ParseStringValue(root, tokens));
            }
            else if (tokens.TryExpect(TokenType.LiteralString))
            {
                return(ParseLiteralString(root, tokens));
            }
            else if (tokens.TryExpect(TokenType.MultilineString))
            {
                return(ParseMultilineString(root, tokens));
            }
            else if (tokens.TryExpect(TokenType.MultilineLiteralString))
            {
                return(ParseMultilineLiteralString(root, tokens));
            }
            else if (tokens.TryExpect(TokenType.Bool))
            {
                return(new TomlBool(root, bool.Parse(tokens.Consume().value)));
            }
            else if (tokens.TryExpect(TokenType.LBrac))
            {
                return(ParseTomlArray(root, tokens));
            }

            return(null);
        }
コード例 #5
0
ファイル: CommentProduction.cs プロジェクト: paiden/Nett
        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;
        }
コード例 #6
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);
        }
コード例 #7
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);
        }
コード例 #8
0
ファイル: CommentProduction.cs プロジェクト: paiden/Nett
        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;
        }
コード例 #9
0
ファイル: CommentProduction.cs プロジェクト: paiden/Nett
        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;
        }
コード例 #10
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);
        }
コード例 #11
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);
        }
コード例 #12
0
ファイル: TableKeyProduction.cs プロジェクト: paiden/Nett
        public static IList<string> Apply(TokenBuffer tokens)
        {
            List<string> keyChain = new List<string>();
            var key = KeyProduction.Apply(tokens);
            keyChain.Add(key);

            while (tokens.TryExpect(TokenType.Dot))
            {
                tokens.Consume();
                keyChain.Add(KeyProduction.TryApply(tokens));
            }

            return keyChain;
        }
コード例 #13
0
ファイル: TableKeyProduction.cs プロジェクト: mvacha/Nett
        public static IList <TomlKey> Apply(TokenBuffer tokens)
        {
            List <TomlKey> keyChain = new List <TomlKey>();
            var            key      = KeyProduction.Apply(tokens);

            keyChain.Add(key);

            while (tokens.TryExpect(TokenType.Dot))
            {
                tokens.Consume();
                keyChain.Add(KeyProduction.TryApply(tokens));
            }

            return(keyChain);
        }
コード例 #14
0
ファイル: InlineTableProduction.cs プロジェクト: mvacha/Nett
        public static TomlTable Apply(ITomlRoot root, TokenBuffer tokens)
        {
            TomlTable inlineTable = new TomlTable(root, TomlTable.TableTypes.Inline);

            tokens.ExpectAndConsume(TokenType.LCurly);

            if (!tokens.TryExpect(TokenType.RBrac))
            {
                var kvp = KeyValuePairProduction.Apply(root, tokens);
                inlineTable.AddRow(kvp.Item1, kvp.Item2);

                while (tokens.TryExpect(TokenType.Comma))
                {
                    tokens.Consume();
                    kvp = KeyValuePairProduction.Apply(root, tokens);
                    inlineTable.AddRow(kvp.Item1, kvp.Item2);
                }
            }

            tokens.ExpectAndConsume(TokenType.RCurly);
            return(inlineTable);
        }
コード例 #15
0
ファイル: InlineTableProduction.cs プロジェクト: paiden/Nett
        public static TomlTable Apply(ITomlRoot root, TokenBuffer tokens)
        {
            TomlTable inlineTable = new TomlTable(root, TomlTable.TableTypes.Inline);

            tokens.ExpectAndConsume(TokenType.LCurly);

            if (!tokens.TryExpect(TokenType.RBrac))
            {
                var kvp = KeyValuePairProduction.Apply(root, tokens);
                inlineTable.Add(kvp.Item1, kvp.Item2);

                while (tokens.TryExpect(TokenType.Comma))
                {
                    tokens.Consume();
                    kvp = KeyValuePairProduction.Apply(root, tokens);
                    inlineTable.Add(kvp.Item1, kvp.Item2);
                }
            }

            tokens.ExpectAndConsume(TokenType.RCurly);
            return inlineTable;
        }
コード例 #16
0
ファイル: ValueProduction.cs プロジェクト: mister-walter/Nett
        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);
        }
コード例 #17
0
ファイル: ValueProduction.cs プロジェクト: paiden/Nett
        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;
        }
コード例 #18
0
ファイル: ValueProduction.cs プロジェクト: paiden/Nett
        private static TomlValue ParseTomlValue(ITomlRoot root, TokenBuffer tokens)
        {
            if (tokens.TryExpect(TokenType.Integer)) { return ParseTomlInt(root, tokens); }
            else if (tokens.TryExpect(TokenType.Float)) { return ParseTomlFloat(root, tokens); }
            else if (tokens.TryExpect(TokenType.DateTime)) { return TomlDateTime.Parse(root, tokens.Consume().value); }
            else if (tokens.TryExpect(TokenType.Timespan)) { return new TomlTimeSpan(root, TimeSpan.Parse(tokens.Consume().value, CultureInfo.InvariantCulture)); }
            else if (tokens.TryExpect(TokenType.String)) { return ParseStringValue(root, tokens); }
            else if (tokens.TryExpect(TokenType.LiteralString)) { return ParseLiteralString(root, tokens); }
            else if (tokens.TryExpect(TokenType.MultilineString)) { return ParseMultilineString(root, tokens); }
            else if (tokens.TryExpect(TokenType.MultilineLiteralString)) { return ParseMultilineLiteralString(root, tokens); }
            else if (tokens.TryExpect(TokenType.Bool)) { return new TomlBool(root, bool.Parse(tokens.Consume().value)); }
            else if (tokens.TryExpect(TokenType.LBrac)) { return ParseTomlArray(root, tokens); }

            return null;
        }
コード例 #19
0
        private static TomlValue ParseTomlValue(ITomlRoot root, TokenBuffer tokens)
        {
            if (tokens.TryExpect(TokenType.Integer))
            {
                return(ParseTomlInt(root, tokens, long.TryParse));
            }
            else if (tokens.TryExpect(TokenType.HexInteger))
            {
                return(ParseTomlInt(root, tokens, TryParseHexInt));
            }
            else if (tokens.TryExpect(TokenType.OctalInteger))
            {
                return(ParseTomlInt(root, tokens, TryParseOctInt));
            }
            else if (tokens.TryExpect(TokenType.BinaryInteger))
            {
                return(ParseTomlInt(root, tokens, TryParseBinInt));
            }
            else if (tokens.TryExpect(TokenType.Float))
            {
                return(ParseTomlFloat(root, tokens));
            }
            else if (tokens.TryExpect(TokenType.DateTime))
            {
                return(TomlDateTime.Parse(root, tokens.Consume().value));
            }
            else if (tokens.TryExpect(TokenType.LocalTime))
            {
                return(ParseLocalDateTime(root, tokens.Consume().value));
            }
            else if (tokens.TryExpect(TokenType.Duration))
            {
                return(TomlDuration.Parse(root, tokens.Consume().value));
            }
            else if (tokens.TryExpect(TokenType.String))
            {
                return(ParseStringValue(root, tokens));
            }
            else if (tokens.TryExpect(TokenType.LiteralString))
            {
                return(ParseLiteralString(root, tokens));
            }
            else if (tokens.TryExpect(TokenType.MultilineString))
            {
                return(ParseMultilineString(root, tokens));
            }
            else if (tokens.TryExpect(TokenType.MultilineLiteralString))
            {
                return(ParseMultilineLiteralString(root, tokens));
            }
            else if (tokens.TryExpect(TokenType.Bool))
            {
                return(new TomlBool(root, bool.Parse(tokens.Consume().value)));
            }
            else if (tokens.TryExpect(TokenType.LBrac))
            {
                return(ParseTomlArray(root, tokens));
            }

            return(null);
        }