Skip to content

Commit

Permalink
Allow whitespace in alignment (addresses #130)
Browse files Browse the repository at this point in the history
  • Loading branch information
untoldwind committed Mar 11, 2024
1 parent 6c69a9a commit a161e41
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
8 changes: 7 additions & 1 deletion TO2-Test/to2Core/string_interpolation.to2
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ test sync fn test_string_interpolation() -> Unit = {

assert_string("ab edfg with 3456", $"ab {a} with {b}")

assert_string("ab edfg with 3801", $"ab {a,-10} with {b + 345,10}")
assert_string("ab edfg with 3801", $"ab {a, -10} with {b + 345,10 }")

const c = 12.34

assert_string("a fixed point 12.3400", $"a fixed point {c,10 :N4}")

assert_string("a fixed point 12.3 ", $"a fixed point {c,-10:N1}")
}
2 changes: 1 addition & 1 deletion TO2/Parser/ScriptParser.StringInterpolation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class TO2ParserStringInterpolation {

private static readonly Parser<string> AlignOrFormat =
Recognize(Seq(
Opt(Char(',').Then(Opt(Char('-')).Then(Digits1))),
Opt(Char(',').Then(WhiteSpaces0).Then(Opt(Char('-')).Then(Digits1).Then(WhiteSpaces0))),
Opt(Char(':').Then(CharsExcept1("\\\"\r\n{}", "align or format")))));

private static Parser<StringInterpolation> StringInterpolationContent( Parser<Expression> expression) => Many0(Alt<StringInterpolationPart>(
Expand Down
2 changes: 1 addition & 1 deletion TO2/Runtime/CoreTesting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static void AssertFalse(bool actual) {
public static void AssertString(string expected, string actual) {
if (TestContext != null) TestContext.IncrAssertions();
else throw new AssertException("assert_string: called without context");
if (expected != actual) throw new AssertException($"assert_string: {expected} != {actual}");
if (expected != actual) throw new AssertException($"assert_string: \"{expected}\" != \"{actual}\"");
}

[KSFunction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const extendedEscapedStringChar = alt(

export const alignOrFormat = recognize(
seq(
opt(preceded(tag(","), preceded(opt(tag("-")), digits1))),
opt(preceded(tag(","), between(whitespace0, preceded(opt(tag("-")), digits1), whitespace0))),
opt(preceded(tag(":"), charsExcept1('\\"\r\n{}'))),
),
);
Expand Down

0 comments on commit a161e41

Please sign in to comment.