From 5d21083214048c733d7417942f69ae61fac7766c Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Sat, 23 Dec 2023 12:20:04 +0100 Subject: [PATCH] Optionally normalize line endings when outputting strings in structured format --- src/Visp.Compiler/Syntax/Syntax.fs | 12 ++++-- tests/Visp.Compiler.UnitTests/TestUtils.fs | 2 + ...g_strings-0.structured output.verified.txt | 20 +++++++-- ...g_strings-1.structured output.verified.txt | 41 +++++++++++++++---- 4 files changed, 60 insertions(+), 15 deletions(-) diff --git a/src/Visp.Compiler/Syntax/Syntax.fs b/src/Visp.Compiler/Syntax/Syntax.fs index b4b3289..5d097d6 100644 --- a/src/Visp.Compiler/Syntax/Syntax.fs +++ b/src/Visp.Compiler/Syntax/Syntax.fs @@ -4,7 +4,6 @@ namespace rec Visp.Compiler.Syntax -open Visp.Compiler.Writer open Visp.Compiler.Text open System.Diagnostics open Visp.Common @@ -112,6 +111,14 @@ type SynStringKind = | Interpolated of plain: int | InterpolatedTripleQuote of triple: int +type SyntaxWriteUtilThreadStatics = + [] + static val mutable private normalizeLineEndings: bool + + static member NormalizeLineEndings + with get () = SyntaxWriteUtilThreadStatics.normalizeLineEndings + and set v = SyntaxWriteUtilThreadStatics.normalizeLineEndings <- v + [] type SynConst = | Unit @@ -158,8 +165,7 @@ type SynConst = for ch in text do match ch with - | '\r' -> ignore (sb.Append "\\r") - | '\n' -> ignore (sb.Append "\\n") + | '\r' when SyntaxWriteUtilThreadStatics.NormalizeLineEndings -> () | it -> ignore (sb.Append it) sb.Append "\", " |> ignore diff --git a/tests/Visp.Compiler.UnitTests/TestUtils.fs b/tests/Visp.Compiler.UnitTests/TestUtils.fs index 0bdf602..f701332 100644 --- a/tests/Visp.Compiler.UnitTests/TestUtils.fs +++ b/tests/Visp.Compiler.UnitTests/TestUtils.fs @@ -54,6 +54,8 @@ let runStructuredOutputTest (name: string) = let nameParam = name.Replace('/', '_').Replace('\\', '_') + Syntax.SyntaxWriteUtilThreadStatics.NormalizeLineEndings <- true + let output = (sprintf "%120A" parsed).Replace("\\r\\n", "\\n").Replace("\r\n", "\n") return! verify output "parsing-snapshots" nameParam diff --git a/tests/Visp.Compiler.UnitTests/parsing-snapshots/tests_parsing_strings-0.structured output.verified.txt b/tests/Visp.Compiler.UnitTests/parsing-snapshots/tests_parsing_strings-0.structured output.verified.txt index 7292c37..b19ba9e 100644 --- a/tests/Visp.Compiler.UnitTests/parsing-snapshots/tests_parsing_strings-0.structured output.verified.txt +++ b/tests/Visp.Compiler.UnitTests/parsing-snapshots/tests_parsing_strings-0.structured output.verified.txt @@ -4,13 +4,21 @@ (FunctionCall (Symbol (SynSymbol printfn), [Const (String ("string is: '%s'", Regular, (9,9--9,26)), (9,9--9,26)); - Const (String ("\n this is\n a multiline\n string\n", Regular, (9,27--13,1)), (9,27--13,1))], - (9,1--13,1)), (9,0--13,2)); + Const (String (" + this is + a multiline + string +", Regular, (9,27--13,1)), (9,27--13,1))], (9,1--13,1)), + (9,0--13,2)); Expr (FunctionCall (Symbol (SynSymbol printfn), [Const (String ("string is: '%s'", Regular, (15,9--15,26)), (15,9--15,26)); - Const (String ("\n this is\n a multiline\n string\n", TripleQuote, (15,27--19,3)), (15,27--19,3))], + Const (String (" + this is + a multiline + string +", TripleQuote, (15,27--19,3)), (15,27--19,3))], (15,1--19,3)), (15,0--19,4)); Expr (FunctionDef @@ -20,7 +28,11 @@ FunctionCall (Symbol (SynSymbol sprintf), [Const - (String ("\nlet ``can parse %s`` =\n TestUtils.runTest %s\n |> Async.AwaitTask\n ", TripleQuote, (22,25--26,5)), + (String (" +let ``can parse %s`` = + TestUtils.runTest %s + |> Async.AwaitTask + ", TripleQuote, (22,25--26,5)), (22,25--26,5)); Symbol (SynSymbol path); Symbol (SynSymbol path)], (22,17--26,15)), None, (22,3--26,16)); Symbol (SynSymbol template)], (21,1--28,10)), (21,0--28,11)); Expr diff --git a/tests/Visp.Compiler.UnitTests/parsing-snapshots/tests_parsing_strings-1.structured output.verified.txt b/tests/Visp.Compiler.UnitTests/parsing-snapshots/tests_parsing_strings-1.structured output.verified.txt index 77320f2..e3f51a9 100644 --- a/tests/Visp.Compiler.UnitTests/parsing-snapshots/tests_parsing_strings-1.structured output.verified.txt +++ b/tests/Visp.Compiler.UnitTests/parsing-snapshots/tests_parsing_strings-1.structured output.verified.txt @@ -25,8 +25,10 @@ (FunctionCall (Symbol (SynSymbol printfn), [Const (String ("string is '%s'", Regular, (13,9--13,25)), (13,9--13,25)); - Const (String ("\n multiple lines are okay?\n", TripleQuote, (13,26--15,3)), (13,26--15,3))], - (13,1--15,3)), (13,0--15,4)); + Const (String (" + multiple lines are okay? +", TripleQuote, (13,26--15,3)), (13,26--15,3))], (13,1--15,3)), + (13,0--15,4)); Expr (Begin ([Begin @@ -35,7 +37,9 @@ (Symbol (SynSymbol printfn), [Const (String ("string is '%s'", Regular, (19,15--19,31)), (19,15--19,31)); Const - (String ("\n multiple lines are okay?\n ", TripleQuote, (19,32--21,9)), + (String (" + multiple lines are okay? + ", TripleQuote, (19,32--21,9)), (19,32--21,9))], (19,7--21,9))], Do, (18,5--21,10))], Do, (17,3--22,5))], Do, (16,1--22,6)), (16,0--22,7)); Expr @@ -46,7 +50,16 @@ (FunctionCall (Symbol (SynSymbol sprintf), [Const - (String ("\n // AUTO GENERATED\n // DO NOT MODIFY BY HAND\n\n namespace %s\n\n open Xunit\n\n module %s\n ", TripleQuote, (25,16--34,5)), + (String (" + // AUTO GENERATED + // DO NOT MODIFY BY HAND + + namespace %s + + open Xunit + + module %s + ", TripleQuote, (25,16--34,5)), (25,16--34,5)); Symbol (SynSymbol moduleName); Symbol (SynSymbol moduleName)], (25,8--34,27)), (25,7--34,28))], (25,3--34,28))], (24,1--35,5)), (24,0--35,6)); Expr @@ -85,23 +98,35 @@ FunctionCall (Symbol (SynSymbol sprintf), [Const - (String ("\n []\n let ``%s %s`` () = TestUtils.runTest "%s"\n ", TripleQuote, (45,29--48,9)), + (String (" + [] + let ``%s %s`` () = TestUtils.runTest "%s" + ", TripleQuote, (45,29--48,9)), (45,29--48,9)); Symbol (SynSymbol prefix); Symbol (SynSymbol testname); Symbol (SynSymbol path)], (45,21--48,30)), None, (45,7--48,31)); LetOrUse (Named (SynSymbol template2, (49,11--49,20)), Const - (String ("\n []\n let ``interpolated {prefix} {testname}`` () = TestUtils.runTest "{path}"\n ", InterpolatedTripleQuote 1, (49,21--52,9)), + (String (" + [] + let ``interpolated {prefix} {testname}`` () = TestUtils.runTest "{path}" + ", InterpolatedTripleQuote 1, (49,21--52,9)), (49,21--52,9)), None, (49,7--52,9)); LetOrUse (Named (SynSymbol template3, (53,11--53,20)), Const - (String ("\n []\n let ``interpolated {{prefix}} {{testname}}`` () = TestUtils.runTest "{{path}} {path}"\n ", InterpolatedTripleQuote 2, (53,21--56,9)), + (String (" + [] + let ``interpolated {{prefix}} {{testname}}`` () = TestUtils.runTest "{{path}} {path}" + ", InterpolatedTripleQuote 2, (53,21--56,9)), (53,21--56,9)), None, (53,7--56,9)); LetOrUse (Named (SynSymbol template4, (57,11--57,20)), Const - (String ("\n []\n let ``interpolated {{prefix}} {{testname}}`` () = TestUtils.runTest "{{(+ 1 2 3)}} {{(+ 1 2 3)}} {(+ 1 2 3)}"\n ", InterpolatedTripleQuote 2, (57,21--60,9)), + (String (" + [] + let ``interpolated {{prefix}} {{testname}}`` () = TestUtils.runTest "{{(+ 1 2 3)}} {{(+ 1 2 3)}} {(+ 1 2 3)}" + ", InterpolatedTripleQuote 2, (57,21--60,9)), (57,21--60,9)), None, (57,7--60,9)); Op (Infix