diff options
| author | Chouser <chouser@n01se.net> | 2015-01-28 08:27:32 -0500 |
|---|---|---|
| committer | Chouser <chouser@n01se.net> | 2015-01-30 12:54:43 -0500 |
| commit | 16b177329cac77136b236dfb3645e4be4e3df297 (patch) | |
| tree | aa70f831b5f50212ab53e7d6ad8ab89603f89ec9 /ocaml/reader.ml | |
| parent | cc916d9d819d17cc47d34321440bf5a2683eac2e (diff) | |
| download | mal-16b177329cac77136b236dfb3645e4be4e3df297.tar.gz mal-16b177329cac77136b236dfb3645e4be4e3df297.zip | |
Ocaml: fix string escaping and printing
Diffstat (limited to 'ocaml/reader.ml')
| -rw-r--r-- | ocaml/reader.ml | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/ocaml/reader.ml b/ocaml/reader.ml index 9754444..96404aa 100644 --- a/ocaml/reader.ml +++ b/ocaml/reader.ml @@ -6,6 +6,11 @@ let find_re re str = (List.filter (function | Str.Delim x -> true | Str.Text x -> false) (Str.full_split re str)) ;; +let gsub re f str = + String.concat + "" (List.map (function | Str.Delim x -> f x | Str.Text x -> x) + (Str.full_split re str)) + let token_re = (Str.regexp "~@\\|[][{}()'`~^@]\\|\"\\(\\\\.\\|[^\"]\\)*\"\\|;.*\\|[^][ \n{}('\"`,;)]*") type reader = { @@ -26,9 +31,11 @@ let read_atom token = | _ -> match token.[0] with | '0'..'9' -> T.Int (int_of_string token) - | '"' -> T.String (Str.global_replace (Str.regexp "\\\\\\(.\\)") - "\\1" - (String.sub token 1 ((String.length token) - 2))) + | '"' -> T.String (gsub (Str.regexp "\\\\.") + (function + | "\\n" -> "\n" + | x -> String.sub x 1 1) + (String.sub token 1 ((String.length token) - 2))) | ':' -> T.Keyword (Str.replace_first (Str.regexp "^:") "" token) | _ -> Types.symbol token |
