1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use crate::*;

// -----------------------------------------------------------------------------

#[tracable_parser]
#[packrat_parser]
pub(crate) fn config_declaration(s: Span) -> IResult<Span, ConfigDeclaration> {
    let (s, a) = context("config", keyword("config"))(s)?;
    let (s, b) = config_identifier(s)?;
    let (s, c) = symbol(";")(s)?;
    let (s, d) = many0(pair(local_parameter_declaration, symbol(";")))(s)?;
    let (s, e) = design_statement(s)?;
    let (s, (f, g)) = many_till(config_rule_statement, keyword("endconfig"))(s)?;
    let (s, h) = opt(pair(symbol(":"), config_identifier))(s)?;
    Ok((
        s,
        ConfigDeclaration {
            nodes: (a, b, c, d, e, f, g, h),
        },
    ))
}

#[tracable_parser]
#[packrat_parser]
pub(crate) fn design_statement(s: Span) -> IResult<Span, DesignStatement> {
    let (s, a) = keyword("design")(s)?;
    let (s, (b, c)) = many_till(
        pair(opt(pair(library_identifier, symbol("."))), cell_identifier),
        symbol(";"),
    )(s)?;
    Ok((s, DesignStatement { nodes: (a, b, c) }))
}

#[tracable_parser]
#[packrat_parser]
pub(crate) fn config_rule_statement(s: Span) -> IResult<Span, ConfigRuleStatement> {
    alt((
        config_rule_statement_default,
        config_rule_statement_inst_lib,
        config_rule_statement_inst_use,
        config_rule_statement_cell_lib,
        config_rule_statement_cell_use,
    ))(s)
}

#[tracable_parser]
#[packrat_parser]
pub(crate) fn config_rule_statement_default(s: Span) -> IResult<Span, ConfigRuleStatement> {
    let (s, a) = default_clause(s)?;
    let (s, b) = liblist_clause(s)?;
    let (s, c) = symbol(";")(s)?;
    Ok((
        s,
        ConfigRuleStatement::Default(Box::new(ConfigRuleStatementDefault { nodes: (a, b, c) })),
    ))
}

#[tracable_parser]
#[packrat_parser]
pub(crate) fn config_rule_statement_inst_lib(s: Span) -> IResult<Span, ConfigRuleStatement> {
    let (s, a) = inst_clause(s)?;
    let (s, b) = liblist_clause(s)?;
    let (s, c) = symbol(";")(s)?;
    Ok((
        s,
        ConfigRuleStatement::InstLib(Box::new(ConfigRuleStatementInstLib { nodes: (a, b, c) })),
    ))
}

#[tracable_parser]
#[packrat_parser]
pub(crate) fn config_rule_statement_inst_use(s: Span) -> IResult<Span, ConfigRuleStatement> {
    let (s, a) = inst_clause(s)?;
    let (s, b) = use_clause(s)?;
    let (s, c) = symbol(";")(s)?;
    Ok((
        s,
        ConfigRuleStatement::InstUse(Box::new(ConfigRuleStatementInstUse { nodes: (a, b, c) })),
    ))
}

#[tracable_parser]
#[packrat_parser]
pub(crate) fn config_rule_statement_cell_lib(s: Span) -> IResult<Span, ConfigRuleStatement> {
    let (s, a) = cell_clause(s)?;
    let (s, b) = liblist_clause(s)?;
    let (s, c) = symbol(";")(s)?;
    Ok((
        s,
        ConfigRuleStatement::CellLib(Box::new(ConfigRuleStatementCellLib { nodes: (a, b, c) })),
    ))
}

#[tracable_parser]
#[packrat_parser]
pub(crate) fn config_rule_statement_cell_use(s: Span) -> IResult<Span, ConfigRuleStatement> {
    let (s, a) = cell_clause(s)?;
    let (s, b) = use_clause(s)?;
    let (s, c) = symbol(";")(s)?;
    Ok((
        s,
        ConfigRuleStatement::CellUse(Box::new(ConfigRuleStatementCellUse { nodes: (a, b, c) })),
    ))
}

