sv_parser_parser/expressions/
expressions.rs

1use crate::*;
2
3// -----------------------------------------------------------------------------
4
5#[tracable_parser]
6#[packrat_parser]
7pub(crate) fn inc_or_dec_expression(s: Span) -> IResult<Span, IncOrDecExpression> {
8    alt((inc_or_dec_expression_prefix, inc_or_dec_expression_suffix))(s)
9}
10
11#[tracable_parser]
12#[packrat_parser]
13pub(crate) fn inc_or_dec_expression_prefix(s: Span) -> IResult<Span, IncOrDecExpression> {
14    let (s, a) = inc_or_dec_operator(s)?;
15    let (s, b) = many0(attribute_instance)(s)?;
16    let (s, c) = variable_lvalue(s)?;
17    Ok((
18        s,
19        IncOrDecExpression::Prefix(Box::new(IncOrDecExpressionPrefix { nodes: (a, b, c) })),
20    ))
21}
22
23#[recursive_parser]
24#[tracable_parser]
25#[packrat_parser]
26pub(crate) fn inc_or_dec_expression_suffix(s: Span) -> IResult<Span, IncOrDecExpression> {
27    let (s, a) = variable_lvalue(s)?;
28    let (s, b) = many0(attribute_instance)(s)?;
29    let (s, c) = inc_or_dec_operator(s)?;
30    Ok((
31        s,
32        IncOrDecExpression::Suffix(Box::new(IncOrDecExpressionSuffix { nodes: (a, b, c) })),
33    ))
34}
35
36#[recursive_parser]
37#[tracable_parser]
38#[packrat_parser]
39pub(crate) fn conditional_expression(s: Span) -> IResult<Span, ConditionalExpression> {
40    let (s, a) = cond_predicate_ternary(s)?;
41    let (s, b) = symbol("?")(s)?;
42    let (s, c) = many0(attribute_instance)(s)?;
43    let (s, d) = expression(s)?;
44    let (s, e) = symbol(":")(s)?;
45    let (s, f) = expression(s)?;
46    Ok((
47        s,
48        ConditionalExpression {
49            nodes: (a, b, c, d, e, f),
50        },
51    ))
52}
53
54#[recursive_parser]
55#[tracable_parser]
56#[packrat_parser]
57pub(crate) fn cond_predicate_ternary(s: Span) -> IResult<Span, CondPredicate> {
58    let (s, a) = list(symbol("&&&"), expression_or_cond_pattern_ternary)(s)?;
59    Ok((s, CondPredicate { nodes: (a,) }))
60}
61
62#[tracable_parser]
63#[packrat_parser]
64pub(crate) fn expression_or_cond_pattern_ternary(
65    s: Span,
66) -> IResult<Span, ExpressionOrCondPattern> {
67    alt((
68        map(cond_pattern, |x| {
69            ExpressionOrCondPattern::CondPattern(Box::new(x))
70        }),
71        map(expression, |x| {
72            ExpressionOrCondPattern::Expression(Box::new(x))
73        }),
74    ))(s)
75}
76
77#[tracable_parser]
78#[packrat_parser]
79pub(crate) fn constant_expression(s: Span) -> IResult<Span, ConstantExpression> {
80    alt((
81        constant_expression_ternary,
82        constant_expression_binary,
83        constant_expression_unary,
84        map(constant_primary, |x| {
85            ConstantExpression::ConstantPrimary(Box::new(x))
86        }),
87    ))(s)
88}
89
90#[tracable_parser]
91#[packrat_parser]
92pub(crate) fn constant_expression_unary(s: Span) -> IResult<Span, ConstantExpression> {
93    let (s, a) = unary_operator(s)?;
94    let (s, b) = many0(attribute_instance)(s)?;
95    let (s, c) = constant_primary(s)?;
96    Ok((
97        s,
98        ConstantExpression::Unary(Box::new(ConstantExpressionUnary { nodes: (a, b, c) })),
99    ))
100}
101
102#[recursive_parser]
103#[tracable_parser]
104#[packrat_parser]
105pub(crate) fn constant_expression_binary(s: Span) -> IResult<Span, ConstantExpression> {
106    let (s, a) = constant_expression(s)?;
107    let (s, b) = binary_operator(s)?;
108    let (s, c) = many0(attribute_instance)(s)?;
109    let (s, d) = constant_expression(s)?;
110    Ok((
111        s,
112        ConstantExpression::Binary(Box::new(ConstantExpressionBinary {
113            nodes: (a, b, c, d),
114        })),
115    ))
116}
117
118#[recursive_parser]
119#[tracable_parser]
120#[packrat_parser]
121pub(crate) fn constant_expression_ternary(s: Span) -> IResult<Span, ConstantExpression> {
122    let (s, a) = constant_expression(s)?;
123    let (s, b) = symbol("?")(s)?;
124    let (s, c) = many0(attribute_instance)(s)?;
125    let (s, d) = constant_expression(s)?;
126    let (s, e) = symbol(":")(s)?;
127    let (s, f) = constant_expression(s)?;
128    Ok((
129        s,
130        ConstantExpression::Ternary(Box::new(ConstantExpressionTernary {
131            nodes: (a, b, c, d, e, f),
132        })),
133    ))
134}
135
136#[tracable_parser]
137#[packrat_parser]
138pub(crate) fn constant_mintypmax_expression(s: Span) -> IResult<Span, ConstantMintypmaxExpression> {
139    alt((
140        constant_mintypmax_expression_ternary,
141        map(constant_expression, |x| {
142            ConstantMintypmaxExpression::Unary(Box::new(x))
143        }),
144    ))(s)
145}
146
147#[recursive_parser]
148#[tracable_parser]
149#[packrat_parser]
150pub(crate) fn constant_mintypmax_expression_ternary(
151    s: Span,
152) -> IResult<Span, ConstantMintypmaxExpression> {
153    let (s, a) = constant_expression(s)?;
154    let (s, b) = symbol(":")(s)?;
155    let (s, c) = constant_expression(s)?;
156    let (s, d) = symbol(":")(s)?;
157    let (s, e) = constant_expression(s)?;
158    Ok((
159        s,
160        ConstantMintypmaxExpression::Ternary(Box::new(ConstantMintypmaxExpressionTernary {
161            nodes: (a, b, c, d, e),
162        })),
163    ))
164}
165
166#[tracable_parser]
167#[packrat_parser]
168pub(crate) fn constant_param_expression(s: Span) -> IResult<Span, ConstantParamExpression> {
169    alt((
170        map(constant_mintypmax_expression, |x| {
171            ConstantParamExpression::ConstantMintypmaxExpression(Box::new(x))
172        }),
173        map(data_type, |x| {
174            ConstantParamExpression::DataType(Box::new(x))
175        }),
176        map(symbol("$"), |x| {
177            ConstantParamExpression::Dollar(Box::new(x))
178        }),
179    ))(s)
180}
181
182#[tracable_parser]
183#[packrat_parser]
184pub(crate) fn param_expression(s: Span) -> IResult<Span, ParamExpression> {
185    alt((
186        map(terminated(mintypmax_expression, peek(none_of("#"))), |x| {
187            ParamExpression::MintypmaxExpression(Box::new(x))
188        }),
189        map(data_type, |x| ParamExpression::DataType(Box::new(x))),
190        map(symbol("$"), |x| ParamExpression::Dollar(Box::new(x))),
191    ))(s)
192}
193
194#[tracable_parser]
195#[packrat_parser]
196pub(crate) fn constant_range_expression(s: Span) -> IResult<Span, ConstantRangeExpression> {
197    alt((
198        map(constant_part_select_range, |x| {
199            ConstantRangeExpression::ConstantPartSelectRange(Box::new(x))
200        }),
201        map(constant_expression, |x| {
202            ConstantRangeExpression::ConstantExpression(Box::new(x))
203        }),
204    ))(s)
205}
206
207#[tracable_parser]
208#[packrat_parser]
209pub(crate) fn constant_part_select_range(s: Span) -> IResult<Span, ConstantPartSelectRange> {
210    alt((
211        map(constant_range, |x| {
212            ConstantPartSelectRange::ConstantRange(Box::new(x))
213        }),
214        map(constant_indexed_range, |x| {
215            ConstantPartSelectRange::ConstantIndexedRange(Box::new(x))
216        }),
217    ))(s)
218}
219
220#[recursive_parser]
221#[tracable_parser]
222#[packrat_parser]
223pub(crate) fn constant_range(s: Span) -> IResult<Span, ConstantRange> {
224    let (s, a) = constant_expression(s)?;
225    let (s, b) = symbol(":")(s)?;
226    let (s, c) = constant_expression(s)?;
227    Ok((s, ConstantRange { nodes: (a, b, c) }))
228}
229
230#[recursive_parser]
231#[tracable_parser]
232#[packrat_parser]
233pub(crate) fn constant_indexed_range(s: Span) -> IResult<Span, ConstantIndexedRange> {
234    let (s, a) = constant_expression(s)?;
235    let (s, b) = alt((symbol("+:"), symbol("-:")))(s)?;
236    let (s, c) = constant_expression(s)?;
237    Ok((s, ConstantIndexedRange { nodes: (a, b, c) }))
238}
239
240#[tracable_parser]
241#[packrat_parser]
242pub(crate) fn expression(s: Span) -> IResult<Span, Expression> {
243    alt((
244        map(terminated(primary, peek(one_of(",();"))), |x| {
245            Expression::Primary(Box::new(x))
246        }),
247        expression_binary,
248        map(conditional_expression, |x| {
249            Expression::ConditionalExpression(Box::new(x))
250        }),
251        map(inside_expression, |x| {
252            Expression::InsideExpression(Box::new(x))
253        }),
254        expression_unary,
255        map(inc_or_dec_expression, |x| {
256            Expression::IncOrDecExpression(Box::new(x))
257        }),
258        expression_operator_assignment,
259        map(tagged_union_expression, |x| {
260            Expression::TaggedUnionExpression(Box::new(x))
261        }),
262        map(primary, |x| Expression::Primary(Box::new(x))),
263    ))(s)
264}
265
266#[tracable_parser]
267#[packrat_parser]
268pub(crate) fn expression_unary(s: Span) -> IResult<Span, Expression> {
269    let (s, x) = unary_operator(s)?;
270    let (s, y) = many0(attribute_instance)(s)?;
271    let (s, z) = primary(s)?;
272    Ok((
273        s,
274        Expression::Unary(Box::new(ExpressionUnary { nodes: (x, y, z) })),
275    ))
276}
277
278#[tracable_parser]
279#[packrat_parser]
280pub(crate) fn expression_operator_assignment(s: Span) -> IResult<Span, Expression> {
281    let (s, a) = paren(operator_assignment)(s)?;
282    Ok((
283        s,
284        Expression::OperatorAssignment(Box::new(ExpressionOperatorAssignment { nodes: (a,) })),
285    ))
286}
287
288#[recursive_parser]
289#[tracable_parser]
290#[packrat_parser]
291pub(crate) fn expression_binary(s: Span) -> IResult<Span, Expression> {
292    let (s, a) = expression(s)?;
293    let (s, b) = binary_operator(s)?;
294    let (s, c) = many0(attribute_instance)(s)?;
295    let (s, d) = expression(s)?;
296    Ok((
297        s,
298        Expression::Binary(Box::new(ExpressionBinary {
299            nodes: (a, b, c, d),
300        })),
301    ))
302}
303
304#[tracable_parser]
305#[packrat_parser]
306pub(crate) fn tagged_union_expression(s: Span) -> IResult<Span, TaggedUnionExpression> {
307    let (s, a) = keyword("tagged")(s)?;
308    let (s, b) = member_identifier(s)?;
309    let (s, c) = opt(expression)(s)?;
310    Ok((s, TaggedUnionExpression { nodes: (a, b, c) }))
311}
312
313#[recursive_parser]
314#[tracable_parser]
315#[packrat_parser]
316pub(crate) fn inside_expression(s: Span) -> IResult<Span, InsideExpression> {
317    let (s, a) = expression(s)?;
318    let (s, b) = keyword("inside")(s)?;
319    let (s, c) = brace(open_range_list)(s)?;
320    Ok((s, InsideExpression { nodes: (a, b, c) }))
321}
322
323#[tracable_parser]
324#[packrat_parser]
325pub(crate) fn value_range(s: Span) -> IResult<Span, ValueRange> {
326    alt((
327        value_range_binary,
328        map(expression, |x| ValueRange::Expression(Box::new(x))),
329    ))(s)
330}
331
332#[tracable_parser]
333#[packrat_parser]
334pub(crate) fn value_range_binary(s: Span) -> IResult<Span, ValueRange> {
335    let (s, a) = bracket(triple(expression, symbol(":"), expression))(s)?;
336    Ok((
337        s,
338        ValueRange::Binary(Box::new(ValueRangeBinary { nodes: (a,) })),
339    ))
340}
341
342#[tracable_parser]
343#[packrat_parser]
344pub(crate) fn mintypmax_expression(s: Span) -> IResult<Span, MintypmaxExpression> {
345    alt((
346        mintypmax_expression_ternary,
347        map(expression, |x| MintypmaxExpression::Expression(Box::new(x))),
348    ))(s)
349}
350
351#[recursive_parser]
352#[tracable_parser]
353#[packrat_parser]
354pub(crate) fn mintypmax_expression_ternary(s: Span) -> IResult<Span, MintypmaxExpression> {
355    let (s, a) = expression(s)?;
356    let (s, b) = symbol(":")(s)?;
357    let (s, c) = expression(s)?;
358    let (s, d) = symbol(":")(s)?;
359    let (s, e) = expression(s)?;
360    Ok((
361        s,
362        MintypmaxExpression::Ternary(Box::new(MintypmaxExpressionTernary {
363            nodes: (a, b, c, d, e),
364        })),
365    ))
366}
367
368#[recursive_parser]
369#[tracable_parser]
370#[packrat_parser]
371pub(crate) fn module_path_conditional_expression(
372    s: Span,
373) -> IResult<Span, ModulePathConditionalExpression> {
374    let (s, a) = module_path_expression(s)?;
375    let (s, b) = symbol("?")(s)?;
376    let (s, c) = many0(attribute_instance)(s)?;
377    let (s, d) = module_path_expression(s)?;
378    let (s, e) = symbol(":")(s)?;
379    let (s, f) = module_path_expression(s)?;
380    Ok((
381        s,
382        ModulePathConditionalExpression {
383            nodes: (a, b, c, d, e, f),
384        },
385    ))
386}
387
388#[tracable_parser]
389#[packrat_parser]
390pub(crate) fn module_path_expression(s: Span) -> IResult<Span, ModulePathExpression> {
391    alt((
392        module_path_expression_binary,
393        map(module_path_conditional_expression, |x| {
394            ModulePathExpression::ModulePathConditionalExpression(Box::new(x))
395        }),
396        map(module_path_primary, |x| {
397            ModulePathExpression::ModulePathPrimary(Box::new(x))
398        }),
399        module_path_expression_unary,
400    ))(s)
401}
402
403#[tracable_parser]
404#[packrat_parser]
405pub(crate) fn module_path_expression_unary(s: Span) -> IResult<Span, ModulePathExpression> {
406    let (s, a) = unary_module_path_operator(s)?;
407    let (s, b) = many0(attribute_instance)(s)?;
408    let (s, c) = module_path_primary(s)?;
409    Ok((
410        s,
411        ModulePathExpression::Unary(Box::new(ModulePathExpressionUnary { nodes: (a, b, c) })),
412    ))
413}
414
415#[recursive_parser]
416#[tracable_parser]
417#[packrat_parser]
418pub(crate) fn module_path_expression_binary(s: Span) -> IResult<Span, ModulePathExpression> {
419    let (s, a) = module_path_expression(s)?;
420    let (s, b) = binary_module_path_operator(s)?;
421    let (s, c) = many0(attribute_instance)(s)?;
422    let (s, d) = module_path_expression(s)?;
423    Ok((
424        s,
425        ModulePathExpression::Binary(Box::new(ModulePathExpressionBinary {
426            nodes: (a, b, c, d),
427        })),
428    ))
429}
430
431#[tracable_parser]
432#[packrat_parser]
433pub(crate) fn module_path_mintypmax_expression(
434    s: Span,
435) -> IResult<Span, ModulePathMintypmaxExpression> {
436    alt((
437        module_path_mintypmax_expression_ternary,
438        map(module_path_expression, |x| {
439            ModulePathMintypmaxExpression::ModulePathExpression(Box::new(x))
440        }),
441    ))(s)
442}
443
444#[recursive_parser]
445#[tracable_parser]
446#[packrat_parser]
447pub(crate) fn module_path_mintypmax_expression_ternary(
448    s: Span,
449) -> IResult<Span, ModulePathMintypmaxExpression> {
450    let (s, a) = module_path_expression(s)?;
451    let (s, b) = symbol(":")(s)?;
452    let (s, c) = module_path_expression(s)?;
453    let (s, d) = symbol(":")(s)?;
454    let (s, e) = module_path_expression(s)?;
455    Ok((
456        s,
457        ModulePathMintypmaxExpression::Ternary(Box::new(ModulePathMintypmaxExpressionTernary {
458            nodes: (a, b, c, d, e),
459        })),
460    ))
461}
462
463#[tracable_parser]
464#[packrat_parser]
465pub(crate) fn part_select_range(s: Span) -> IResult<Span, PartSelectRange> {
466    alt((
467        map(constant_range, |x| {
468            PartSelectRange::ConstantRange(Box::new(x))
469        }),
470        map(indexed_range, |x| {
471            PartSelectRange::IndexedRange(Box::new(x))
472        }),
473    ))(s)
474}
475
476#[recursive_parser]
477#[tracable_parser]
478#[packrat_parser]
479pub(crate) fn indexed_range(s: Span) -> IResult<Span, IndexedRange> {
480    let (s, a) = expression(s)?;
481    let (s, b) = alt((symbol("+:"), symbol("-:")))(s)?;
482    let (s, c) = constant_expression(s)?;
483    Ok((s, IndexedRange { nodes: (a, b, c) }))
484}
485
486#[tracable_parser]
487#[packrat_parser]
488pub(crate) fn genvar_expression(s: Span) -> IResult<Span, GenvarExpression> {
489    let (s, a) = constant_expression(s)?;
490    Ok((s, GenvarExpression { nodes: (a,) }))
491}