From 34a478e0e32ca257554ba990269f194bd49d3edf Mon Sep 17 00:00:00 2001 From: Martin <29605222+Martin521@users.noreply.github.com> Date: Mon, 4 Nov 2024 14:46:54 +0100 Subject: [PATCH] fix OriginalLine (#17949) --- src/Compiler/Facilities/prim-lexing.fs | 4 +- .../CompilerDirectives/Line.fs | 61 +++++++++++++++++-- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/Compiler/Facilities/prim-lexing.fs b/src/Compiler/Facilities/prim-lexing.fs index d1b965f100f..c305052587c 100644 --- a/src/Compiler/Facilities/prim-lexing.fs +++ b/src/Compiler/Facilities/prim-lexing.fs @@ -238,13 +238,13 @@ type internal Position = Position(x.FileIndex, x.Line, x.OriginalLine, x.StartOfLineAbsoluteOffset, x.StartOfLineAbsoluteOffset - 1) member x.ApplyLineDirective(fileIdx, line) = - Position(fileIdx, line, x.OriginalLine, x.AbsoluteOffset, x.AbsoluteOffset) + Position(fileIdx, line, x.OriginalLine + 1, x.AbsoluteOffset, x.AbsoluteOffset) override p.ToString() = $"({p.Line},{p.Column})" static member Empty = Position() - static member FirstLine fileIdx = Position(fileIdx, 1, 0, 0, 0) + static member FirstLine fileIdx = Position(fileIdx, 1, 1, 0, 0) type internal LexBufferFiller<'Char> = LexBuffer<'Char> -> unit diff --git a/tests/FSharp.Compiler.ComponentTests/CompilerDirectives/Line.fs b/tests/FSharp.Compiler.ComponentTests/CompilerDirectives/Line.fs index 4844fb232b3..105670d1adb 100644 --- a/tests/FSharp.Compiler.ComponentTests/CompilerDirectives/Line.fs +++ b/tests/FSharp.Compiler.ComponentTests/CompilerDirectives/Line.fs @@ -2,16 +2,20 @@ namespace CompilerDirectives open Microsoft.FSharp.Control open Xunit -open FSharp.Test.Compiler +open Internal.Utilities +open FSharp.Compiler open FSharp.Compiler.CodeAnalysis -open FSharp.Compiler.Text +open FSharp.Compiler.DiagnosticsLogger +open FSharp.Compiler.Features +open FSharp.Compiler.Lexhelp open FSharp.Compiler.Syntax +open FSharp.Compiler.Text +open FSharp.Compiler.UnicodeLexing module Line = - let checker = FSharpChecker.Create() - let parse (source: string) = + let checker = FSharpChecker.Create() let langVersion = "preview" let sourceFileName = __SOURCE_FILE__ let parsingOptions = @@ -63,4 +67,51 @@ printfn "" | ParsedInput.SigFile _ -> failwith "unexpected: sig file" if exprRange <> expectedRange then failwith $"case{case}: expected: {expectedRange}, found {exprRange}" - \ No newline at end of file + + + + + + let private getTokens sourceText = + let langVersion = LanguageVersion.Default + let lexargs = + mkLexargs ( + [], + IndentationAwareSyntaxStatus(true, false), + LexResourceManager(), + [], + DiscardErrorsLogger, + PathMap.empty, + true + ) + let lexbuf = StringAsLexbuf(true, langVersion, None, sourceText) + resetLexbufPos "test.fs" lexbuf + let tokenizer _ = + let t = Lexer.token lexargs true lexbuf + let p = lexbuf.StartPos + t, FileIndex.fileOfFileIndex p.FileIndex, p.OriginalLine, p.Line + let isNotEof(t,_,_,_) = match t with Parser.EOF _ -> false | _ -> true + Seq.initInfinite tokenizer |> Seq.takeWhile isNotEof |> Seq.toList + + let private code = """ +1 +#line 5 "other.fs" +2 +#line 10 "test.fs" +3 +""" + + let private expected = [ + "test.fs", 2, 2 + "other.fs", 4, 5 + "test.fs", 6, 10 + ] + + [] + let checkOriginalLineNumbers() = + let tokens = getTokens code + Assert.Equal(expected.Length, tokens.Length) + for ((e_idx, e_oLine, e_line), (_, idx, oLine, line)) in List.zip expected tokens do + Assert.Equal(e_idx, idx) + Assert.Equal(e_oLine, oLine) + Assert.Equal(e_line, line)