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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use std::fmt::Write;
use std::str::FromStr;

use crate::ast::*;
use crate::error::Result;
use crate::parser::FromPair;
use crate::parser::Rule;

/// A clause appearing in a typedef frame.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum TypedefClause {
    IsAnonymous(bool),
    Name(UnquotedString),
    Namespace(NamespaceIdent),
    AltId(Ident),
    Def(QuotedString, XrefList),
    Comment(UnquotedString),
    Subset(SubsetIdent),
    Synonym(Synonym),
    Xref(Xref),
    PropertyValue(PropertyValue),
    Domain(ClassIdent), // QUESTION(@althonos): Should be ID ?
    Range(ClassIdent),  // QUESTION(@althonos): same.
    Builtin(bool),
    HoldsOverChain(RelationIdent, RelationIdent),
    IsAntiSymmetric(bool),
    IsCyclic(bool),
    IsReflexive(bool),
    IsSymmetric(bool),
    IsAsymmetric(bool),
    IsTransitive(bool),
    IsFunctional(bool),
    IsInverseFunctional(bool),
    IsA(RelationIdent),
    IntersectionOf(RelationIdent),
    UnionOf(RelationIdent),
    EquivalentTo(RelationIdent),
    DisjointFrom(RelationIdent),
    InverseOf(RelationIdent),
    TransitiveOver(RelationIdent),
    EquivalentToChain(RelationIdent, RelationIdent),
    DisjointOver(RelationIdent),
    Relationship(RelationIdent, RelationIdent),
    IsObsolete(bool),
    ReplacedBy(RelationIdent),
    Consider(Ident),
    CreatedBy(UnquotedString),
    CreationDate(IsoDateTime),
    ExpandAssertionTo(QuotedString, XrefList),
    ExpandExpressionTo(QuotedString, XrefList),
    IsMetadataTag(bool),
    IsClassLevel(bool),
}

