Skip to content

Commit

Permalink
Add token-snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vipentti committed Dec 23, 2023
1 parent 5475881 commit ace8b1f
Show file tree
Hide file tree
Showing 107 changed files with 8,502 additions and 21 deletions.
7 changes: 7 additions & 0 deletions src/Visp.Compiler/CoreParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ let state = { Todo = () }

reraise ()

let debugLexFile filePath =
let (stream, reader, lexbuf) = UnicodeFileAsLexbuf(filePath, None)
use _ = stream
use _ = reader
let args = mkDefaultLextArgs ()
ParseUtils.debugTokenOutput args lexbuf |> List.ofSeq

let getTokens str fileName =
let lexbuf = LexBuffer<_>.FromString str
lexbuf.EndPos <- Position.FirstLine fileName
Expand Down
10 changes: 8 additions & 2 deletions src/Visp.Compiler/ParseUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ let parseStringToExpr fileName str =
reraise ()

let debugTokenOutput args (lexbuf: LexBuffer<_>) =
let debugToken =
function
| STRING(text, kind, cont) ->
Syntax.StringWriterUtils.writeDebugStringType "STRING" text kind cont
| it -> sprintf "%A" it

seq {
let tokenizer = mkTokenizerWithArgs args

Expand All @@ -132,8 +138,8 @@ let debugTokenOutput args (lexbuf: LexBuffer<_>) =

yield
sprintf
"%A %A %i %i %A"
next
"%s %A %i %i %A"
(debugToken next)
args.mode
args.depth
args.ContextCount
Expand Down
42 changes: 23 additions & 19 deletions src/Visp.Compiler/Syntax/Syntax.fs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,28 @@ type SyntaxWriteUtilThreadStatics =
with get () = SyntaxWriteUtilThreadStatics.normalizeLineEndings
and set v = SyntaxWriteUtilThreadStatics.normalizeLineEndings <- v

module StringWriterUtils =
let inline writeDebugStringType (name: string) (text: string) kind range =
let sb = PooledStringBuilder.Get()
// Reserve capacity for the text + all the extra data being written
sb.EnsureCapacity(text.Length + 60) |> ignore
sb.Append name |> ignore
sb.Append " (\"" |> ignore

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

sb.Append "\", " |> ignore
sb.Append(sprintf "%A" kind) |> ignore
sb.Append(", ") |> ignore
sb.Append(sprintf "%A" range) |> ignore

sb.Append ")" |> ignore

sb.ToStringAndReturn()

[<NoEquality; NoComparison; RequireQualifiedAccess; StructuredFormatDisplay("{StructuredText}")>]
type SynConst =
| Unit
Expand Down Expand Up @@ -159,25 +181,7 @@ type SynConst =
| 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()
// Reserve capacity for the text + all the extra data being written
sb.EnsureCapacity(text.Length + 60) |> ignore
sb.Append "String (\"" |> ignore

for ch in text do
match ch with
| '\r' when SyntaxWriteUtilThreadStatics.NormalizeLineEndings -> ()
| 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()
| String(text, k, r) -> StringWriterUtils.writeDebugStringType "String" text k r

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

Expand Down
15 changes: 15 additions & 0 deletions tests/Visp.Compiler.UnitTests/TestUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ let inline private verify (out: string) dir (param: string) =
return! (task |> Async.AwaitTask)
}

let runTokenTest (name: string) =
async {
parseCoreLibs ()
let path = getVispFilePath name

try
let parsed = CoreParser.debugLexFile path
let nameParam = name.Replace('/', '_').Replace('\\', '_')
Syntax.SyntaxWriteUtilThreadStatics.NormalizeLineEndings <- true
let output = parsed |> String.concat "\n"
return! verify output "token-snapshots" nameParam
with :? ParseHelpers.SyntaxError as syn ->
return raise <| (LexHelpers.syntaxErrorToParseError syn)
}

let runStructuredOutputTest (name: string) =
async {
parseCoreLibs ()
Expand Down
Loading

0 comments on commit ace8b1f

Please sign in to comment.