tree-builder 0.0.3

Parser Generator library which uses Nom and procedural macros to generate recursive-descent parsers.
Documentation
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
use std::collections::HashMap;
use tree_builder::{build_tree, Parser};

const TEST1: &str = r#"
[
  {
    "_id": "64b13dbc4848c3a4c22284c7",
    "index": 0,
    "guid": "2acbba8f-939c-4266-a2a2-cc44fc4f1bfb",
    "isActive": true,
    "balance": "$3,212.68",
    "picture": "http://placehold.it/32x32",
    "age": 29,
    "eyeColor": "blue",
    "name": "Alice Sargent",
    "gender": "female",
    "company": "INEAR",
    "email": "alicesargent@inear.com",
    "phone": "+1 (974) 407-3296",
    "address": "369 Colin Place, Falconaire, Alabama, 3946",
    "about": "Eu duis ad veniam magna reprehenderit ea irure consequat quis nulla ex velit sit esse. Dolore eu sit qui ad. Aute amet consequat sit aliquip pariatur eiusmod irure cillum do. Ex irure veniam ad fugiat fugiat.\r\n",
    "registered": "2014-11-09T05:06:52 -01:00",
    "latitude": 33.061942,
    "longitude": 175.617123,
    "tags": [
      "consectetur",
      "reprehenderit",
      "non",
      "do",
      "dolore",
      "Lorem",
      "eiusmod"
    ],
    "friends": [
      {
        "id": 0,
        "name": "Harrison Clayton"
      },
      {
        "id": 1,
        "name": "Chapman Bonner"
      },
      {
        "id": 2,
        "name": "Perez Alston"
      }
    ],
    "greeting": "Hello, Alice Sargent! You have 3 unread messages.",
    "favoriteFruit": "strawberry"
  },
  {
    "_id": "64b13dbc6b842506323ed605",
    "index": 1,
    "guid": "af395b6b-abb7-4c25-83da-016c6af99ff4",
    "isActive": true,
    "balance": "$2,568.10",
    "picture": "http://placehold.it/32x32",
    "age": 33,
    "eyeColor": "blue",
    "name": "Marian Rosales",
    "gender": "female",
    "company": "ORBIN",
    "email": "marianrosales@orbin.com",
    "phone": "+1 (877) 416-2903",
    "address": "324 Howard Alley, Bayview, Utah, 5811",
    "about": "Aliquip elit nulla cupidatat fugiat adipisicing. Qui ea est aliquip duis do est ut magna irure do incididunt. Ipsum quis eiusmod ad aliquip officia. Elit nostrud ea anim incididunt cupidatat voluptate ad id. Veniam anim ut non nisi ut voluptate. Ea in nisi mollit veniam.\r\n",
    "registered": "2023-06-13T01:15:46 -02:00",
    "latitude": -16.370648,
    "longitude": -31.48681,
    "tags": [
      "in",
      "tempor",
      "do",
      "sit",
      "esse",
      "reprehenderit",
      "amet"
    ],
    "friends": [
      {
        "id": 0,
        "name": "Taylor Holland"
      },
      {
        "id": 1,
        "name": "Manuela Sims"
      },
      {
        "id": 2,
        "name": "Nita Nichols"
      }
    ],
    "greeting": "Hello, Marian Rosales! You have 6 unread messages.",
    "favoriteFruit": "strawberry"
  },
  {
    "_id": "64b13dbc84d4ca298c701f83",
    "index": 2,
    "guid": "51f9b01e-b915-4d02-be07-4e2852c552e5",
    "isActive": true,
    "balance": "$1,478.54",
    "picture": "http://placehold.it/32x32",
    "age": 38,
    "eyeColor": "green",
    "name": "Holcomb Pickett",
    "gender": "male",
    "company": "INTRAWEAR",
    "email": "holcombpickett@intrawear.com",
    "phone": "+1 (842) 563-3307",
    "address": "432 Lloyd Street, Matthews, Oklahoma, 7005",
    "about": "Magna velit laborum adipisicing ut ex sunt cupidatat. Mollit id eu eu Lorem eu aliquip pariatur laboris fugiat laborum excepteur dolor. Mollit officia laboris laborum tempor. Nisi pariatur deserunt culpa duis est excepteur eiusmod aliquip. Incididunt ad non mollit nisi amet consequat elit exercitation exercitation tempor. Cillum deserunt in Lorem in quis deserunt ut qui adipisicing. Non minim nisi duis adipisicing ad nulla quis.\r\n",
    "registered": "2021-05-22T09:39:01 -02:00",
    "latitude": -13.40349,
    "longitude": -151.844763,
    "tags": [
      "in",
      "tempor",
      "exercitation",
      "veniam",
      "laborum",
      "officia",
      "id"
    ],
    "friends": [
      {
        "id": 0,
        "name": "Atkinson Willis"
      },
      {
        "id": 1,
        "name": "Slater Roach"
      },
      {
        "id": 2,
        "name": "Miriam Holman"
      }
    ],
    "greeting": "Hello, Holcomb Pickett! You have 1 unread messages.",
    "favoriteFruit": "banana"
  },
  {
    "_id": "64b13dbcf413340c5d3189ee",
    "index": 3,
    "guid": "05d93e0d-4463-425f-b3cd-bd79808c3e0a",
    "isActive": true,
    "balance": "$1,160.99",
    "picture": "http://placehold.it/32x32",
    "age": 22,
    "eyeColor": "green",
    "name": "Delores Malone",
    "gender": "female",
    "company": "SNOWPOKE",
    "email": "deloresmalone@snowpoke.com",
    "phone": "+1 (951) 565-2959",
    "address": "712 McKinley Avenue, Brewster, Rhode Island, 378",
    "about": "Elit ea officia dolor quis ipsum commodo elit. Fugiat labore do ad eiusmod laboris ipsum. Adipisicing nulla do officia do excepteur sunt aliqua excepteur incididunt labore ipsum.\r\n",
    "registered": "2022-05-16T11:29:21 -02:00",
    "latitude": 17.271941,
    "longitude": 27.991326,
    "tags": [
      "cupidatat",
      "eiusmod",
      "culpa",
      "aliquip",
      "nulla",
      "nulla",
      "proident"
    ],
    "friends": [
      {
        "id": 0,
        "name": "Fletcher Tate"
      },
      {
        "id": 1,
        "name": "Myra Casey"
      },
      {
        "id": 2,
        "name": "Herman Bowman"
      }
    ],
    "greeting": "Hello, Delores Malone! You have 8 unread messages.",
    "favoriteFruit": "strawberry"
  },
  {
    "_id": "64b13dbc93408c717d1a7524",
    "index": 4,
    "guid": "edac1510-15c2-4bf8-8ebd-bd8889d12460",
    "isActive": true,
    "balance": "$2,083.48",
    "picture": "http://placehold.it/32x32",
    "age": 38,
    "eyeColor": "brown",
    "name": "Priscilla Albert",
    "gender": "female",
    "company": "JUMPSTACK",
    "email": "priscillaalbert@jumpstack.com",
    "phone": "+1 (997) 411-3544",
    "address": "958 Vandam Street, Osmond, California, 8773",
    "about": "Consequat in eu officia enim sunt nulla in dolore. Cillum consequat veniam minim labore est sunt aute. Pariatur pariatur Lorem cillum adipisicing ex exercitation. Irure aute ullamco dolor enim ullamco qui. Occaecat amet elit proident aliquip eu et nostrud non incididunt deserunt officia ullamco. Magna sunt sunt do irure consequat excepteur sunt consectetur ad. Sint mollit eu incididunt deserunt sunt laborum consectetur do aliquip.\r\n",
    "registered": "2019-04-03T07:52:13 -02:00",
    "latitude": -51.763731,
    "longitude": 65.890049,
    "tags": [
      "sit",
      "pariatur",
      "ipsum",
      "et",
      "incididunt",
      "reprehenderit",
      "dolor"
    ],
    "friends": [
      {
        "id": 0,
        "name": "Blanche Mccarty"
      },
      {
        "id": 1,
        "name": "Mccray Walter"
      },
      {
        "id": 2,
        "name": "Georgina Anderson"
      }
    ],
    "greeting": "Hello, Priscilla Albert! You have 9 unread messages.",
    "favoriteFruit": "strawberry"
  },
  {
    "_id": "64b13dbcccbf4ff49ea8e5ab",
    "index": 5,
    "guid": "4d33c9b6-7e78-42dc-b53d-70c03bd357b1",
    "isActive": true,
    "balance": "$3,830.25",
    "picture": "http://placehold.it/32x32",
    "age": 23,
    "eyeColor": "brown",
    "name": "Darla Rodriguez",
    "gender": "female",
    "company": "QIMONK",
    "email": "darlarodriguez@qimonk.com",
    "phone": "+1 (898) 482-2944",
    "address": "146 Eldert Lane, Fairview, Arkansas, 6958",
    "about": "Officia pariatur aliqua dolore nisi anim occaecat excepteur in esse qui nisi. Dolore anim proident fugiat eiusmod dolor duis ea Lorem consectetur officia Lorem laboris non exercitation. Anim eu amet sit aute amet id nostrud fugiat excepteur velit. Magna dolor aliquip consequat eiusmod aute est sunt duis. Irure amet nostrud minim cillum qui laborum nostrud est veniam nisi sunt Lorem velit. Exercitation irure dolor magna sit voluptate aute enim enim consectetur sit velit proident anim ad. Sint et officia ipsum duis cillum mollit enim aliquip eu.\r\n",
    "registered": "2017-04-07T01:53:29 -02:00",
    "latitude": -60.503285,
    "longitude": -161.891942,
    "tags": [
      "ullamco",
      "fugiat",
      "fugiat",
      "reprehenderit",
      "non",
      "ea",
      "voluptate"
    ],
    "friends": [
      {
        "id": 0,
        "name": "Rosalie Cummings"
      },
      {
        "id": 1,
        "name": "Stark Carroll"
      },
      {
        "id": 2,
        "name": "Alyson Sharpe"
      }
    ],
    "greeting": "Hello, Darla Rodriguez! You have 6 unread messages.",
    "favoriteFruit": "banana"
  },
  {
    "_id": "64b13dbc6e2cf83674248e18",
    "index": 6,
    "guid": "95ee8391-4045-4201-8ecd-80180e597cb6",
    "isActive": false,
    "balance": "$1,394.69",
    "picture": "http://placehold.it/32x32",
    "age": 29,
    "eyeColor": "brown",
    "name": "Reba Singleton",
    "gender": "female",
    "company": "DOGNOSIS",
    "email": "rebasingleton@dognosis.com",
    "phone": "+1 (832) 411-2158",
    "address": "340 Dorset Street, Roy, Delaware, 2885",
    "about": "Ut occaecat cillum sint esse deserunt elit cillum qui quis exercitation in. Velit nisi ipsum quis sint mollit ea exercitation cupidatat non nisi ut. Nostrud duis esse dolor Lorem. Nisi tempor aliqua pariatur officia. Commodo nulla eiusmod cupidatat aliquip in mollit. Anim ex nulla do sit. Esse exercitation esse Lorem sit consectetur qui exercitation qui ipsum deserunt.\r\n",
    "registered": "2018-01-31T12:29:27 -01:00",
    "latitude": -2.43137,
    "longitude": 133.655031,
    "tags": [
      "nisi",
      "in",
      "exercitation",
      "enim",
      "exercitation",
      "magna",
      "ea"
    ],
    "friends": [
      {
        "id": 0,
        "name": "Ratliff Tucker"
      },
      {
        "id": 1,
        "name": "Shana Copeland"
      },
      {
        "id": 2,
        "name": "Levy Barlow"
      }
    ],
    "greeting": "Hello, Reba Singleton! You have 9 unread messages.",
    "favoriteFruit": "banana"
  }
]
"#;

