Fable: fable-providers-regex broken for fable 0.7.18

Created on 5 Dec 2016  Â·  11Comments  Â·  Source: fable-compiler/Fable

When trying to use Fable.Providers.Regex I got this error:

[0] [ERROR] Cannot find replacement for Fable.Providers.Regex.Internal.Helper.CreateRegex (L68,4-L68,47) (L:\Squiibz\src\Browser\Pages/Prestation/Prestation_Create.fs)

Code for reproduction:

open Fable.Providers.Regex

let regexTime =
  SafeRegex.Create<"^\d$", ignoreCase=true>()
bug

All 11 comments

I still have this error with fable 0.7.22.

I don't have the right to reopen this issue.

I don't believe you ;) Hehe, just kidding. Would it be possible to create a repo to reproduce (.fsx, fableconfig, package.json)?

Yes sure 😊

Here is a project to reproduce the error :)

RegexReproduction.zip

Usage:

➜ npm i
➜ node build.js

Working with the new version of fable-providers-regex

Hi is it possible to let this also works on .fs format?

@CliveWongTohSoon Not sure to undetstand your point here. Could you please explain a bit more ?

I am working on a project using Fable + Electron using fable-loader, and in the code System.Text.RegularExpressions are needed (specifically, Regex(s).match is needed). However, when I tried to compile it, it gives error saying "error FABLE: Cannot find replacement for System.Text.RegularExpressions.GroupCollection::GetEnumerator".

I tried to resort to using fable-provider-regex, only to realise it can't be used in my project as they need to be in .fs format, but fable-provider-regex only works in .fsx (script file). I wonder is there anything I can do to make it work on my .fs codes.

Hi @CliveWongTohSoon. Type providers are not working at the moment for Fable 1.x but we expect this to be fixed soon. For what you describe, in you case probably the easiest solution is to implement the replacement for "System.Text.RegularExpressions.GroupCollection::GetEnumerator". Do you have a sample of the code that's failing so we can add it as a test? Cheers!

Here is the snipper of the code. I believe the error is thrown from regexMatch, where it returns m.Groups which fable-loader doesn't support yet.

let regexMatch (regex:string) (str:string) =
    let m = System.Text.RegularExpressions.Regex(regex).Match(str)
    if m.Success
    then
    let mLst = [ for x in m.Groups -> x.Value ]
    Some (List.head mLst, List.tail mLst)
    else None

let (|LexMatchD|_|) debug regex (lexData: LexData) = 
    match regexMatch regex (lexData.Txt) with
    | None -> 
        if debug 
        then printfn "Match of '%s' with '%s' failed." lexData.Txt regex
        None
    | Some (mStr, rst) -> 
        let mChars = String.length mStr
        if mChars = 0 then
            failwithf "Unexpected 0 character match '%s' " regex
        if debug then
            printfn "Match of '%s' with '%s' OK: match is '%s'" lexData.Txt regex mStr
        let lexData' = {lexData with Txt = lexData.Txt.[mChars..]}
        Some (mStr, lexData')

let (|LexMatch|_|) = (|LexMatchD|_|) false

let nextToken ld = 
    let incr ld = {ld with Num = ld.Num + 1}
    match ld with
    | LexMatch @"^\," (_, ld') -> None, ld'
    | LexMatch @"^[ ]" (_, ld') -> None, ld'
    | LexMatch @"^#.*" (exp, ld') -> Some (Exp (exp)), ld'
    | LexMatch @"^[rR][0-9]+" (reg, ld') -> Some (R (reg, ld'.Num)), incr ld'
    | LexMatch @"^[lL][sS][lL]" (opr, ld') -> Some (Opr (opr)), ld'
    | LexMatch @"^[lL][sS][rR]" (opr, ld') -> Some(Opr(opr)), ld'
    | LexMatch @"^[aA][sS][rR]" (opr, ld') -> Some(Opr(opr)), ld'
    | LexMatch @"^[rR][rR][xX]" (opr, ld') -> Some(Opr(opr)), ld'
    | LexMatch @"^[rR][oO][rR]" (opr, ld') -> Some(Opr(opr)), ld'
    | _ -> failwithf "Matching failed in lexer"

Hmm, Regex.Groups should be supported. The code below is working for me in the REPL. I just set lexData to be string because I didn't have the LexData type. What version of Fable are you using (dotnet fable --version)?

let regexMatch (regex:string) (str:string) =
    let m = System.Text.RegularExpressions.Regex(regex).Match(str)
    if m.Success then
        let mLst = [ for x in m.Groups -> x.Value ]
        Some (List.head mLst, List.tail mLst)
    else None

let (|LexMatchD|_|) debug regex (lexData: string) = 
    match regexMatch regex lexData with
    | None -> 
        if debug 
        then printfn "Match of '%s' with '%s' failed." lexData regex
        None
    | Some (mStr, rst) -> 
        let mChars = String.length mStr
        if mChars = 0 then
            failwithf "Unexpected 0 character match '%s' " regex
        if debug then
            printfn "Match of '%s' with '%s' OK: match is '%s'" lexData regex mStr
        let lexData' = lexData.[mChars..]
        Some (mStr, lexData')

let (|LexMatch|_|) = (|LexMatchD|_|) false
Was this page helpful?
0 / 5 - 0 ratings

Related issues

alfonsogarciacaro picture alfonsogarciacaro  Â·  3Comments

ncave picture ncave  Â·  3Comments

MangelMaxime picture MangelMaxime  Â·  3Comments

alfonsogarciacaro picture alfonsogarciacaro  Â·  3Comments

krauthaufen picture krauthaufen  Â·  3Comments