asciidoc_parser/inlines/
inline.rs

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
use crate::{inlines::InlineMacro, span::MatchedItem, HasSpan, Span};

/// An inline element is a phrase (i.e., span of content) within a block element
/// or one of its attributes in an AsciiDoc document.
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Inline<'src> {
    /// Uninterpreted text (i.e., plain text) is text (character data) for which
    /// all inline grammar rules fail to match.
    Uninterpreted(Span<'src>),

    /// A sequence of other inline blocks.
    Sequence(Vec<Self>, Span<'src>),

    /// An inline macro.
    Macro(InlineMacro<'src>),
}

impl<'src> Inline<'src> {
    /// Parse a span (typically a line) of any type and return an `Inline` that
    /// describes it.
    ///
    /// Returns `None` if input doesn't start with a non-empty line.
    pub(crate) fn parse(source: Span<'src>) -> Option<MatchedItem<'src, Self>> {
        let line = source.take_non_empty_line()?;
        let mut span = line.item;

        // Special-case optimization: If the entire span is one
        // uninterpreted block, just return that without the allocation
        // overhead of the Vec of inlines.

        let mut uninterp = parse_uninterpreted(span);
        if uninterp.after.is_empty() {
            return Some(MatchedItem {
                item: Self::Uninterpreted(uninterp.item),
                after: line.after,
            });
        }

        let mut inlines: Vec<Self> = vec![];

        loop {
            if !uninterp.item.is_empty() {
                inlines.push(Self::Uninterpreted(uninterp.item));
            }

            span = uninterp.after;
            if span.is_empty() {
                break;
            }

            let interp = parse_interpreted(span)?;
            if interp.after.is_empty() && inlines.is_empty() {
                return Some(interp);
            }

            inlines.push(interp.item);
            span = interp.after;

            uninterp = parse_uninterpreted(span);
        }

        Some(MatchedItem {
            item: Self::Sequence(inlines, source.trim_remainder(line.after)),
            after: line.after,
        })
    }

    /// Parse a sequence of non-empty lines as a single `Inline` that
    /// describes it.
    ///
    /// Returns `None` if there is not at least one non-empty line at
    /// beginning of input.
    pub(crate) fn parse_lines(source: Span<'src>) -> Option<MatchedItem<'src, Self>> {
        let mut inlines: Vec<Inline<'src>> = vec![];
        let mut next = source;

        while let Some(inline) = Self::parse(next) {
            next = inline.after;
            inlines.push(inline.item);
        }

        if inlines.len() < 2 {
            inlines.pop().map(|inline| MatchedItem {
                item: inline,
                after: next,
            })
        } else {
            let source = source.trim_remainder(next);
            Some(MatchedItem {
                item: Self::Sequence(inlines, source),
                after: next,
            })
        }
    }
}

impl<'src> HasSpan<'src> for Inline<'src> {
    fn span(&'src self) -> &'src Span<'src> {
        match self {
            Self::Uninterpreted(source) => source,
            Self::Sequence(_, source) => source,
            Self::Macro(m) => m.span(),
        }
    }
}

// Parse the largest possible block of "uninterpreted" text.
// Remainder is either empty span or first span that requires
// special interpretation.
fn parse_uninterpreted(source: Span<'_>) -> MatchedItem<Span> {
    // Optimization: If line doesn't contain special markup chars,
    // then it's all uninterpreted.

    if !source.contains(':') {
        return source.into_parse_result(source.len());
    }

    let mut after = source;

    while !after.is_empty() {
        if InlineMacro::parse(after).is_some() {
            break;
        }

        let word = after.take_while(|c| c != ' ' && c != '\t');
        let spaces = word.after.take_whitespace();
        after = spaces.after;
    }

    MatchedItem {
        item: source.trim_remainder(after),
        after,
    }
}

// Parse the block as a special "interpreted" inline sequence or error out.
fn parse_interpreted(source: Span<'_>) -> Option<MatchedItem<Inline<'_>>> {
    InlineMacro::parse(source).map(|inline| MatchedItem {
        item: Inline::Macro(inline.item),
        after: inline.after,
    })
}