texlang/parse/
variable.rs

1use crate::prelude as txl;
2use crate::traits::*;
3use crate::*;
4
5/// When parsed, this type consumes an optional equals from the token stream.
6pub struct OptionalEquals;
7
8impl Parsable for OptionalEquals {
9    fn parse_impl<S: TexlangState>(input: &mut vm::ExpandedStream<S>) -> txl::Result<Self> {
10        // scan_optional_equals
11        parse_optional_equals(input)?;
12        Ok(OptionalEquals {})
13    }
14}
15
16/// When parsed, this type consumes an optional equals from the token stream without performing expansion.
17pub struct OptionalEqualsUnexpanded;
18
19impl Parsable for OptionalEqualsUnexpanded {
20    fn parse_impl<S: TexlangState>(input: &mut vm::ExpandedStream<S>) -> txl::Result<Self> {
21        parse_optional_equals(input.unexpanded())?;
22        Ok(OptionalEqualsUnexpanded {})
23    }
24}
25
26// Corresponds to the `scan_optional_equals` procedure in Knuth's TeX (405)
27fn parse_optional_equals<S: TexlangState, I: TokenStream<S = S>>(input: &mut I) -> txl::Result<()> {
28    while let Some(found_equals) = get_optional_element![
29        input,
30        token::Value::Other('=') => true,
31        token::Value::Space(_) => false,
32    ] {
33        if found_equals {
34            break;
35        }
36    }
37    // TODO: this is not correct: this function should not scan spaces after the equals.
38    // A separate routine corresponding to Knuth TeX 404 needs to be added and used instead
39    // at the right call sites.
40    while get_optional_element![
41        input,
42        token::Value::Space(_) => (),
43    ]
44    .is_some()
45    {}
46    Ok(())
47}