org-rust-parser 0.1.7

parser for org mode documents
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
use crate::constants::{
    BACKSLASH, CARET, COLON, DOLLAR, EQUAL, HYPHEN, LANGLE, LBRACE, LBRACK, NEWLINE, PLUS, POUND,
    RBRACE, RBRACK, SLASH, STAR, TILDE, UNDERSCORE, VBAR,
};
use crate::node_pool::NodeID;

use crate::element::{
    Block, Comment, Drawer, FootnoteDef, Heading, Item, Keyword, LatexEnv, Paragraph, PlainList,
    Table,
};
use crate::object::{
    Bold, Code, Emoji, ExportSnippet, FootnoteRef, InlineSrc, Italic, LatexFragment, MacroCall,
    RegularLink, StrikeThrough, Subscript, Superscript, Target, Underline, Verbatim,
    parse_angle_link, parse_plain_link,
};
use crate::types::{Cursor, Expr, MarkupKind, MatchError, ParseOpts, Parseable, Parser, Result};
use crate::utils::verify_markup;

pub(crate) fn parse_element<'a>(
    parser: &mut Parser<'a>,
    mut cursor: Cursor<'a>,
    parent: Option<NodeID>,
    parse_opts: ParseOpts,
) -> Result<NodeID> {
    if let Some(id) = parser.cache.get(&cursor.index) {
        return Ok(*id);
    }

    cursor.curr_valid()?;
    // means a newline checking thing called this, and newline breaks all
    // table rows
    if parse_opts.markup.contains(MarkupKind::Table) {
        return Err(MatchError::MarkupEnd(MarkupKind::Table));
    }

    // indentation check
    let mut indented_loc = cursor.index;
    let mut new_opts = parse_opts;
    loop {
        let byte = *cursor.get(indented_loc).ok_or(MatchError::InvalidLogic)?;
        if byte.is_ascii_whitespace() {
            if byte == NEWLINE {
                return Ok(parser.alloc(Expr::BlankLine, cursor.index, indented_loc + 1, parent));
            } else {
                new_opts.indentation_level += 1;
                indented_loc += 1;
            }
        }
        // every element will explode if there's an indentation level
        // except for lists
        else {
            break;
        }
    }

    let indentation_level = indented_loc - cursor.index;

    // the min indentation level is 0, if it manages to be less than parse_opts' indentation
    // level then we're in a list

    cursor.move_to(indented_loc);
    if let Some(id) = parser.cache.get(&cursor.index) {
        return Ok(*id);
    }

    // let elements have child paragraph elements when they propogate,
    // from_paragraph is only to prevent recursing into a paragraph
    // TODO: less weird to express this maybe..?
    let mut no_para_opts = parse_opts;
    no_para_opts.from_paragraph = false;
    new_opts.from_paragraph = false;

    // for lists: items don't keep track of their indentation level
    if !parse_opts.list_line {
        if indentation_level + 1 == parse_opts.indentation_level.into()
            && parse_opts.from_list
            // stop unindented headings from being lists
            && !(indentation_level == 0 && cursor.curr() == STAR)
        {
            if let ret @ Ok(_) = Item::parse(parser, cursor, parent, new_opts) {
                return ret;
            } else {
                return Err(MatchError::InvalidIndentation);
            }
        } else if indentation_level < parse_opts.indentation_level.into() {
            return Err(MatchError::InvalidIndentation);
        }
    }

    match cursor.curr() {
        STAR => {
            // parse_opts, (doesn't totally matter to use the default vs preloaded,
            // since we account for it, but default makes more sense maybe>?)
            if (indentation_level) > 0 {
                if let ret @ Ok(_) = PlainList::parse(parser, cursor, parent, new_opts) {
                    return ret;
                }
            } else if let ret @ Ok(_) = Heading::parse(parser, cursor, parent, ParseOpts::default())
            {
                return ret;
            }
        }
        PLUS => {
            if let ret @ Ok(_) = PlainList::parse(parser, cursor, parent, new_opts) {
                return ret;
            }
        }
        HYPHEN => {
            let hrule_parse = |parser: &mut Parser,
                               mut cursor: Cursor,
                               parent: Option<NodeID>,
                               _new_opts: ParseOpts|
             -> Result<NodeID> {
                // handle Hrule: at least 5 consecutive hyphens
                let start = cursor.index;
                while let Ok(curr_item) = cursor.try_curr() {
                    if HYPHEN == curr_item {
                        cursor.next();
                    } else {
                        break;
                    }
                }
                if NEWLINE == cursor.try_curr()? && cursor.index - start >= 5 {
                    Ok(parser.alloc(Expr::HorizontalRule, start, cursor.index + 1, parent))
                } else {
                    Err(MatchError::InvalidLogic)
                }
            };

            if let ret @ Ok(_) = hrule_parse(parser, cursor, parent, new_opts) {
                return ret;
            } else if let ret @ Ok(_) = PlainList::parse(parser, cursor, parent, new_opts) {
                return ret;
            }
        }
        chr if chr.is_ascii_alphanumeric() => {
            if let ret @ Ok(_) = PlainList::parse(parser, cursor, parent, new_opts) {
                return ret;
            }
        }
        POUND => {
            if let ret @ Ok(_) = Keyword::parse(parser, cursor, parent, no_para_opts) {
                return ret;
            } else if let ret @ Ok(_) = Block::parse(parser, cursor, parent, no_para_opts) {
                return ret;
            } else if let ret @ Ok(_) = Comment::parse(parser, cursor, parent, no_para_opts) {
                return ret;
            }
        }
        BACKSLASH => {
            if let ret @ Ok(_) = LatexEnv::parse(parser, cursor, parent, no_para_opts) {
                return ret;
            }
        }
        VBAR => {
            if let ret @ Ok(_) = Table::parse(parser, cursor, parent, no_para_opts) {
                return ret;
            }
        }
        COLON => {
            if let ret @ Ok(_) = Drawer::parse(parser, cursor, parent, no_para_opts) {
                return ret;
            }
        }
        LBRACK => {
            if indentation_level == 0
                && let ret @ Ok(_) = FootnoteDef::parse(parser, cursor, parent, no_para_opts)
            {
                return ret;
            }
        }
        _ => {}
    }

    if !parse_opts.from_paragraph {
        Paragraph::parse(parser, cursor, parent, parse_opts)
    } else {
        Err(MatchError::InvalidLogic)
    }
}

