public static TomlTable Build(StartNode node, TomlSettings settings) { TomlTable.RootTable rootTable = new TomlTable.RootTable(settings); TomlTable current = rootTable; if (node == null) { return(rootTable); } System.Collections.Generic.IEnumerable <ExpressionNode> expressions = node.Expressions(); foreach (ExpressionNode e in expressions) { switch (e) { case KeyValueExpressionNode kvn: var val = ToTomlValue(rootTable, kvn.Value.SyntaxNode()); val.AddComments(kvn); if (kvn.Key.SyntaxNode().IsDottedKey()) { var chain = KeyChain.FromSegments(kvn.Key.SyntaxNode().GetSegments()); var owner = FindOrCreateOwnerTableForTable(current, chain, out var last, TomlTable.TableTypes.Dotted); owner.AddRow(last, val); } else { current.AddRow(kvn.Key.SyntaxNode().ExpressionKey(), val); } break; case TableNode tn: current = CreateTableOrArrayOfTables(rootTable, tn); break; case CommentExpressionNode cen: var loc = rootTable.Rows.Any() ? CommentLocation.Append : CommentLocation.Prepend; foreach (var c in cen.Comments) { rootTable.AddComment(c.Value, loc); } break; default: throw new InvalidOperationException($"Encountered unexpected expression of type '{e.GetType()}'."); } } return(rootTable); }
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)); } }