summaryrefslogtreecommitdiffstats
path: root/src/poem/read/parse.rs
blob: 0836eea478037120ae6370745ae3ba09986a181c (plain)
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
/// Look ahead one character in the input
///
/// May need to look ahead one character in the input string to determine the
/// proper rune. For instance `&`, vs `&&`.
#[macro_export]
macro_rules! next {
    ($chars:expr, $i:expr, $otherwise:expr, $rune:expr, $ahead:expr) => {
        match $chars.clone().peekable().peek() {
            Some(c) if *c == $ahead => {
                $chars.next();
                $i += 1;
                $rune
            }
            Some(_) => $otherwise,
            None => $otherwise,
        }
    };
}

/// Keep pushing to the [Word][super::super::elements::word::Word] stack
///
/// If a [Rune::String][super::super::elements::rune::Rune] character is found,
/// stop interpreting special characters, and push all characters to the
/// [Word][super::super::elements::word::Word] stack, until the corresponding
/// [Rune::String][super::super::elements::rune::Rune] character is found.
#[macro_export]
macro_rules! string {
    ($chars:expr, $j:expr, $i:expr, $c:expr, $word:expr) => {
        let token = $c;
        loop {
            match $chars.next() {
                None => return Err(Mishap::PartialMishap($j, $i, $c)),
                Some(c) if c == token => break,
                Some(c) if token == '\'' && c == '$' => {
                    $word.push('\x0e');
                    $i += 1;
                }
                Some(c) => {
                    $word.push(c);
                    $i += 1;
                }
            }
        }
        continue;
    };
}

/// Same as [string!] macro, but look for newline or EOF
#[macro_export]
macro_rules! remark {
    ($chars:expr) => {
        loop {
            match $chars.next() {
                None => break,
                Some(c) if c == '\n' => break,
                Some(_) => {}
            }
        }
        continue;
    };
}

/// Same as the [string!] macro, but don't `continue`
#[macro_export]
macro_rules! poem {
    ($chars:expr, $j:expr, $i:expr, $c:expr, $verse:expr, $word:expr) => {
        let token = $c;
        let mut poetry = Word::new();
        loop {
            match $chars.next() {
                None => return Err(Mishap::PartialMishap($j, $i, $c)),
                Some(c) if c == token => break,
                Some(c) => {
                    poetry.push(c);
                    $i += 1;
                }
            }
        }
        let sp = Poem::read(poetry.iter().collect());
        let sp = match sp {
            Ok(sp) => sp,
            Err(e) => return Err(e),
        };
        $verse.poems.push(sp);
        $word.push('\x0b');
    };
}