sct-ecl 0.1.33

The SNOMED CT Expression Constraint Language lexer, parser, and evaluator
Documentation
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! The syntax tree of an expression constraint, one type per grammar rule
//! that carries meaning (`ECL.g4`; the rules that only spell a keyword or a
//! character class are folded into their parents).

/// A SNOMED CT identifier as written in an expression (`sctid`: 6 to 18
/// digits, the first not zero).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Sctid(pub u64);

/// `eclconceptreference`: an identifier with its optional term.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ConceptReference {
    /// The identifier.
    pub id: Sctid,
    /// The term between pipes, trimmed.
    pub term: Option<String>,
}

/// `altidentifier`: a code from another scheme (`LOINC#54486-6`).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AltIdentifier {
    /// The scheme alias before the `#`.
    pub scheme: String,
    /// The code after the `#`.
    pub code: String,
    /// The term between pipes, trimmed.
    pub term: Option<String>,
}

/// `eclfocusconcept`, or a parenthesized nested constraint.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum FocusConcept {
    /// A concept reference.
    Reference(ConceptReference),
    /// `*`, any concept.
    Wildcard,
    /// An alternate identifier.
    AltIdentifier(AltIdentifier),
    /// `( expressionconstraint )`.
    Nested(Box<ExpressionConstraint>),
}

/// `constraintoperator`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ConstraintOperator {
    /// `<`
    DescendantOf,
    /// `<<`
    DescendantOrSelfOf,
    /// `<!`
    ChildOf,
    /// `<<!`
    ChildOrSelfOf,
    /// `>`
    AncestorOf,
    /// `>>`
    AncestorOrSelfOf,
    /// `>!`
    ParentOf,
    /// `>>!`
    ParentOrSelfOf,
    /// `!!>`
    Top,
    /// `!!<`
    Bottom,
}

/// The reference set fields a `memberof` names (`^ [field, field]`).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RefsetFields {
    /// `[*]`.
    Any,
    /// `[name, name]`.
    Names(Vec<String>),
}

/// `memberof`: `^`, with its optional field selection.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MemberOf {
    /// The fields between brackets, when given.
    pub fields: Option<RefsetFields>,
}

/// `subexpressionconstraint`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SubExpressionConstraint {
    /// The constraint operator before the focus.
    pub operator: Option<ConstraintOperator>,
    /// The `^` before the focus.
    pub member_of: Option<MemberOf>,
    /// The focus concept or nested constraint.
    pub focus: FocusConcept,
    /// `memberfilterconstraint`s, one list per `{{ M ... }}`.
    pub member_filters: Vec<Vec<MemberFilter>>,
    /// The description and concept filter constraints, in order.
    pub filters: Vec<FilterConstraint>,
    /// `historysupplement`.
    pub history: Option<HistorySupplement>,
}

/// `descriptionfilterconstraint` or `conceptfilterconstraint`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum FilterConstraint {
    /// `{{ D ... }}`.
    Description(Vec<DescriptionFilter>),
    /// `{{ C ... }}`.
    Concept(Vec<ConceptFilter>),
}

/// `expressionconstraint`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ExpressionConstraint {
    /// `refinedexpressionconstraint`: a focus with `:` and a refinement.
    Refined {
        /// The constraint refined.
        focus: SubExpressionConstraint,
        /// The refinement.
        refinement: Box<Refinement>,
    },
    /// `conjunctionexpressionconstraint`, two or more operands.
    Conjunction(Vec<SubExpressionConstraint>),
    /// `disjunctionexpressionconstraint`, two or more operands.
    Disjunction(Vec<SubExpressionConstraint>),
    /// `exclusionexpressionconstraint`.
    Exclusion {
        /// The set kept.
        left: SubExpressionConstraint,
        /// The set removed.
        right: SubExpressionConstraint,
    },
    /// `dottedexpressionconstraint`: a focus followed by `. attribute` steps.
    Dotted {
        /// The constraint the walk starts from.
        focus: SubExpressionConstraint,
        /// The attribute names walked, in order.
        attributes: Vec<SubExpressionConstraint>,
    },
    /// A bare `subexpressionconstraint`.
    Sub(SubExpressionConstraint),
}

/// `eclrefinement`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Refinement {
    /// One sub-refinement.
    Single(Box<SubRefinement>),
    /// Sub-refinements joined by `AND` or `,`, two or more.
    Conjunction(Vec<SubRefinement>),
    /// Sub-refinements joined by `OR`, two or more.
    Disjunction(Vec<SubRefinement>),
}

/// `subrefinement`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SubRefinement {
    /// An attribute set.
    AttributeSet(AttributeSet),
    /// `eclattributegroup`: `[c] { attributes }`.
    Group {
        /// The group cardinality.
        cardinality: Option<Cardinality>,
        /// The attributes in the group.
        attributes: AttributeSet,
    },
    /// `( eclrefinement )`.
    Nested(Box<Refinement>),
}

