4 people like it.

Ini file type provider

Parse INI file (hi old school :) ) via type providers. WARNING: add to project ProvidedTypes-0.2.fs & ProvidedTypes-0.2.fsi from http://fsharp3sample.codeplex.com/SourceControl/latest#SampleProviders/Shared/ProvidedTypes-0.2.fs

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
//Ini Type provider
//type test=IniProv<PathDefaultIniFile:string,?EncodingName:string>
//(default to Encoding.Default)
//IniProv<...>() - default schema
//IniProv<...>(path:string)load ini from path

//example
//open IniProvider
//let z=new IniProv<"c:\test.ini","utf-8">("c:\test.ini")
//z.NextSection.xkey|>printf "%s"

module IniProvider
open System.IO
open System.Text
open System.Text.RegularExpressions
open System.Reflection
open Samples.FSharp.ProvidedTypes
open Microsoft.FSharp.Core.CompilerServices

let (|Match|_|) pt str=
  Regex.Match(str,pt)|>function
    |m when m.Success->seq{for g in m.Groups->g.Value}|>Seq.toArray|>Some
    |_->None
let (|Section|PropVal|Coments|)=function
  |Match "\[(.*)\]" x->Section x.[1]
  |Match "(.*?)=(.*)" x->PropVal(x.[1],x.[2])
  |x->Coments x
let DelComents=function
  |Match "(.*?);" x->x.[1]
  |x->x
let ReadIniData path (enc:string)=
  File.ReadLines(path,Encoding.GetEncoding(enc))|>Seq.map DelComents
   |>Seq.fold (fun (a,name,props) x->x|>function
                                      |Section x->(name,props|>List.rev)::a,x,[]
                                      |PropVal propval->a,name,propval::props
                                      |Coments _->a,name,props)
              ([],"",[])|>fun (a,name,props)->(name,props|>List.rev)::a|>List.rev|>List.tail

[<TypeProvider>]
type Ini() as this =
    inherit TypeProviderForNamespaces()
    let asm,ns = System.Reflection.Assembly.GetExecutingAssembly(),"IniProvider"
    let IniTy = ProvidedTypeDefinition(asm, ns, "IniProv", Some(typeof<obj>))
    do IniTy.DefineStaticParameters([ProvidedStaticParameter("path", typeof<string>);
                                     ProvidedStaticParameter("encoding", typeof<string>,Encoding.Default.BodyName)],
                                    fun tyName [|:? string as path; :? string as enc|] ->
                                         let ty = ProvidedTypeDefinition(asm, ns, tyName, Some(typeof<obj>))
                                         let cte=ProvidedConstructor([],InvokeCode=(fun args -> <@@ ReadIniData path enc @@>))
                                         let ctl=ProvidedConstructor([ProvidedParameter("file",typeof<string>)],
                                                                     InvokeCode=(fun [file]-> <@@ ReadIniData %%file enc @@>))
                                         ty.AddMembers [cte;ctl]
                                         let data=ReadIniData path enc
                                         data|>Seq.iter (fun (name,grp)->
                                           let sty=ProvidedTypeDefinition(name,Some(typeof<obj>))
                                           ty.AddMember sty
                                           grp|>Seq.iter (fun (p,v) ->let prop=ProvidedProperty(p,typeof<string>,
                                                                                               GetterCode=fun [arg]-> <@@ ((%%arg:obj):?>(string*string) list)|>Seq.find (fun (a,b)->a=p)|>snd @@>)
                                                                      sty.AddMember prop)
                                           let prop=ProvidedProperty(name,sty,GetterCode=fun [arg]-> <@@ ((%%arg:obj):?>(string*(string*string) list) list)|>Seq.find (fun (a,_)->a=name)|>snd @@>)
                                           ty.AddMember prop)
                                         ty)
                                         
       this.AddNamespace(ns, [IniTy])
[<TypeProviderAssembly>]
do()
module IniProvider
namespace System
namespace System.IO
namespace System.Text
namespace System.Text.RegularExpressions
namespace System.Reflection
namespace Microsoft.FSharp
namespace Microsoft
namespace Microsoft.FSharp.Core
namespace Microsoft.FSharp.Core.CompilerServices
type Match =
  inherit Group
  member Groups : GroupCollection
  member NextMatch : unit -> Match
  member Result : replacement:string -> string
  static member Empty : Match
  static member Synchronized : inner:Match -> Match

Full name: System.Text.RegularExpressions.Match
val pt : string
val str : string
Multiple items
type Regex =
  new : pattern:string -> Regex + 1 overload
  member GetGroupNames : unit -> string[]
  member GetGroupNumbers : unit -> int[]
  member GroupNameFromNumber : i:int -> string
  member GroupNumberFromName : name:string -> int
  member IsMatch : input:string -> bool + 1 overload
  member Match : input:string -> Match + 2 overloads
  member Matches : input:string -> MatchCollection + 1 overload
  member Options : RegexOptions
  member Replace : input:string * replacement:string -> string + 5 overloads
  ...

Full name: System.Text.RegularExpressions.Regex

--------------------
Regex(pattern: string) : unit
Regex(pattern: string, options: RegexOptions) : unit
Regex.Match(input: string, pattern: string) : Match
Regex.Match(input: string, pattern: string, options: RegexOptions) : Match
val m : Match
property Group.Success: bool
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val g : Group
property Match.Groups: GroupCollection
property Capture.Value: string
module Seq

