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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
use nom::branch::alt;
use nom::bytes::complete::{escaped, is_not, tag, take_while, take_while_m_n};
use nom::character::complete::{multispace0, none_of, one_of};
use nom::combinator::eof;
use nom::multi::{fold_many1, many0};
use nom::sequence::{delimited, preceded, tuple};
use nom::IResult;
use nom_locate::{position, LocatedSpan};
use serde::{ser::*, Deserialize, Serialize};

/// Describes a section of text at a known location in a source file.
pub type Span<'a> = LocatedSpan<&'a str>;

/// Describes a location in a source file starting from an offset.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
pub struct SpanOffset {
    from: usize,
    offset: usize,
    line: u32,
    column: usize,
}

impl SpanOffset {
    fn adjust_with_prefix(self, offset: usize) -> Self {
        Self {
            from: self.from + offset,
            ..self
        }
    }
}

fn span_serialize<S>(x: &Option<Span<'_>>, s: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    match *x {
        Some(ref x) => {
            let mut sp = s.serialize_struct("Span", 2)?;
            sp.serialize_field("column", &x.get_utf8_column())?;
            sp.serialize_field("line", &x.location_line())?;
            sp.end()
        }
        None => s.serialize_none(),
    }
}

fn hex2(input: Span) -> IResult<Span, u8> {
    let (s, b) = take_while_m_n(2, 2, |c: char| c.is_digit(16))(input)?;

    Ok((
        s,
        u8::from_str_radix(&b, 16)
            .map_err(|_| nom::Err::Error(nom::error::Error::new(s, nom::error::ErrorKind::Tag)))?,
    ))
}

fn parse_hex_color(input: Span) -> IResult<Span, (u8, u8, u8)> {
    let (input, _) = tag("#")(input)?;
    let (input, (red, green, blue)) = tuple((hex2, hex2, hex2))(input)?;

    Ok((input, (red, green, blue)))
}

fn parse_single_quoted(input: Span) -> IResult<Span, Span> {
    let esc = escaped(none_of("\\\'"), '\\', tag("'"));
    let esc_or_empty = alt((esc, tag("")));

    delimited(tag("'"), esc_or_empty, tag("'"))(input)
}

fn parse_double_quoted(input: Span) -> IResult<Span, Span> {
    let esc = escaped(none_of("\\\""), '\\', tag("\""));
    let esc_or_empty = alt((esc, tag("")));

    delimited(tag("\""), esc_or_empty, tag("\""))(input)
}

fn parse_bracket(input: Span) -> IResult<Span, Span> {
    let esc = escaped(none_of("\\[]"), '\\', one_of("[]"));
    let esc_or_empty = alt((esc, tag("")));

    delimited(tag("["), esc_or_empty, tag("]"))(input)
}

fn arg(s: Span) -> IResult<Span, (Span, String)> {
    let (s, pos) = position(s)?;
    let (s, bits) = fold_many1(
        alt((parse_single_quoted, parse_double_quoted, is_not(" '\"\\[]"))),
        Vec::new(),
        |mut acc: Vec<_>, item| {
            acc.push(item);
            acc
        },
    )(s)?;

    Ok((
        s,
        bits.into_iter()
            .fold((pos, String::with_capacity(32)), |acc, x| {
                let (pos, mut s) = acc;
                s.push_str(&x);
                (pos, s)
            }),
    ))
}

fn var(s: Span) -> IResult<Span, Span> {
    let (s, val) = preceded(tag("$"), is_not(" '\"\\[],\n;+"))(s)?;
    let mut ending = alt((tag(" "), tag("\t"), tag("\n"), tag("\r"), eof));

    match ending(s) {
        Ok(_) => Ok((s, val)),
        Err(e) => Err(e),
    }
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "kind", content = "atom")]
pub enum AtomContent {
    Arg(String),
    Var(String),
    Bracket(String),
    Rgb(u8, u8, u8),
}

impl Into<String> for AtomContent {
    fn into(self) -> String {
        match self {
            AtomContent::Arg(a) => a,
            AtomContent::Var(v) => {
                let mut out = String::with_capacity(v.len() + 1);
                out.push('$');
                out.push_str(&v);
                out
            }
            AtomContent::Bracket(v) => {
                let mut out = String::with_capacity(v.len() + 2);
                out.push('[');
                out.push_str(&v);
                out.push(']');
                out
            }
            AtomContent::Rgb(r, g, b) => String::from(format!("#{:02X}{:02X}{:02X}", r, g, b)),
        }
    }
}

/// The smallest unit of information that makes up a command.
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Atom {
    pub content: AtomContent,
    pub start_offset: usize,
    pub end_offset: usize,
    pub line: u32,
    pub column: usize,
}

