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
562
563
// Copyright (c) ZeroC, Inc.

use crate::grammar::CompilationMode;
use crate::implement_diagnostic_functions;
use crate::utils::string_util::indefinite_article;

#[derive(Debug)]
pub enum Error {
    // ----------------  Generic Errors ---------------- //
    IO {
        action: &'static str,
        path: String,
        error: std::io::Error,
    },

    Syntax {
        message: String,
    },

    // ---------------- Dictionary Errors ---------------- //
    /// Dictionaries cannot use optional types as keys.
    KeyMustBeNonOptional,

    /// An unsupported type was used as a dictionary key type.
    KeyTypeNotSupported {
        /// The type and/or identifier of the type that was used as a dictionary key type.
        kind: String,
    },

    /// Struct contains a field that cannot be used as a dictionary key type.
    StructKeyContainsDisallowedType {
        /// The identifier of the struct.
        struct_identifier: String,
    },

    /// Structs must be compact to be used as a dictionary key type.
    StructKeyMustBeCompact,

    // ----------------  Compilation Mode Errors ---------------- //
    /// The user specified the compilation mode multiple times in a single Slice file.
    MultipleCompilationModes,

    /// A Slice construct was defined in a compilation mode that it isn't supported by.
    NotSupportedInCompilationMode {
        /// The kind that is not supported.
        kind: String,
        /// The identifier of the kind that is not supported.
        identifier: String,
        /// The compilation mode the construct was defined in.
        mode: CompilationMode,
    },

    /// Optionals of this kind cannot be used in Slice1 mode.
    OptionalsNotSupported {
        /// The kind that is not supported.
        kind: String,
    },

    /// Streamed parameters cannot be used in Slice1 mode.
    StreamedParametersNotSupported,

    /// A slice type was used in a compilation mode that it isn't supported by.
    UnsupportedType {
        /// The kind that was used.
        kind: String,
        /// The compilation mode the type was used in.
        mode: CompilationMode,
    },

    // ----------------  Enum Errors ---------------- //
    /// Enumerator values must be unique.
    DuplicateEnumeratorValue {
        /// The value of the enumerator that was already used.
        enumerator_value: i128,
    },

    /// Enumerators cannot declare explicit values when their enclosing enum doesn't have an underlying type.
    EnumeratorCannotDeclareExplicitValue {
        enumerator_identifier: String,
    },

    /// Enumerators cannot declare associated fields when their enclosing enum has an underlying type.
    EnumeratorCannotDeclareAssociatedFields {
        enumerator_identifier: String,
    },

    /// Enums cannot have optional underlying types.
    CannotUseOptionalUnderlyingType {
        /// The identifier of the enum.
        enum_identifier: String,
    },

    /// An enumerator was found that was out of bounds of the underlying type of the parent enum.
    EnumeratorValueOutOfBounds {
        /// The identifier of the enumerator.
        enumerator_identifier: String,
        /// The value of the out of bounds enumerator.
        value: i128,
        /// The minimum value of the underlying type of the enum.
        min: i128,
        /// The maximum value of the underlying type of the enum.
        max: i128,
    },

    /// Enums must be contain at least one enumerator.
    MustContainEnumerators {
        /// The identifier of the enum.
        enum_identifier: String,
    },

    /// Enum underlying types must be integral types.
    EnumUnderlyingTypeNotSupported {
        /// The identifier of the enum.
        enum_identifier: String,
        /// The name of the non-integral type that was used as the underlying type of the enum.
        kind: Option<String>,
    },

    // ----------------  Exception Errors ---------------- //
    /// Exception specifications can only be used in Slice1 mode.
    ExceptionSpecificationNotSupported,

    // ----------------  Operation Errors ---------------- //
    /// A streamed parameter was not the last parameter in the operation.
    StreamedMembersMustBeLast {
        /// The identifier of the parameter that caused the error.
        parameter_identifier: String,
    },

    /// Return tuples for an operation must contain at least two element.
    ReturnTuplesMustContainAtLeastTwoElements,

    /// Multiple streamed parameters were used as parameters for an operation.
    MultipleStreamedMembers,

    // ----------------  Struct Errors ---------------- //
    /// Compact structs cannot be empty.
    CompactStructCannotBeEmpty,

    /// Compact structs cannot contain tagged fields.
    CompactStructCannotContainTaggedFields,

    // ----------------  Tag Errors ---------------- //
    /// A duplicate tag value was found.
    CannotHaveDuplicateTag {
        /// The identifier of the tagged member.
        identifier: String,
    },

    /// Cannot tag a class.
    CannotTagClass {
        /// The identifier of the tagged member.
        identifier: String,
    },

    /// Cannot tag a member that contains a class.
    CannotTagContainingClass {
        /// The identifier of the tagged member.
        identifier: String,
    },

    /// A tag value was not in the expected range, 0 .. i32::MAX.
    TagValueOutOfBounds,

    /// A tagged member was not set to optional.
    TaggedMemberMustBeOptional {
        /// The identifier of the tagged member.
        identifier: String,
    },

