Skip to content

Commit

Permalink
Add support for multiline inline tables (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Dec 22, 2024
1 parent b84c814 commit b2377a1
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 67 deletions.
17 changes: 17 additions & 0 deletions src/Tomlyn.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,22 @@ public void SimpleTest()
var doc = Toml.Parse(test);
Assert.AreEqual(test, doc.ToString());
}

[Test]
public void TestInlineArray()
{
var input = @"x = [1,
2,
3
]
";
var model = Toml.ToModel(input);
var array = model["x"] as TomlArray;
Assert.NotNull(array);
Assert.AreEqual(3, array.Count);
Assert.AreEqual(1, array[0]);
Assert.AreEqual(2, array[1]);
Assert.AreEqual(3, array[2]);
}
}
}
29 changes: 14 additions & 15 deletions src/Tomlyn.Tests/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@
using NUnit.Framework;
using Tomlyn.Model;

namespace Tomlyn.Tests
namespace Tomlyn.Tests;

public class SerializationTests
{
public class SerializationTests
[Test]
public void TestCrlfInMultilineString()
{
[Test]
public void TestCrlfInMultilineString()
var model = new TomlTable
{
PropertiesMetadata = new TomlPropertiesMetadata()
};
model.PropertiesMetadata.SetProperty("property", new TomlPropertyMetadata
{
var model = new TomlTable
{
PropertiesMetadata = new TomlPropertiesMetadata()
};
model.PropertiesMetadata.SetProperty("property", new TomlPropertyMetadata
{
DisplayKind = TomlPropertyDisplayKind.StringLiteralMulti
});
DisplayKind = TomlPropertyDisplayKind.StringLiteralMulti
});

model["property"] = "string\r\nwith\r\nnewlines";
model["property"] = "string\r\nwith\r\nnewlines";

Assert.AreEqual("property = '''string\r\nwith\r\nnewlines'''" + Environment.NewLine, Toml.FromModel(model));
}
Assert.AreEqual("property = '''string\r\nwith\r\nnewlines'''" + Environment.NewLine, Toml.FromModel(model));
}
}
122 changes: 77 additions & 45 deletions src/Tomlyn.Tests/TomlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,81 @@ public void TestDescendants()
test.sub.key = ""yes""
[[array]]
hello = true
".ReplaceLineEndings("\r\n");
";
var expected = """
trivia: (1,1)-(1,19) Comment "# This is a comment"
trivia: (1,20)-(1,21) NewLine "\r\n"
token: (2,1)-(2,1) "["
token: (2,2)-(2,6) "table"
token: (2,7)-(2,7) "]"
token: (2,8)-(2,9) "\r\n"
token: (3,1)-(3,3) "key"
trivia: (3,4)-(3,4) Whitespaces " "
token: (3,5)-(3,5) "="
trivia: (3,6)-(3,6) Whitespaces " "
token: (3,7)-(3,7) "1"
trivia: (3,8)-(3,8) Whitespaces " "
trivia: (3,9)-(3,33) Comment "# This is another comment"
token: (3,34)-(3,35) "\r\n"
token: (4,1)-(4,4) "test"
token: (4,5)-(4,5) "."
token: (4,6)-(4,8) "sub"
token: (4,9)-(4,9) "."
token: (4,10)-(4,12) "key"
trivia: (4,13)-(4,13) Whitespaces " "
token: (4,14)-(4,14) "="
trivia: (4,15)-(4,15) Whitespaces " "
token: (4,16)-(4,20) "\"yes\""
token: (4,21)-(4,22) "\r\n"
token: (5,1)-(5,2) "[["
token: (5,3)-(5,7) "array"
token: (5,8)-(5,9) "]]"
token: (5,10)-(5,11) "\r\n"
token: (6,1)-(6,5) "hello"
trivia: (6,6)-(6,6) Whitespaces " "
token: (6,7)-(6,7) "="
trivia: (6,8)-(6,8) Whitespaces " "
token: (6,9)-(6,12) "true"
token: (6,13)-(6,14) "\r\n"
""";
AssertDocumentSyntax(expected, input);
}

