Elm: step 1 using parser combinator
[jackhill/mal.git] / elm / Utils.elm
CommitLineData
0d8a8d94
JB
1module Utils
2 exposing
3 ( decodeString
4 , encodeString
5 , makeCall
6 , wrap
7 , maybeToList
8 )
9
10import Regex exposing (replace, regex, HowMany(All))
11import Types exposing (MalExpr(..))
12
13
14decodeString : String -> String
15decodeString =
16 let
17 unescape { match } =
18 case match of
19 "\\n" ->
20 "\n"
21
22 "\\\"" ->
23 "\""
24
25 "\\\\" ->
26 "\\"
27
28 other ->
29 other
30 in
31 String.slice 1 -1
32 >> replace All (regex "\\\\[\\\"\\\\n]") unescape
33
34
35encodeString : String -> String
36encodeString =
37 let
38 escape { match } =
39 case match of
40 "\n" ->
41 "\\n"
42
43 "\"" ->
44 "\\\""
45
46 "\\" ->
47 "\\\\"
48
49 other ->
50 other
51 in
52 wrap "\"" "\""
53 << replace All (regex "[\\n\\\"\\\\]") escape
54
55
56makeCall : String -> List MalExpr -> MalExpr
57makeCall symbol args =
58 MalList <| (MalSymbol symbol) :: args
59
60
61wrap : String -> String -> String -> String
62wrap prefix suffix str =
63 prefix ++ str ++ suffix
64
65
66maybeToList : Maybe a -> List a
67maybeToList m =
68 case m of
69 Just x ->
70 [ x ]
71
72 Nothing ->
73 []