macro_rules! handle_markup {
    ($name: tt, $parser: ident, $cursor: ident, $parent: ident, $parse_opts: ident) => {
        if $parse_opts.markup.contains(MarkupKind::$name) {
            if verify_markup($cursor, true) {
                return Err(MatchError::MarkupEnd(MarkupKind::$name));
            } else {
                return Err(MatchError::InvalidLogic);
            }
        } else if let ret @ Ok(_) = $name::parse($parser, $cursor, $parent, $parse_opts) {
            return ret;
        }
    };
}

pub(crate) fn parse_object<'a>(
    parser: &mut Parser<'a>,
    mut cursor: Cursor<'a>,
    parent: Option<NodeID>,
    mut parse_opts: ParseOpts,
) -> Result<NodeID> {
    if let Some(id) = parser.cache.get(&cursor.index) {
        return Ok(*id);
    }

    match cursor.try_curr()? {
        SLASH => {
            handle_markup!(Italic, parser, cursor, parent, parse_opts);
        }
        STAR => {
            handle_markup!(Bold, parser, cursor, parent, parse_opts);
        }
        UNDERSCORE => {
            handle_markup!(Underline, parser, cursor, parent, parse_opts);

            if let ret @ Ok(_) = Subscript::parse(parser, cursor, parent, parse_opts) {
                return ret;
            }
        }
        PLUS => {
            handle_markup!(StrikeThrough, parser, cursor, parent, parse_opts);
        }
        EQUAL => {
            if let ret @ Ok(_) = Verbatim::parse(parser, cursor, parent, parse_opts) {
                return ret;
            }
        }
        TILDE => {
            if let ret @ Ok(_) = Code::parse(parser, cursor, parent, parse_opts) {
                return ret;
            }
        }
        LBRACK => {
            if let ret @ Ok(_) = RegularLink::parse(parser, cursor, parent, parse_opts) {
                return ret;
            } else if let ret @ Ok(_) = FootnoteRef::parse(parser, cursor, parent, parse_opts) {
                return ret;
            }
        }
        RBRACK => {
            // ripped off handle_markup
            // TODO: abstract this
            // if we're in a link description, and we hit ]] , return the ending
            if parse_opts.markup.contains(MarkupKind::Link)
                && let Ok(byte) = cursor.peek(1)
                && byte == RBRACK
            {
                return Err(MatchError::MarkupEnd(MarkupKind::Link));
            } else if parse_opts.markup.contains(MarkupKind::FootnoteRef) {
                return Err(MatchError::MarkupEnd(MarkupKind::FootnoteRef));
            }
        }
        BACKSLASH => {
            if cursor.peek(1)? == BACKSLASH {
                //  \\SPACE
                // SPACE:  Zero or more tab and space characters.
                let start = cursor.index;
                cursor.index += 2;
                cursor.skip_ws();
                // this checks if try_curr is a newline, doesn't capture
                if let Ok(NEWLINE) = cursor.try_curr() {
                    return Ok(parser.alloc(Expr::LineBreak, start, cursor.index + 1, parent));
                } else {
                    cursor.index = start;
                }
            } else if let ret @ Ok(_) = LatexFragment::parse(parser, cursor, parent, parse_opts) {
                return ret;
            }
        }
        DOLLAR => {
            if let ret @ Ok(_) = LatexFragment::parse(parser, cursor, parent, parse_opts) {
                return ret;
            }
        }
        NEWLINE => {
            parse_opts.list_line = false;
            // REVIEW: added to make parsing  a table from a NEWLINE
            // work, not sure if needed elsewhere i.e. why didn't i catch
            // this earlier? any other affected elements?
            parse_opts.from_object = false;

            match parse_element(parser, cursor.adv_copy(1), parent, parse_opts) {
                Err(MatchError::InvalidLogic) => {
                    return Ok(parser.alloc(
                        Expr::SoftBreak,
                        cursor.index,
                        cursor.index + 1,
                        parent,
                    ));
                }
                // EofError isn't exactly the right error for the Ok(_) case
                // but we do it to send a signal to `parse_text` to stop collecting:
                // it keeps collecting while eating InvalidLogic
                Ok(_) | Err(MatchError::EofError) => return Err(MatchError::EofError),
                // propogate the error back up
                ret @ Err(_) => return ret,
            }
        }
        LANGLE => {
            if let ret @ Ok(_) = parse_angle_link(parser, cursor, parent, parse_opts) {
                return ret;
            } else if let ret @ Ok(_) = Target::parse(parser, cursor, parent, parse_opts) {
                return ret;
            }
        }
        VBAR => {
            if parse_opts.markup.contains(MarkupKind::Table) {
                return Err(MatchError::MarkupEnd(MarkupKind::Table));
            }
        }
        COLON => {
            if let ret @ Ok(_) = Emoji::parse(parser, cursor, parent, parse_opts) {
                return ret;
            }
        }
        RBRACE => {
            if parse_opts.markup.contains(MarkupKind::SupSub) {
                return Err(MatchError::MarkupEnd(MarkupKind::SupSub));
            }
        }
        CARET => {
            if let ret @ Ok(_) = Superscript::parse(parser, cursor, parent, parse_opts) {
                return ret;
            }
        }
        b's' => {
            if let ret @ Ok(_) = InlineSrc::parse(parser, cursor, parent, parse_opts) {
                return ret;
            }
        }
        LBRACE => {
            if let ret @ Ok(_) = MacroCall::parse(parser, cursor, parent, parse_opts) {
                return ret;
            }
        }
        b'@' => {
            if let ret @ Ok(_) = ExportSnippet::parse(parser, cursor, parent, parse_opts) {
                return ret;
            }
        }

        _ => {}
    }

    if let Ok(plain_link_match) = parse_plain_link(cursor) {
        return Ok(parser.alloc(
            plain_link_match.obj,
            cursor.index,
            plain_link_match.end,
            parent,
        ));
    }

    if parse_opts.from_object {
        Err(MatchError::InvalidLogic)
    } else {
        parse_opts.from_object = true;
        Ok(parse_text(parser, cursor, parent, parse_opts))
    }
}

