Advertisement
Guest User

Untitled

a guest
Jan 12th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.22 KB | None | 0 0
  1. let tokenize (str : string) : pos tok list =
  2.   let (toks, _, _) = List.fold_left
  3.     (fun ((toks : pos tok list), (line : int), (col : int)) (tok : Str.split_result) ->
  4.       match tok with
  5.       | Delim t ->
  6.          if t = " " then (toks, line, col + 1)
  7.          else if t = "\t" then (toks, line, col + 1)
  8.          else if t = "\n" then (toks, line + 1, 0)
  9.          else if t = "(" then (LPAREN (line, col, line, col + 1) :: toks, line, col + 1)
  10.          else if t = ")" then (RPAREN (line, col, line, col + 1) :: toks, line, col + 1)
  11.          else
  12.            let tLen = String.length t
  13.            in ((TSym (t, (line, col, line, col + tLen))) :: toks, line, col + tLen)
  14.       | Text t ->
  15.          if t = "true" then (TBool (true, (line, col, line, col + 4)) :: toks, line, col + 4)
  16.          else if t = "false" then (TBool (false, (line, col, line, col + 5)) :: toks, line, col + 5)
  17.          else
  18.            let tLen = String.length t
  19.            in try ((TInt (int_of_string t, (line, col, line, col + tLen))) :: toks, line, col + tLen) with
  20.               | Failure _ -> (TSym (t, (line, col, line, col + tLen)) :: toks, line, col + tLen)
  21.     )
  22.     ([], 0, 0)
  23.     (full_split (regexp "[()\n\t ]") str)
  24.   in List.rev toks
  25. ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement