Skip to content

Commit

Permalink
Normalize SynConst.String structured output
Browse files Browse the repository at this point in the history
  • Loading branch information
vipentti committed Dec 23, 2023
1 parent 14abfc5 commit e8e5b3f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 72 deletions.
41 changes: 40 additions & 1 deletion src/Visp.Compiler/Syntax/Syntax.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace rec Visp.Compiler.Syntax
open Visp.Compiler.Writer
open Visp.Compiler.Text
open System.Diagnostics
open Visp.Common

[<Struct; NoEquality; NoComparison; DebuggerDisplay("{idText}({idRange})")>]
type Ident(text: string, range: range) =
Expand Down Expand Up @@ -111,7 +112,7 @@ type SynStringKind =
| Interpolated of plain: int
| InterpolatedTripleQuote of triple: int

[<NoEquality; NoComparison; RequireQualifiedAccess>]
[<NoEquality; NoComparison; RequireQualifiedAccess; StructuredFormatDisplay("{StructuredText}")>]
type SynConst =
| Unit
| Nil
Expand All @@ -132,6 +133,44 @@ type SynConst =
| Decimal of System.Decimal
| String of text: string * synStringKind: SynStringKind * range: range

member t.StructuredText =
match t with
| Unit -> "Unit"
| Nil -> "Nil"
| Bool it -> sprintf "Bool %A" it
| SByte it -> sprintf "SByte %A" it
| Byte it -> sprintf "Byte %A" it
| Int16 it -> sprintf "Int16 %A" it
| UInt16 it -> sprintf "UInt16 %A" it
| UInt32 it -> sprintf "UInt32 %A" it
| UInt64 it -> sprintf "UInt64 %A" it
| IntPtr it -> sprintf "IntPtr %A" it
| UIntPtr it -> sprintf "UIntPtr %A" it
| Single it -> sprintf "Single %A" it
| Double it -> sprintf "Double %A" it
| Int64 it -> sprintf "Int64 %A" it
| Int32 it -> sprintf "Int32 %A" it
| Char it -> sprintf "Char %A" it
| Decimal it -> sprintf "Decimal %A" it
| String(text, k, r) ->
let sb = PooledStringBuilder.Get()
sb.Append "String (\"" |> ignore

for ch in text do
match ch with
| '\r' -> ignore (sb.Append "\\r")
| '\n' -> ignore (sb.Append "\\n")
| it -> ignore (sb.Append it)

sb.Append "\", " |> ignore
sb.Append(sprintf "%A" k) |> ignore
sb.Append(", ") |> ignore
sb.Append(sprintf "%A" r) |> ignore

sb.Append ")" |> ignore

sb.ToStringAndReturn()

type SynTyped = SynTyped of name: SynSymbol * argtype: SynType * range: range

[<NoComparison; RequireQualifiedAccess>]
Expand Down
2 changes: 1 addition & 1 deletion tests/Visp.Compiler.UnitTests/TestUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ let runStructuredOutputTest (name: string) =

let nameParam = name.Replace('/', '_').Replace('\\', '_')

let output = (sprintf "%120A" parsed).Replace("\r\n", "\n")
let output = (sprintf "%120A" parsed).Replace("\\r\\n", "\\n").Replace("\r\n", "\n")