[Test]
public void TestInlineArray()
{
var input = @"x = [1,
2,
3
]
";
var expected = """
token: (1,1)-(1,1) "x"
trivia: (1,2)-(1,2) Whitespaces " "
token: (1,3)-(1,3) "="
trivia: (1,4)-(1,4) Whitespaces " "
token: (1,5)-(1,5) "["
token: (1,6)-(1,6) "1"
token: (1,7)-(1,7) ","
trivia: (1,8)-(1,9) NewLine "\r\n"
token: (2,1)-(2,1) "2"
token: (2,2)-(2,2) ","
trivia: (2,3)-(2,4) NewLine "\r\n"
token: (3,1)-(3,1) "3"
trivia: (3,2)-(3,3) NewLine "\r\n"
token: (4,1)-(4,1) "]"
token: (4,2)-(4,3) "\r\n"
""";
AssertDocumentSyntax(expected, input);
}

var tokens = Toml.Parse(input).Tokens().ToList();
private static void AssertDocumentSyntax(string expected, string input)
{
input = input.ReplaceLineEndings("\r\n");
var doc = Toml.Parse(input);
var tokens = doc.Tokens().ToList();
var builder = new StringBuilder();
foreach (var node in tokens)
{
Expand All @@ -49,48 +121,8 @@ public void TestDescendants()
builder.AppendLine($"token: {token.Span} {(token.Text is not null ? TomlFormatHelper.ToString(token.Text, TomlPropertyDisplayKind.Default) : string.Empty)}");
}
}


var actual = builder.ToString();
var expected = @"trivia: (1,1)-(1,19) Comment ""# This is a comment""
trivia: (1,20)-(1,21) NewLine ""\r\n""
token: (2,1)-(2,1) ""[""
token: (2,2)-(2,6) ""table""
token: (2,7)-(2,7) ""]""
token: (2,8)-(2,9) ""\r\n""
token: (3,1)-(3,3) ""key""
trivia: (3,4)-(3,4) Whitespaces "" ""
token: (3,5)-(3,5) ""=""
trivia: (3,6)-(3,6) Whitespaces "" ""
token: (3,7)-(3,7) ""1""
trivia: (3,8)-(3,8) Whitespaces "" ""
trivia: (3,9)-(3,33) Comment ""# This is another comment""
token: (3,34)-(3,35) ""\r\n""
token: (4,1)-(4,4) ""test""
token: (4,5)-(4,5) "".""
token: (4,6)-(4,8) ""sub""
token: (4,9)-(4,9) "".""
token: (4,10)-(4,12) ""key""
trivia: (4,13)-(4,13) Whitespaces "" ""
token: (4,14)-(4,14) ""=""
trivia: (4,15)-(4,15) Whitespaces "" ""
token: (4,16)-(4,20) ""\""yes\""""
token: (4,21)-(4,22) ""\r\n""
token: (5,1)-(5,2) ""[[""
token: (5,3)-(5,7) ""array""
token: (5,8)-(5,9) ""]]""
token: (5,10)-(5,11) ""\r\n""
token: (6,1)-(6,5) ""hello""
trivia: (6,6)-(6,6) Whitespaces "" ""
token: (6,7)-(6,7) ""=""
trivia: (6,8)-(6,8) Whitespaces "" ""
token: (6,9)-(6,12) ""true""
token: (6,13)-(6,14) ""\r\n""
";
//Console.WriteLine(builder.ToString());

AssertHelper.AreEqualNormalizeNewLine(expected, actual, true);

var docStr = builder.ToString();
AssertHelper.AreEqualNormalizeNewLine(expected, docStr, true);
}


}
7 changes: 0 additions & 7 deletions src/Tomlyn/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,15 +371,10 @@ private InlineTableSyntax ParseInlineTable()

var previousState = _lexer.State;
_lexer.State = LexerState.Key;
var previousLine = _hideNewLine;
_hideNewLine = false;
inlineTable.OpenBrace = EatToken(TokenKind.OpenBrace);
try
{

// toml-specs: Inline tables are intended to appear on a single line.
// -> So we don't hide newlines

bool? expectingEndOfInitializer = null;

var previousSpan = inlineTable.OpenBrace.Span;
Expand All @@ -392,7 +387,6 @@ private InlineTableSyntax ParseInlineTable()
LogError(previousSpan, $"Unexpected trailing comma `,` not permitted after the last key/value pair in an inline table.");
}

_hideNewLine = previousLine;
_lexer.State = previousState;
inlineTable.CloseBrace = EatToken();
break;
Expand Down Expand Up @@ -428,7 +422,6 @@ private InlineTableSyntax ParseInlineTable()
finally
{
_lexer.State = previousState;
_hideNewLine = previousLine;
}

return Close(inlineTable);
Expand Down

0 comments on commit b2377a1

Please sign in to comment.