private static TomlTable CreateTableArray(TomlTable.RootTable root, TableArrayNode tableArray, IHasComments comments) { System.Collections.Generic.IEnumerable <TerminalNode> keySegments = tableArray.Key.SyntaxNode().GetSegments(); KeyChain chain = KeyChain.FromSegments(keySegments); if (chain.IsEmpty) { throw new InvalidOperationException("Empty TOML key is not allowed."); } TomlTable owner = FindOrCreateOwnerTableForTableArray(root, chain, out TomlKey last); TomlObject existing = owner.TryGetValue(last); if (existing != null && existing is TomlTableArray existingArray) { TomlTable newTable = new TomlTable(root); existingArray.Add(newTable); newTable.AddComments(comments); return(newTable); } else if (existing == null) { TomlTableArray newTableArray = new TomlTableArray(root); owner.AddRow(last, newTableArray); TomlTable newTable = new TomlTable(root); newTable.AddComments(comments); newTableArray.Add(newTable); return(newTable); } else { throw new InvalidOperationException($"Cannot define table array '{last}' as an object of type " + $"'{existing.ReadableTypeName}' exists already."); } }
private static TomlTable FindOrCreateOwnerTableForTableArray(TomlTable current, KeyChain keys, out TomlKey last) { if (keys.IsLastSegment) { last = keys.Key; return(current); } TomlObject row = current.TryGetValue(keys.Key); if (row != null) { if (row is TomlTable tbl) { return(FindOrCreateOwnerTableForTable(tbl, keys.Next, out last)); } else if (row is TomlTableArray ta) { return(FindOrCreateOwnerTableForTableArray(ta.Items.Last(), keys.Next, out last)); } else { throw new InvalidOperationException( $"Key '{keys}' corresponds to a TOML object hat is not of type TOML table or TOML table array."); } } else { TomlTableArray a = new TomlTableArray(current.Root); TomlTable t = new TomlTable(current.Root); a.Add(t); current.AddRow(keys.Key, a); return(FindOrCreateOwnerTableForTableArray(t, keys.Next, out last)); } }
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; }
public void WriteTomlTableArray(TomlKey key, TomlTableArray tableArray) { this.WritePrependComments(tableArray); const string assignment = " = [ "; this.writer.Write(key.ToString()); this.writer.Write(assignment); int indentLen = key.ToString().Length + assignment.Length; string indent = new string(' ', indentLen); for (int i = 0; i < tableArray.Items.Count; i++) { this.WriteInlineTableBody(tableArray.Items[i]); if (i < tableArray.Items.Count - 1) { this.writer.Write(","); this.writer.WriteLine(); this.writer.Write(indent); } } this.writer.WriteLine(" ]"); }
public static TomlTable TransformToSourceTable(this TomlTable table, IConfigSource source, TomlObject rootSource = null) { table.CheckNotNull(nameof(table)); var sourcesTable = rootSource == null ? Toml.Create(table.TableType) : TomlObjectFactory.CreateEmptyAttachedTable(rootSource, table.TableType); foreach (var r in table.Rows) { if (r.Value.TomlType == TomlObjectType.Table) { sourcesTable[r.Key] = ((TomlTable)r.Value).TransformToSourceTable(source, sourcesTable); } else if (r.Value.TomlType == TomlObjectType.ArrayOfTables) { var arr = (TomlTableArray)r.Value; var sourcesArray = new TomlTableArray(table.Root, arr.Items.Select(t => t.TransformToSourceTable(source, sourcesTable))); sourcesTable[r.Key] = sourcesArray; } else { sourcesTable[r.Key] = new TomlSource(sourcesTable.Root, source); } } return(sourcesTable); }
private List <TimestampDefinition> ConvertTimeStampList(ITomlRoot root, TomlTableArray tomlTableArray) { return(tomlTableArray .Items .Select(t => CreateTimestampFromTable(root, t)) .ToList()); }
public static TomlTableArray Clone(this TomlTableArray source) { source.CheckNotNull(nameof(source)); var cloned = new TomlTableArray(source.Root, source.Items.Select(i => i.Clone())); return cloned; }
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); }
private static void ProcessTableArray(Dictionary <string, string> dict, TomlTableArray tableArray, string keyPrefix) { for (int i = 0; i < tableArray.Items.Count; i++) { TomlTable table = tableArray.Items[i]; ProcessTable(dict, table, $"{keyPrefix}:{i}:"); } }
public static TomlTableArray Clone(this TomlTableArray source) { source.CheckNotNull(nameof(source)); var cloned = new TomlTableArray(source.Root, source.Items.Select(i => i.Clone())); return(cloned); }
public void Visit(TomlTableArray tableArray) { for (int i = 0; i < tableArray.Count; i++) { PushContext(i.ToString()); tableArray[i].Visit(this); PopContext(); } }
internal static string WriteTomlFragment(TomlTableArray array) { return(WriteIntoStream(WriteTomlTableArray)); void WriteTomlTableArray(FormattingStreamWriter stream) { var writer = new TomlTableWriter(stream, array.Root.Settings); writer.WriteTomlTableArray(string.Empty, KeyNotAvailable, array); } }
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); }
private static TomlTable GetExistingOrCreateAndAddTableArray(TomlTable tbl, TomlKey key) { Func <TomlTable, TomlTable> createNew = (e) => { var array = new TomlTableArray(tbl.Root); var newTable = new TomlTable(tbl.Root); array.Add(newTable); tbl.AddRow(key, array); return(newTable); }; return(GetExistinOrCreateAndAdd(tbl, key, createNew)); }
void ITomlObjectVisitor.Visit(TomlTableArray a) { sb.Append("["); for (int i = 0; i < a.Count - 1; i++) { a[i].Visit(this); this.sb.Append(","); } if (a.Count > 0) { a[a.Count - 1].Visit(this); } this.sb.Append("]"); }
private void WriteTomlTableArray(string parentKey, TomlKey key, TomlTableArray tableArray) { if (IsInlineTomlTableArray(tableArray)) { var inlineWriter = new TomlInlineTableWriter(this.writer, this.settings); inlineWriter.WriteTomlTableArray(key, tableArray); } else { this.WritePrependComments(tableArray); foreach (var t in tableArray.Items) { this.writer.Write("[["); this.writer.Write(parentKey + key.ToString()); this.writer.Write("]]"); this.writer.WriteLine(); this.WriteAppendComments(tableArray); this.WriteTableRows(CombineKey(parentKey, key), t); } } }
private static TomlTableArray GetExistingOrCreateAndAdd(TomlTable target, TomlKey key, Token errorPosition) { TomlObject existing = target.TryGetValue(key); var typed = existing as TomlTableArray; if (existing != null && typed == null) { throw Parser.CreateParseError( errorPosition, $"Cannot create array of tables with key '{key}' because there already is an row with that key of type '{existing.ReadableTypeName}'."); } else if (typed != null) { return(typed); } var newTableArray = new TomlTableArray(target.Root); target.AddRow(key, newTableArray); return(newTableArray); }
private static TomlTableArray GetExistingOrCreateAndAdd(TomlTable target, string name, Token errorPosition) { TomlObject existing = target.TryGetValue(name); var typed = existing as TomlTableArray; if (existing != null && typed == null) { throw Parser.CreateParseError( errorPosition, $"Cannot create array of tables with name '{name}' because there already is an row with that key of type '{existing.ReadableTypeName}'."); } else if (typed != null) { return typed; } var newTableArray = new TomlTableArray(target.Root); target.Add(name, newTableArray); return newTableArray; }
private static TomlTable GetExistingOrCreateAndAddTableArray(TomlTable tbl, string key) { Func<TomlTable, TomlTable> createNew = (e) => { var array = new TomlTableArray(tbl.Root); var newTable = new TomlTable(tbl.Root); array.Add(newTable); tbl.Add(key, array); return newTable; }; return GetExistinOrCreateAndAdd(tbl, key, createNew); }
private void WriteTomlTableArray(string parentKey, string key, TomlTableArray tableArray) { this.WritePrependComments(tableArray); foreach (var t in tableArray.Items) { this.writer.Write("[["); this.writer.Write(parentKey + key); this.writer.Write("]]"); this.writer.WriteLine(); this.WriteAppendComments(tableArray); this.WriteTableRows(CombineKey(parentKey, key), t); } }
private static bool IsInlineTomlTableArray(TomlTableArray a) => a.Items.Any(t => t.TableType == TomlTable.TableTypes.Inline);
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); } }