/// Parses an atom.
pub fn atom(mut s: Span) -> IResult<Span, Atom> {
    if let Ok((i, _)) = multispace0::<_, ()>(s) {
        s = i;
    }
    let (s, p) = position(s)?;

    if let Ok((s, var)) = var(s) {
        let str = var.fragment().to_string();
        return Ok((
            s,
            Atom {
                start_offset: var.location_offset() - 1,
                end_offset: s.location_offset(),
                content: AtomContent::Var(str),
                line: 0,
                column: 0,
            },
        ));
    }

    if let Ok((s, b)) = parse_bracket(s) {
        return Ok((
            s,
            Atom {
                start_offset: p.location_offset(),
                end_offset: s.location_offset(),
                content: AtomContent::Bracket(b.to_string()),
                line: 0,
                column: 0,
            },
        ));
    }

    if let Ok((s, (r, g, b))) = parse_hex_color(s) {
        return Ok((
            s,
            Atom {
                start_offset: p.location_offset(),
                end_offset: s.location_offset(),
                content: AtomContent::Rgb(r, g, b),
                line: 0,
                column: 0,
            },
        ));
    }

    if let Ok((s, arg)) = arg(s) {
        let (pos, arg) = arg;
        return Ok((
            s,
            Atom {
                start_offset: pos.location_offset(),
                end_offset: s.location_offset(),
                content: AtomContent::Arg(arg),
                line: 0,
                column: 0,
            },
        ));
    }

    return Err(nom::Err::Error(nom::error::Error::new(
        s,
        nom::error::ErrorKind::Tag,
    )));
}

/// Line describes a single unit of configuration.
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Line<'a> {
    #[serde(serialize_with = "span_serialize")]
    #[serde(skip_deserializing)]
    pub start: Option<Span<'a>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub continuation_spans: Option<Vec<SpanOffset>>,
    pub line: String,
}

impl<'a> Line<'a> {
    pub(crate) fn atoms(&'a self) -> Result<Vec<Atom>, nom::Err<nom::error::Error<Span<'a>>>> {
        let input = Span::new(&self.line);
        let span = self.start.unwrap();

        let (_, mut atoms) = many0(atom)(input)?;

        // Compute the offsets/locations for each atom.
        for atom in atoms.iter_mut() {
            let (s, e) = (atom.start_offset, atom.end_offset);
            let (start, end) = (span.location_offset() + s, span.location_offset() + e);
            *atom = Atom {
                start_offset: start,
                end_offset: end,
                line: span.location_line(),
                column: span.get_utf8_column() + atom.start_offset,
                ..atom.clone()
            };

            // There may be continuation spans present, which would mean we
            // need to update our offset and line information.
            for span in self.continuation_spans.iter().flatten() {
                // println!("span={:?}, atom={:?}\n", span, atom);
                if s >= span.from {
                    *atom = Atom {
                        start_offset: span.offset + (s - span.from),
                        end_offset: span.offset + (e - span.from),
                        line: span.line,
                        column: span.column + (s - span.from),
                        ..atom.clone()
                    };
                }
                if e >= span.from {
                    *atom = Atom {
                        end_offset: span.offset + (e - span.from),
                        ..atom.clone()
                    };
                }
            }
        }

        Ok(atoms)
    }
}

#[inline(always)]
fn is_cmd_char(chr: char) -> bool {
    chr != '\n' && chr != '{' && chr != '}'
}

pub fn line(i: Span) -> IResult<Span, Line> {
    let (s, pos) = position(i)?;
    let (s, v) = take_while(is_cmd_char)(s)?;

    if v.fragment().len() == 0 {
        return Err(nom::Err::Error(nom::error::Error::new(
            s,
            nom::error::ErrorKind::Tag,
        )));
    }

    // Handle potential line continuation
    if v.fragment().ends_with('\\') {
        if let Ok((mut s2, _newline)) = tag::<_, _, ()>("\n")(s) {
            let mut line_tmp = v.fragment().to_string();
            line_tmp.pop(); // Drop the trailing backslash

            if let Ok((s3, space)) = multispace0::<_, ()>(s2) {
                s2 = s3;
                line_tmp.push_str(&space);
            }

            let (s2, eol_pos) = position(s2)?;
            return Ok(match line(s2) {
                Ok((s2, l)) => {
                    let mut offsets: Vec<SpanOffset> = Vec::new();
                    offsets.push(SpanOffset {
                        from: line_tmp.len(),
                        offset: eol_pos.location_offset(),
                        line: eol_pos.location_line(),
                        column: eol_pos.get_utf8_column(),
                    });
                    if let Some(o) = l.continuation_spans {
                        offsets.extend_from_slice(
                            &o.into_iter()
                                .map(|so| so.adjust_with_prefix(line_tmp.len()))
                                .collect::<Vec<_>>(),
                        );
                    }

                    line_tmp.push_str(&l.line);
                    (
                        s2,
                        Line {
                            start: Some(pos),
                            continuation_spans: Some(offsets),
                            line: line_tmp,
                        },
                    )
                }
                _ => (
                    s2,
                    Line {
                        start: Some(pos),
                        continuation_spans: None,
                        line: line_tmp,
                    },
                ),
            });
        }
    }

    Ok((
        s,
        Line {
            start: Some(pos),
            continuation_spans: None,
            line: v.fragment().to_string(),
        },
    ))
}

pub fn unary_comment(i: Span) -> IResult<Span, Line> {
    let (s, pos) = position(i)?;
    let (s, _) = tag("#")(s)?;
    let (s, v) = take_while(|chr| chr != '\n')(s)?;

    Ok((
        s,
        Line {
            start: Some(pos),
            continuation_spans: None,
            line: v.fragment().to_string(),
        },
    ))
}

/// Token describes the type and location of some character
/// that has meaning when parsing.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
pub struct Token<'a> {
    #[serde(serialize_with = "span_serialize")]
    #[serde(skip_deserializing)]
    pub position: Option<Span<'a>>,
    pub tok: char,
}