/// `eclattributeset`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum AttributeSet {
    /// One sub-attribute set.
    Single(Box<SubAttributeSet>),
    /// Joined by `AND` or `,`, two or more.
    Conjunction(Vec<SubAttributeSet>),
    /// Joined by `OR`, two or more.
    Disjunction(Vec<SubAttributeSet>),
}

/// `subattributeset`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SubAttributeSet {
    /// An attribute.
    Attribute(Box<Attribute>),
    /// `( eclattributeset )`.
    Nested(Box<AttributeSet>),
}

/// `eclattribute`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Attribute {
    /// `[min..max]` before the name.
    pub cardinality: Option<Cardinality>,
    /// The reverse flag `R`.
    pub reverse: bool,
    /// `eclattributename`, a sub-expression constraint.
    pub name: SubExpressionConstraint,
    /// The comparison and value.
    pub value: AttributeValue,
}

/// The comparison and value of an attribute or a member field.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum AttributeValue {
    /// `= sub` or `!= sub`.
    Expression {
        /// The operator.
        operator: Equality,
        /// The value constraint.
        value: SubExpressionConstraint,
    },
    /// `op #number`.
    Numeric {
        /// The operator.
        operator: Comparison,
        /// The number.
        value: NumericValue,
    },
    /// `= "string"`, `= wild:"..."`, or a set of them.
    String {
        /// The operator.
        operator: Equality,
        /// The search terms; one prints bare, more print as a set.
        terms: Vec<TypedSearchTerm>,
    },
    /// `= true` or `= false`.
    Boolean {
        /// The operator.
        operator: Equality,
        /// The value.
        value: bool,
    },
}

/// `cardinality`: `min..max`, `max` absent for `*`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Cardinality {
    /// The minimum.
    pub min: u32,
    /// The maximum, `None` for `*`.
    pub max: Option<u32>,
}

/// `expressioncomparisonoperator`, `stringcomparisonoperator`,
/// `booleancomparisonoperator`, and `idcomparisonoperator`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Equality {
    /// `=`
    Equal,
    /// `!=`
    NotEqual,
}

/// `numericcomparisonoperator` and `timecomparisonoperator`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Comparison {
    /// `=`
    Equal,
    /// `!=`
    NotEqual,
    /// `<`
    Less,
    /// `<=`
    LessOrEqual,
    /// `>`
    Greater,
    /// `>=`
    GreaterOrEqual,
}

/// `numericvalue`: the literal after `#`, as written (an optional sign,
/// digits, an optional fraction).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct NumericValue(pub String);

/// `typedsearchterm`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TypedSearchTerm {
    /// `"word word"` or `match:"..."`: every word must match as a prefix.
    Match(Vec<String>),
    /// `wild:"..."`: a pattern with `*` wildcards.
    Wild(String),
}

/// `descriptionfilter`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum DescriptionFilter {
    /// `term op terms`.
    Term {
        /// The operator.
        operator: Equality,
        /// The search terms.
        terms: Vec<TypedSearchTerm>,
    },
    /// `language op codes`.
    Language {
        /// The operator.
        operator: Equality,
        /// Two-letter language codes.
        codes: Vec<String>,
    },
    /// `typeId op concepts`.
    TypeId {
        /// The operator.
        operator: Equality,
        /// The description type concepts.
        value: ConceptSet,
    },
    /// `type op tokens`.
    Type {
        /// The operator.
        operator: Equality,
        /// The description type tokens.
        tokens: Vec<TypeToken>,
    },
    /// `dialectId op value [acceptability]`.
    DialectId {
        /// The operator.
        operator: Equality,
        /// The language reference sets.
        value: DialectIdValue,
        /// The acceptability that applies to every dialect named.
        acceptability: Option<AcceptabilitySet>,
    },
    /// `dialect op aliases [acceptability]`.
    Dialect {
        /// The operator.
        operator: Equality,
        /// The dialect aliases with their own acceptability.
        aliases: Vec<DialectAlias>,
        /// The acceptability that applies to every dialect named.
        acceptability: Option<AcceptabilitySet>,
    },
    /// `moduleId op concepts`.
    Module {
        /// The operator.
        operator: Equality,
        /// The modules.
        value: ConceptSet,
    },
    /// `effectiveTime op times`.
    EffectiveTime {
        /// The operator.
        operator: Comparison,
        /// The times.
        values: Vec<TimeValue>,
    },
    /// `active op value`.
    Active {
        /// The operator.
        operator: Equality,
        /// The value.
        value: bool,
    },
    /// `id op ids`.
    Id {
        /// The operator.
        operator: Equality,
        /// The description identifiers.
        ids: Vec<Sctid>,
    },
}

