Parse out the optional value objects.

This commit is contained in:
Tom Alexander 2023-10-11 18:29:07 -04:00
parent 6679db98a8
commit aeb2b6fe68
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 30 additions and 2 deletions

View File

@ -1,7 +1,18 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use nom::bytes::complete::tag;
use nom::bytes::complete::take_until;
use nom::character::complete::anychar;
use nom::combinator::all_consuming; use nom::combinator::all_consuming;
use nom::combinator::eof;
use nom::combinator::map;
use nom::combinator::map_parser;
use nom::combinator::opt;
use nom::combinator::peek;
use nom::combinator::recognize;
use nom::multi::many0; use nom::multi::many0;
use nom::multi::many_till;
use nom::sequence::tuple;
use super::object_parser::standard_set_object; use super::object_parser::standard_set_object;
use crate::context::parser_with_context; use crate::context::parser_with_context;
@ -45,6 +56,23 @@ pub(crate) fn parse_affiliated_keywords<'g, 's>(
let initial_context = ContextElement::document_context(); let initial_context = ContextElement::document_context();
let initial_context = Context::new(global_settings, List::new(&initial_context)); let initial_context = Context::new(global_settings, List::new(&initial_context));
let (_remaining, optional_objects) = opt(all_consuming(map(
tuple((
take_until("["),
tag("["),
map_parser(
recognize(many_till(anychar, peek(tuple((tag("]"), eof))))),
all_consuming(many0(parser_with_context!(standard_set_object)(
&initial_context,
))),
),
tag("]"),
eof,
)),
|(_, _, objects, _, _)| objects,
)))(kw.key.into())
.expect("Object parser should always succeed.");
// TODO: This should be omitting footnote references // TODO: This should be omitting footnote references
let (_remaining, objects) = all_consuming(many0(parser_with_context!( let (_remaining, objects) = all_consuming(many0(parser_with_context!(
standard_set_object standard_set_object
@ -55,7 +83,7 @@ pub(crate) fn parse_affiliated_keywords<'g, 's>(
}); });
match list_of_lists { match list_of_lists {
AffiliatedKeywordValue::ListOfListsOfObjects(list_of_lists) => { AffiliatedKeywordValue::ListOfListsOfObjects(list_of_lists) => {
list_of_lists.push(objects); list_of_lists.push((optional_objects, objects));
} }
_ => panic!("Invalid AffiliatedKeywordValue type."), _ => panic!("Invalid AffiliatedKeywordValue type."),
} }

View File

@ -6,7 +6,7 @@ use super::Object;
pub enum AffiliatedKeywordValue<'s> { pub enum AffiliatedKeywordValue<'s> {
SingleString(&'s str), SingleString(&'s str),
ListOfStrings(Vec<&'s str>), ListOfStrings(Vec<&'s str>),
ListOfListsOfObjects(Vec<Vec<Object<'s>>>), ListOfListsOfObjects(Vec<(Option<Vec<Object<'s>>>, Vec<Object<'s>>)>),
} }
#[derive(Debug)] #[derive(Debug)]