#[tracable_parser]
#[packrat_parser]
pub(crate) fn default_clause(s: Span) -> IResult<Span, DefaultClause> {
    let (s, a) = keyword("default")(s)?;
    Ok((s, DefaultClause { nodes: (a,) }))
}

#[tracable_parser]
#[packrat_parser]
pub(crate) fn inst_clause(s: Span) -> IResult<Span, InstClause> {
    let (s, a) = keyword("instance")(s)?;
    let (s, b) = inst_name(s)?;
    Ok((s, InstClause { nodes: (a, b) }))
}

#[tracable_parser]
#[packrat_parser]
pub(crate) fn inst_name(s: Span) -> IResult<Span, InstName> {
    let (s, a) = topmodule_identifier(s)?;
    let (s, b) = many0(pair(symbol("."), instance_identifier))(s)?;
    Ok((s, InstName { nodes: (a, b) }))
}

#[tracable_parser]
#[packrat_parser]
pub(crate) fn cell_clause(s: Span) -> IResult<Span, CellClause> {
    let (s, a) = keyword("cell")(s)?;
    let (s, b) = opt(pair(library_identifier, symbol(".")))(s)?;
    let (s, c) = cell_identifier(s)?;
    Ok((s, CellClause { nodes: (a, b, c) }))
}

#[tracable_parser]
#[packrat_parser]
pub(crate) fn liblist_clause(s: Span) -> IResult<Span, LiblistClause> {
    let (s, a) = keyword("liblist")(s)?;
    let (s, b) = many0(library_identifier)(s)?;
    Ok((s, LiblistClause { nodes: (a, b) }))
}

#[tracable_parser]
#[packrat_parser]
pub(crate) fn use_clause(s: Span) -> IResult<Span, UseClause> {
    alt((use_clause_cell, use_clause_named, use_clause_cell_named))(s)
}

#[tracable_parser]
#[packrat_parser]
pub(crate) fn use_clause_cell(s: Span) -> IResult<Span, UseClause> {
    let (s, a) = keyword("use")(s)?;
    let (s, b) = opt(pair(library_identifier, symbol(".")))(s)?;
    let (s, c) = cell_identifier(s)?;
    let (s, d) = opt(pair(symbol(":"), config))(s)?;
    Ok((
        s,
        UseClause::Cell(Box::new(UseClauseCell {
            nodes: (a, b, c, d),
        })),
    ))
}

#[tracable_parser]
#[packrat_parser]
pub(crate) fn use_clause_named(s: Span) -> IResult<Span, UseClause> {
    let (s, a) = keyword("use")(s)?;
    let (s, b) = list(symbol(","), named_parameter_assignment)(s)?;
    let (s, c) = opt(pair(symbol(":"), config))(s)?;
    Ok((
        s,
        UseClause::Named(Box::new(UseClauseNamed { nodes: (a, b, c) })),
    ))
}

#[tracable_parser]
#[packrat_parser]
pub(crate) fn use_clause_cell_named(s: Span) -> IResult<Span, UseClause> {
    let (s, a) = keyword("use")(s)?;
    let (s, b) = opt(pair(library_identifier, symbol(".")))(s)?;
    let (s, c) = cell_identifier(s)?;
    let (s, d) = list(symbol(","), named_parameter_assignment)(s)?;
    let (s, e) = opt(pair(symbol(":"), config))(s)?;
    Ok((
        s,
        UseClause::CellNamed(Box::new(UseClauseCellNamed {
            nodes: (a, b, c, d, e),
        })),
    ))
}

#[tracable_parser]
#[packrat_parser]
pub(crate) fn config(s: Span) -> IResult<Span, Config> {
    let (s, a) = keyword("config")(s)?;
    Ok((s, Config { nodes: (a,) }))
}