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
//! Low-level representation of a bibliography file.

use std::collections::HashMap;
use std::iter::Peekable;
use std::str::Chars;

/// A literal representation of a bibliography file, with abbreviations not yet
/// resolved.
#[derive(Debug, Clone)]
pub struct RawBibliography<'s> {
    /// TeX commands to be prepended to the document, only supported by BibTeX.
    pub preamble: String,
    /// The collection of citation keys and bibliography entries.
    pub entries: Vec<RawEntry<'s>>,
    /// A map of reusable abbreviations, only supported by BibTeX.
    pub abbreviations: HashMap<&'s str, &'s str>,
}

/// A raw extracted entry, with abbreviations not yet resolved.
#[derive(Debug, Clone)]
pub struct RawEntry<'s> {
    /// The citation key.
    pub key: &'s str,
    /// Denotes the type of bibliographic item (e.g. `article`).
    pub entry_type: &'s str,
    /// Maps from field names to their values.
    pub fields: HashMap<&'s str, &'s str>,
}

impl<'s> RawBibliography<'s> {
    /// Parse a raw bibliography from a source string.
    pub fn parse(src: &'s str) -> Self {
        BiblatexParser::new(src).parse()
    }
}

/// Backing struct for parsing a Bib(La)TeX file into a [`RawBibliography`].
struct BiblatexParser<'s> {
    src: &'s str,
    mode: ParseMode,
    index: usize,
    comment: bool,
    iter: Peekable<Chars<'s>>,
    res: RawBibliography<'s>,
}

/// Symbols that may be found when parsing a file.
#[derive(Debug, PartialEq)]
enum Symbols {
    Quotes,
    Braces,
}

#[derive(Debug, PartialEq)]
enum ParseMode {
    Outside,
    Type,
    KeyMode,
    PreambleMode,
    EntryMode,
}

impl<'s> BiblatexParser<'s> {
    /// Constructs a new parser.
    pub fn new(src: &'s str) -> Self {
        Self {
            src,
            mode: ParseMode::Outside,
            index: 0,
            comment: false,
            iter: src.chars().peekable(),
            res: RawBibliography {
                preamble: String::new(),
                entries: vec![],
                abbreviations: HashMap::new(),
            },
        }
    }

    /// Parses the file, consuming the parser in the process.
    pub fn parse(mut self) -> RawBibliography<'s> {
        while let Some(c) = self.eat() {
            if c == '@' && !self.comment {
                self.parse_entry();
            } else if c == '%' {
                self.comment = true;
            } else if c == '\n' || c == '\r' {
                self.comment = false;
            }
        }

        self.res
    }

    /// Parse a bibliography entry.
    fn parse_entry(&mut self) {
        self.mode = ParseMode::Type;

        let type_start = self.index;
        let mut type_end = self.index;
        let mut key_start: usize = 0;
        let mut key_end: usize = 0;
        let mut is_string = false;

        let mut entry_type = None;
        let mut fields = HashMap::new();

        while let Some(c) = self.peek() {
            match self.mode {
                ParseMode::Type => {
                    if is_ident(c, type_start == type_end) {
                        self.eat();
                        type_end = self.index;
                    } else {
                        entry_type = Some(&self.src[type_start .. type_end]);
                        if c.is_whitespace() {
                            self.eat();
                        } else if c == '{' {
                            self.eat();

                            let lower_type =
                                &self.src[type_start .. type_end].to_lowercase();

                            if lower_type == "string" {
                                self.mode = ParseMode::EntryMode;
                                is_string = true;
                            } else if lower_type == "preamble" {
                                self.mode = ParseMode::PreambleMode;
                            } else {
                                key_start = self.index;
                                key_end = self.index;
                                self.mode = ParseMode::KeyMode;
                            }
                        } else {
                            // TODO: Invalid
                            self.mode = ParseMode::Outside;
                            break;
                        }
                    }
                }

                ParseMode::KeyMode => {
                    if is_ident(c, key_start == key_end) {
                        self.eat();
                        key_end = self.index;
                    } else if c.is_whitespace() {
                        self.eat();
                    } else if c == ',' {
                        self.eat();
                        self.mode = ParseMode::EntryMode;
                    } else {
                        // TODO: Invalid
                        self.mode = ParseMode::Outside;
                        break;
                    }
                }

                ParseMode::PreambleMode => {
                    self.skip_ws();

                    // This does not allow string concatenation in preambles.
                    if c == '\"' {
                        self.eat();
                        while let Some(c) = self.eat() {
                            if c == '\"' {
                                break;
                            }
                            self.res.preamble.push(c);
                        }
                    }

                    self.mode = ParseMode::Outside;
                }

                ParseMode::EntryMode => {
                    self.skip_ws();
                    if self.peek() == Some('}') {
                        self.mode = ParseMode::Outside;
                        continue;
                    }
                    let s = self.read_prop().expect("Hallo");
                    if is_string {
                        self.res.abbreviations.insert(s.0, s.1);
                    } else {
                        fields.insert(s.0, s.1);
                    }
                }
                _ => break,
            }
        }

        if !is_string {
            self.res.entries.push(RawEntry {
                key: &self.src[key_start .. key_end],
                entry_type: entry_type.unwrap_or_default(),
                fields,
            });
        }

        self.mode = ParseMode::Outside;
    }

    /// Read a field.
    fn read_prop(&mut self) -> Result<(&'s str, &'s str), ()> {
        self.skip_ws();

        let start = self.index;
        let mut end = self.index;

        while let Some(c) = self.peek() {
            if is_ident(c, start == end) {
                self.eat();
                end = self.index;
            } else {
                break;
            }
        }

        let name = &self.src[start .. end];
        while let Some(c) = self.eat() {
            if c == '=' {
                break;
            }
        }
        self.skip_ws();

        let mut stack: Vec<Symbols> = vec![];
        let val_start = self.index;
        let mut val_end = self.index;
        let mut escape = false;

        while let Some(c) = self.eat() {
            match c {
                '\\' => {
                    escape = true;
                    continue;
                }
                '{' if escape => {}
                '}' if escape => {}
                '"' if escape => {}
                ',' if stack.is_empty() => break,
                '}' if stack.is_empty() => {
                    self.mode = ParseMode::Outside;
                    break;
                }
                '"' if stack.last() == Some(&Symbols::Quotes) => {
                    stack.pop();
                }
                '"' if stack.is_empty() => stack.push(Symbols::Quotes),
                '{' => stack.push(Symbols::Braces),
                '}' => {
                    let x = stack.pop();
                    if x != Some(Symbols::Braces) {
                        return Err(());
                    }
                }
                _ => {}
            }
            escape = false;
            val_end = self.index;
        }

        Ok((name, &self.src[val_start .. val_end]))
    }

    /// Get the next character without advancing the parsing file iterator.
    fn peek(&mut self) -> Option<char> {
        self.iter.peek().copied()
    }

    /// Advance the parsing file iterator to the next non-whitespace or comment
    /// character.
    fn skip_ws(&mut self) {
        while let Some(c) = self.peek() {
            if c == '%' {
                self.comment = true;
                self.eat();
                continue;
            } else if c == '\n' || c == '\r' {
                self.comment = false;
            } else if self.comment {
                self.eat();
                continue;
            }

            if c.is_whitespace() || c == '\n' || c == '\r' {
                self.eat();
            } else {
                break;
            }
        }
    }

    /// Advance the parsing file iterator by one and return the consumed character.
    fn eat(&mut self) -> Option<char> {
        let c = self.iter.next()?;
        self.index += c.len_utf8();
        Some(c)
    }
}

/// Characters allowable in identifiers like cite keys.
fn is_ident(c: char, first: bool) -> bool {
    match c {
        ' ' | '@' | '{' | '}' | '"' | '#' | '\'' | '(' | ')' | ',' | '=' | '%' | '\\'
        | '~' => false,
        ':' | '<' | '-' | '>' | '_' if first => false,
        _ => true,
    }
}

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

    fn test_prop(key: &str, value: &str) -> String {
        let test = format!("@article{{test, {}={}}}", key, value);
        let bt = RawBibliography::parse(&test);
        let article = &bt.entries[0];
        article.fields.get(key).expect("fail").to_string()
    }

    #[test]
    fn test_parse_article() {
        let file = "@article{haug2020,
            title = \"Great proceedings\\{\",
            year=2002,
            author={Haug, {Martin} and Haug, Gregor}}";

        let bt = RawBibliography::parse(file);
        let article = &bt.entries[0];

        assert_eq!(article.entry_type, "article");
        assert_eq!(article.fields.get("title"), Some(&"\"Great proceedings\\{\""));
        assert_eq!(article.fields.get("year"), Some(&"2002"));
        assert_eq!(article.fields.get("author"), Some(&"{Haug, {Martin} and Haug, Gregor}"));
    }

    #[test]
    fn test_resolve_string() {
        let bt = RawBibliography::parse("@string{BT = \"bibtex\"}");
        assert_eq!(bt.abbreviations.get("BT"), Some(&"\"bibtex\""));
    }

    #[test]
    fn test_escape() {
        assert_eq!(test_prop("author", "{Mister A\\}\"B\"}"), "{Mister A\\}\"B\"}");
    }
}