pub fn open_brace(i: Span) -> IResult<Span, Token> {
    let (s, pos) = position(i)?;
    let (s, _) = tag("{")(s)?;
    Ok((
        s,
        Token {
            position: Some(pos),
            tok: '{',
        },
    ))
}

pub fn close_brace(i: Span) -> IResult<Span, Token> {
    let (s, pos) = position(i)?;
    let (s, _) = tag("}")(s)?;
    Ok((
        s,
        Token {
            position: Some(pos),
            tok: '}',
        },
    ))
}

#[cfg(test)]
mod tests {
    use super::*;
    use nom::character::complete::multispace0;
    use pretty_assertions::assert_eq as assert_pretty;

    #[test]
    fn line_basic() {
        let input = Span::new("Lorem ipsum \n foobar");
        let output = line(input);
        let line = &output.as_ref().unwrap().1.line;
        let position = output.as_ref().unwrap().1.start.unwrap();
        assert_eq!(line, "Lorem ipsum ");
        assert_eq!(position.get_column(), 1);
    }

    #[test]
    fn line_no_newline() {
        let input = Span::new("Lorem ipsum ");
        let output = line(input);
        let line = &output.as_ref().unwrap().1.line;
        let position = output.as_ref().unwrap().1.start.unwrap();
        assert_eq!(line, "Lorem ipsum ");
        assert_eq!(position.get_column(), 1);
    }

    #[test]
    fn line_with_brace() {
        let input = Span::new("Lorem ipsum { YE");
        let output = line(input);
        let line = &output.as_ref().unwrap().1.line;
        let position = output.as_ref().unwrap().1.start.unwrap();
        assert_eq!(line, "Lorem ipsum ");
        assert_eq!(position.get_column(), 1);
    }

    #[test]
    fn unary_comment_basic() {
        let input = Span::new("#Blueberries");
        let output = unary_comment(input);
        let line = &output.as_ref().unwrap().1.line;
        let position = output.as_ref().unwrap().1.start.unwrap();
        assert_eq!(line, "Blueberries");
        assert_eq!(position.get_column(), 1);
    }
    #[test]
    fn unary_comment_err() {
        let input = Span::new("set $mod Mod4");
        let output = unary_comment(input);
        assert!(
            !output.is_ok(),
            "parser did not error, returned {:?}",
            output
        );
    }

    #[test]
    fn open_brace_basic() {
        let input = Span::new("    {\nsomething");
        let (input, _) = multispace0::<_, ()>(input).unwrap();
        let output = open_brace(input);
        let tok = output.as_ref().unwrap().1;
        assert_eq!(tok.position.unwrap().get_column(), 5);
        assert_eq!(output.as_ref().unwrap().0.fragment(), &"\nsomething");
    }

    #[test]
    fn line_continuation_spans() {
        let input = Span::new("Something \\\n that  do\\\nes\\\n  cross lines!");
        let output = line(input);
        let line = &output.as_ref().unwrap().1.line;
        let position = output.as_ref().unwrap().1.start.unwrap();
        assert_eq!(line, "Something  that  does  cross lines!");
        assert_eq!(position.get_column(), 1);

        for (i, span) in output
            .as_ref()
            .unwrap()
            .1
            .continuation_spans
            .as_ref()
            .unwrap()
            .iter()
            .enumerate()
        {
            match i {
                0 => {
                    assert_eq!(&line[..span.from], "Something  ");
                    assert_eq!(span.line, 2);
                    assert_eq!(span.column, 2);
                    assert_eq!(span.offset, 13);
                }
                1 => {
                    assert_eq!(&line[11..span.from], "that  do");
                    assert_eq!(span.line, 3);
                    assert_eq!(span.column, 1);
                    assert_eq!(span.offset, 23);
                }
                2 => {
                    assert_eq!(&line[19..span.from], "es  ");
                    assert_eq!(span.line, 4);
                    assert_eq!(span.column, 3);
                    assert_eq!(span.offset, 29);

                    assert_eq!(&line[span.from..], "cross lines!");
                }
                _ => assert!(false, "Unexpected span index: {}", i),
            }
        }
    }

