Skip to content

Commit

Permalink
fix OriginalLine (#17949)
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin521 authored Nov 4, 2024
1 parent 658b245 commit 34a478e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Compiler/Facilities/prim-lexing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
61 changes: 56 additions & 5 deletions tests/FSharp.Compiler.ComponentTests/CompilerDirectives/Line.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -63,4 +67,51 @@ printfn ""
| ParsedInput.SigFile _ -> failwith "unexpected: sig file"
if exprRange <> expectedRange then
failwith $"case{case}: expected: {expectedRange}, found {exprRange}"






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
]

[<Fact>]
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)

0 comments on commit 34a478e

Please sign in to comment.