sv_parser_parser/behavioral_statements/
randsequence.rs

1use crate::*;
2
3// -----------------------------------------------------------------------------
4
5#[tracable_parser]
6#[packrat_parser]
7pub(crate) fn randsequence_statement(s: Span) -> IResult<Span, RandsequenceStatement> {
8    let (s, a) = keyword("randsequence")(s)?;
9    let (s, b) = paren(opt(production_identifier))(s)?;
10    let (s, c) = production(s)?;
11    let (s, (d, e)) = many_till(production, keyword("endsequence"))(s)?;
12    Ok((
13        s,
14        RandsequenceStatement {
15            nodes: (a, b, c, d, e),
16        },
17    ))
18}
19
20#[tracable_parser]
21#[packrat_parser]
22pub(crate) fn production(s: Span) -> IResult<Span, Production> {
23    let (s, a) = opt(terminated(data_type_or_void, peek(production_identifier)))(s)?;
24    let (s, b) = production_identifier(s)?;
25    let (s, c) = opt(paren(tf_port_list))(s)?;
26    let (s, d) = symbol(":")(s)?;
27    let (s, e) = list(symbol("|"), rs_rule)(s)?;
28    let (s, f) = symbol(";")(s)?;
29    Ok((
30        s,
31        Production {
32            nodes: (a, b, c, d, e, f),
33        },
34    ))
35}
36
37#[tracable_parser]
38#[packrat_parser]
39pub(crate) fn rs_rule(s: Span) -> IResult<Span, RsRule> {
40    let (s, a) = rs_production_list(s)?;
41    let (s, b) = opt(triple(
42        symbol(":="),
43        weight_specification,
44        opt(rs_code_block),
45    ))(s)?;
46    Ok((s, RsRule { nodes: (a, b) }))
47}
48
49#[tracable_parser]
50#[packrat_parser]
51pub(crate) fn rs_production_list(s: Span) -> IResult<Span, RsProductionList> {
52    alt((rs_production_list_prod, rs_production_list_join))(s)
53}
54
55#[tracable_parser]
56#[packrat_parser]
57pub(crate) fn rs_production_list_prod(s: Span) -> IResult<Span, RsProductionList> {
58    let (s, a) = rs_prod(s)?;
59    let (s, b) = many0(rs_prod)(s)?;
60    Ok((
61        s,
62        RsProductionList::Prod(Box::new(RsProductionListProd { nodes: (a, b) })),
63    ))
64}
65
66#[tracable_parser]
67#[packrat_parser]
68pub(crate) fn rs_production_list_join(s: Span) -> IResult<Span, RsProductionList> {
69    let (s, a) = keyword("rand")(s)?;
70    let (s, b) = keyword("join")(s)?;
71    let (s, c) = opt(paren(expression))(s)?;
72    let (s, d) = production_item(s)?;
73    let (s, e) = production_item(s)?;
74    let (s, f) = many0(production_item)(s)?;
75    Ok((
76        s,
77        RsProductionList::Join(Box::new(RsProductionListJoin {
78            nodes: (a, b, c, d, e, f),
79        })),
80    ))
81}
82
83#[tracable_parser]
84#[packrat_parser]
85pub(crate) fn weight_specification(s: Span) -> IResult<Span, WeightSpecification> {
86    alt((
87        map(integral_number, |x| {
88            WeightSpecification::IntegralNumber(Box::new(x))
89        }),
90        map(ps_identifier, |x| {
91            WeightSpecification::PsIdentifier(Box::new(x))
92        }),
93        weight_specification_expression,
94    ))(s)
95}
96
97#[tracable_parser]
98#[packrat_parser]
99pub(crate) fn weight_specification_expression(s: Span) -> IResult<Span, WeightSpecification> {
100    let (s, a) = paren(expression)(s)?;
101    Ok((
102        s,
103        WeightSpecification::Expression(Box::new(WeightSpecificationExpression { nodes: (a,) })),
104    ))
105}
106
107#[tracable_parser]
108#[packrat_parser]
109pub(crate) fn rs_code_block(s: Span) -> IResult<Span, RsCodeBlock> {
110    let (s, a) = brace(pair(many0(data_declaration), many0(statement_or_null)))(s)?;
111    Ok((s, RsCodeBlock { nodes: (a,) }))
112}
113
114#[tracable_parser]
115#[packrat_parser]
116pub(crate) fn rs_prod(s: Span) -> IResult<Span, RsProd> {
117    alt((
118        map(production_item, |x| RsProd::ProductionItem(Box::new(x))),
119        map(rs_code_block, |x| RsProd::RsCodeBlock(Box::new(x))),
120        map(rs_if_else, |x| RsProd::RsIfElse(Box::new(x))),
121        map(rs_repeat, |x| RsProd::RsRepeat(Box::new(x))),
122        map(rs_case, |x| RsProd::RsCase(Box::new(x))),
123    ))(s)
124}
125
126#[tracable_parser]
127#[packrat_parser]
128pub(crate) fn production_item(s: Span) -> IResult<Span, ProductionItem> {
129    let (s, a) = production_identifier(s)?;
130    let (s, b) = opt(paren(list_of_arguments))(s)?;
131    Ok((s, ProductionItem { nodes: (a, b) }))
132}
133
134#[tracable_parser]
135#[packrat_parser]
136pub(crate) fn rs_if_else(s: Span) -> IResult<Span, RsIfElse> {
137    let (s, a) = keyword("if")(s)?;
138    let (s, b) = paren(expression)(s)?;
139    let (s, c) = production_item(s)?;
140    let (s, d) = opt(pair(keyword("else"), production_item))(s)?;
141    Ok((
142        s,
143        RsIfElse {
144            nodes: (a, b, c, d),
145        },
146    ))
147}
148
149#[tracable_parser]
150#[packrat_parser]
151pub(crate) fn rs_repeat(s: Span) -> IResult<Span, RsRepeat> {
152    let (s, a) = keyword("repeat")(s)?;
153    let (s, b) = paren(expression)(s)?;
154    let (s, c) = production_item(s)?;
155    Ok((s, RsRepeat { nodes: (a, b, c) }))
156}
157
158#[tracable_parser]
159#[packrat_parser]
160pub(crate) fn rs_case(s: Span) -> IResult<Span, RsCase> {
161    let (s, a) = keyword("case")(s)?;
162    let (s, b) = paren(case_expression)(s)?;
163    let (s, c) = rs_case_item(s)?;
164    let (s, (d, e)) = many_till(rs_case_item, keyword("endcase"))(s)?;
165    Ok((
166        s,
167        RsCase {
168            nodes: (a, b, c, d, e),
169        },
170    ))
171}
172
173#[tracable_parser]
174#[packrat_parser]
175pub(crate) fn rs_case_item(s: Span) -> IResult<Span, RsCaseItem> {
176    alt((rs_case_item_nondefault, rs_case_item_default))(s)
177}
178
179#[recursive_parser]
180#[tracable_parser]
181#[packrat_parser]
182pub(crate) fn rs_case_item_nondefault(s: Span) -> IResult<Span, RsCaseItem> {
183    let (s, a) = list(symbol(","), case_item_expression)(s)?;
184    let (s, b) = symbol(":")(s)?;
185    let (s, c) = production_item(s)?;
186    let (s, d) = symbol(";")(s)?;
187    Ok((
188        s,
189        RsCaseItem::NonDefault(Box::new(RsCaseItemNondefault {
190            nodes: (a, b, c, d),
191        })),
192    ))
193}
194
195#[tracable_parser]
196#[packrat_parser]
197pub(crate) fn rs_case_item_default(s: Span) -> IResult<Span, RsCaseItem> {
198    let (s, a) = keyword("default")(s)?;
199    let (s, b) = opt(symbol(":"))(s)?;
200    let (s, c) = production_item(s)?;
201    let (s, d) = symbol(";")(s)?;
202    Ok((
203        s,
204        RsCaseItem::Default(Box::new(RsCaseItemDefault {
205            nodes: (a, b, c, d),
206        })),
207    ))
208}