    // ----------------  General Errors ---------------- //
    /// A compact ID was not in the expected range, 0 .. i32::MAX.
    CompactIdOutOfBounds,

    /// An identifier was redefined.
    Redefinition {
        /// The identifier that was redefined.
        identifier: String,
    },

    /// A self-referential type alias has no concrete type.
    SelfReferentialTypeAliasNeedsConcreteType {
        /// The name of the type alias.
        identifier: String,
    },

    /// An identifier was used to shadow another identifier.
    Shadows {
        /// The identifier that is shadowing a previously defined identifier.
        identifier: String,
    },

    /// Used to indicate when two types should match, but do not.
    TypeMismatch {
        /// The name of the expected kind.
        expected: String,
        /// The name of the found kind.
        actual: String,
        /// Whether the expected type was a concrete type (true) or a trait type (false).
        is_concrete: bool,
    },

    /// An integer literal was outside the parsable range of 0..i128::MAX.
    IntegerLiteralOverflows,

    /// An integer literal contained illegal characters for its base.
    InvalidIntegerLiteral {
        /// The base of the integer literal; Ex: 16 (hex), 10 (dec).
        base: u32,
    },

    /// An invalid compilation mode was specified.
    InvalidCompilationMode {
        /// The compilation mode that was specified.
        mode: String,
    },

    /// An self-referential type had an infinite size cycle.
    InfiniteSizeCycle {
        /// The type id of the type that caused the error.
        type_id: String,
        /// The cycle that was found.
        cycle: String,
    },

    /// No element with the specified identifier was found.
    DoesNotExist {
        /// The identifier that was not found.
        identifier: String,
    },

    // ----------------  Attribute Errors ---------------- //
    /// An invalid argument was provided to an attribute directive.
    ArgumentNotSupported {
        /// The argument that was provided.
        argument: String,
        /// The directive it was provided to.
        directive: String,
    },

    // The following are errors that are needed to report cs attribute errors.
    MissingRequiredArgument {
        argument: String,
    },

    MissingRequiredAttribute {
        attribute: String,
    },

    TooManyArguments {
        expected: String,
    },

    UnexpectedAttribute {
        attribute: String,
    },

    AttributeIsNotRepeatable {
        attribute: String,
    },

    // ----------------  Type Alias Errors ---------------- //
    /// A type alias had an optional underlying type.
    TypeAliasOfOptional,
}

