Skip to content

Commit

Permalink
Fix categorization of newlines as whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed Jun 17, 2024
1 parent 39fa4b4 commit 9e5a008
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
17 changes: 16 additions & 1 deletion Broccolini.Test/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ namespace Broccolini.Test;

internal static class TestData
{
// This tests against a bug in the tokenizer that classifies whitespace + newline as whitespace.
private const string NewLineWithPrecedingWhiteSpace = "\t\n";

// TODO: test parsing with newline at the end
public static IEnumerable<string> GarbageNodes
=> Sequence.Return(
Expand Down Expand Up @@ -41,6 +44,7 @@ public static IEnumerable<SectionWithName> SectionsWithNames
.SelectMany(VaryClosingBracket)
.SelectMany(VaryTrailingWhitespace)
.SelectMany(VaryNewLines)
.SelectMany(VaryLeadingNewLines)
.Distinct();

public static IEnumerable<KeyValuePairWithKeyAndValue> KeyValuePairsWithKeyAndValue
Expand All @@ -62,7 +66,8 @@ public static IEnumerable<KeyValuePairWithKeyAndValue> KeyValuePairsWithKeyAndVa
new KeyValuePairWithKeyAndValue("# key = not a comment", "# key", "not a comment"),
new KeyValuePairWithKeyAndValue("key = \"quoted value'", "key", "\"quoted value'"),
new KeyValuePairWithKeyAndValue("key = 'quoted value\"", "key", "'quoted value\""))
.Concat(KeyValuePairsWithQuotes);
.Concat(KeyValuePairsWithQuotes)
.SelectMany(VaryLeadingNewLines);
// TODO: vary leading and trailing whitespace and line break

public static IEnumerable<string> NewLines => Sequence.Return("\r\n", "\r", "\n");
Expand Down Expand Up @@ -120,6 +125,16 @@ private static IEnumerable<SectionWithName> VaryLeadingWhitespace(SectionWithNam
sectionWithName,
sectionWithName with { Input = " \t\v\f " + sectionWithName.Input });

private static IEnumerable<SectionWithName> VaryLeadingNewLines(SectionWithName sectionWithName)
=> [sectionWithName,
sectionWithName with { Input = "\n" + sectionWithName.Input },
sectionWithName with { Input = "; comment" + NewLineWithPrecedingWhiteSpace + sectionWithName.Input }];

private static IEnumerable<KeyValuePairWithKeyAndValue> VaryLeadingNewLines(KeyValuePairWithKeyAndValue keyValuePair)
=> [keyValuePair,
keyValuePair with { Input = "\n" + keyValuePair.Input },
keyValuePair with { Input = "; comment" + NewLineWithPrecedingWhiteSpace + keyValuePair.Input }];

private static IEnumerable<SectionWithName> VaryTrailingWhitespace(SectionWithName sectionWithName)
=> Sequence.Return(
sectionWithName,
Expand Down
6 changes: 6 additions & 0 deletions Broccolini.Test/TokenizerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public void TokenizesWhiteSpace(char whitespace)
Assert.Single(Tokenize(whitespace.ToString()), new IniToken.WhiteSpace(whitespace.ToString()));
}

[Fact]
public void CorrectlyTokenizesNewLineFollowingWhitesSpace()
{
Assert.Equal([new IniToken.WhiteSpace("\t"), new IniToken.NewLine("\n")], Tokenize("\t\n"));
}

private static TheoryData<char> GetWhiteSpaceData() => WhiteSpace.ToTheoryData();

private static TheoryData<string, string> GetConsecutiveNewLinesData()
Expand Down
2 changes: 1 addition & 1 deletion Broccolini/Tokenization/Rules/CharPredicates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ internal static class CharPredicates

public static bool IsNewLine(char c) => c is '\r' or '\n';

public static bool IsWhitespace(char c) => c <= ' ';
public static bool IsWhitespace(char c) => c <= ' ' && !IsNewLine(c);
}

0 comments on commit 9e5a008

Please sign in to comment.