impl Display for TypedefClause {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        use self::TypedefClause::*;
        match self {
            IsAnonymous(b) => f.write_str("is_anonymous: ").and(b.fmt(f)),
            Name(name) => f.write_str("name: ").and(name.fmt(f)),
            Namespace(ns) => f.write_str("namespace: ").and(ns.fmt(f)),
            AltId(id) => f.write_str("alt_id: ").and(id.fmt(f)),
            Def(desc, xrefs) => f
                .write_str("def: ")
                .and(desc.fmt(f))
                .and(f.write_char(' '))
                .and(xrefs.fmt(f)),
            Comment(comment) => f.write_str("comment: ").and(comment.fmt(f)),
            Subset(id) => f.write_str("subset: ").and(id.fmt(f)),
            Synonym(syn) => f.write_str("synonym: ").and(syn.fmt(f)),
            Xref(xref) => f.write_str("xref: ").and(xref.fmt(f)),
            PropertyValue(pv) => f.write_str("property_value: ").and(pv.fmt(f)),
            Domain(id) => f.write_str("domain: ").and(id.fmt(f)),
            Range(id) => f.write_str("range: ").and(id.fmt(f)),
            Builtin(b) => f.write_str("builtin: ").and(b.fmt(f)),
            HoldsOverChain(r1, r2) => f
                .write_str("holds_over_chain: ")
                .and(r1.fmt(f))
                .and(f.write_char(' '))
                .and(r2.fmt(f)),
            IsAntiSymmetric(b) => f.write_str("is_anti_symmetric: ").and(b.fmt(f)),
            IsCyclic(b) => f.write_str("is_cyclic: ").and(b.fmt(f)),
            IsReflexive(b) => f.write_str("is_reflexive: ").and(b.fmt(f)),
            IsSymmetric(b) => f.write_str("is_symmetric: ").and(b.fmt(f)),
            IsAsymmetric(b) => f.write_str("is_asymmetric: ").and(b.fmt(f)),
            IsTransitive(b) => f.write_str("is_transitive: ").and(b.fmt(f)),
            IsFunctional(b) => f.write_str("is_functional: ").and(b.fmt(f)),
            IsInverseFunctional(b) => f.write_str("is_inverse_functional: ").and(b.fmt(f)),
            IsA(r) => f.write_str("is_a: ").and(r.fmt(f)),
            IntersectionOf(r) => f.write_str("intersection_of: ").and(r.fmt(f)),
            UnionOf(r) => f.write_str("union_of: ").and(r.fmt(f)),
            EquivalentTo(r) => f.write_str("equivalent_to: ").and(r.fmt(f)),
            DisjointFrom(r) => f.write_str("disjoint_from: ").and(r.fmt(f)),
            InverseOf(r) => f.write_str("inverse_of: ").and(r.fmt(f)),
            TransitiveOver(r) => f.write_str("transitive_over: ").and(r.fmt(f)),
            EquivalentToChain(r1, r2) => f
                .write_str("equivalent_to_chain: ")
                .and(r1.fmt(f))
                .and(f.write_char(' '))
                .and(r2.fmt(f)),
            DisjointOver(r) => f.write_str("disjoint_over: ").and(r.fmt(f)),
            Relationship(r1, r2) => f
                .write_str("relationship: ")
                .and(r1.fmt(f))
                .and(f.write_char(' '))
                .and(r2.fmt(f)),
            IsObsolete(b) => f.write_str("is_obsolete: ").and(b.fmt(f)),
            ReplacedBy(r) => f.write_str("replaced_by: ").and(r.fmt(f)),
            Consider(id) => f.write_str("consider: ").and(id.fmt(f)),
            CreatedBy(s) => f.write_str("created_by: ").and(s.fmt(f)),
            CreationDate(date) => f.write_str("creation_date: ").and(date.fmt(f)),
            ExpandAssertionTo(desc, xrefs) => f
                .write_str("expand_assertion_to: ")
                .and(desc.fmt(f))
                .and(f.write_char(' '))
                .and(xrefs.fmt(f)),
            ExpandExpressionTo(desc, xrefs) => f
                .write_str("expand_expression_to: ")
                .and(desc.fmt(f))
                .and(f.write_char(' '))
                .and(xrefs.fmt(f)),
            IsMetadataTag(b) => f.write_str("is_metadata_tag: ").and(b.fmt(f)),
            IsClassLevel(b) => f.write_str("is_class_level: ").and(b.fmt(f)),
        }
    }
}

impl<'i> FromPair<'i> for Line<TypedefClause> {
    const RULE: Rule = Rule::TypedefClauseLine;
    unsafe fn from_pair_unchecked(pair: Pair<'i, Rule>) -> Result<Self> {
        let mut inner = pair.into_inner();
        let clause = TypedefClause::from_pair_unchecked(inner.next().unwrap())?;
        let eol = inner.next().unwrap();
        Ok(Eol::from_pair_unchecked(eol)?.and_inner(clause))
    }
}
impl_fromstr!(Line<TypedefClause>);