return! verify output "parsing-snapshots" nameParam
with :? ParseHelpers.SyntaxError as syn ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@
(FunctionCall
(Symbol (SynSymbol printfn),
[Const (String ("string is: '%s'", Regular, (9,9--9,26)), (9,9--9,26));
Const (String ("
this is
a multiline
string
", Regular, (9,27--13,1)), (9,27--13,1))], (9,1--13,1)),
(9,0--13,2));
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));
Expr
(FunctionCall
(Symbol (SynSymbol printfn),
[Const (String ("string is: '%s'", Regular, (15,9--15,26)), (15,9--15,26));
Const (String ("
this is
a multiline
string
", TripleQuote, (15,27--19,3)), (15,27--19,3))],
Const (String ("\n this is\n a multiline\n string\n", TripleQuote, (15,27--19,3)), (15,27--19,3))],
(15,1--19,3)), (15,0--19,4));
Expr
(FunctionDef
Expand All @@ -28,14 +20,9 @@
FunctionCall
(Symbol (SynSymbol sprintf),
[Const
(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));
(String ("\nlet ``can parse %s`` =\n TestUtils.runTest %s\n |> Async.AwaitTask\n ", 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
(LetOrUse
(Named (SynSymbol visp_result_todo, (30,0--30,49)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@
(FunctionCall
(Symbol (SynSymbol printfn),
[Const (String ("string is '%s'", Regular, (13,9--13,25)), (13,9--13,25));
Const (String ("
multiple lines are okay?
", TripleQuote, (13,26--15,3)), (13,26--15,3))], (13,1--15,3)),
(13,0--15,4));
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));
Expr
(Begin
([Begin
Expand All @@ -37,9 +35,7 @@
(Symbol (SynSymbol printfn),
[Const (String ("string is '%s'", Regular, (19,15--19,31)), (19,15--19,31));
Const
(String ("
multiple lines are okay?
", TripleQuote, (19,32--21,9)),
(String ("\n multiple lines are okay?\n ", 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 @@ -50,20 +46,9 @@
(FunctionCall
(Symbol (SynSymbol sprintf),
[Const
(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));
(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)),
(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
(LetOrUse
(Named (SynSymbol lf, (37,5--37,7)), Symbol (SynSymbol System.Environment.NewLine), None, (37,1--37,34)),
Expand Down Expand Up @@ -100,40 +85,24 @@
FunctionCall
(Symbol (SynSymbol sprintf),
[Const
(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));
(String ("\n [<Fact>]\n let ``%s %s`` () = TestUtils.runTest "%s"\n ", 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
("
[<Fact>]
let ``interpolated {prefix} {testname}`` () = TestUtils.runTest "{path}"
",
InterpolatedTripleQuote 1, (49,21--52,9)), (49,21--52,9)), None, (49,7--52,9));
(String ("\n [<Fact>]\n let ``interpolated {prefix} {testname}`` () = TestUtils.runTest "{path}"\n ", 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
("
[<Fact>]
let ``interpolated {{prefix}} {{testname}}`` () = TestUtils.runTest "{{path}} {path}"
",
InterpolatedTripleQuote 2, (53,21--56,9)), (53,21--56,9)), None, (53,7--56,9));
(String ("\n [<Fact>]\n let ``interpolated {{prefix}} {{testname}}`` () = TestUtils.runTest "{{path}} {path}"\n ", 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
("
[<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));
(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)),
(57,21--60,9)), None, (57,7--60,9));
Op
(Infix
(SynSymbol +,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
(Symbol (SynSymbol printfn),
[Const (String ("string is '%s'", Regular, (12,9--12,25)), (12,9--12,25));
Const
(String
("interpolated: {value} {{ value }} {{ value.Length }}", InterpolatedTripleQuote 2, (12,26--12,86)),
(String ("interpolated: {value} {{ value }} {{ value.Length }}", InterpolatedTripleQuote 2, (12,26--12,86)),
(12,26--12,86))], (12,1--12,86)), (12,0--12,87));
Expr
(LetOrUse
Expand All @@ -32,9 +31,8 @@
(Symbol (SynSymbol printfn),
[Const (String ("string is '%s'", Regular, (13,9--13,25)), (13,9--13,25));
Const
(String
("interpolated: {value} {{ value }} {{{ value }}} {{{ value.Length }}}", InterpolatedTripleQuote 3,
(13,26--13,103)), (13,26--13,103))], (13,1--13,103)), None, (13,0--13,104)), (13,0--13,104));
(String ("interpolated: {value} {{ value }} {{{ value }}} {{{ value.Length }}}", InterpolatedTripleQuote 3, (13,26--13,103)),
(13,26--13,103))], (13,1--13,103)), None, (13,0--13,104)), (13,0--13,104));
Expr
(FunctionCall
(Symbol (SynSymbol printfn),
Expand Down

0 comments on commit e8e5b3f

Please sign in to comment.