SHARE
TWEET

Parser

a guest Mar 31st, 2020 132 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. open System
  2.  
  3. type Token =
  4. | STRING of string
  5. | DELIMITER
  6. | EOL
  7.  
  8. type chars = char list
  9.  
  10. module Chars =
  11.  
  12.     let toString : chars -> _ = List.rev >> String.Concat
  13.  
  14. let parse (input : string) =
  15.  
  16.     let rec parseEscapeSeq (acc : chars) = function
  17.     | ('"' | '\\') as c :: t -> t |> parseString (c :: acc)
  18.     | _ -> failwith "Invalid escape sequence"
  19.  
  20.     and parseString (acc : chars) = function
  21.     | '\\' :: t -> t |> parseEscapeSeq acc
  22.     | '"' :: t -> acc, t
  23.     | c :: t -> t |> parseString (c :: acc)
  24.     | [] -> failwith "Unterminated string literal"
  25.  
  26.     and parse (tokens : Token list) = function
  27.     | '\n' :: t -> t |> parse (Token.EOL :: tokens)
  28.     | h :: t when Char.IsWhiteSpace h -> t |> parse tokens
  29.     | ',' :: t -> t |> parse (Token.DELIMITER :: tokens)
  30.     | '"' :: t ->
  31.         let str, t = t |> parseString []
  32.         t |> parse ((str |> Chars.toString |> Token.STRING) :: tokens)
  33.     | [] -> tokens
  34.     | _ -> failwith "Invalid symbol"
  35.  
  36.     input |> List.ofSeq |> parse [] |> List.rev
  37.  
  38. """
  39. "variable1", "variable2"
  40. "test with quote\"", "test2\\", "test3"
  41. """|> parse
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top