Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
 void ITomlObjectVisitor.Visit(TomlOffsetDateTime dt) =>
 sb.Append("{").Append("\"type\":\"datetime\", \"value\":\"").Append(XmlConvert.ToString(dt.Value.UtcDateTime, XmlDateTimeSerializationMode.Utc)).Append("\"}");
 public void Visit(TomlOffsetDateTime dt) => data[currentPath] = dt.Value.ToString(CultureInfo.InvariantCulture);