diff --git a/src/Visp.Compiler/Syntax/Syntax.fs b/src/Visp.Compiler/Syntax/Syntax.fs index 92bf425..b4b3289 100644 --- a/src/Visp.Compiler/Syntax/Syntax.fs +++ b/src/Visp.Compiler/Syntax/Syntax.fs @@ -7,6 +7,7 @@ namespace rec Visp.Compiler.Syntax open Visp.Compiler.Writer open Visp.Compiler.Text open System.Diagnostics +open Visp.Common [] type Ident(text: string, range: range) = @@ -111,7 +112,7 @@ type SynStringKind = | Interpolated of plain: int | InterpolatedTripleQuote of triple: int -[] +[] type SynConst = | Unit | Nil @@ -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 [] diff --git a/tests/Visp.Compiler.UnitTests/TestUtils.fs b/tests/Visp.Compiler.UnitTests/TestUtils.fs index e33748d..0bdf602 100644 --- a/tests/Visp.Compiler.UnitTests/TestUtils.fs +++ b/tests/Visp.Compiler.UnitTests/TestUtils.fs @@ -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 -> 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 9857097..7292c37 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,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 @@ -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)), 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 d361839..77320f2 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,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 @@ -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 @@ -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)), @@ -100,40 +85,24 @@ FunctionCall (Symbol (SynSymbol sprintf), [Const - (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)); + (String ("\n []\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 - (" - [] - let ``interpolated {prefix} {testname}`` () = TestUtils.runTest "{path}" - ", - InterpolatedTripleQuote 1, (49,21--52,9)), (49,21--52,9)), None, (49,7--52,9)); + (String ("\n []\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 - (" - [] - 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 []\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 - (" - [] - 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 []\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 +, diff --git a/tests/Visp.Compiler.UnitTests/parsing-snapshots/tests_parsing_strings-interpolation-0.structured output.verified.txt b/tests/Visp.Compiler.UnitTests/parsing-snapshots/tests_parsing_strings-interpolation-0.structured output.verified.txt index aa66f53..e2b3b5f 100644 --- a/tests/Visp.Compiler.UnitTests/parsing-snapshots/tests_parsing_strings-interpolation-0.structured output.verified.txt +++ b/tests/Visp.Compiler.UnitTests/parsing-snapshots/tests_parsing_strings-interpolation-0.structured output.verified.txt @@ -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 @@ -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),