sv_parser_parser/declarations/
task_declarations.rs

1use crate::*;
2
3// -----------------------------------------------------------------------------
4
5#[tracable_parser]
6#[packrat_parser]
7pub(crate) fn task_declaration(s: Span) -> IResult<Span, TaskDeclaration> {
8    let (s, a) = keyword("task")(s)?;
9    let (s, b) = opt(lifetime)(s)?;
10    let (s, c) = task_body_declaration(s)?;
11    Ok((s, TaskDeclaration { nodes: (a, b, c) }))
12}
13
14#[tracable_parser]
15#[packrat_parser]
16pub(crate) fn task_body_declaration(s: Span) -> IResult<Span, TaskBodyDeclaration> {
17    alt((
18        task_body_declaration_without_port,
19        task_body_declaration_with_port,
20    ))(s)
21}
22
23#[tracable_parser]
24#[packrat_parser]
25pub(crate) fn task_body_declaration_without_port(s: Span) -> IResult<Span, TaskBodyDeclaration> {
26    let (s, a) = opt(interface_identifier_or_class_scope)(s)?;
27    let (s, b) = task_identifier(s)?;
28    let (s, c) = symbol(";")(s)?;
29    let (s, d) = many0(tf_item_declaration)(s)?;
30    let (s, (e, f)) = many_till(statement_or_null, keyword("endtask"))(s)?;
31    let (s, g) = opt(pair(symbol(":"), task_identifier))(s)?;
32    Ok((
33        s,
34        TaskBodyDeclaration::WithoutPort(Box::new(TaskBodyDeclarationWithoutPort {
35            nodes: (a, b, c, d, e, f, g),
36        })),
37    ))
38}
39
40#[tracable_parser]
41#[packrat_parser]
42pub(crate) fn task_body_declaration_with_port(s: Span) -> IResult<Span, TaskBodyDeclaration> {
43    let (s, a) = opt(interface_identifier_or_class_scope)(s)?;
44    let (s, b) = task_identifier(s)?;
45    let (s, c) = paren(opt(tf_port_list))(s)?;
46    let (s, d) = symbol(";")(s)?;
47    let (s, e) = many0(block_item_declaration)(s)?;
48    let (s, (f, g)) = many_till(statement_or_null, keyword("endtask"))(s)?;
49    let (s, h) = opt(pair(symbol(":"), task_identifier))(s)?;
50    Ok((
51        s,
52        TaskBodyDeclaration::WithPort(Box::new(TaskBodyDeclarationWithPort {
53            nodes: (a, b, c, d, e, f, g, h),
54        })),
55    ))
56}
57
58#[tracable_parser]
59#[packrat_parser]
60pub(crate) fn tf_item_declaration(s: Span) -> IResult<Span, TfItemDeclaration> {
61    alt((
62        map(block_item_declaration, |x| {
63            TfItemDeclaration::BlockItemDeclaration(Box::new(x))
64        }),
65        map(tf_port_declaration, |x| {
66            TfItemDeclaration::TfPortDeclaration(Box::new(x))
67        }),
68    ))(s)
69}
70
71#[tracable_parser]
72#[packrat_parser]
73pub(crate) fn tf_port_list(s: Span) -> IResult<Span, TfPortList> {
74    let (s, a) = list(symbol(","), tf_port_item)(s)?;
75    Ok((s, TfPortList { nodes: (a,) }))
76}
77
78#[tracable_parser]
79#[packrat_parser]
80pub(crate) fn tf_port_item(s: Span) -> IResult<Span, TfPortItem> {
81    let (s, a) = many0(attribute_instance)(s)?;
82    let (s, b) = opt(tf_port_direction)(s)?;
83    let (s, c) = opt(var)(s)?;
84    let (s, d) = data_type_or_implicit_tf_port_item(s)?;
85    let (s, e) = opt(triple(
86        port_identifier,
87        many0(variable_dimension),
88        opt(pair(symbol("="), expression)),
89    ))(s)?;
90    Ok((
91        s,
92        TfPortItem {
93            nodes: (a, b, c, d, e),
94        },
95    ))
96}
97
98#[tracable_parser]
99#[packrat_parser]
100pub(crate) fn data_type_or_implicit_tf_port_item(s: Span) -> IResult<Span, DataTypeOrImplicit> {
101    alt((
102        map(
103            terminated(
104                data_type,
105                peek(pair(
106                    opt(triple(
107                        port_identifier,
108                        many0(variable_dimension),
109                        opt(pair(symbol("="), expression)),
110                    )),
111                    alt((symbol(","), symbol(")"))),
112                )),
113            ),
114            |x| DataTypeOrImplicit::DataType(Box::new(x)),
115        ),
116        map(
117            terminated(
118                implicit_data_type,
119                peek(pair(
120                    opt(triple(
121                        port_identifier,
122                        many0(variable_dimension),
123                        opt(pair(symbol("="), expression)),
124                    )),
125                    alt((symbol(","), symbol(")"))),
126                )),
127            ),
128            |x| DataTypeOrImplicit::ImplicitDataType(Box::new(x)),
129        ),
130    ))(s)
131}
132
133#[tracable_parser]
134#[packrat_parser]
135pub(crate) fn tf_port_direction(s: Span) -> IResult<Span, TfPortDirection> {
136    alt((
137        map(port_direction, |x| {
138            TfPortDirection::PortDirection(Box::new(x))
139        }),
140        map(pair(keyword("const"), keyword("ref")), |x| {
141            TfPortDirection::ConstRef(Box::new(x))
142        }),
143    ))(s)
144}
145
146#[tracable_parser]
147#[packrat_parser]
148pub(crate) fn tf_port_declaration(s: Span) -> IResult<Span, TfPortDeclaration> {
149    let (s, a) = many0(attribute_instance)(s)?;
150    let (s, b) = tf_port_direction(s)?;
151    let (s, c) = opt(var)(s)?;
152    let (s, d) = data_type_or_implicit_tf_port_declaration(s)?;
153    let (s, e) = list_of_tf_variable_identifiers(s)?;
154    let (s, f) = symbol(";")(s)?;
155    Ok((
156        s,
157        TfPortDeclaration {
158            nodes: (a, b, c, d, e, f),
159        },
160    ))
161}
162
163#[tracable_parser]
164#[packrat_parser]
165pub(crate) fn data_type_or_implicit_tf_port_declaration(
166    s: Span,
167) -> IResult<Span, DataTypeOrImplicit> {
168    alt((
169        map(terminated(data_type, peek(variable_identifier)), |x| {
170            DataTypeOrImplicit::DataType(Box::new(x))
171        }),
172        map(
173            terminated(implicit_data_type, peek(variable_identifier)),
174            |x| DataTypeOrImplicit::ImplicitDataType(Box::new(x)),
175        ),
176    ))(s)
177}
178
179#[tracable_parser]
180#[packrat_parser]
181pub(crate) fn task_prototype(s: Span) -> IResult<Span, TaskPrototype> {
182    let (s, a) = keyword("task")(s)?;
183    let (s, b) = task_identifier(s)?;
184    let (s, c) = opt(paren(opt(tf_port_list)))(s)?;
185    Ok((s, TaskPrototype { nodes: (a, b, c) }))
186}