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
use crate::constants::{LBRACE, LBRACK, NEWLINE, RBRACE, RBRACK};
use crate::node_pool::NodeID;
use crate::types::{Cursor, MatchError, ParseOpts, Parseable, Parser, Result};
use crate::utils::Match;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InlineSrc<'a> {
    pub lang: &'a str,
    pub headers: Option<&'a str>,
    pub body: &'a str,
}

impl<'a> Parseable<'a> for InlineSrc<'a> {
    fn parse(
        parser: &mut Parser<'a>,
        mut cursor: Cursor<'a>,
        parent: Option<NodeID>,
        parse_opts: ParseOpts,
    ) -> Result<NodeID> {
        let start = cursor.index;
        cursor.word("src_")?;

        let lang =
            cursor.fn_until(|chr: u8| chr == b'[' || chr == b'{' || chr.is_ascii_whitespace())?;

        cursor.index = lang.end;

        match cursor.curr() {
            LBRACE => {
                let body = Self::parse_body(cursor)?;
                Ok(parser.alloc(
                    Self {
                        lang: lang.obj,
                        headers: None,
                        body: body.obj,
                    },
                    start,
                    body.end,
                    None,
                ))
            }
            LBRACK => {
                let header = Self::parse_header(cursor)?;
                cursor.move_to(header.end);
                if cursor.curr() == LBRACE {
                    let body = Self::parse_body(cursor)?;
                    Ok(parser.alloc(
                        Self {
                            lang: lang.obj,
                            headers: Some(header.obj),
                            body: body.obj,
                        },
                        start,
                        body.end,
                        parent,
                    ))
                } else {
                    Err(MatchError::InvalidLogic)
                }
            }
            // We are whitespace here, which means there was whitespace after the src_
            // so blow up
            _ => Err(MatchError::InvalidLogic),
        }
    }
}

impl<'a> InlineSrc<'a> {
    // the logic is exactly the same, except for the perimeters
    fn parse_header(cursor: Cursor) -> Result<Match<&str>> {
        InlineSrc::parse_src(cursor, LBRACK, RBRACK)
    }
    fn parse_body(cursor: Cursor) -> Result<Match<&str>> {
        InlineSrc::parse_src(cursor, LBRACE, RBRACE)
    }
    #[inline(always)]
    fn parse_src(mut cursor: Cursor, lperim: u8, rperim: u8) -> Result<Match<&str>> {
        // Brackets have to be balanced
        // -1 for left bracket
        // +1 for right bracket
        let mut bracket_count: i32 = 0;

        let start = cursor.index;
        loop {
            match cursor.curr() {
                chr if chr == lperim => {
                    bracket_count -= 1;
                }
                chr if chr == rperim => {
                    bracket_count += 1;
                    if bracket_count == 0 {
                        return Ok(Match {
                            start,
                            // +1 to skip past lperim and rperim
                            end: cursor.index + 1,
                            obj: cursor.clamp_backwards(start + 1),
                        });
                    }
                }
                NEWLINE => {
                    return Err(MatchError::InvalidLogic);
                }
                _ => {}
            } // end of match

            cursor.next();
        } // end of loop
    }
}

#[cfg(test)]
mod tests {
    use crate::expr_in_pool;
    use crate::object::InlineSrc;
    use crate::parse_org;
    use crate::types::Expr;
    use pretty_assertions::assert_eq;

    #[test]
    fn basic_src() {
        let input = "src_python{neat}";

        let parsed = parse_org(input);
        let l = expr_in_pool!(parsed, InlineSrc).unwrap();

        assert_eq!(
            l,
            &InlineSrc {
                lang: "python",
                headers: None,
                body: "neat"
            }
        )
    }

    #[test]
    fn inlinesrc_header() {
        let input = "src_python[fun]{rad}";

        let parsed = parse_org(input);
        let l = expr_in_pool!(parsed, InlineSrc).unwrap();

        assert_eq!(
            l,
            &InlineSrc {
                lang: "python",
                headers: Some("fun"),
                body: "rad"
            }
        )
    }
}