private static TomlObject ToTomlValue(ITomlRoot root, ValueNode node) { switch (node.Value.SyntaxNode()) { case TerminalNode tn: return(CreateValueFromTerminal(tn.Terminal)); case ArrayNode an: return(CreateArrayOrTableArray(an)); case InlineTableNode it: return(CreateInlineTable(it)); default: throw new Exception($"Cannot create TomlValue from node with type '{node.GetType()}'."); } TomlValue CreateValueFromTerminal(Token terminal) { switch (terminal.Type) { case TokenType.Integer: return(new TomlInt(root, Convert.ToInt64(Cleanup(terminal.Value, 0)), TomlInt.IntTypes.Decimal)); case TokenType.HexInteger: return(new TomlInt(root, Convert.ToInt64(Cleanup(terminal.Value, 2), 16), TomlInt.IntTypes.Hex)); case TokenType.BinaryInteger: return(new TomlInt(root, Convert.ToInt64(Cleanup(terminal.Value, 2), 2), TomlInt.IntTypes.Binary)); case TokenType.OctalInteger: return(new TomlInt(root, Convert.ToInt64(Cleanup(terminal.Value, 2), 8), TomlInt.IntTypes.Octal)); case TokenType.Bool: return(new TomlBool(root, Convert.ToBoolean(terminal.Value))); case TokenType.String: return(new TomlString(root, terminal.Value.Unescape(terminal))); case TokenType.LiteralString: return(new TomlString(root, terminal.Value, TomlString.TypeOfString.Literal)); case TokenType.MultilineLiteralString: return(new TomlString(root, terminal.Value, TomlString.TypeOfString.MultilineLiteral)); case TokenType.MultilineString: return(new TomlString(root, terminal.Value, TomlString.TypeOfString.Multiline)); case TokenType.Float: return(TomlFloat.FromTerminal(root, terminal)); case TokenType.OffsetDateTime: return(TomlOffsetDateTime.Parse(root, terminal.Value)); case TokenType.LocalTime: return(TomlLocalTime.Parse(root, terminal.Value)); case TokenType.Duration: return(TomlDuration.Parse(root, terminal.Value)); case TokenType.LocalDate: return(TomlLocalDate.Parse(root, terminal.Value)); case TokenType.LocalDateTime: return(TomlLocalDateTime.Parse(root, terminal.Value)); default: throw new NotSupportedException(); } string Cleanup(string s, int sub) => s.Substring(sub).Replace("_", string.Empty); } TomlObject CreateArrayOrTableArray(ArrayNode array) { var values = CreateValues(array.GetValues()).ToList(); var tables = values.OfType <TomlTable>().ToList(); if (tables.Count > 0 && tables.Count == values.Count) { var ta = new TomlTableArray(root, tables); ta.AddComments(array); return(ta); } else if (tables.Count == 0) { var arr = new TomlArray(root, values.Cast <TomlValue>().ToArray()); arr.AddComments(array); return(arr); } else { throw new InvalidOperationException("Array is a mixture of a value array and a TOML table array."); } IEnumerable <TomlObject> CreateValues(IEnumerable <ValueNode> nodes) { var linked = nodes.Select(n => Tuple.Create(n, ToTomlValue(root, n))).ToList(); var expectedType = linked.DistinctBy(n => n.Item2.GetType()).FirstOrDefault(); var wrongType = linked.DistinctBy(n => n.Item2.GetType()).Skip(1).FirstOrDefault(); if (wrongType != null) { string msg = $"Expected array value of type '{expectedType.Item2.ReadableTypeName}' " + $"but value of type '{wrongType.Item2.ReadableTypeName}' was found.'"; throw ParseException.MessageForNode(wrongType.Item1, msg); } return(linked.Select(l => l.Item2)); } } TomlTable CreateInlineTable(InlineTableNode it) { TomlTable table = new TomlTable(root, TomlTable.TableTypes.Inline); table.AddComments(it); System.Collections.Generic.IEnumerable <KeyValueExpressionNode> expressions = it.GetExpressions(); foreach (KeyValueExpressionNode e in expressions) { table.AddRow(e.Key.SyntaxNode().ExpressionKey(), ToTomlValue(root, e.Value.SyntaxNode())); } return(table); } }
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.AddComments(prep); a.AddComments(CommentProduction.TryParseComments(tokens, CommentLocation.Append)); return(a); } else { List <TomlValue> values = new List <TomlValue>(); // Parse first !required! array value var v = ParseArrayValue(); v.AddComments(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.AddComments(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().AddComments(CommentProduction.TryParseComments(tokens, CommentLocation.Append)); tokens.ExpectAndConsume(TokenType.RBrac); a.AddComments(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.AddComments(prepComments); value.AddComments(CommentProduction.TryParseComments(tokens, CommentLocation.Append)); return(value); } } return(a); }