Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type token =
- | DoubleUnderscore
- | Underscore
- | DoubleTimes
- | Times
- | EmptyToken
- | Literal of string
- let (|StartsWith|_|) (p:string) (s:string) =
- if s.StartsWith(p) then
- Some(s.Substring(p.Length))
- else
- None
- let buildToken charlist =
- match charlist with
- | [] -> EmptyToken
- | _ -> Literal(System.String.Concat<char> (List.rev charlist))
- let rec tokenize str =
- let rec helper str litAccu accu =
- match str with
- | StartsWith "__" _ -> helper (str.Substring 2) [] (DoubleUnderscore :: (buildToken litAccu) :: accu)
- | StartsWith "**" _ -> helper (str.Substring 2) [] (DoubleTimes :: (buildToken litAccu) :: accu)
- | StartsWith "_" _ -> helper (str.Substring 1) [] (Underscore :: (buildToken litAccu) :: accu)
- | StartsWith "*" _ -> helper (str.Substring 1) [] (Times :: (buildToken litAccu) :: accu)
- | "" -> (buildToken litAccu) :: accu
- | _ -> helper (str.Substring 1) (str.[0] :: litAccu) accu
- helper str [] [] |> List.rev |> List.filter (fun x -> x <> EmptyToken)
- tokenize "foo_bar__**baz!!a*sereje"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement