Skip to content

Instantly share code, notes, and snippets.

@JeffreyZhao
Created October 21, 2009 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffreyZhao/215256 to your computer and use it in GitHub Desktop.
Save JeffreyZhao/215256 to your computer and use it in GitHub Desktop.
#light
module Token
let rec private readToken inQuote tokenChars restChars =
let chars2Token chars = new string(chars |> Array.ofList |> Array.rev)
if inQuote then
match restChars with
| '\'' :: [] -> (chars2Token tokenChars, [])
| '\'' :: '-' :: cs -> (chars2Token tokenChars, '-' :: cs)
| '\'' :: '\'' :: cs -> readToken true ('\'' :: tokenChars) cs
| c :: cs -> readToken true (c :: tokenChars) cs
| _ ->
let rest = new string(restChars |> Array.ofList);
failwith ("expect an close quote but failed. rest: " + rest)
else
match restChars with
| []
| '-' :: _ -> (chars2Token tokenChars, restChars)
| '\'' :: _ -> failwith "start quote unexpected."
| c :: cs -> readToken false (c :: tokenChars) cs
let rec private readTokenGroup tokens chars =
match chars with
| []
| '-' :: '-' :: _ -> (tokens |> List.rev, chars)
| '-' :: cs -> readTokenGroup tokens cs
| '\'' :: cs ->
let (t, rest) = readToken true [] cs
readTokenGroup (t :: tokens) rest
| _ ->
let (t, rest) = readToken false [] chars
readTokenGroup (t :: tokens) rest
let parse (text : string) =
let rec parse' groups chars =
match chars with
| [] -> groups |> List.rev
| '-' :: '-' :: cs -> parse' groups cs
| _ ->
let (g, rest) = readTokenGroup [] chars
parse' (g :: groups) rest
text |> List.ofSeq |> parse' []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment