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 super::{combinator::*, identifier::*};
use crate::ast::*;
pub fn abstract_entity_declaration(input: &str) -> ParseResult<Constraint> {
tag("ABSTRACT")
.map(|_| Constraint::AbstractEntity)
.parse(input)
}
pub fn abstract_supertype_declaration(input: &str) -> ParseResult<Constraint> {
tuple((tag("ABSTRACT"), tag("SUPERTYPE"), opt(subtype_constraint)))
.map(|(_abstract, _supertype, expr)| Constraint::AbstractSuperType(expr))
.parse(input)
}
pub fn subsuper(input: &str) -> ParseResult<(Option<Constraint>, Option<SubTypeDecl>)> {
tuple((opt(supertype_constraint), opt(subtype_declaration))).parse(input)
}
pub fn subtype_declaration(input: &str) -> ParseResult<SubTypeDecl> {
tuple((
tag("SUBTYPE"),
tag("OF"),
char('('),
comma_separated(entity_ref),
char(')'),
))
.map(|(_subtype, _of, _open, entity_references, _close)| SubTypeDecl { entity_references })
.parse(input)
}
pub fn subtype_constraint(input: &str) -> ParseResult<SuperTypeExpression> {
tuple((tag("OF"), char('('), supertype_expression, char(')')))
.map(|(_of, _open, expr, _close)| expr)
.parse(input)
}
pub fn supertype_constraint(input: &str) -> ParseResult<Constraint> {
alt((
abstract_supertype_declaration,
abstract_entity_declaration,
supertype_rule,
))
.parse(input)
}
pub fn supertype_expression(input: &str) -> ParseResult<SuperTypeExpression> {
tuple((
supertype_factor,
many0(tuple((tag("ANDOR"), supertype_factor))),
))
.map(|(first, tails)| {
if !tails.is_empty() {
let mut factors = vec![first];
for (_andor, factor) in tails {
factors.push(factor)
}
SuperTypeExpression::AndOr { factors }
} else {
first
}
})
.parse(input)
}
pub fn supertype_factor(input: &str) -> ParseResult<SuperTypeExpression> {
tuple((supertype_term, many0(tuple((tag("AND"), supertype_term)))))
.map(|(first, tails)| {
if !tails.is_empty() {
let mut terms = vec![first];
for (_and, term) in tails {
terms.push(term)
}
SuperTypeExpression::And { terms }
} else {
first
}
})
.parse(input)
}
pub fn supertype_term(input: &str) -> ParseResult<SuperTypeExpression> {
let expr =
tuple((char('('), supertype_expression, char(')'))).map(|(_open, expr, _close)| expr);
alt((entity_ref.map(SuperTypeExpression::Reference), one_of, expr)).parse(input)
}
pub fn supertype_rule(input: &str) -> ParseResult<Constraint> {
tuple((tag("SUPERTYPE"), subtype_constraint))
.map(|(_supertype, constraint)| Constraint::SuperTypeRule(constraint))
.parse(input)
}
pub fn one_of(input: &str) -> ParseResult<SuperTypeExpression> {
tuple((
tag("ONEOF"),
char('('),
comma_separated(supertype_expression),
char(')'),
))
.map(|(_oneof, _open, exprs, _close)| SuperTypeExpression::OneOf { exprs })
.parse(input)
}
pub fn subtype_constraint_decl(input: &str) -> ParseResult<SubTypeConstraint> {
tuple((
subtype_constraint_head,
subtype_constraint_body,
tag("END_SUBTYPE_CONSTRAINT"),
char(';'),
))
.map(
|((name, entity), (is_abstract, total_over, expr), _end, _semicolon)| SubTypeConstraint {
name,
entity,
is_abstract,
total_over,
expr,
},
)
.parse(input)
}
pub fn subtype_constraint_head(input: &str) -> ParseResult<(String, String)> {
tuple((
tag("SUBTYPE_CONSTRAINT"),
subtype_constraint_id,
tag("FOR"),
entity_ref,
char(';'),
))
.map(|(_start, id, _for, entity, _semicolon)| (id, entity))
.parse(input)
}
pub fn subtype_constraint_body(
input: &str,
) -> ParseResult<(bool, Option<Vec<String>>, Option<SuperTypeExpression>)> {
tuple((
opt(abstract_supertype).map(|opt| opt.is_some()),
opt(total_over),
opt(tuple((supertype_expression, char(';'))).map(|(expr, _semicolon)| expr)),
))
.parse(input)
}
pub fn total_over(input: &str) -> ParseResult<Vec<String>> {
tuple((
tag("TOTAL_OVER"),
char('('),
many1(entity_ref),
char(')'),
char(';'),
))
.map(|(_start, _open, references, _close, _semicolon)| references)
.parse(input)
}
pub fn abstract_supertype(input: &str) -> ParseResult<()> {
tuple((tag("ABSTRACT"), tag("SUPERTYPE"), char(';')))
.map(|(_abstract, _supertype, _semicolon)| ())
.parse(input)
}
#[cfg(test)]
mod tests {
use nom::Finish;
#[test]
fn subtype_constraint_oneof() {
let exp_str = r#"
SUBTYPE_CONSTRAINT separate_species FOR pet;
ABSTRACT SUPERTYPE;
ONEOF(cat, rabbit, dog);
END_SUBTYPE_CONSTRAINT;
"#
.trim();
let (residual, (entity, _remark)) =
super::subtype_constraint_decl(exp_str).finish().unwrap();
dbg!(&entity);
assert_eq!(residual, "");
}
}