-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathDecode.elm
267 lines (210 loc) · 6.58 KB
/
Decode.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
module Xml.Decode exposing (decode, decodeInt, decodeString, decodeFloat, decodeBool, decodeChildren)
{-|
@docs decode
@docs decodeInt, decodeFloat, decodeString, decodeString, decodeBool
@docs decodeChildren
-}
import Dict
import Regex exposing (Regex)
import Xml exposing (Value(..))
{-| Try and decode the props from a string
-}
decodeProps : String -> Result String Value
decodeProps str =
List.foldl
(\decoder val ->
case val of
Ok _ ->
val
Err _ ->
decoder str
)
(Err "")
[ decodeBool, decodeInt, decodeFloat, decodeString ]
parseProps : List String -> List ( String, Value )
parseProps =
List.filterMap
(\n ->
case String.split "=" n of
[ name, value ] ->
let
withoutQuotes =
value
|> String.dropLeft 1
|> String.dropRight 1
in
case decodeProps withoutQuotes of
Err _ ->
Nothing
Ok v ->
Just ( name, v )
_ ->
Nothing
)
propRegex : Regex.Regex
propRegex =
Regex.regex " .+?=\".+?\""
findProps : List String -> Dict.Dict String Value
findProps =
List.tail
>> Maybe.withDefault []
>> String.join " "
>> (\s -> " " ++ s)
>> Regex.find Regex.All propRegex
>> List.map (.match >> String.trim)
>> parseProps
>> Dict.fromList
parseSlice : Int -> Int -> String -> Result String ( Value, Int )
parseSlice first firstClose trimmed =
let
beforeClose =
String.slice (first + 1) firstClose trimmed
words =
beforeClose
|> String.words
tagName =
words
|> List.head
|> Maybe.withDefault ""
props =
findProps words
closeTag =
"</" ++ tagName ++ ">"
in
case String.indexes closeTag trimmed of
[] ->
if String.startsWith "?" tagName then
Ok ( DocType tagName props, firstClose + 1 )
else if String.endsWith "/" beforeClose then
let
tag = if String.endsWith "/" tagName then
String.slice 0 -1 tagName
else
tagName
in
Ok ( Tag tag props (Object []), firstClose + 1 )
else
"Failed to find close tag for "
++ tagName
|> Err
firstCloseTag :: _ ->
let
contents =
String.slice (firstClose + 1) (firstCloseTag) trimmed
in
case decodeChildren contents of
Err s ->
Err s
Ok v ->
Ok ( Tag tagName props v, firstCloseTag + (String.length closeTag) )
actualDecode : String -> Result String (List Value)
actualDecode text =
let
openIndexes =
String.indexes "<" text
closeIndexes =
String.indexes ">" text
in
case ( openIndexes, closeIndexes ) of
( first :: restFirst, firstClose :: restFirstClose ) ->
parseSlice first firstClose text
|> Result.andThen
(\( foundValue, firstCloseTag ) ->
case actualDecode (String.slice firstCloseTag (String.length text + 1) text) of
Err err ->
if err == "Nothing left" then
Ok [ foundValue ]
else
Err ("Parsed to " ++ toString foundValue ++ ", but then hit " ++ err)
Ok thing ->
[ foundValue ]
++ thing
|> Ok
)
_ ->
Err "Nothing left"
{-| Try to decode a string and turn it into an XML value
import Xml exposing(Value(Tag, Object))
import Xml.Encode exposing (null)
import Dict
decode "<name></name>"
--> Ok (Object [Tag "name" Dict.empty null])
-}
decode : String -> Result String Value
decode text =
case String.trim text of
"" ->
Ok (Object [])
trimmed ->
actualDecode trimmed
|> Result.map Object
{-| Decode a string
import Xml exposing (Value(StrNode))
decodeString "hello"
--> Ok (StrNode "hello")
-}
decodeString : String -> Result String Value
decodeString str =
StrNode str
|> Ok
{-| Decode a int
import Xml exposing (Value(IntNode))
decodeInt "hello"
--> Err "could not convert string 'hello' to an Int"
decodeInt "5"
--> Ok (IntNode 5)
-}
decodeInt : String -> Result String Value
decodeInt str =
case String.toInt str of
Err s ->
Err s
Ok v ->
IntNode v
|> Ok
{-| Decode a float
import Xml exposing (Value(FloatNode))
decodeFloat "hello"
--> Err "could not convert string 'hello' to a Float"
decodeFloat "5"
--> Ok (FloatNode 5.0)
decodeFloat "5.5"
--> Ok (FloatNode 5.5)
-}
decodeFloat : String -> Result String Value
decodeFloat str =
case String.toFloat str of
Err s ->
Err s
Ok v ->
FloatNode v
|> Ok
{-| Decode a bool
-}
decodeBool : String -> Result String Value
decodeBool str =
if str == "true" then
BoolNode True
|> Ok
else if str == "false" then
BoolNode False
|> Ok
else
Err "Not a bool"
{-| Decode children from a string
import Xml exposing (Value(Object, Tag, StrNode))
decodeChildren "<name>hello</name>"
--> Ok (Object [Tag "name" Dict.empty (StrNode "hello")] )
-}
decodeChildren : String -> Result String Value
decodeChildren str =
List.foldl
(\decoder val ->
case val of
Ok _ ->
val
Err _ ->
decoder str
)
(Err "")
[ decode, decodeInt, decodeFloat, decodeString ]