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
// #![allow(dead_code)]
#![allow(unused_variables)]

pub mod element;
pub mod object;

pub(crate) mod node_pool;
pub(crate) mod types;
pub(crate) mod utils;

mod parse;

pub use node_pool::{NodeID, NodePool};
pub use types::{Attr, Expr, Node, Parser};
pub use utils::Match;

use std::collections::HashMap;

use parse::{parse_element, parse_object};
use types::{Cursor, NodeCache, ParseOpts};

#[rustfmt::skip]
pub(crate) mod constants {
    pub const SLASH       : u8 = b'/';
    pub const STAR        : u8 = b'*';
    pub const POUND       : u8 = b'#';
    pub const PLUS        : u8 = b'+';
    pub const HYPHEN      : u8 = b'-';
    pub const UNDERSCORE  : u8 = b'_';
    pub const LBRACK      : u8 = b'[';
    pub const RBRACK      : u8 = b']';
    pub const LBRACE      : u8 = b'{';
    pub const RBRACE      : u8 = b'}';
    pub const COLON       : u8 = b':';
    pub const SPACE       : u8 = b' ';
    pub const VBAR        : u8 = b'|';
    pub const BACKSLASH   : u8 = b'\\';
    pub const CARET       : u8 = b'^';
    pub const DOLLAR      : u8 = b'$';
    pub const TILDE       : u8 = b'~';
    pub const EQUAL       : u8 = b'=';
    pub const LANGLE      : u8 = b'<';
    pub const RANGLE      : u8 = b'>';
    pub const PERIOD      : u8 = b'.';
    pub const COMMA       : u8 = b',';
    pub const NEWLINE     : u8 = b'\n';
    pub const LPAREN      : u8 = b'(';
    pub const RPAREN      : u8 = b')';
}

/// The main entry point to the parser.
///
/// Repeatedly parses elements until EOF, then returns a [`Parser`].
pub fn parse_org(input: &str) -> Parser<'_> {
    let mut cursor = Cursor::new(input.as_bytes());
    let parse_opts = ParseOpts::default();
    let mut pool = NodePool::new();
    let parent = pool.reserve_id();
    let mut content_vec: Vec<NodeID> = Vec::new();

    let cache = NodeCache::new();
    let mut parser = Parser {
        pool,
        cache,
        targets: HashMap::new(),
        macros: HashMap::new(),
        keywords: HashMap::new(),
        target_occurences: HashMap::new(),
        footnotes: HashMap::new(),
        source: input,
    };
    // main loop
    while let Ok(id) = parse_element(&mut parser, cursor, Some(parent), parse_opts) {
        content_vec.push(id);
        cursor.move_to(parser.pool[id].end);
    }
    parser.alloc_with_id(Expr::Root(content_vec), 0, cursor.index, None, parent);

    parser
}

/// An alternative entry point to the parser for parsing macros.
///
/// Unlike [`parse_org`], this function parses objects, not elements.
pub fn parse_macro_call(input: &str) -> Parser {
    let mut cursor = Cursor::new(input.as_bytes());
    let parse_opts = ParseOpts::default();
    let mut pool = NodePool::new();
    let parent = pool.reserve_id();
    let mut content_vec: Vec<NodeID> = Vec::new();

    let mut parser = Parser {
        pool,
        cache: NodeCache::new(),
        targets: HashMap::new(),
        macros: HashMap::new(),
        keywords: HashMap::new(),
        target_occurences: HashMap::new(),
        footnotes: HashMap::new(),
        source: input,
    };
    while let Ok(id) = parse_object(&mut parser, cursor, Some(parent), parse_opts) {
        content_vec.push(id);
        cursor.move_to(parser.pool[id].end);
    }
    parser.alloc_with_id(Expr::Root(content_vec), 0, cursor.index, None, parent);

    parser
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_basic_paragraph() {
        let inp = "hello_world\n";

        dbg!(parse_org(inp));
    }

    #[test]
    /// Tests whether we handle unexpected eof
    fn test_basic_paragraph_no_nl() {
        let inp = "hello_world";

        dbg!(parse_org(inp));
    }

    #[test]
    fn test_basic_paragraph_newline() {
        let inp = "hello_world\nsame_paragraph\n";

        dbg!(parse_org(inp));
    }

    #[test]
    fn test_basic_markup() {
        let inp = "hello /italic/ more text after\n";

        let pool = parse_org(inp);
        pool.print_tree();
    }

    #[test]
    fn test_newline_in_italic_markup() {
        let inp = "hello /italic \n newline/ more text after\n";

        let pool = parse_org(inp);
        pool.print_tree();
    }

    #[test]
    fn test_newline_in_verbatim() {
        let inp = "hello =italic \n newline= more text after\n";

        // dbg!(parse_org(inp));
        println!("{:?}", parse_org(inp));
    }

    #[test]
    fn lots() {
        let input = r#"
#+macro: greet Hello $1, nice typing... $1.
* Basic Heading

{{{greet(user)}}}

** Child Heading

- https://plain_links.com.
  - <mailto:okiedokie@cool.com>
    - src_python{(technically) inline_src}
- [[Child Heading]]
  - \aleph \leftarrow entities

#+begin_export
<style type="text/css" media="screen">
table, th, td {
  border: 1px solid;
}
</style>
#+end_export

|tables!|[[targets][links to output target]]|styled,, manually :sweat_smile:
|no|default css (yet)|
|||||||||table

1. +does+
2. *it*
3. /all/
4. ~code~
5. =code, again..=
6. /so _nested_, *t^o_o*./

emojis :flushed: :tada: :sunglasses:

\begin{align}
x &+ 4\\
abc &+ 10\\
\end{align}
output MathML, little janky atm (might switch to katex..?)

Target here: <<targets>>\\


# doesn't look the best, imo
-----

#+begin_src rust
nothing styled for source blocks yet, too.
#+end_src

"#;
        let pool = parse_org(input);
        pool.print_tree();
        dbg!(pool);
    }

    #[test]
    fn correctness_cache() {
        let input = r"
- one
- two

--------------
";
        let pool = parse_org(input);
        // dbg!(&pool);
        pool.print_tree();
    }

    #[test]
    fn basic_unicode() {
        let input = r"é
";

        let pool = parse_org(input);
        pool.print_tree();
    }
}