aboutsummaryrefslogtreecommitdiffstats
path: root/src/construct/partial_data.rs
blob: f9b79478d9b93a7561aef44c0e5eb7a78248d807 (plain) (blame)
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
//! Data occurs in [text][] and [string][].
//!
//! It can include anything (including line endings), and stops at certain
//! characters.
//!
//! [string]: crate::content::string
//! [text]: crate::content::text

use crate::event::{Kind, Name};
use crate::resolve::Name as ResolveName;
use crate::state::{Name as StateName, State};
use crate::tokenizer::Tokenizer;

/// At the beginning of data.
///
/// ```markdown
/// > | abc
///     ^
/// ```
pub fn start(tokenizer: &mut Tokenizer) -> State {
    match tokenizer.current {
        // Make sure to eat the first `markers`.
        Some(byte) if tokenizer.tokenize_state.markers.contains(&byte) => {
            tokenizer.enter(Name::Data);
            tokenizer.consume();
            State::Next(StateName::DataInside)
        }
        _ => State::Retry(StateName::DataAtBreak),
    }
}

/// Before something.
///
/// ```markdown
/// > | abc
///     ^
/// ```
pub fn at_break(tokenizer: &mut Tokenizer) -> State {
    match tokenizer.current {
        None => State::Ok,
        Some(b'\n') => {
            tokenizer.enter(Name::LineEnding);
            tokenizer.consume();
            tokenizer.exit(Name::LineEnding);
            State::Next(StateName::DataAtBreak)
        }
        Some(byte) if tokenizer.tokenize_state.markers.contains(&byte) => {
            tokenizer.register_resolver_before(ResolveName::Data);
            State::Ok
        }
        _ => {
            tokenizer.enter(Name::Data);
            State::Retry(StateName::DataInside)
        }
    }
}

/// In data.
///
/// ```markdown
/// > | abc
///     ^^^
/// ```
pub fn inside(tokenizer: &mut Tokenizer) -> State {
    let done = match tokenizer.current {
        None | Some(b'\n') => true,
        Some(byte) if tokenizer.tokenize_state.markers.contains(&byte) => true,
        _ => false,
    };

    if done {
        tokenizer.exit(Name::Data);
        State::Retry(StateName::DataAtBreak)
    } else {
        tokenizer.consume();
        State::Next(StateName::DataInside)
    }
}

/// Merge adjacent data events.
pub fn resolve(tokenizer: &mut Tokenizer) {
    let len = tokenizer.events.len();
    let mut index = 0;

    // Loop through events and merge adjacent data events.
    while index < len {
        let event = &tokenizer.events[index];

        if event.kind == Kind::Enter && event.name == Name::Data {
            let exit_index = index + 1;
            let mut exit_far_index = exit_index;

            // Find multiple `data` events.
            while exit_far_index + 1 < len
                && tokenizer.events[exit_far_index + 1].name == Name::Data
            {
                exit_far_index += 2;
            }

            if exit_far_index > exit_index {
                tokenizer
                    .map
                    .add(exit_index, exit_far_index - exit_index, vec![]);

                // Change positional info.
                let exit_far = &tokenizer.events[exit_far_index];
                tokenizer.events[exit_index].point = exit_far.point.clone();
                index = exit_far_index;

                continue;
            }
        }

        index += 1;
    }
}