void ITomlObjectVisitor.Visit(TomlFloat f) => sb.Append("{").Append("\"type\":\"float\", \"value\":\"").Append(f.Value.ToString(CultureInfo.InvariantCulture)).Append("\"}");
public void Visit(TomlFloat f) => data[currentPath] = f.Value.ToString(CultureInfo.InvariantCulture);
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); } }
void ITomlObjectVisitor.Visit(TomlFloat f) => this.table[this.currentKey] = f.Value;
public void Visit(TomlFloat f) => this.Item = f.Value;
/// <summary> /// Saves the current configuration out to the config file. /// </summary> public void Save() { // Physical Paramters TomlTable physicalParameters = Toml.Create(); // Passes TomlInt passes = physicalParameters.Add("Passes", Passes).Added; passes.AddComment(" The number of times to run the complete etching path", CommentLocation.Append); // PixelSize TomlFloat pixelSize = physicalParameters.Add("PixelSize", PixelSize).Added; pixelSize.AddComment(" The size of each pixel (mm per pixel)", CommentLocation.Append); // OriginX TomlFloat originX = physicalParameters.Add("OriginX", OriginX).Added; originX.AddComment(" The X coordinate of the top-left corner, in mm", CommentLocation.Append); // OriginY TomlFloat originY = physicalParameters.Add("OriginY", OriginY).Added; originY.AddComment(" The Y coordinate of the top-left corner, in mm", CommentLocation.Append); // ZHeight TomlFloat zHeight = physicalParameters.Add("ZHeight", ZHeight).Added; zHeight.AddComment(" The Z height to set the laser cutter during etching, in mm", CommentLocation.Append); // TravelSpeed TomlFloat travelSpeed = physicalParameters.Add("TravelSpeed", TravelSpeed).Added; travelSpeed.AddComment(" The speed to move the head between etching operations (when the laser is off), in mm per minute", CommentLocation.Append); // EtchSpeed TomlFloat etchSpeed = physicalParameters.Add("EtchSpeed", EtchSpeed).Added; etchSpeed.AddComment(" The speed to move the head during etching operations (when the laser is on), in mm per minute", CommentLocation.Append); // G-code Commands TomlTable gCodeCommands = Toml.Create(); // LaserOffCommand TomlString laserOffCommand = gCodeCommands.Add("LaserOffCommand", LaserOffCommand).Added; laserOffCommand.AddComment(" The G-code command to turn the laser off", CommentLocation.Append); // LaserLowCommand TomlString laserLowCommand = gCodeCommands.Add("LaserLowCommand", LaserLowCommand).Added; laserLowCommand.AddComment(" The G-code command to turn the laser on, but at a low power level (used for the pre-etch trace preview)", CommentLocation.Append); // LaserHighCommand TomlString laserHighCommand = gCodeCommands.Add("LaserHighCommand", LaserHighCommand).Added; laserHighCommand.AddComment(" The G-code command to turn the laser on full power during etching", CommentLocation.Append); // MoveCommand TomlString moveCommand = gCodeCommands.Add("MoveCommand", MoveCommand).Added; moveCommand.AddComment(" The G-code command to use during moves", CommentLocation.Append); // CommentMode TomlString commentMode = gCodeCommands.Add("CommentMode", CommentMode.ToString()).Added; commentMode.AddComment(" The G-code comment format to use (Semicolon or Parentheses)", CommentLocation.Append); // HomeXY TomlBool homeXY = gCodeCommands.Add("HomeXY", HomeXY).Added; homeXY.AddComment(" True to home the X and Y axes at the start, false to leave them where they were and assume that they are already homed", CommentLocation.Append); // Pre-Etch Trace Preview TomlTable preEtchTracePreview = Toml.Create(); // IsBoundaryPreviewEnabled TomlBool isBoundaryPreviewEnabled = preEtchTracePreview.Add("IsBoundaryPreviewEnabled", IsBoundaryPreviewEnabled).Added; isBoundaryPreviewEnabled.AddComment(" True to perform the pre-etch boundary trace preview, false to disable it and get right to etching", CommentLocation.Append); // PreviewDelay TomlInt previewDelay = preEtchTracePreview.Add("PreviewDelay", PreviewDelay).Added; previewDelay.AddComment(" The delay, in milliseconds, to wait at the start and end of the pre-etch trace preview", CommentLocation.Append); TomlTable settingsTable = Toml.Create(); settingsTable.Add("Physical-Parameters", physicalParameters); settingsTable.Add("G-Code-Commands", gCodeCommands); settingsTable.Add("Pre-Etch-Trace-Preview", preEtchTracePreview); Toml.WriteFile(settingsTable, ConfigFileName); }