-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathannotation.rs
351 lines (300 loc) · 8.14 KB
/
annotation.rs
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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
use std::fmt::{self, Write};
use crate::{
common::{identifier::Identifier, token, Span, Spanned},
util::write_joined,
value::{IntegerLiteral, Literal, StringLiteral},
};
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Annotation {
Abstract(Abstract),
Cardinality(Cardinality),
Cascade(Cascade),
Distinct(Distinct),
Independent(Independent),
Key(Key),
Range(Range),
Regex(Regex),
Subkey(Subkey),
Unique(Unique),
Values(Values),
}
impl Spanned for Annotation {
fn span(&self) -> Option<Span> {
match self {
Annotation::Abstract(annotation) => annotation.span(),
Annotation::Cardinality(annotation) => annotation.span(),
Annotation::Cascade(annotation) => annotation.span(),
Annotation::Distinct(annotation) => annotation.span(),
Annotation::Independent(annotation) => annotation.span(),
Annotation::Key(annotation) => annotation.span(),
Annotation::Range(annotation) => annotation.span(),
Annotation::Regex(annotation) => annotation.span(),
Annotation::Subkey(annotation) => annotation.span(),
Annotation::Unique(annotation) => annotation.span(),
Annotation::Values(annotation) => annotation.span(),
}
}
}
impl fmt::Display for Annotation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Abstract(inner) => fmt::Display::fmt(inner, f),
Self::Cardinality(inner) => fmt::Display::fmt(inner, f),
Self::Cascade(inner) => fmt::Display::fmt(inner, f),
Self::Distinct(inner) => fmt::Display::fmt(inner, f),
Self::Independent(inner) => fmt::Display::fmt(inner, f),
Self::Key(inner) => fmt::Display::fmt(inner, f),
Self::Range(inner) => fmt::Display::fmt(inner, f),
Self::Regex(inner) => fmt::Display::fmt(inner, f),
Self::Subkey(inner) => fmt::Display::fmt(inner, f),
Self::Unique(inner) => fmt::Display::fmt(inner, f),
Self::Values(inner) => fmt::Display::fmt(inner, f),
}
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Abstract {
pub span: Option<Span>,
}
impl Abstract {
pub fn new(span: Option<Span>) -> Self {
Self { span }
}
}
impl Spanned for Abstract {
fn span(&self) -> Option<Span> {
self.span
}
}
impl fmt::Display for Abstract {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "@{}", token::Annotation::Abstract)
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Cardinality {
pub span: Option<Span>,
pub range: CardinalityRange,
}
impl Cardinality {
pub fn new(span: Option<Span>, range: CardinalityRange) -> Self {
Self { span, range }
}
}
impl Spanned for Cardinality {
fn span(&self) -> Option<Span> {
self.span
}
}
impl fmt::Display for Cardinality {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "@{}({})", token::Annotation::Cardinality, self.range)
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum CardinalityRange {
Exact(IntegerLiteral),
Range(IntegerLiteral, Option<IntegerLiteral>),
}
impl fmt::Display for CardinalityRange {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Exact(exact) => write!(f, "{}", exact),
Self::Range(min, None) => write!(f, "{}..", min),
Self::Range(min, Some(max)) => write!(f, "{}..{}", min, max),
}
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Cascade {
pub span: Option<Span>,
}
impl Cascade {
pub fn new(span: Option<Span>) -> Self {
Self { span }
}
}
impl Spanned for Cascade {
fn span(&self) -> Option<Span> {
self.span
}
}
impl fmt::Display for Cascade {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "@{}", token::Annotation::Cascade)
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Distinct {
pub span: Option<Span>,
}
impl Distinct {
pub fn new(span: Option<Span>) -> Self {
Self { span }
}
}
impl Spanned for Distinct {
fn span(&self) -> Option<Span> {
self.span
}
}
impl fmt::Display for Distinct {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "@{}", token::Annotation::Distinct)
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Independent {
pub span: Option<Span>,
}
impl Independent {
pub fn new(span: Option<Span>) -> Self {
Self { span }
}
}
impl Spanned for Independent {
fn span(&self) -> Option<Span> {
self.span
}
}
impl fmt::Display for Independent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "@{}", token::Annotation::Independent)
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Key {
pub span: Option<Span>,
}
impl Key {
pub fn new(span: Option<Span>) -> Self {
Self { span }
}
}
impl Spanned for Key {
fn span(&self) -> Option<Span> {
self.span
}
}
impl fmt::Display for Key {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "@{}", token::Annotation::Key)
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Range {
pub span: Option<Span>,
pub min: Option<Literal>,
pub max: Option<Literal>,
}
impl Range {
pub fn new(span: Option<Span>, min: Option<Literal>, max: Option<Literal>) -> Self {
Self { span, min, max }
}
}
impl Spanned for Range {
fn span(&self) -> Option<Span> {
self.span
}
}
impl fmt::Display for Range {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "@{}(", token::Annotation::Range)?;
if let Some(min) = &self.min {
write!(f, "{}", min)?;
}
f.write_str("..")?;
if let Some(max) = &self.max {
write!(f, "{}", max)?;
}
f.write_char(')')?;
Ok(())
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Regex {
pub span: Option<Span>,
pub regex: StringLiteral,
}
impl Regex {
pub fn new(span: Option<Span>, regex: StringLiteral) -> Self {
Self { span, regex }
}
}
impl Spanned for Regex {
fn span(&self) -> Option<Span> {
self.span
}
}
impl fmt::Display for Regex {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "@{}({})", token::Annotation::Regex, self.regex.value)
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Subkey {
pub span: Option<Span>,
pub ident: Identifier,
}
impl Subkey {
pub fn new(span: Option<Span>, ident: Identifier) -> Self {
Self { span, ident }
}
}
impl Spanned for Subkey {
fn span(&self) -> Option<Span> {
self.span
}
}
impl fmt::Display for Subkey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "@{}({})", token::Annotation::Subkey, self.ident)
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Unique {
pub span: Option<Span>,
}
impl Unique {
pub fn new(span: Option<Span>) -> Self {
Self { span }
}
}
impl Spanned for Unique {
fn span(&self) -> Option<Span> {
self.span
}
}
impl fmt::Display for Unique {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "@{}", token::Annotation::Unique)
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Values {
pub span: Option<Span>,
pub values: Vec<Literal>,
}
impl Values {
pub fn new(span: Option<Span>, values: Vec<Literal>) -> Self {
Self { span, values }
}
}
impl Spanned for Values {
fn span(&self) -> Option<Span> {
self.span
}
}
impl fmt::Display for Values {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "@{}(", token::Annotation::Values)?;
write_joined!(f, ", ", &self.values)?;
f.write_char(')')?;
Ok(())
}
}