Re: StringCvt.cs with regex matching

Matthew Fluet <[email protected]> Tue, 18 Oct 2016 09:19:21 -0400
Newsgroups gmane.comp.lang.ml.mlton.user
Message-ID <CAMrhFL5JGXY5EaQ-k9YRCnc0EvZongF+NkXx2b5nySW=zbbJMA@mail.gmail.com>
On Sun, Oct 16, 2016 at 6:54 PM, Thomas Logan <[email protected]>
wrote:
> I'm using StringCvt.scanString for regex matching, which requires a
> StringCvt.cs, and thus the match_tree is required to use StringCvt.cs.
The
> type checker won't allow using StringCvt.cs as an int.  Is there a way to
> convert it to an int?
>
> Error: ...
>   Case object and rules disagree.
>     object type:  (_ * {pos: [StringCvt.cs], ...} MatchTree.match_tree)
option
>     rules expect: (_ * {pos: [int], ...} MatchTree.match_tree) option

It looks like you are using the SML/NJ RegExp library:
  http://www.smlnj.org/doc/smlnj-lib/Manual/regexp-lib-part.html#section:0

The Basis Library specification states that the StringCvt.cs type is
opaque, so there isn't a conversion from StringCvt.cs to int (in MLton;
SML/NJ seems to expose the implementation of StringCvt.cs as int).

You can write your own "scanString" function that reveals the state type as
int:


structure RE = RegExpFn(structure P = AwkSyntax
                        structure E = BackTrackEngine)
structure MatchTree =
struct
  open MatchTree
  fun toString s (Match (x, mts)) : string =
     concat ["Match (", s x, ", ",
             "[", String.concatWith "," (List.map (toString s) mts), "]",
             ")"]
end

fun scanString f s =
   let
      fun strRdr i =
         if i >= String.size s
            then NONE
            else SOME (String.sub (s, i), i + 1)
   in
      case f strRdr 0 of
         NONE => NONE
       | SOME (x, _) => SOME x
   end

val re = RE.compileString "l(mn)o"
val s = "abcdefghijklmnopqrstuvwxyz"

val _ =
   case scanString (RE.find re) s of
      NONE => print "no match\n"
    | SOME mt => print (concat ["Match: ",
                                MatchTree.toString
                                (fn {len, pos} => concat ["{len = ",
Int.toString len,
                                                          ",pos = ",
Int.toString pos, "}"])
                                mt,
                                "\n"])

[mtf@uller tmp]$ mlton z.mlb

[mtf@uller tmp]$ ./z

Match: Match ({len = 4,pos = 11}, [Match ({len = 2,pos = 12}, [])])

-- 
You received this message because you are subscribed to the Google Groups "MLton-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most 
engaging tech sites, SlashDot.org! http://sdm.link/slashdot

_______________________________________________
MLton-user mailing list
[email protected]; [email protected]
https://lists.sourceforge.net/lists/listinfo/mlton-user