slicec 0.4.0

The Slice parser and other core components for Slice compilers.
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
// Copyright (c) ZeroC, Inc.

use crate::utils::string_util::indefinite_article;

use std::ops::Range;

#[derive(Debug)]
pub enum Error {
    // ----------------  Generic Errors ---------------- //
    Other {
        message: String,
    },

    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,

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

    /// Enumerators cannot contain fields when their enclosing enum has an underlying type.
    EnumeratorCannotContainFields {
        enumerator_identifier: String,
    },

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

    /// A type was marked 'compact' when it was invalid to do so.
    CannotBeCompact {
        /// The kind of type that was marked compact.
        kind: &'static str,
        /// The identifier of the type.
        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>,
    },

    // ----------------  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,

    // ----------------  Tag Errors ---------------- //
    /// A duplicate tag value was found.
    CannotHaveDuplicateTag {
        /// 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,
    },

    /// Compact types cannot contain tagged fields.
    CompactTypeCannotContainTaggedFields {
        /// The kind of type that contains the fields.
        kind: &'static str,
    },

    // ----------------  General Errors ---------------- //
    /// 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 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 attribute was applied to a Slice element for which it's invalid.
    /// For example: applying `[oneway]` to a struct ('oneway' is only allowed on operations).
    InvalidAttribute {
        /// The directive of the invalid attribute.
        directive: String,
    },

    /// An unknown attribute was encountered, which uses a known prefix.
    /// For example: if a C# code-generator encountered the following attribute: `[cs::foobar]`.
    UnknownAttribute {
        /// The directive of the unknown attribute.
        directive: String,
    },

    /// An element requires a specific attribute to be applied to it, which is not present.
    /// For example: custom types must have their mapped type specified with a `[xxx:type(...)]` attribute.
    MissingRequiredAttribute {
        /// The missing attribute; should include placeholder names for expected arguments.
        expected_attribute: String,
    },

    /// A non-repeatable attribute was applied to the same element multiple times.
    /// For example: `[compress(Args)] [compress(Return)] myOperation()`; 'compress' is not repeatable.
    AttributeIsNotRepeatable {
        /// The directive of the non-repeatable attribute.
        directive: String,
    },

    /// An invalid argument was provided to an attribute which otherwise accepts arguments.
    /// For example: `[compress(FooBar)] myOperation()`; 'compress' accepts arguments but 'FooBar' is not a valid one.
    InvalidAttributeArgument {
        /// The directive of the attribute.
        directive: String,
        /// The invalid argument that was provided.
        argument: String,
    },

    /// Too few or too many arguments were supplied to an otherwise valid attribute.
    /// For example: `[oneway(FooBar)]` ('oneway' takes no arguments) or `[compress]` ('compress' requires arguments).
    IncorrectAttributeArgumentCount {
        /// The directive of the attribute.
        directive: String,
        /// A range representing the number of arguments this attribute can be given.
        expected_count: Range<usize>,
        /// The number of arguments this attribute was actually given.
        actual_count: usize,
    },

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

impl Error {
    /// Returns the error code corresponding to this particular [`Error`].
    pub fn error_code(&self) -> &'static str {
        match self {
            Self::Other { .. } => "E000",
            Self::IO { .. } => "E001",
            Self::Syntax { .. } => "E002",
            Self::KeyMustBeNonOptional => "E003",
            Self::StructKeyMustBeCompact => "E004",
            Self::KeyTypeNotSupported { .. } => "E005",
            Self::StructKeyContainsDisallowedType { .. } => "E006",
            Self::CannotUseOptionalUnderlyingType { .. } => "E007",
            Self::MustContainEnumerators { .. } => "E008",
            Self::EnumUnderlyingTypeNotSupported { .. } => "E009",
            Self::Redefinition { .. } => "E010",
            Self::Shadows { .. } => "E011",
            Self::CannotHaveDuplicateTag { .. } => "E012",
            Self::StreamedMembersMustBeLast { .. } => "E013",
            Self::ReturnTuplesMustContainAtLeastTwoElements => "E014",
            Self::CompactTypeCannotContainTaggedFields { .. } => "E015",
            Self::TaggedMemberMustBeOptional { .. } => "E016",
            Self::TypeMismatch { .. } => "E017",
            Self::CompactStructCannotBeEmpty => "E018",
            Self::SelfReferentialTypeAliasNeedsConcreteType { .. } => "E019",
            Self::EnumeratorValueOutOfBounds { .. } => "E020",
            Self::TagValueOutOfBounds => "E021",
            Self::DuplicateEnumeratorValue { .. } => "E022",
            Self::InvalidAttribute { .. } => "E023",
            Self::UnknownAttribute { .. } => "E024",
            Self::MissingRequiredAttribute { .. } => "E025",
            Self::AttributeIsNotRepeatable { .. } => "E026",
            Self::InvalidAttributeArgument { .. } => "E027",
            Self::IncorrectAttributeArgumentCount { .. } => "E028",
            Self::MultipleStreamedMembers => "E029",
            Self::IntegerLiteralOverflows => "E030",
            Self::InvalidIntegerLiteral { .. } => "E031",
            Self::InfiniteSizeCycle { .. } => "E032",
            Self::DoesNotExist { .. } => "E033",
            Self::TypeAliasOfOptional => "E034",
            Self::EnumeratorCannotContainFields { .. } => "E035",
            Self::CannotBeCompact { .. } => "E036",
        }
    }