    #[test]
    fn atoms_basic() {
        let input = Span::new("set $alt   Mod1");
        let want = vec![
            Atom {
                content: AtomContent::Arg("set".to_string()),
                start_offset: 0,
                end_offset: 3,
                line: 1,
                column: 1,
            },
            Atom {
                content: AtomContent::Var("alt".to_string()),
                start_offset: 4,
                end_offset: 8,
                line: 1,
                column: 5,
            },
            Atom {
                content: AtomContent::Arg("Mod1".to_string()),
                start_offset: 11,
                end_offset: 15,
                line: 1,
                column: 12,
            },
        ];

        let atoms = line(input).unwrap().1.atoms().expect("atom parsing failed");
        assert_pretty!(atoms, want);
    }

    #[test]
    fn atoms_strings_single_quote() {
        let input = Span::new("cmd 'swiggity swooty' 'Yolo swaggins'");
        let want = vec![
            Atom {
                content: AtomContent::Arg("cmd".to_string()),
                start_offset: 0,
                end_offset: 3,
                line: 1,
                column: 1,
            },
            Atom {
                content: AtomContent::Arg("swiggity swooty".to_string()),
                start_offset: 4,
                end_offset: 21,
                line: 1,
                column: 5,
            },
            Atom {
                content: AtomContent::Arg("Yolo swaggins".to_string()),
                start_offset: 22,
                end_offset: 37,
                line: 1,
                column: 23,
            },
        ];

        let atoms = line(input).unwrap().1.atoms().expect("atom parsing failed");
        assert_pretty!(atoms, want);
    }

    #[test]
    fn atoms_strings_double_quote() {
        let input = Span::new("cmd \"swiggity swooty\" \"I'm \\\"quoted\\\"\"");
        let want = vec![
            Atom {
                content: AtomContent::Arg("cmd".to_string()),
                start_offset: 0,
                end_offset: 3,
                line: 1,
                column: 1,
            },
            Atom {
                content: AtomContent::Arg("swiggity swooty".to_string()),
                start_offset: 4,
                end_offset: 21,
                line: 1,
                column: 5,
            },
            Atom {
                content: AtomContent::Arg("I'm \\\"quoted\\\"".to_string()),
                start_offset: 22,
                end_offset: 38,
                line: 1,
                column: 23,
            },
        ];

        let atoms = line(input).unwrap().1.atoms().expect("atom parsing failed");
        assert_pretty!(atoms, want);
    }

    #[test]
    fn atoms_multiline_in_quotes() {
        let input = Span::new("cmd 'ye\\\now' \\\narg");
        let want = vec![
            Atom {
                content: AtomContent::Arg("cmd".to_string()),
                start_offset: 0,
                end_offset: 3,
                line: 1,
                column: 1,
            },
            Atom {
                content: AtomContent::Arg("yeow".to_string()),
                start_offset: 4,
                end_offset: 12,
                line: 1,
                column: 5,
            },
            Atom {
                content: AtomContent::Arg("arg".to_string()),
                start_offset: 15,
                end_offset: 18,
                line: 3,
                column: 1,
            },
        ];

        let atoms = line(input).unwrap().1.atoms().expect("atom parsing failed");
        assert_pretty!(atoms, want);
    }

    #[test]
    fn escaped_in_braces() {
        let input = Span::new(" [cmd=\"\\[\\]\"]");
        let want = vec![Atom {
            content: AtomContent::Bracket("cmd=\"\\[\\]\"".to_string()),
            start_offset: 1,
            end_offset: 13,
            line: 1,
            column: 2,
        }];

        let atoms = line(input).unwrap().1.atoms().expect("atom parsing failed");
        assert_pretty!(atoms, want);
    }

    #[test]
    fn atoms_bindings() {
        let input = Span::new("set $alt+$mod+t");
        let want = vec![
            Atom {
                content: AtomContent::Arg("set".to_string()),
                start_offset: 0,
                end_offset: 3,
                line: 1,
                column: 1,
            },
            Atom {
                content: AtomContent::Arg("$alt+$mod+t".to_string()),
                start_offset: 4,
                end_offset: 15,
                line: 1,
                column: 5,
            },
        ];

        let atoms = line(input).unwrap().1.atoms().expect("atom parsing failed");
        assert_pretty!(atoms, want);
    }
}