impl<'i> FromPair<'i> for TypedefClause {
    const RULE: Rule = Rule::TypedefClause;
    unsafe fn from_pair_unchecked(pair: Pair<'i, Rule>) -> Result<Self> {
        let mut inner = pair.into_inner();
        match inner.next().unwrap().as_rule() {
            Rule::IsAnonymousTag => {
                let b = bool::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::IsAnonymous(b))
            }
            Rule::NameTag => {
                let n = UnquotedString::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::Name(n))
            }
            Rule::NamespaceTag => {
                let ns = NamespaceIdent::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::Namespace(ns))
            }
            Rule::AltIdTag => {
                let id = Ident::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::AltId(id))
            }
            Rule::DefTag => {
                let desc = QuotedString::from_pair_unchecked(inner.next().unwrap())?;
                let xrefs = XrefList::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::Def(desc, xrefs))
            }
            Rule::CommentTag => {
                let comment = UnquotedString::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::Comment(comment))
            }
            Rule::SubsetTag => {
                let id = SubsetIdent::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::Subset(id))
            }
            Rule::SynonymTag => {
                let syn = Synonym::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::Synonym(syn))
            }
            Rule::XrefTag => {
                let xref = Xref::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::Xref(xref))
            }
            Rule::PropertyValueTag => {
                let pv = PropertyValue::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::PropertyValue(pv))
            }
            Rule::DomainTag => {
                let id = ClassIdent::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::Domain(id))
            }
            Rule::RangeTag => {
                let id = ClassIdent::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::Range(id))
            }
            Rule::BuiltinTag => {
                let b = bool::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::Builtin(b))
            }
            Rule::HoldsOverChainTag => {
                let r1 = RelationIdent::from_pair_unchecked(inner.next().unwrap())?;
                let r2 = RelationIdent::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::HoldsOverChain(r1, r2))
            }
            Rule::IsAntiSymmetricTag => {
                let b = bool::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::IsAntiSymmetric(b))
            }
            Rule::IsCyclicTag => {
                let b = bool::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::IsCyclic(b))
            }
            Rule::IsReflexiveTag => {
                let b = bool::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::IsReflexive(b))
            }
            Rule::IsSymmetricTag => {
                let b = bool::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::IsSymmetric(b))
            }
            Rule::IsAsymmetricTag => {
                let b = bool::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::IsAsymmetric(b))
            }
            Rule::IsTransitiveTag => {
                let b = bool::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::IsTransitive(b))
            }
            Rule::IsFunctionalTag => {
                let b = bool::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::IsFunctional(b))
            }
            Rule::IsInverseFunctionalTag => {
                let b = bool::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::IsInverseFunctional(b))
            }
            Rule::IsATag => {
                let id = RelationIdent::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::IsA(id))
            }
            Rule::IntersectionOfTag => {
                let id = RelationIdent::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::IntersectionOf(id))
            }
            Rule::UnionOfTag => {
                let id = RelationIdent::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::UnionOf(id))
            }
            Rule::EquivalentToTag => {
                let id = RelationIdent::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::EquivalentTo(id))
            }
            Rule::DisjointFromTag => {
                let id = RelationIdent::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::DisjointFrom(id))
            }
            Rule::InverseOfTag => {
                let id = RelationIdent::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::InverseOf(id))
            }
            Rule::TransitiveOverTag => {
                let id = RelationIdent::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::TransitiveOver(id))
            }
            Rule::EquivalentToChainTag => {
                let r1 = RelationIdent::from_pair_unchecked(inner.next().unwrap())?;
                let r2 = RelationIdent::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::EquivalentToChain(r1, r2))
            }
            Rule::DisjointOverTag => {
                let id = RelationIdent::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::DisjointOver(id))
            }
            Rule::RelationshipTag => {
                let r1 = RelationIdent::from_pair_unchecked(inner.next().unwrap())?;
                let r2 = RelationIdent::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::Relationship(r1, r2))
            }
            Rule::IsObsoleteTag => {
                let b = bool::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::IsObsolete(b))
            }
            Rule::ReplacedByTag => {
                let id = RelationIdent::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::ReplacedBy(id))
            }
            Rule::ConsiderTag => {
                let id = Ident::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::Consider(id))
            }
            Rule::CreatedByTag => {
                let person = UnquotedString::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::CreatedBy(person))
            }
            Rule::CreationDateTag => {
                let date = IsoDateTime::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::CreationDate(date))
            }
            Rule::ExpandAssertionToTag => {
                let desc = QuotedString::from_pair_unchecked(inner.next().unwrap())?;
                let xrefs = XrefList::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::ExpandAssertionTo(desc, xrefs))
            }
            Rule::ExpandExpressionToTag => {
                let desc = QuotedString::from_pair_unchecked(inner.next().unwrap())?;
                let xrefs = XrefList::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::ExpandExpressionTo(desc, xrefs))
            }
            Rule::IsMetadataTagTag => {
                let b = bool::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::IsMetadataTag(b))
            }
            Rule::IsClassLevelTag => {
                let b = bool::from_pair_unchecked(inner.next().unwrap())?;
                Ok(TypedefClause::IsClassLevel(b))
            }
            _ => unreachable!(),
        }
    }
}
impl_fromstr!(TypedefClause);