    /// Returns a message describing this [`Error`] in detail.
    pub fn message(&self) -> String {
        match self {
            Self::Other { message } => message.clone(),

            Self::IO { action, path, error } => {
                let message = match error.kind() {
                    std::io::ErrorKind::NotFound => "No such file or directory".to_owned(),
                    _ => error.to_string(),
                };
                format!("unable to {action} '{path}': {message}")
            }

            Self::Syntax { message } => format!("invalid syntax: {message}"),

            Self::KeyMustBeNonOptional => "optional types are not valid dictionary key types".to_owned(),

            Self::StructKeyMustBeCompact => "structs must be compact to be used as a dictionary key type".to_owned(),

            Self::KeyTypeNotSupported { kind } => format!("invalid dictionary key type: {kind}"),

            Self::StructKeyContainsDisallowedType { struct_identifier }
                => format!("struct '{struct_identifier}' contains fields that are not a valid dictionary key types"),

            Self::CannotUseOptionalUnderlyingType { enum_identifier }
                => format!("invalid enum '{enum_identifier}': enums cannot have optional underlying types"),

            Self::MustContainEnumerators { enum_identifier }
                => format!("invalid enum '{enum_identifier}': enums must contain at least one enumerator"),

            Self::EnumUnderlyingTypeNotSupported { enum_identifier, kind } => {
                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")
                }
            }

            Self::Redefinition { identifier } => format!("redefinition of '{identifier}'"),

            Self::Shadows { identifier } => format!("'{identifier}' shadows another symbol"),

            Self::CannotHaveDuplicateTag { identifier }
                => format!("invalid tag on member '{identifier}': tags must be unique"),

            Self::StreamedMembersMustBeLast { parameter_identifier }
                => format!("invalid parameter '{parameter_identifier}': only the last parameter in an operation can use the stream modifier"),

            Self::ReturnTuplesMustContainAtLeastTwoElements => "return tuples must have at least 2 elements".to_owned(),

            Self::CompactTypeCannotContainTaggedFields { kind }
                => format!("tagged fields are not supported in compact {kind}s; consider removing the tag, or making the {kind} non-compact"),

            Self::TaggedMemberMustBeOptional { identifier }
                => format!("invalid tag on member '{identifier}': tagged members must be optional"),

            Self::TypeMismatch { expected, actual, is_concrete } => {
                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))
                    }
                )
            }

            Self::CompactStructCannotBeEmpty => "compact structs must be non-empty".to_owned(),

            Self::SelfReferentialTypeAliasNeedsConcreteType { identifier }
                => format!("self-referential type alias '{identifier}' has no concrete type"),

            Self::EnumeratorValueOutOfBounds { enumerator_identifier, value, min, max }
                => format!("invalid enumerator '{enumerator_identifier}': enumerator value '{value}' is out of bounds. The value must be between '{min}..{max}', inclusive"),

            Self::TagValueOutOfBounds => "tag values must be within the range 0 <= value <= 2147483647".to_owned(),

            Self::DuplicateEnumeratorValue { enumerator_value }
                => format!("enumerator values must be unique; the value '{enumerator_value}' is already in use"),

            Self::InvalidAttribute { directive } => format!("invalid attribute '{directive}'"),

            Self::UnknownAttribute { directive } => format!("unknown attribute '{directive}'"),

            Self::MissingRequiredAttribute { expected_attribute }
                => format!("missing required attribute '{expected_attribute}'"),

            Self::AttributeIsNotRepeatable { directive } => format!("duplicate attribute '{directive}'"),

            Self::InvalidAttributeArgument { directive, argument }
                => format!("'{argument}' is not a valid argument to the '{directive}' attribute"),

            Self::IncorrectAttributeArgumentCount { directive, expected_count, actual_count } => {
                let args_provided = if *actual_count == 1 {
                    String::from("1 argument was provided")
                } else {
                    format!("{actual_count} arguments were provided")
                };
                if expected_count.len() == 1 {
                    // If the range is only 1 element long, then this attribute requires an exact number of arguments.
                    let expected = expected_count.start;
                    if expected == 0 {
                        format!("attribute '{directive}' does not take any arguments, but {args_provided}")
                    } else {
                        let args = if expected == 1 { "argument" } else { "arguments" };
                        format!("attribute '{directive}' takes exactly {expected} {args}, but {args_provided}")
                    }
                } else if expected_count.end == usize::MAX {
                    // If the range has no upper bound, then this attribute only requires a minimum number of arguments.
                    let expected = expected_count.start;
                    let args = if expected == 1 { "argument" } else { "arguments" };
                    format!("attribute '{directive}' requires at least {expected} {args}, but only {args_provided}")
                } else {
                    // Otherwise, this attribute accepts a specific range of possible arguments.
                    let min = expected_count.start;
                    let max = expected_count.end - 1;
                    format!("attribute '{directive}' takes between {min} and {max} arguments (inclusive), but {args_provided}")
                }
            }

            Self::MultipleStreamedMembers => "cannot have multiple streamed members".to_owned(),

            Self::IntegerLiteralOverflows
                => "integer literal is outside the parsable range of -2^127 <= i <= 2^127 - 1".to_owned(),

            Self::InvalidIntegerLiteral { base } => format!("integer literal contains illegal characters for base-{base}"),

            Self::InfiniteSizeCycle { type_id, cycle } => format!("type {type_id} illegally references itself: {cycle}"),

            Self::DoesNotExist { identifier } => format!("no element with identifier '{identifier}' exists"),

            Self::TypeAliasOfOptional => "optional types cannot be aliased".to_owned(),

            Self::EnumeratorCannotContainFields { enumerator_identifier }
                => format!("invalid enumerator '{enumerator_identifier}': fields cannot be declared within enums that specify an underlying type"),

            Self::CannotBeCompact { kind, identifier } => format!("'{kind}' '{identifier}' cannot be marked compact"),
        }
    }
}