fn parse_text<'a>(
    parser: &mut Parser<'a>,
    mut cursor: Cursor<'a>,
    parent: Option<NodeID>,
    parse_opts: ParseOpts,
) -> NodeID {
    let start = cursor.index;

    while let Err(MatchError::InvalidLogic) = parse_object(parser, cursor, parent, parse_opts) {
        cursor.next();
    }

    parser.alloc(cursor.clamp_backwards(start), start, cursor.index, parent)
}

#[cfg(test)]
mod tests {
    use crate::{expr_in_pool, parse_org};

    use super::*;

    #[test]
    fn check_valid_hrule() {
        let src = "------\n";
        let parsed = parse_org(src);

        let hrule = parsed
            .pool
            .iter()
            .find_map(|x| {
                if let Expr::HorizontalRule = &x.obj {
                    Some(x)
                } else {
                    None
                }
            })
            .unwrap();
    }
    #[test]
    fn check_invalid_hrule() {
        let src = "--------s\n";
        let parsed = parse_org(src);

        let hrule = parsed.pool.iter().find_map(|x| {
            if let Expr::HorizontalRule = &x.obj {
                Some(x)
            } else {
                None
            }
        });
        if let Some(_) = hrule {
            unreachable!()
        }
    }

    #[test]
    #[should_panic] // would like this not to panic, but indentation / leading spaces are weird right now
    fn only_spaces() {
        let src = "   ";
        let parsed = parse_org(src);
        let plain = expr_in_pool!(parsed, Plain).unwrap();
        assert_eq!(plain, &"   ");
    }
}