/// A sub-expression constraint or a set of two or more concept references
/// (`subexpressionconstraint | eclconceptreferenceset`).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ConceptSet {
    /// A constraint.
    Expression(SubExpressionConstraint),
    /// `( ref ref ... )`, two or more.
    Set(Vec<ConceptReference>),
}

/// `typetoken`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TypeToken {
    /// `syn`
    Synonym,
    /// `fsn`
    FullySpecifiedName,
    /// `def`
    Definition,
}

/// The value of a `dialectidfilter`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum DialectIdValue {
    /// A constraint.
    Expression(SubExpressionConstraint),
    /// `( ref [acceptability] ... )`, one or more.
    Set(Vec<(ConceptReference, Option<AcceptabilitySet>)>),
}

/// One alias of a `dialectaliasfilter`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DialectAlias {
    /// The alias (`en-gb`).
    pub alias: String,
    /// The acceptability for this alias alone.
    pub acceptability: Option<AcceptabilitySet>,
}

/// `acceptabilityset`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum AcceptabilitySet {
    /// Acceptability concepts, one or more.
    Concepts(Vec<ConceptReference>),
    /// `accept` and `prefer` tokens, one or more.
    Tokens(Vec<Acceptability>),
}

/// `acceptabilitytoken`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Acceptability {
    /// `accept`
    Acceptable,
    /// `prefer`
    Preferred,
}

/// `timevalue`: `YYYYMMDD` or empty.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TimeValue(pub String);

/// `conceptfilter`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ConceptFilter {
    /// `definitionStatusId op concepts`.
    DefinitionStatusId {
        /// The operator.
        operator: Equality,
        /// The definition status concepts.
        value: ConceptSet,
    },
    /// `definitionStatus op tokens`.
    DefinitionStatus {
        /// The operator.
        operator: Equality,
        /// The tokens.
        tokens: Vec<DefinitionStatus>,
    },
    /// `moduleId op concepts`.
    Module {
        /// The operator.
        operator: Equality,
        /// The modules.
        value: ConceptSet,
    },
    /// `effectiveTime op times`.
    EffectiveTime {
        /// The operator.
        operator: Comparison,
        /// The times.
        values: Vec<TimeValue>,
    },
    /// `active op value`.
    Active {
        /// The operator.
        operator: Equality,
        /// The value.
        value: bool,
    },
}

/// `definitionstatustoken`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DefinitionStatus {
    /// `primitive`
    Primitive,
    /// `defined`
    Defined,
}

/// `memberfilter`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum MemberFilter {
    /// `moduleId op concepts`.
    Module {
        /// The operator.
        operator: Equality,
        /// The modules.
        value: ConceptSet,
    },
    /// `effectiveTime op times`.
    EffectiveTime {
        /// The operator.
        operator: Comparison,
        /// The times.
        values: Vec<TimeValue>,
    },
    /// `active op value`.
    Active {
        /// The operator.
        operator: Equality,
        /// The value.
        value: bool,
    },
    /// `memberfieldfilter`: a reference set field compared to a value.
    Field {
        /// The field name.
        name: String,
        /// The comparison.
        value: FieldValue,
    },
}

/// The comparison of a `memberfieldfilter`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum FieldValue {
    /// `= sub` or `!= sub`.
    Expression {
        /// The operator.
        operator: Equality,
        /// The value constraint.
        value: SubExpressionConstraint,
    },
    /// `op #number`.
    Numeric {
        /// The operator.
        operator: Comparison,
        /// The number.
        value: NumericValue,
    },
    /// `= "string"` or a set.
    String {
        /// The operator.
        operator: Equality,
        /// The search terms.
        terms: Vec<TypedSearchTerm>,
    },
    /// `= true` or `= false`.
    Boolean {
        /// The operator.
        operator: Equality,
        /// The value.
        value: bool,
    },
    /// `< "time"` and the other ordering operators (`=` and `!=` with a
    /// quoted value are string comparisons, the grammar's first match).
    Time {
        /// The operator: `Less`, `LessOrEqual`, `Greater`, or `GreaterOrEqual`.
        operator: Comparison,
        /// The times.
        values: Vec<TimeValue>,
    },
}

/// `historysupplement`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum HistorySupplement {
    /// `{{ + HISTORY }}`, the default profile.
    Default,
    /// `{{ + HISTORY-MIN }}`.
    Minimum,
    /// `{{ + HISTORY-MOD }}`.
    Moderate,
    /// `{{ + HISTORY-MAX }}`.
    Maximum,
    /// `{{ + HISTORY ( constraint ) }}`: the association reference sets.
    Subset(Box<ExpressionConstraint>),
}