implement_diagnostic_functions!(
    Error,
    (
        "E001",
        IO,
        format!("unable to {action} '{path}': {}", io_error_message(error)),
        action,
        path,
        error
    ),
    (
        "E002",
        Syntax,
        format!("invalid syntax: {message}"),
        message
    ),
    (
        "E004",
        ArgumentNotSupported,
        format!("'{argument}' is not a legal argument for the '{directive}' attribute"),
        argument,
        directive
    ),
    (
        "E005",
        KeyMustBeNonOptional,
        "optional types are not valid dictionary key types"
    ),
    (
        "E006",
        StructKeyMustBeCompact,
        "structs must be compact to be used as a dictionary key type"
    ),
    (
        "E007",
        KeyTypeNotSupported,
        format!("invalid dictionary key type: {kind}"),
        kind
    ),
    (
        "E008",
        StructKeyContainsDisallowedType,
        format!("struct '{struct_identifier}' contains fields that are not a valid dictionary key types"),
        struct_identifier
    ),
    (
        "E009",
        CannotUseOptionalUnderlyingType,
        format!("invalid enum '{enum_identifier}': enums cannot have optional underlying types"),
        enum_identifier
    ),
    (
        "E010",
        MustContainEnumerators,
        format!("invalid enum '{enum_identifier}': enums must contain at least one enumerator"),
        enum_identifier
    ),
    (
        "E011",
        EnumUnderlyingTypeNotSupported,
        {
            if let Some(kind) = kind {
                format!("invalid enum '{enum_identifier}': underlying type '{kind}' is not supported", )
            } else {
                format!("invalid enum '{enum_identifier}': missing required underlying type")
            }
        },
        enum_identifier,
        kind
    ),
    (
        "E012",
        Redefinition,
        format!("redefinition of '{identifier}'"),
        identifier
    ),
    (
        "E013",
        Shadows,
        format!("'{identifier}' shadows another symbol"),
        identifier
    ),
    (
        "E014",
        CannotHaveDuplicateTag,
        format!("invalid tag on member '{identifier}': tags must be unique"),
        identifier
    ),
    (
        "E016",
        StreamedMembersMustBeLast,
        format!("invalid parameter '{parameter_identifier}': only the last parameter in an operation can use the stream modifier"),
        parameter_identifier
    ),
    (
        "E017",
        ReturnTuplesMustContainAtLeastTwoElements,
        "return tuples must have at least 2 elements"
    ),
    (
        "E018",
        CompactStructCannotContainTaggedFields,
        "tagged fields are not supported in compact structs\nconsider removing the tag, or making the struct non-compact"
    ),
    (
        "E019",
        TaggedMemberMustBeOptional,
        format!("invalid tag on member '{identifier}': tagged members must be optional"),
        identifier
    ),
    (
        "E020",
        CannotTagClass,
        format!("invalid tag on member '{identifier}': tagged members cannot be classes"),
        identifier
    ),
    (
        "E021",
        CannotTagContainingClass,
        format!("invalid tag on member '{identifier}': tagged members cannot contain classes"),
        identifier
    ),
    (
        "E022",
        TypeMismatch,
        format!(
            "type mismatch: expected {} '{expected}' but found {} '{actual}'{}",
            indefinite_article(expected),
            indefinite_article(actual),
            if *is_concrete {
                "".to_owned()
            } else {
                format!(" (which isn't {} '{expected}')", indefinite_article(expected))
            }
        ),
        expected,
        actual,
        is_concrete
    ),
    (
        "E024",
        CompactStructCannotBeEmpty,
        "compact structs must be non-empty"
    ),
    (
        "E025",
        SelfReferentialTypeAliasNeedsConcreteType,
        format!("self-referential type alias '{identifier}' has no concrete type"),
        identifier
    ),
    (
        "E026",
        EnumeratorValueOutOfBounds,
        format!(
            "invalid enumerator '{enumerator_identifier}': enumerator value '{value}' is out of bounds. The value must be between '{min}..{max}', inclusive",
        ),
        enumerator_identifier, value, min, max
    ),
    (
        "E027",
        TagValueOutOfBounds,
        "tag values must be within the range 0 <= value <= 2147483647"
    ),
    (
        "E028",
        DuplicateEnumeratorValue,
        format!("enumerator values must be unique; the value '{enumerator_value}' is already in use"),
        enumerator_value
    ),
    (
        "E029",
        NotSupportedInCompilationMode,
        format!("{kind} '{identifier}' cannot be defined in {mode} mode"),
        kind, identifier, mode
    ),
    (
        "E030",
        UnsupportedType,
        format!("the type '{kind}' cannot be used in {mode} mode"),
        kind,
        mode
    ),
    (
        "E032",
        OptionalsNotSupported,
        format!("optionals of type '{kind}' cannot be used in Slice1 mode"),
        kind
    ),
    (
        "E033",
        StreamedParametersNotSupported,
        "streamed parameters cannot be used in Slice1 mode"
    ),
    (
        "E034",
        UnexpectedAttribute,
        format!("unexpected attribute '{attribute}'"),
        attribute
    ),
    (
        "E035",
        MissingRequiredArgument,
        format!("missing required argument '{argument}'"),
        argument
    ),
    (
        "E036",
        TooManyArguments,
        format!("too many arguments, expected '{expected}'"),
        expected
    ),
    (
        "E037",
        MissingRequiredAttribute,
        format!("missing required attribute '{attribute}'"),
        attribute
    ),
    (
        "E038",
        MultipleStreamedMembers,
        "cannot have multiple streamed members"
    ),
    (
        "E039",
        CompactIdOutOfBounds,
        "compact IDs must be within the range 0 <= ID <= 2147483647"
    ),
    (
        "E040",
        IntegerLiteralOverflows,
        "integer literal is outside the parsable range of -2^127 <= i <= 2^127 - 1"
    ),
    (
        "E041",
        InvalidIntegerLiteral,
        format!("integer literal contains illegal characters for base-{base}"),
        base
    ),
    (
        "E042",
        InvalidCompilationMode,
        format!("'{mode}' is not a valid Slice compilation mode"),
        mode
    ),
    (
        "E043",
        MultipleCompilationModes,
        "the compilation mode can only be specified once per file"
    ),
    (
        "E047",
        InfiniteSizeCycle,
        format!("self-referential type {type_id} has infinite size.\n{cycle}"),
        type_id, cycle
    ),
    (
        "E049",
        DoesNotExist,
        format!("no element with identifier '{identifier}' exists"),
        identifier
    ),
    (
        "E050",
        AttributeIsNotRepeatable,
        format!("duplicate attribute '{attribute}'"),
        attribute
    ),
    (
        "E051",
        TypeAliasOfOptional,
        "optional types cannot be aliased"
    ),
    (
        "E052",
        ExceptionSpecificationNotSupported,
        "exceptions can only be thrown by operations defined in Slice1 mode"
    ),
    (
        "E053",
        EnumeratorCannotDeclareExplicitValue,
        format!("invalid enumerator '{enumerator_identifier}': explicit values can only be declared within enums that specify an underlying type"),
        enumerator_identifier
    ),
    (
        "E054",
        EnumeratorCannotDeclareAssociatedFields,
        format!("invalid enumerator '{enumerator_identifier}': associated fields cannot be declared within enums that specify an underlying type"),
        enumerator_identifier
    )
);

fn io_error_message(error: &std::io::Error) -> String {
    match error.kind() {
        std::io::ErrorKind::NotFound => "No such file or directory".to_owned(),
        _ => error.to_string(),
    }
}