Skip to content

Commit

Permalink
Optionally normalize line endings when outputting strings in structur…
Browse files Browse the repository at this point in the history
…ed format
  • Loading branch information
vipentti committed Dec 23, 2023
1 parent bd07bf3 commit 5d21083
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
12 changes: 9 additions & 3 deletions src/Visp.Compiler/Syntax/Syntax.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace rec Visp.Compiler.Syntax

open Visp.Compiler.Writer
open Visp.Compiler.Text
open System.Diagnostics
open Visp.Common
Expand Down Expand Up @@ -112,6 +111,14 @@ type SynStringKind =
| Interpolated of plain: int
| InterpolatedTripleQuote of triple: int

type SyntaxWriteUtilThreadStatics =
[<System.ThreadStatic; DefaultValue>]
static val mutable private normalizeLineEndings: bool

static member NormalizeLineEndings
with get () = SyntaxWriteUtilThreadStatics.normalizeLineEndings
and set v = SyntaxWriteUtilThreadStatics.normalizeLineEndings <- v

[<NoEquality; NoComparison; RequireQualifiedAccess; StructuredFormatDisplay("{StructuredText}")>]
type SynConst =
| Unit
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/Visp.Compiler.UnitTests/TestUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -85,23 +98,35 @@
FunctionCall
(Symbol (SynSymbol sprintf),
[Const
(String ("\n [<Fact>]\n let ``%s %s`` () = TestUtils.runTest "%s"\n ", TripleQuote, (45,29--48,9)),
(String ("
[<Fact>]
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 [<Fact>]\n let ``interpolated {prefix} {testname}`` () = TestUtils.runTest "{path}"\n ", InterpolatedTripleQuote 1, (49,21--52,9)),
(String ("
[<Fact>]
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 [<Fact>]\n let ``interpolated {{prefix}} {{testname}}`` () = TestUtils.runTest "{{path}} {path}"\n ", InterpolatedTripleQuote 2, (53,21--56,9)),
(String ("
[<Fact>]
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 [<Fact>]\n let ``interpolated {{prefix}} {{testname}}`` () = TestUtils.runTest "{{(+ 1 2 3)}} {{(+ 1 2 3)}} {(+ 1 2 3)}"\n ", InterpolatedTripleQuote 2, (57,21--60,9)),
(String ("
[<Fact>]
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
Expand Down

0 comments on commit 5d21083

Please sign in to comment.