Daniel
Daniel

Reputation: 1610

File and line numbers in F# code quotations

Is there a way to extract the file name and line number from code quotation so that we may add add debug information for transformations of the quoted code. Any alternative ways?

Upvotes: 3

Views: 366

Answers (2)

Jim Wallace
Jim Wallace

Reputation: 1046

This is now built into F#

https://msdn.microsoft.com/en-us/library/dd233234.aspx

__LINE__
__SOURCE_DIRECTORY__
__SOURCE_FILE__

Upvotes: 0

kvb
kvb

Reputation: 55185

Try something like this:

let sourceInfo (e:Quotations.Expr) = 
    let (|Val|_|) e : 't option = 
        match e with
        | Quotations.Patterns.Value(:? 't as v,_) -> Some v
        | _ -> None
    let (|Tup|_|) = Quotations.Patterns.(|NewTuple|_|)

    e.CustomAttributes
    |> List.tryPick (function | Tup [Val("DebugRange")
                                     Tup [Val(file:string)
                                          Val(startLine:int) 
                                          Val(startCol:int)
                                          Val(endLine:int)
                                          Val(endCol:int)]] 
                                  -> Some(file,startLine,startCol,endLine,endCol) 
                              | _ -> None)

sourceInfo <@ "test" @>

Upvotes: 3

Related Questions