public static DateTimeOffset?AsDateTimeOffset(this TomlValue value)
        {
            if (value is TomlDateTimeOffset dateTimeOffset)
            {
                return(dateTimeOffset.Value);
            }

            if (value is TomlLocalDateTime dateTime)
            {
                return(dateTime.Value);
            }

            if (value is TomlLocalDate localDate)
            {
                return(new DateTime(localDate.Year, localDate.Month, localDate.Day));
            }

            if (value is TomlLocalTime localTime)
            {
                return(new DateTime(1, 1, 1, localTime.Hours, localTime.Minutes, localTime.Seconds, localTime.Milliseconds));
            }


            return(null);
        }
Example #2
0
 private static void TomlValueToXml(TomlValue value, XmlElement node, XmlDocument document)
 {
     if (value is TomlTable table)
     {
         XmlElement tableNode = document.CreateElement(table.Name ?? "table");
         node.AppendChild(tableNode);
         foreach (System.Collections.Generic.KeyValuePair <String, TomlValue> i in table.pairs)
         {
             TomlValueToXml(i.Key, i.Value, tableNode, document);
         }
     }
     else if (value is TomlArray array)
     {
         XmlElement arrayNode = document.CreateElement("items");
         node.AppendChild(arrayNode);
         foreach (TomlValue i in array.Value)
         {
             TomlValueToXml("item", i, arrayNode, document);
         }
     }
     else
     {
         XmlAttribute attr = document.CreateAttribute("value");
         attr.Value = ExtractString(value);
         node.Attributes.Append(attr);
     }
 }
Example #3
0
        private static String ExtractString(TomlValue value)
        {
            if (value is TomlString tomlString)
            {
                return(tomlString.Value);
            }

            return(value.ToString());
        }
        public static String AsString(this TomlValue value)
        {
            if (value is TomlString tomlString)
            {
                return(tomlString.Value);
            }

            return(null);
        }
        public static Double?AsDouble(this TomlValue value)
        {
            if (value is TomlDouble tomlDouble)
            {
                return(tomlDouble.Value);
            }

            return(null);
        }
        public static Int64?AsInt(this TomlValue value)
        {
            if (value is TomlInt tomlInt)
            {
                return(tomlInt.Value);
            }

            return(null);
        }
        public static Boolean?AsBoolean(this TomlValue value)
        {
            if (value is TomlBool tomlInt)
            {
                return(tomlInt.Value);
            }

            return(null);
        }
Example #8
0
        public static void ToXml(TomlValue value, String resultFile)
        {
            XmlDocument document = new XmlDocument();

            document.LoadXml($"<?xml version=\"1.0\" encoding=\"UTF-8\"?><root></root>");

            TomlValueToXml(value, document.DocumentElement, document);

            document.Save(resultFile);
        }
Example #9
0
        private TomlValue ParsePlus()
        {
            TomlValue value = this.ParseValue();

            if (value is TomlInt || value is TomlDouble)
            {
                return(value);
            }

            throw new TomlSemanticException("expected numeric value", this.currentFile, this.currentLine);
        }
Example #10
0
        public TomlValue Eval(TomlValue table)
        {
            var tomlValue = result?.Eval(table) ?? table;

            if (tomlValue is TomlTable tomlTable)
            {
                return(tomlTable.pairs.ElementAt(index).Value);
            }
            else if (tomlValue is TomlArray tomlArray)
            {
                return(tomlArray.Value[index]);
            }

            throw new TPathException("indexation only support arrays and tables");
        }
Example #11
0
 public TomlValue Eval(TomlValue value)
 {
     if (value is TomlTable table)
     {
         return(new TomlArray(table.pairs.Values.ToList()));
     }
     else
     {
         List <TomlValue> result = new List <TomlValue>();
         foreach (var item in value.AsList())
         {
             result.AddRange(this.Eval(item).AsList());
         }
         return(new TomlArray(result));
     }
 }
Example #12
0
        private TomlValue ParseMinus()
        {
            TomlValue value = this.ParseValue();

            if (value is TomlInt ti)
            {
                return(new TomlInt(-ti.Value));
            }

            if (value is TomlDouble td)
            {
                return(new TomlDouble(-td.Value));
            }

            throw new TomlSemanticException("expected numeric value", this.currentFile, this.currentLine);
        }
        private static IEnumerable <object> ConvertContent(TomlValue value)
        {
            switch (value.Type)
            {
            case TomlItemType.Array:
                foreach (var a in (IEnumerable <ArrayItem>)value.Value)
                {
                    foreach (var c in a.Before)
                    {
                        yield return(new XComment(c.Text));
                    }

                    if (a.Value != null)
                    {
                        yield return(new XElement("item",
                                                  new XAttribute("type", XUtils.GetJsonTypeString(a.Value.Type)),
                                                  new XAttribute("toml", a.Value.Type.ToString()),
                                                  ConvertContent(a.Value)
                                                  ));
                    }

                    foreach (var c in a.After)
                    {
                        yield return(new XComment(c.Text));
                    }
                }
                break;

            case TomlItemType.InlineTable:
                foreach (var n in ToXNodes((IEnumerable <KeyValue>)value.Value))
                {
                    yield return(n);
                }
                break;

            default:
                yield return(value.Value);

                break;
            }
        }
