ftml/parsing/rule/impls/
variable.rs

1/*
2 * parsing/rule/impls/variable.rs
3 *
4 * ftml - Library to parse Wikidot text
5 * Copyright (C) 2019-2025 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
21use super::prelude::*;
22use once_cell::sync::Lazy;
23use regex::Regex;
24
25static VARIABLE_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\{\$(.+)\}").unwrap());
26
27pub const RULE_VARIABLE: Rule = Rule {
28    name: "variable",
29    position: LineRequirement::Any,
30    try_consume_fn,
31};
32
33fn try_consume_fn<'r, 't>(
34    parser: &mut Parser<'r, 't>,
35) -> ParseResult<'r, 't, Elements<'t>> {
36    debug!("Consuming token by placing variable contents");
37
38    let ExtractedToken { slice, .. } = parser.current();
39
40    let variable = VARIABLE_REGEX
41        .captures(slice)
42        .expect("Variable regex didn't match")
43        .get(1)
44        .expect("Capture group not found")
45        .as_str();
46
47    ok!(Element::Variable(cow!(variable)))
48}