from Microsoft.FSharp.Collections
val toArray : source:seq<'T> -> 'T []

Full name: Microsoft.FSharp.Collections.Seq.toArray
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
Multiple items
active recognizer Match: string -> string -> string [] option

Full name: IniProvider.( |Match|_| )

--------------------
type Match =
  inherit Group
  member Groups : GroupCollection
  member NextMatch : unit -> Match
  member Result : replacement:string -> string
  static member Empty : Match
  static member Synchronized : inner:Match -> Match

Full name: System.Text.RegularExpressions.Match
val x : string []
val x : string
val DelComents : _arg1:string -> string

Full name: IniProvider.DelComents
val ReadIniData : path:string -> enc:string -> (string * (string * string) list) list

Full name: IniProvider.ReadIniData
val path : string
val enc : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
type File =
  static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
  static member AppendAllText : path:string * contents:string -> unit + 1 overload
  static member AppendText : path:string -> StreamWriter
  static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
  static member Create : path:string -> FileStream + 3 overloads
  static member CreateText : path:string -> StreamWriter
  static member Decrypt : path:string -> unit
  static member Delete : path:string -> unit
  static member Encrypt : path:string -> unit
  static member Exists : path:string -> bool
  ...

Full name: System.IO.File
File.ReadLines(path: string) : System.Collections.Generic.IEnumerable<string>
File.ReadLines(path: string, encoding: Encoding) : System.Collections.Generic.IEnumerable<string>
type Encoding =
  member BodyName : string
  member Clone : unit -> obj
  member CodePage : int
  member DecoderFallback : DecoderFallback with get, set
  member EncoderFallback : EncoderFallback with get, set
  member EncodingName : string
  member Equals : value:obj -> bool
  member GetByteCount : chars:char[] -> int + 3 overloads
  member GetBytes : chars:char[] -> byte[] + 5 overloads
  member GetCharCount : bytes:byte[] -> int + 2 overloads
  ...

Full name: System.Text.Encoding
Encoding.GetEncoding(name: string) : Encoding
Encoding.GetEncoding(codepage: int) : Encoding
Encoding.GetEncoding(name: string, encoderFallback: EncoderFallback, decoderFallback: DecoderFallback) : Encoding
Encoding.GetEncoding(codepage: int, encoderFallback: EncoderFallback, decoderFallback: DecoderFallback) : Encoding
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val fold : folder:('State -> 'T -> 'State) -> state:'State -> source:seq<'T> -> 'State

Full name: Microsoft.FSharp.Collections.Seq.fold
val a : (string * (string * string) list) list
val name : string
val props : (string * string) list
active recognizer Section: string -> Choice<string,(string * string),string>

Full name: IniProvider.( |Section|PropVal|Coments| )
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val rev : list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.rev
active recognizer PropVal: string -> Choice<string,(string * string),string>

Full name: IniProvider.( |Section|PropVal|Coments| )
val propval : string * string
active recognizer Coments: string -> Choice<string,(string * string),string>

Full name: IniProvider.( |Section|PropVal|Coments| )
val tail : list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.tail
Multiple items
type TypeProviderAttribute =
  inherit Attribute
  new : unit -> TypeProviderAttribute

Full name: Microsoft.FSharp.Core.CompilerServices.TypeProviderAttribute

--------------------
new : unit -> TypeProviderAttribute
Multiple items
type Ini =
  inherit obj
  new : unit -> Ini

Full name: IniProvider.Ini

--------------------
new : unit -> Ini
val this : Ini
type Assembly =
  member CodeBase : string
  member CreateInstance : typeName:string -> obj + 2 overloads
  member EntryPoint : MethodInfo
  member Equals : o:obj -> bool
  member EscapedCodeBase : string
  member Evidence : Evidence
  member FullName : string
  member GetCustomAttributes : inherit:bool -> obj[] + 1 overload
  member GetCustomAttributesData : unit -> IList<CustomAttributeData>
  member GetExportedTypes : unit -> Type[]
  ...

Full name: System.Reflection.Assembly
Assembly.GetExecutingAssembly() : Assembly
val typeof<'T> : System.Type

Full name: Microsoft.FSharp.Core.Operators.typeof
type obj = System.Object

Full name: Microsoft.FSharp.Core.obj
property Encoding.Default: Encoding
property Encoding.BodyName: string
val iter : action:('T -> unit) -> source:seq<'T> -> unit

Full name: Microsoft.FSharp.Collections.Seq.iter
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val find : predicate:('T -> bool) -> source:seq<'T> -> 'T

Full name: Microsoft.FSharp.Collections.Seq.find
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd
Multiple items
type TypeProviderAssemblyAttribute =
  inherit Attribute
  new : unit -> TypeProviderAssemblyAttribute
  new : assemblyName:string -> TypeProviderAssemblyAttribute
  member AssemblyName : string

Full name: Microsoft.FSharp.Core.CompilerServices.TypeProviderAssemblyAttribute

--------------------
new : unit -> TypeProviderAssemblyAttribute
new : assemblyName:string -> TypeProviderAssemblyAttribute
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/lx
Posted:10 years ago
Author:Zhukoff Dima
Tags: ini , type provider