Example #14
0
        private void ParseDottedKeyValuePair(TomlTable parentTable)
        {
            String    key;
            TomlTable table = parentTable;

            while (this.LookMatch(1, TokenType.DOT))
            {
                key   = this.Consume(TokenType.BAREKEY).Text;
                table = this.GetTableOrCreateDottedIfAbsent(key, table);
                this.Match(TokenType.DOT);
            }

            key = this.Consume(TokenType.BAREKEY).Text;

            this.Consume(TokenType.ASSIGNMENT);

            // There is no value
            if (this.Match(TokenType.NL) || this.Match(TokenType.EOF))
            {
                throw new TomlSyntaxException("unspecified value", this.currentFile, this.currentLine);
            }

            TomlValue value = this.ParseValue();

            if (!this.Match(TokenType.NL) && !this.LookMatch(0, TokenType.EOF))
            {
                throw new TomlSyntaxException("there must be a newline or end of file after a key/value pair", this.currentFile, this.currentLine);
            }

            if (parentTable.Contains(key))
            {
                throw new TomlSemanticException($"key '{key}' is already defined in this table", this.currentFile, this.currentLine);
            }

            if (table.IsClosed)
            {
                throw new TomlSemanticException($"table '{table.Name}' is closed ", this.currentFile, this.currentLine);
            }

            table[key] = value;
        }
Example #15
0
        public TomlValue Eval(TomlValue value)
        {
            if (value is TomlTable table)
            {
                if (table.Contains(key))
                {
                    return(table[key]);
                }
                else
                {
                    throw new TPathException($"table does not contains key '{this.key}'");
                }
            }

            if (value is TomlArray array)
            {
                return(new TomlArray(array.Value.Select(i => this.Eval(i)).ToList()));
            }

            throw new TPathException($"operation . does not supported with type {value.GetType()}");
        }
Example #16
0
        private static void TomlValueToXml(String name, TomlValue value, XmlElement node, XmlDocument document)
        {
            XmlElement newNode;

            if (Lexer.IsValidId(name))
            {
                newNode = document.CreateElement(name);
            }
            else
            {
                newNode = document.CreateElement("pair");

                XmlAttribute attr = document.CreateAttribute("name");
                attr.Value = name;
                newNode.Attributes.Append(attr);
            }

            if (value is TomlTable table)
            {
                foreach (System.Collections.Generic.KeyValuePair <String, TomlValue> i in table.pairs)
                {
                    TomlValueToXml(i.Key, i.Value, newNode, document);
                }
            }
            else if (value is TomlArray array)
            {
                foreach (TomlValue i in array.Value)
                {
                    TomlValueToXml("item", i, newNode, document);
                }
            }
            else
            {
                XmlAttribute attr = document.CreateAttribute("value");
                attr.Value = ExtractString(value);
                newNode.Attributes.Append(attr);
            }

            node.AppendChild(newNode);
        }
Example #17
0
        private void ReadKeyValuePair(StringReader reader, out string key, out TomlValue value)
        {
            //Read the key
            key = ReadKey(reader);

            //Consume the equals sign, potentially with whitespace either side.
            reader.SkipWhitespace();
            if (!reader.ExpectAndConsume('='))
            {
                if (reader.TryPeek(out var shouldHaveBeenEquals))
                {
                    throw new TomlMissingEqualsException(_lineNumber, (char)shouldHaveBeenEquals);
                }

                throw new TomlEOFException(_lineNumber);
            }

            reader.SkipWhitespace();

            //Read the value
            value = ReadValue(reader);
        }
 public ArrayItem(TomlValue value, IEnumerable <Comment> before, IEnumerable <Comment> after)
 {
     this.Value  = value;
     this.Before = before ?? Enumerable.Empty <Comment>();
     this.After  = after ?? Enumerable.Empty <Comment>();
 }
 public KeyValue(IEnumerable <char> key, TomlValue value, Comment comment)
 {
     this.Key     = string.Concat(key);
     this.Value   = value;
     this.Comment = comment;
 }
Example #20
0
 private static Color ReadColor(TomlValue value)
 {
     float[] floats = MelonPreferences.Mapper.ReadArray <float>(value);
     if (floats == null || floats.Length != 4)
     {
         return(default);
Example #21
0
 public static List <TomlValue> AsList(this TomlValue value)
 {
     return((value as TomlArray).Value);
 }
Example #22
0
 public T FromToml <T>(TomlValue value) => TomletMain.To <T>(value);
Example #23
0
 private static LemonTuple <int, int> ReadLemonTupleInt(TomlValue value)
 {
     int[] ints = MelonPreferences.Mapper.ReadArray <int>(value);
     if (ints == null || ints.Length != 2)
     {
         return(default);
Example #24
0
 public T[] ReadArray <T>(TomlValue value) => TomletMain.To <T[]>(value);
Example #25
0
 public List <T> ReadList <T>(TomlValue value) => TomletMain.To <List <T> >(value);
Example #26
0
 public abstract void Load(TomlValue obj);
Example #27
0
        public static object To(Type what, TomlValue value)
        {
            var deserializer = TomlSerializationMethods.GetDeserializer(what) ?? TomlSerializationMethods.GetCompositeDeserializer(what);

            return(deserializer.Invoke(value));
        }
Example #28
0
        public static T To <T>(TomlValue value)
        {
            var deserializer = TomlSerializationMethods.GetDeserializer <T>() ?? TomlSerializationMethods.GetCompositeDeserializer <T>();

            return(deserializer.Invoke(value));
        }
Example #29
0
 public static Boolean IsBool(this TomlValue value)
 {
     return(value is TomlBool);
 }
Example #30
0
 public static string GetUnit(this TomlValue value, string def = "")
 => value.Unit ?? def;