build_tree! {
    JSON => #s*, @JValue, #s*;

    Number #=> "-"?, ("0" | [1-9], [0-9]*), (".", [0-9]+, EPart?)?;
    EPart #=> ("E" | "e"), ("-" | "+")?, #d+;

    JString => "\"",
                    @StringFragment*
              ,"\"";

    StringFragment => @[^"\""]+ <NormalChars>
                   |  @Escape <EscapedChar>;

    Escape => "\\", @EscapeType;

    EscapeType =>"n" <Newline>
               | "\\" <Backslash>
               | "\"" <DQuote>
               | "r" <Return>
               | "t" <Tab>
               | "f" <FormFeed>
               | "/" <ForwardSlash>
               | "u", @Hex, @Hex, @Hex, @Hex <Unicode>;

    JValue  =>  @JString
            | @Number
            | @Object
            | @Array
            | "true" <True>
            | "false" <False>
            | "null" <Null>;

    Object => "{", #s*, @ObjectElems?, #s*, "}";
    ObjectKV => @JString, #s*, ":", #s*, @JValue, #s*;
    ObjectElems => @ObjectKV, @(",", #s*, @ObjectKV)*;

    Array => "[", #s*, @ArrayElems?, #s*, "]";
    ArrayElems => @JValue, #s*, @(",", #s*, @JValue, #s*)*;

    Hex #=> [0-9a-fA-F];
}

impl TryInto<HashMap<String, JValue>> for Object {
    type Error = ();
    fn try_into(self) -> Result<HashMap<String, JValue>, Self::Error> {
        let mut hash_map: HashMap<String, JValue> = HashMap::new();
        let elems = match self.0 {
            Some(a) => *a,
            None => return Ok(HashMap::new()),
        };
        let ObjectElems(first, rest) = elems;
        let ObjectKV(k, v) = *first;
        hash_map.insert(k.to_string(), *v).unwrap();
        for item in rest {
            let ObjectKV(k, v) = *item;
            hash_map.insert(k.to_string(), *v).ok_or(())?;
        }
        Ok(hash_map)
    }
}

impl From<Array> for Vec<Box<JValue>> {
    fn from(arr: Array) -> Vec<Box<JValue>> {
        let elems = match arr.0 {
            Some(a) => *a,
            None => return vec![],
        };
        let ArrayElems(first, mut rest) = elems;
        rest.insert(0, first);
        rest
    }
}

impl Into<f64> for Number {
    fn into(self) -> f64 {
        self.0.parse().unwrap()
    }
}

impl std::string::ToString for JString {
    fn to_string(&self) -> String {
        self.0
            .iter()
            .fold("".to_owned(), |x, y| match (*y).as_ref() {
                StringFragment::NormalChars(str) => x + &str,
                StringFragment::EscapedChar(esc) => {
                    let Escape(esc) = esc.as_ref();
                    match esc.as_ref() {
                        EscapeType::Tab() => x + "\t",
                        EscapeType::DQuote() => x + "\"",
                        EscapeType::Return() => x + "\r",
                        EscapeType::Newline() => x + "\n",
                        EscapeType::Backslash() => x + "\\",
                        EscapeType::FormFeed() => x + "\u{000C}",
                        EscapeType::ForwardSlash() => x + "/",
                        EscapeType::Unicode(a, b, c, d) => {
                            let (Hex(a), Hex(b), Hex(c), Hex(d)) =
                                (a.as_ref(), b.as_ref(), c.as_ref(), d.as_ref());
                            let val = format!("{}{}{}{}", a, b, c, d);
                            let val = u32::from_str_radix(&val, 16)
                                .ok()
                                .and_then(std::char::from_u32)
                                .unwrap()
                                .to_string();
                            x + &val
                        }
                    }
                }
            })
    }
}

fn main() {
    let parsed = Array::parse(
        r#"[ true, -1888300249.8418272, 1140199957, -1319010735, "success", -1523848246.6416917 ]"#,
    );
    let _parsed = match parsed {
        Ok((_, out)) => out,
        Err(nom::Err::Error(e)) => panic!(
            "{}",
            nom::error::convert_error(
                r#"[ true, -1888300249.8418272, 1140199957, -1319010735, "success", -1523848246.6416917 ]"#,
                e
            )
        ),
        _ => panic!("??"),
    };
    // println!("{:#?}", parsed);

    // let input = std::fs::read_to_string("../../../examples/test.json").unwrap();
    //
    //    // let t = std::time::Instant::now();
    // let t = std::time::Instant::now();
    let parsed = JSON::parse(TEST1);
    // println!("Time to parse: {:?}", t.elapsed());
    let _parsed: Vec<Box<JValue>> = match parsed {
        Ok((_, out)) => match *out {
            JSON(val) => match *val {
                JValue::Array(arr) => (*arr).into(),
                _ => panic!("Unexpected JSON Datatype parsed"),
            },
        },
        Err(nom::Err::Error(e)) => panic!("{}", nom::error::convert_error(
                TEST1,
                e
            )),
        _ => panic!("Some error in the source code"),
    };
    //    println!("{:#?}", parsed);
}