Skip to content

Commit

Permalink
attempt to adjust printing of #line directives.
Browse files Browse the repository at this point in the history
The way it was done breaks syntax coloring in VS:

# 123 "file.name" // broken

vs

#line 123 "file.name" // fixed

see dotnet/fsharp#6400
  • Loading branch information
smoothdeveloper committed May 9, 2019
1 parent 64d0229 commit 7fb41bb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/FsLex/fslex.fs
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ let main() =
let printLinesIfCodeDefined (code,pos:Position) =
if pos <> Position.Empty // If bottom code is unspecified, then position is empty.
then
cfprintfn os "# %d \"%s\"" pos.Line pos.FileName
cfprintfn os "%s" (Position.MakeLineDirective pos)
cfprintfn os "%s" code

printLinesIfCodeDefined spec.TopCode
let code = fst spec.TopCode
lineCount := !lineCount + code.Replace("\r","").Split([| '\n' |]).Length
cfprintfn os "# %d \"%s\"" !lineCount output
cfprintfn os "%s" (Position.MakeSimpleLineDirective !lineCount output)

cfprintfn os "let trans : uint16[] array = "
cfprintfn os " [| "
Expand Down Expand Up @@ -210,19 +210,19 @@ let main() =
cfprintfn os " match _fslex_tables.Interpret(%d,lexbuf) with" startNode.Id
actions |> Seq.iteri (fun i (code:string, pos) ->
cfprintfn os " | %d -> ( " i
cfprintfn os "# %d \"%s\"" pos.Line pos.FileName
cfprintfn os "%s" (Position.MakeLineDirective pos)
let lines = code.Split([| '\r'; '\n' |], StringSplitOptions.RemoveEmptyEntries)
for line in lines do
cfprintfn os " %s" line
cfprintfn os "# %d \"%s\"" !lineCount output
cfprintfn os "%s" (Position.MakeSimpleLineDirective !lineCount output)
cfprintfn os " )")
cfprintfn os " | _ -> failwith \"%s\"" ident


cfprintfn os ""

printLinesIfCodeDefined spec.BottomCode
cfprintfn os "# 3000000 \"%s\"" output
cfprintfn os "%s" (Position.MakeSimpleLineDirective 3000000 output)

with e ->
eprintf "FSLEX: error FSL000: %s" (match e with Failure s -> s | e -> e.ToString())
Expand Down
8 changes: 8 additions & 0 deletions src/FsLexYacc.Runtime/Lexing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ namespace FSharp.Text.Lexing
let pos = pos
{pos with pos_cnum = pos.pos_cnum + by}

static member MakeSimpleLineDirective line fileName =
sprintf
"#line %d \"%s\" // " // todo: add optional line text as a comment
line
fileName

static member MakeLineDirective (pos: Position) = Position.MakeSimpleLineDirective pos.Line pos.FileName

static member Empty =
{ pos_fname=""
pos_lnum= 0
Expand Down
4 changes: 4 additions & 0 deletions src/FsLexYacc.Runtime/Lexing.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ type Position =
/// Get a position corresponding to the first line (line number 1) in a given file
static member FirstLine : filename:string -> Position

static member MakeSimpleLineDirective : line: int -> filename: string -> string
static member MakeLineDirective : pos: Position -> string

[<Sealed>]
#if INTERNALIZED_FSLEXYACC_RUNTIME
type internal LexBuffer<'char> =
Expand Down Expand Up @@ -143,6 +146,7 @@ type LexBuffer<'char> =
/// Create a lex buffer suitable for use with ASCII byte lexing that reads byte inputs from the given binary reader
static member FromBinaryReader: System.IO.BinaryReader -> LexBuffer<byte>



/// The type of tables for an ascii lexer generated by fslex.
[<Sealed>]
Expand Down

0 comments on commit 7fb41bb

Please sign in to comment.