ftml/parsing/collect/container.rs
1/*
2 * parsing/collect/container.rs
3 *
4 * ftml - Library to parse Wikidot text
5 * Copyright (C) 2019-2026 Wikijump Team
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21//! Helper code to parse tokens out to generate recursive containers.
22
23use super::prelude::*;
24use crate::parsing::collect::collect_consume;
25use crate::tree::{AttributeMap, Container, ContainerType, Element};
26
27/// Generic function to consume tokens into a container.
28///
29/// This is a subset of the functionality provided by `collect`,
30/// as it builds `Container`s specifically.
31///
32/// The arguments which differ from `collect` are listed:
33/// See that function for full documentation, as the call here
34/// mostly wraps it.
35///
36/// This call always sets `step_on_final` to `true`.
37///
38/// The kind of container we're building:
39/// Must match the parse rule.
40/// * `container_type`
41pub fn collect_container<'r, 't>(
42 parser: &mut Parser<'r, 't>,
43 rule: Rule,
44 container_type: ContainerType,
45 close_conditions: &[ParseCondition],
46 invalid_conditions: &[ParseCondition],
47 error_kind: Option<ParseErrorKind>,
48) -> ParseResult<'r, 't, Elements<'t>> {
49 debug!(
50 "Trying to consume tokens to produce container {} for {}",
51 container_type.name(),
52 rule.name(),
53 );
54
55 // Iterate and consume all the tokens
56 let (elements, errors, paragraph_safe) = collect_consume(
57 parser,
58 rule,
59 close_conditions,
60 invalid_conditions,
61 error_kind,
62 )?
63 .into();
64
65 // Package into a container
66 ok!(
67 paragraph_safe && container_type.paragraph_safe();
68 Element::Container(Container::new(
69 container_type,
70 elements,
71 AttributeMap::new(),
72 )),
73 errors,
74 )
75}