sv_parser_parser/behavioral_statements/
conditional_statements.rs

1use crate::*;
2
3// -----------------------------------------------------------------------------
4
5#[tracable_parser]
6#[packrat_parser]
7pub(crate) fn conditional_statement(s: Span) -> IResult<Span, ConditionalStatement> {
8    let (s, a) = opt(unique_priority)(s)?;
9    let (s, b) = keyword("if")(s)?;
10    let (s, c) = paren(cond_predicate)(s)?;
11    let (s, d) = statement_or_null(s)?;
12    let (s, e) = many0(tuple((
13        keyword("else"),
14        keyword("if"),
15        paren(cond_predicate),
16        statement_or_null,
17    )))(s)?;
18    let (s, f) = opt(pair(keyword("else"), statement_or_null))(s)?;
19
20    Ok((
21        s,
22        ConditionalStatement {
23            nodes: (a, b, c, d, e, f),
24        },
25    ))
26}
27
28#[tracable_parser]
29#[packrat_parser]
30pub(crate) fn unique_priority(s: Span) -> IResult<Span, UniquePriority> {
31    alt((
32        map(keyword("unique0"), |x| UniquePriority::Unique0(Box::new(x))),
33        map(keyword("unique"), |x| UniquePriority::Unique(Box::new(x))),
34        map(keyword("priority"), |x| {
35            UniquePriority::Priority(Box::new(x))
36        }),
37    ))(s)
38}
39
40#[recursive_parser]
41#[tracable_parser]
42#[packrat_parser]
43pub(crate) fn cond_predicate(s: Span) -> IResult<Span, CondPredicate> {
44    let (s, a) = list(symbol("&&&"), expression_or_cond_pattern)(s)?;
45    Ok((s, CondPredicate { nodes: (a,) }))
46}
47
48#[tracable_parser]
49#[packrat_parser]
50pub(crate) fn expression_or_cond_pattern(s: Span) -> IResult<Span, ExpressionOrCondPattern> {
51    alt((
52        map(cond_pattern, |x| {
53            ExpressionOrCondPattern::CondPattern(Box::new(x))
54        }),
55        map(expression, |x| {
56            ExpressionOrCondPattern::Expression(Box::new(x))
57        }),
58    ))(s)
59}
60
61#[recursive_parser]
62#[tracable_parser]
63#[packrat_parser]
64pub(crate) fn cond_pattern(s: Span) -> IResult<Span, CondPattern> {
65    let (s, a) = expression(s)?;
66    let (s, b) = keyword("matches")(s)?;
67    let (s, c) = pattern(s)?;
68    Ok((s, CondPattern { nodes: (a, b, c) }))
69}