sv_parser_parser/udp_declaration_and_instantiation/
udp_body.rs

1use crate::*;
2
3// -----------------------------------------------------------------------------
4
5#[tracable_parser]
6#[packrat_parser]
7pub(crate) fn udp_body(s: Span) -> IResult<Span, UdpBody> {
8    alt((
9        map(combinational_body, |x| {
10            UdpBody::CombinationalBody(Box::new(x))
11        }),
12        map(sequential_body, |x| UdpBody::SequentialBody(Box::new(x))),
13    ))(s)
14}
15
16#[tracable_parser]
17#[packrat_parser]
18pub(crate) fn combinational_body(s: Span) -> IResult<Span, CombinationalBody> {
19    let (s, a) = keyword("table")(s)?;
20    let (s, b) = combinational_entry(s)?;
21    let (s, (c, d)) = many_till(combinational_entry, keyword("endtable"))(s)?;
22    Ok((
23        s,
24        CombinationalBody {
25            nodes: (a, b, c, d),
26        },
27    ))
28}
29
30#[tracable_parser]
31#[packrat_parser]
32pub(crate) fn combinational_entry(s: Span) -> IResult<Span, CombinationalEntry> {
33    let (s, a) = level_input_list(s)?;
34    let (s, b) = symbol(":")(s)?;
35    let (s, c) = output_symbol(s)?;
36    let (s, d) = symbol(";")(s)?;
37    Ok((
38        s,
39        CombinationalEntry {
40            nodes: (a, b, c, d),
41        },
42    ))
43}
44
45#[tracable_parser]
46#[packrat_parser]
47pub(crate) fn sequential_body(s: Span) -> IResult<Span, SequentialBody> {
48    let (s, a) = opt(udp_initial_statement)(s)?;
49    let (s, b) = keyword("table")(s)?;
50    let (s, c) = sequential_entry(s)?;
51    let (s, (d, e)) = many_till(sequential_entry, keyword("endtable"))(s)?;
52    Ok((
53        s,
54        SequentialBody {
55            nodes: (a, b, c, d, e),
56        },
57    ))
58}
59
60#[tracable_parser]
61#[packrat_parser]
62pub(crate) fn udp_initial_statement(s: Span) -> IResult<Span, UdpInitialStatement> {
63    let (s, a) = keyword("initial")(s)?;
64    let (s, b) = output_port_identifier(s)?;
65    let (s, c) = symbol("=")(s)?;
66    let (s, d) = init_val(s)?;
67    let (s, e) = symbol(";")(s)?;
68    Ok((
69        s,
70        UdpInitialStatement {
71            nodes: (a, b, c, d, e),
72        },
73    ))
74}
75
76#[tracable_parser]
77#[packrat_parser]
78pub(crate) fn init_val(s: Span) -> IResult<Span, InitVal> {
79    alt((
80        map(keyword("1'b0"), |x| InitVal { nodes: (x,) }),
81        map(keyword("1'b1"), |x| InitVal { nodes: (x,) }),
82        map(keyword("1'bx"), |x| InitVal { nodes: (x,) }),
83        map(keyword("1'bX"), |x| InitVal { nodes: (x,) }),
84        map(keyword("1'B0"), |x| InitVal { nodes: (x,) }),
85        map(keyword("1'B1"), |x| InitVal { nodes: (x,) }),
86        map(keyword("1'Bx"), |x| InitVal { nodes: (x,) }),
87        map(keyword("1'BX"), |x| InitVal { nodes: (x,) }),
88        map(keyword("1"), |x| InitVal { nodes: (x,) }),
89        map(keyword("0"), |x| InitVal { nodes: (x,) }),
90    ))(s)
91}
92
93#[tracable_parser]
94#[packrat_parser]
95pub(crate) fn sequential_entry(s: Span) -> IResult<Span, SequentialEntry> {
96    let (s, a) = seq_input_list(s)?;
97    let (s, b) = symbol(":")(s)?;
98    let (s, c) = current_state(s)?;
99    let (s, d) = symbol(":")(s)?;
100    let (s, e) = next_state(s)?;
101    let (s, f) = symbol(";")(s)?;
102    Ok((
103        s,
104        SequentialEntry {
105            nodes: (a, b, c, d, e, f),
106        },
107    ))
108}
109
110#[tracable_parser]
111#[packrat_parser]
112pub(crate) fn seq_input_list(s: Span) -> IResult<Span, SeqInputList> {
113    alt((
114        map(edge_input_list, |x| {
115            SeqInputList::EdgeInputList(Box::new(x))
116        }),
117        map(level_input_list, |x| {
118            SeqInputList::LevelInputList(Box::new(x))
119        }),
120    ))(s)
121}
122
123#[tracable_parser]
124#[packrat_parser]
125pub(crate) fn level_input_list(s: Span) -> IResult<Span, LevelInputList> {
126    let (s, a) = level_symbol(s)?;
127    let (s, b) = many0(level_symbol)(s)?;
128    Ok((s, LevelInputList { nodes: (a, b) }))
129}
130
131#[tracable_parser]
132#[packrat_parser]
133pub(crate) fn edge_input_list(s: Span) -> IResult<Span, EdgeInputList> {
134    let (s, a) = many0(level_symbol)(s)?;
135    let (s, b) = edge_indicator(s)?;
136    let (s, c) = many0(level_symbol)(s)?;
137    Ok((s, EdgeInputList { nodes: (a, b, c) }))
138}
139
140#[tracable_parser]
141#[packrat_parser]
142pub(crate) fn edge_indicator(s: Span) -> IResult<Span, EdgeIndicator> {
143    alt((
144        edge_indicator_paren,
145        map(edge_symbol, |x| EdgeIndicator::EdgeSymbol(Box::new(x))),
146    ))(s)
147}
148
149#[tracable_parser]
150#[packrat_parser]
151pub(crate) fn edge_indicator_paren(s: Span) -> IResult<Span, EdgeIndicator> {
152    let (s, a) = paren(pair(level_symbol, level_symbol))(s)?;
153    Ok((
154        s,
155        EdgeIndicator::Paren(Box::new(EdgeIndicatorParen { nodes: (a,) })),
156    ))
157}
158
159#[tracable_parser]
160#[packrat_parser]
161pub(crate) fn current_state(s: Span) -> IResult<Span, CurrentState> {
162    let (s, a) = level_symbol(s)?;
163    Ok((s, CurrentState { nodes: (a,) }))
164}
165
166#[tracable_parser]
167#[packrat_parser]
168pub(crate) fn next_state(s: Span) -> IResult<Span, NextState> {
169    alt((
170        map(output_symbol, |x| NextState::OutputSymbol(Box::new(x))),
171        map(symbol("-"), |x| NextState::Minus(Box::new(x))),
172    ))(s)
173}
174
175#[tracable_parser]
176#[packrat_parser]
177pub(crate) fn output_symbol(s: Span) -> IResult<Span, OutputSymbol> {
178    alt((
179        map(symbol("0"), |x| OutputSymbol { nodes: (x,) }),
180        map(symbol("1"), |x| OutputSymbol { nodes: (x,) }),
181        map(symbol("x"), |x| OutputSymbol { nodes: (x,) }),
182        map(symbol("X"), |x| OutputSymbol { nodes: (x,) }),
183    ))(s)
184}
185
186#[tracable_parser]
187#[packrat_parser]
188pub(crate) fn level_symbol(s: Span) -> IResult<Span, LevelSymbol> {
189    alt((
190        map(symbol("0"), |x| LevelSymbol { nodes: (x,) }),
191        map(symbol("1"), |x| LevelSymbol { nodes: (x,) }),
192        map(symbol("x"), |x| LevelSymbol { nodes: (x,) }),
193        map(symbol("X"), |x| LevelSymbol { nodes: (x,) }),
194        map(symbol("?"), |x| LevelSymbol { nodes: (x,) }),
195        map(symbol("b"), |x| LevelSymbol { nodes: (x,) }),
196        map(symbol("B"), |x| LevelSymbol { nodes: (x,) }),
197    ))(s)
198}
199
200#[tracable_parser]
201#[packrat_parser]
202pub(crate) fn edge_symbol(s: Span) -> IResult<Span, EdgeSymbol> {
203    alt((
204        map(symbol("r"), |x| EdgeSymbol { nodes: (x,) }),
205        map(symbol("R"), |x| EdgeSymbol { nodes: (x,) }),
206        map(symbol("f"), |x| EdgeSymbol { nodes: (x,) }),
207        map(symbol("F"), |x| EdgeSymbol { nodes: (x,) }),
208        map(symbol("p"), |x| EdgeSymbol { nodes: (x,) }),
209        map(symbol("P"), |x| EdgeSymbol { nodes: (x,) }),
210        map(symbol("n"), |x| EdgeSymbol { nodes: (x,) }),
211        map(symbol("N"), |x| EdgeSymbol { nodes: (x,) }),
212        map(symbol("*"), |x| EdgeSymbol { nodes: (x,) }),
213    ))(s)
214}