Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix OriginalLine processing in lexing #17949

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading