rustre_parser/parser/
ident.rs

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
use super::*;

pub fn parse_lv6_id_ref<'slice, 'src>(input: Input<'slice, 'src>) -> IResult<'slice, 'src> {
    node(
        IdRefNode,
        join((
            t(Ident),
            opt(join((
                t_raw(DoubleColon),
                expect(t_raw(Ident), "expected ident after colon"),
            ))),
        )),
    )(input)
}

pub fn parse_lv6_id<'slice, 'src>(input: Input<'slice, 'src>) -> IResult<'slice, 'src> {
    node(
        IdNode,
        many_delimited(
            t(Ident),
            parse_pragma,
            peek(t(Percent)),
            peek_neg(t(Percent)),
        ),
    )(input)
}

pub fn parse_pragma<'slice, 'src>(input: Input<'slice, 'src>) -> IResult<'slice, 'src> {
    node(
        PragmaNode,
        many_delimited(
            t(Percent),
            expect(
                join((t(Ident), t(Colon), t(Ident))),
                "expected ident:ident inside pragma",
            ),
            eof,
            t(Percent),
        ),
    )(input)
}

/// Parses an Lv6Id or an Lv6IdRef wrapped in an [`IdNode`], with an optional pragma
///
/// This parser should be used when parsing any ID, even when only one of the two types should
/// actually be used. This makes the parser more lax and allows for better diagnostics.
///
/// # Tolerated syntax errors
///
///   * Pragma after Lv6IdRef, when it should only occur after an Lv6Id
///   * This parser is supposed to be called every time an Lv6Id or Lv6IdRef is expected, even if
///     only one of those is actually valid
#[rustfmt::skip]
pub fn parse_id_any<'slice, 'src>(input: Input<'slice, 'src>) -> IResult<'slice, 'src> {
    node(
        IdNode,
        many_delimited(
            join((
                t(Ident),
                opt(join((
                    t_raw(DoubleColon),
                    expect(alt((t_raw(Ident), parse_predef_op_t(t_raw))), "expected ident after colon"),
                ))),
            )),
            parse_pragma,
            success,
            peek_neg(t(Percent)),
        )
    )(input)
}