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
//! Module for parsing of Unimarkup elements.

pub mod symbol;

use logid::capturing::MappedLogId;
use symbol::Symbol;

use crate::{
    config::Config,
    document::Document,
    elements::{
        atomic::{Heading, Paragraph},
        enclosed::Verbatim,
        Blocks,
    },
    metadata::{Metadata, MetadataKind},
    security,
};

use self::symbol::{IntoSymbols, SymbolKind};

/// Parser as function that can parse Unimarkup content
pub type ParserFn = for<'i> fn(&'i [Symbol<'i>]) -> Option<(Blocks, &'i [Symbol<'i>])>;

/// Output of symbol tokenization by a parser of a block.
pub(crate) struct TokenizeOutput<'a, T>
where
    T: 'a,
{
    pub(crate) tokens: Vec<T>,
    pub(crate) rest_of_input: &'a [Symbol<'a>],
}

/// Trait implemented by a parser for each Unimarkup element.
pub(crate) trait ElementParser {
    /// Token type produced by tokenization.
    type Token<'a>;

    /// Function that converts input symbols into tokens specific for the given element.
    fn tokenize<'i>(input: &'i [Symbol<'i>]) -> Option<TokenizeOutput<'i, Self::Token<'i>>>;

    /// Function that parses tokenization output and produces one or more Unimarkup elements.
    fn parse(input: Vec<Self::Token<'_>>) -> Option<Blocks>;
}

// Makes it impossible to implement `ParserGenerator` trait outside of this module,
// but still makes it possible to name `ParserGenerator` and use it as a bound.
mod private {
    pub trait Sealed {}
    impl<'a, T> Sealed for T where T: super::ElementParser + 'a + 'static {}
}

/// Trait implemented by all Unimarkup elements that can generate parser function for their
/// content.
pub trait ParserGenerator: private::Sealed {
    /// Generates parser function for the given Unimarkup element.
    fn generate_parser() -> ParserFn;
}

impl<'a, T> ParserGenerator for T
where
    T: ElementParser + 'a + 'static,
{
    // NOTE: we might need some context information for parsers. An option could be to pass
    // some kind of Context struct into generate_parser and use that for whatever we need to.
    fn generate_parser() -> ParserFn {
        |input| {
            let tokenize_output = T::tokenize(input)?;
            let blocks = T::parse(tokenize_output.tokens)?;

            Some((blocks, tokenize_output.rest_of_input))
        }
    }
}

/// Parser of unimarkup content.
#[derive(Clone)]
pub struct MainParser {
    parsers: Vec<ParserFn>,
    default_parser: ParserFn,
}

impl Default for MainParser {
    fn default() -> Self {
        tracing::info!("Initializing MainParser");

        let default = Paragraph::generate_parser();

        let mut parser = Self {
            parsers: Vec::with_capacity(2),
            default_parser: default,
        };

        // TODO: how to handle preamble parser?
        parser.register_parser(Heading::generate_parser());
        parser.register_parser(Verbatim::generate_parser());

        tracing::info!("MainParser initialized");
        parser
    }
}

impl MainParser {
    fn register_parser(&mut self, parser: ParserFn) {
        self.parsers.push(parser);
    }

    /// Parses Unimarkup content and produces Unimarkup blocks.
    pub fn parse<'s>(&self, input: impl IntoSymbols<'s, &'s [Symbol<'s>]>) -> Blocks {
        let mut input = input.into_symbols();
        let mut blocks = Vec::default();

        #[cfg(debug_assertions)]
        let mut input_len = input.len();

        'outer: while let Some(sym) = input.first() {
            match sym.kind {
                // skip blanklines
                SymbolKind::Blankline => input = &input[1..],

                // stop parsing when end of input is reached
                SymbolKind::EOI => break,

                // no parser will match, parse with default parser
                _ if sym.is_not_keyword() => {
                    let (mut res_blocks, rest_of_input) = (self.default_parser)(input)
                        .expect("Default parser could not parse content!");

                    blocks.append(&mut res_blocks);
                    input = rest_of_input;
                }

                // symbol is start of a block, some parser should match
                _ => {
                    for parser_fn in &self.parsers {
                        if let Some((mut res_blocks, rest_of_input)) = parser_fn(input) {
                            blocks.append(&mut res_blocks);
                            input = rest_of_input;
                            continue 'outer; // start from first parser on next input
                        }
                    }

                    // no registered parser matched -> use default parser
                    let (mut res_blocks, rest_of_input) = (self.default_parser)(input)
                        .expect("Default parser could not parse content!");

                    blocks.append(&mut res_blocks);
                    input = rest_of_input;
                }
            }

            #[cfg(debug_assertions)]
            {
                assert_ne!(input.len(), input_len);
                input_len = input.len();
            }
        }

        blocks
    }
}

/// Parses and returns a Unimarkup document.
pub fn parse_unimarkup(um_content: &str, config: &mut Config) -> Result<Document, MappedLogId> {
    let parser = MainParser::default();

    let symbols = um_content.into_symbols();

    let blocks = parser.parse(&symbols);

    let mut unimarkup = Document {
        config: config.clone(),
        blocks,
        ..Default::default()
    };

    let metadata = Metadata {
        file: config.um_file.clone(),
        contenthash: security::get_contenthash(um_content),
        preamble: String::new(),
        kind: MetadataKind::Root,
        namespace: ".".to_string(),
    };

    unimarkup.metadata.push(metadata);

    Ok(unimarkup)
}