|
(* |
|
* all of ProvidedTypes.fs |
|
* as of https://github.com/fsprojects/FSharp.TypeProviders.StarterPack/blob/245fe3b0126ac6a326a14a0f6b1ef569680b08b3/src/ProvidedTypes.fs |
|
*) |
|
|
|
// then add to the bottom |
|
|
|
interface ITypeProvider2 with |
|
member __.GetStaticParametersForMethod(methodWithoutArguments:MethodBase) = |
|
match methodWithoutArguments.Name with |
|
| "Match" -> [| ProvidedStaticParameter("pattern", typeof<string>) |] |
|
| _ -> null |
|
|
|
member __.ApplyStaticArgumentsForMethod(methodWithoutArguments : MethodBase, methodNameWithArguments : string, staticArguments : obj[]) = |
|
match staticArguments with |
|
| [| :? string as pattern|] -> |
|
let r = Regex(pattern) |
|
|
|
let rootTy = |
|
match methodWithoutArguments.DeclaringType with |
|
| :? ProvidedTypeDefinition as pTypeDef -> pTypeDef |
|
| _ -> failwith "can't find root type" |
|
|
|
let matchTy = RegexTPHelper.GetMatchTy(pattern, rootTy) |
|
RegexTPHelper.GetStaticMatchMethod(methodNameWithArguments, matchTy, pattern, rootTy) |
|
| _ -> failwith "expecting a single static method argument" |
|
|
|
and RegexTPHelper = |
|
static member GetMatchTy(pattern, parentTy : ProvidedTypeDefinition) = |
|
let r = Regex(pattern) |
|
|
|
let matchTy = ProvidedTypeDefinition( |
|
sprintf "Obj%s" pattern, |
|
baseType = Some typeof<obj>, |
|
HideObjectMethods = true) |
|
|
|
parentTy.AddMember matchTy |
|
|
|
for group in r.GetGroupNames() do |
|
// Ignore the group named 0, which represents all input. |
|
if group <> "0" then |
|
let prop = ProvidedProperty( |
|
propertyName = group, |
|
propertyType = typeof<string>, |
|
GetterCode = fun args -> <@@ ((%%args.[0]:obj) :?> Match).Groups.[group].Value @@>) |
|
prop.AddXmlDoc(sprintf @"Gets the ""%s"" group from this match" group) |
|
matchTy.AddMember(prop) |
|
|
|
matchTy |
|
|
|
static member GetStaticMatchMethod(methodName, returnTy, pattern, parentTy : ProvidedTypeDefinition) = |
|
let mthd = |
|
ProvidedMethod( |
|
methodName = methodName, |
|
parameters = [ProvidedParameter("input", typeof<string>)], |
|
returnType = returnTy, |
|
IsStaticMethod = true, |
|
InvokeCode = (fun args -> <@@ Regex.Match(%%args.[0], pattern) :> obj @@>)) |
|
|
|
parentTy.AddMember mthd |
|
mthd :> MethodBase |