mib-rs 0.10.0

SNMP MIB parser and resolver
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
//! Syntax kinds and lexical spelling metadata.
//!
//! [`SyntaxKind`] is the shared kind vocabulary for lexer tokens and future
//! lossless syntax-tree nodes. Its inventory, spellings, keyword aliases,
//! categories, display names, and libsmi names are declared together so lexer,
//! parser, and tooling APIs cannot drift apart.
//!
//! [`SyntaxKind::Whitespace`], [`SyntaxKind::OpaqueText`],
//! [`SyntaxKind::SourceFile`], and [`SyntaxKind::Error`] form the first
//! lossless-CST vocabulary. The lossless lexer emits the token kinds; tree
//! construction is a separate stage.

use std::fmt;

/// Broad classification of a [`SyntaxKind`].
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum SyntaxCategory {
    /// Lexer control or recovery token.
    Special,
    /// Whitespace or comment trivia.
    Trivia,
    /// Uppercase or lowercase identifier.
    Identifier,
    /// Numeric or string literal.
    Literal,
    /// Fixed punctuation or operator.
    Punctuation,
    /// Recognized SMI or ASN.1 keyword.
    Keyword,
    /// Lossless syntax-tree node.
    Node,
}

/// More specific classification for keyword kinds.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum KeywordCategory {
    /// Keywords framing modules and ASN.1 structures.
    Structural,
    /// Keywords introducing macro clauses.
    Clause,
    /// SMI macro invocation keywords.
    Macro,
    /// Built-in SMI type keywords.
    Type,
    /// ASN.1 tag keywords.
    Tag,
    /// Status and access value keywords.
    StatusAccess,
}

macro_rules! define_syntax_kinds {
    (
        special { $( $special:ident => ($special_libsmi:literal, $special_display:literal); )* }
        trivia { $( $trivia:ident => ($trivia_libsmi:literal, $trivia_display:literal); )* }
        identifiers { $( $identifier:ident => ($identifier_libsmi:literal, $identifier_display:literal); )* }
        literals { $( $literal:ident => ($literal_libsmi:literal, $literal_display:literal); )* }
        punctuation { $( $punctuation:ident => ($byte:literal, $spelling:literal, $punctuation_libsmi:literal, $punctuation_display:literal); )* }
        operators { $( $operator:ident => ($operator_spelling:literal, $operator_libsmi:literal, $operator_display:literal); )* }
        keywords { $( $keyword:ident => ($keyword_category:ident, $canonical:literal, [$($alias:literal),* $(,)?], $keyword_libsmi:literal); )* }
        nodes { $( $node:ident => ($node_libsmi:literal, $node_display:literal); )* }
        forbidden { $( $forbidden:literal ),* $(,)? }
    ) => {
        /// Kind of a lexical token or lossless syntax-tree node.
        ///
        /// Values are stable within a crate release and use a 16-bit
        /// representation so the vocabulary can grow with CST node kinds.
        #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
        #[repr(u16)]
        pub enum SyntaxKind {
            $( #[doc = concat!("`", $special_display, "`")] $special, )*
            $( #[doc = $trivia_display] $trivia, )*
            $( #[doc = $identifier_display] $identifier, )*
            $( #[doc = $literal_display] $literal, )*
            $( #[doc = $punctuation_display] $punctuation, )*
            $( #[doc = $operator_display] $operator, )*
            $( #[doc = $canonical] $keyword, )*
            $( #[doc = $node_display] $node, )*
        }

        impl SyntaxKind {
            /// Every declared token and node kind in discriminant order.
            pub const ALL: &'static [Self] = &[
                $( Self::$special, )*
                $( Self::$trivia, )*
                $( Self::$identifier, )*
                $( Self::$literal, )*
                $( Self::$punctuation, )*
                $( Self::$operator, )*
                $( Self::$keyword, )*
                $( Self::$node, )*
            ];

            /// Return the kind for a raw discriminant, if it is declared.
            pub const fn from_raw(raw: u16) -> Option<Self> {
                match raw {
                    $( value if value == Self::$special as u16 => Some(Self::$special), )*
                    $( value if value == Self::$trivia as u16 => Some(Self::$trivia), )*
                    $( value if value == Self::$identifier as u16 => Some(Self::$identifier), )*
                    $( value if value == Self::$literal as u16 => Some(Self::$literal), )*
                    $( value if value == Self::$punctuation as u16 => Some(Self::$punctuation), )*
                    $( value if value == Self::$operator as u16 => Some(Self::$operator), )*
                    $( value if value == Self::$keyword as u16 => Some(Self::$keyword), )*
                    $( value if value == Self::$node as u16 => Some(Self::$node), )*
                    _ => None,
                }
            }

            /// Return the 16-bit discriminant for this kind.
            pub const fn to_raw(self) -> u16 {
                self as u16
            }

            /// Return this kind's broad syntax category.
            pub const fn category(self) -> SyntaxCategory {
                match self {
                    $( Self::$special => SyntaxCategory::Special, )*
                    $( Self::$trivia => SyntaxCategory::Trivia, )*
                    $( Self::$identifier => SyntaxCategory::Identifier, )*
                    $( Self::$literal => SyntaxCategory::Literal, )*
                    $( Self::$punctuation => SyntaxCategory::Punctuation, )*
                    $( Self::$operator => SyntaxCategory::Punctuation, )*
                    $( Self::$keyword => SyntaxCategory::Keyword, )*
                    $( Self::$node => SyntaxCategory::Node, )*
                }
            }

            /// Return this kind's keyword category, or `None` for non-keywords.
            pub const fn keyword_category(self) -> Option<KeywordCategory> {
                match self {
                    $( Self::$keyword => Some(KeywordCategory::$keyword_category), )*
                    _ => None,
                }
            }

            /// Return whether this kind is emitted by the lexer.
            pub const fn is_token(self) -> bool {
                !self.is_node()
            }

            /// Return whether this kind represents a syntax-tree node.
            pub const fn is_node(self) -> bool {
                matches!(self.category(), SyntaxCategory::Node)
            }

            /// Return whether this kind is whitespace or comment trivia.
            pub const fn is_trivia(self) -> bool {
                matches!(self.category(), SyntaxCategory::Trivia)
            }

            /// Return whether this kind is an uppercase or lowercase identifier.
            pub const fn is_identifier(self) -> bool {
                matches!(self.category(), SyntaxCategory::Identifier)
            }

            /// Return whether this kind is a numeric or string literal.
            pub const fn is_literal(self) -> bool {
                matches!(self.category(), SyntaxCategory::Literal)
            }

            /// Return whether this kind is fixed punctuation or an operator.
            pub const fn is_punctuation(self) -> bool {
                matches!(self.category(), SyntaxCategory::Punctuation)
            }

            /// Return whether this kind is any recognized keyword.
            pub const fn is_keyword(self) -> bool {
                matches!(self.category(), SyntaxCategory::Keyword)
            }

            /// Return whether this is a structural keyword.
            pub const fn is_structural_keyword(self) -> bool {
                matches!(self.keyword_category(), Some(KeywordCategory::Structural))
            }

            /// Return whether this is a clause keyword.
            pub const fn is_clause_keyword(self) -> bool {
                matches!(self.keyword_category(), Some(KeywordCategory::Clause))
            }

            /// Return whether this is an SMI macro invocation keyword.
            pub const fn is_macro_keyword(self) -> bool {
                matches!(self.keyword_category(), Some(KeywordCategory::Macro))
            }

            /// Return whether this is a built-in SMI type keyword.
            pub const fn is_type_keyword(self) -> bool {
                matches!(self.keyword_category(), Some(KeywordCategory::Type))
            }

            /// Return whether this is an ASN.1 tag keyword.
            pub const fn is_tag_keyword(self) -> bool {
                matches!(self.keyword_category(), Some(KeywordCategory::Tag))
            }

            /// Return whether this is a status or access value keyword.
            pub const fn is_status_access_keyword(self) -> bool {
                matches!(self.keyword_category(), Some(KeywordCategory::StatusAccess))
            }

            /// Return fixed source text for punctuation and canonical keywords.
            pub const fn fixed_text(self) -> Option<&'static str> {
                match self {
                    $( Self::$punctuation => Some($spelling), )*
                    $( Self::$operator => Some($operator_spelling), )*
                    $( Self::$keyword => Some($canonical), )*
                    _ => None,
                }
            }

            /// Return every accepted spelling for a keyword kind.
            pub const fn keyword_spellings(self) -> &'static [&'static str] {
                match self {
                    $( Self::$keyword => &[$canonical, $($alias),*], )*
                    _ => &[],
                }
            }

            /// Look up a recognized keyword using case-sensitive source spelling.
            pub fn from_keyword(text: &str) -> Option<Self> {
                match text {
                    $( $canonical $(| $alias)* => Some(Self::$keyword), )*
                    _ => None,
                }
            }

            /// Look up fixed punctuation or a canonical keyword spelling.
            pub fn from_fixed_text(text: &str) -> Option<Self> {
                match text {
                    $( $spelling => Some(Self::$punctuation), )*
                    $( $operator_spelling => Some(Self::$operator), )*
                    $( $canonical => Some(Self::$keyword), )*
                    _ => None,
                }
            }

            /// Look up a single-byte punctuation kind.
            pub const fn from_punctuation_byte(byte: u8) -> Option<Self> {
                match byte {
                    $( $byte => Some(Self::$punctuation), )*
                    _ => None,
                }
            }

            /// Return a human-readable name suitable for parser diagnostics.
            pub const fn display_name(self) -> &'static str {
                match self {
                    $( Self::$special => $special_display, )*
                    $( Self::$trivia => $trivia_display, )*
                    $( Self::$identifier => $identifier_display, )*
                    $( Self::$literal => $literal_display, )*
                    $( Self::$punctuation => $punctuation_display, )*
                    $( Self::$operator => $operator_display, )*
                    $( Self::$keyword => $keyword_libsmi, )*
                    $( Self::$node => $node_display, )*
                }
            }

            /// Return the libsmi-compatible uppercase kind name.
            pub const fn libsmi_name(self) -> &'static str {
                match self {
                    $( Self::$special => $special_libsmi, )*
                    $( Self::$trivia => $trivia_libsmi, )*
                    $( Self::$identifier => $identifier_libsmi, )*
                    $( Self::$literal => $literal_libsmi, )*
                    $( Self::$punctuation => $punctuation_libsmi, )*
                    $( Self::$operator => $operator_libsmi, )*
                    $( Self::$keyword => $keyword_libsmi, )*
                    $( Self::$node => $node_libsmi, )*
                }
            }
        }

        /// Reserved ASN.1 words rejected when used as MIB identifiers.
        pub const FORBIDDEN_KEYWORDS: &[&str] = &[$($forbidden),*];

        /// Look up a recognized keyword using case-sensitive source spelling.
        pub fn lookup_keyword(text: &str) -> Option<SyntaxKind> {
            SyntaxKind::from_keyword(text)
        }

        /// Return whether text is a reserved ASN.1 keyword forbidden as a MIB identifier.
        pub fn is_forbidden_keyword(text: &str) -> bool {
            matches!(text, $($forbidden)|*)
        }
    };
}

define_syntax_kinds! {
    special {
        ErrorToken => ("ERROR", "<error>");
        EofToken => ("EOF", "end of file");
        ForbiddenKeyword => ("FORBIDDEN_KEYWORD", "reserved keyword");
        OpaqueText => ("OPAQUE_TEXT", "opaque text");
    }
    trivia {
        Whitespace => ("WHITESPACE", "whitespace");
        Comment => ("COMMENT", "comment");
    }
    identifiers {
        UppercaseIdent => ("UPPERCASE_IDENTIFIER", "identifier");
        LowercaseIdent => ("LOWERCASE_IDENTIFIER", "identifier");
    }
    literals {
        Number => ("NUMBER", "number");
        NegativeNumber => ("NEGATIVENUMBER", "negative number");
        QuotedString => ("QUOTED_STRING", "quoted string");
        HexString => ("HEX_STRING", "hex string");
        BinString => ("BIN_STRING", "binary string");
    }
    punctuation {
        LBracket => (b'[', "[", "LBRACKET", "'['");
        RBracket => (b']', "]", "RBRACKET", "']'");
        LBrace => (b'{', "{", "LBRACE", "'{'");
        RBrace => (b'}', "}", "RBRACE", "'}'");
        LParen => (b'(', "(", "LPAREN", "'('");
        RParen => (b')', ")", "RPAREN", "')'");
        Colon => (b':', ":", "COLON", "':'");
        Semicolon => (b';', ";", "SEMICOLON", "';'");
        Comma => (b',', ",", "COMMA", "','");
        Dot => (b'.', ".", "DOT", "'.'");
        Pipe => (b'|', "|", "PIPE", "'|'");
        Minus => (b'-', "-", "MINUS", "'-'");
    }
    operators {
        DotDot => ("..", "DOT_DOT", "'..'");
        ColonColonEqual => ("::=", "COLON_COLON_EQUAL", "'::='");
    }
    keywords {
        KwDefinitions => (Structural, "DEFINITIONS", [], "DEFINITIONS");
        KwBegin => (Structural, "BEGIN", [], "BEGIN");
        KwEnd => (Structural, "END", [], "END");
        KwImports => (Structural, "IMPORTS", [], "IMPORTS");
        KwExports => (Structural, "EXPORTS", [], "EXPORTS");
        KwFrom => (Structural, "FROM", [], "FROM");
        KwObject => (Structural, "OBJECT", [], "OBJECT");
        KwIdentifier => (Structural, "IDENTIFIER", [], "IDENTIFIER");
        KwSequence => (Structural, "SEQUENCE", [], "SEQUENCE");
        KwOf => (Structural, "OF", [], "OF");
        KwChoice => (Structural, "CHOICE", [], "CHOICE");
        KwMacro => (Structural, "MACRO", [], "MACRO");

        KwSyntax => (Clause, "SYNTAX", [], "SYNTAX");
        KwMaxAccess => (Clause, "MAX-ACCESS", [], "MAX_ACCESS");
        KwMinAccess => (Clause, "MIN-ACCESS", [], "MIN_ACCESS");
        KwAccess => (Clause, "ACCESS", [], "ACCESS");
        KwStatus => (Clause, "STATUS", [], "STATUS");
        KwDescription => (Clause, "DESCRIPTION", [], "DESCRIPTION");
        KwReference => (Clause, "REFERENCE", [], "REFERENCE");
        KwIndex => (Clause, "INDEX", [], "INDEX");
        KwDefval => (Clause, "DEFVAL", [], "DEFVAL");
        KwAugments => (Clause, "AUGMENTS", [], "AUGMENTS");
        KwUnits => (Clause, "UNITS", [], "UNITS");
        KwDisplayHint => (Clause, "DISPLAY-HINT", [], "DISPLAY_HINT");
        KwObjects => (Clause, "OBJECTS", [], "OBJECTS");
        KwNotifications => (Clause, "NOTIFICATIONS", [], "NOTIFICATIONS");
        KwModule => (Clause, "MODULE", [], "MODULE");
        KwMandatoryGroups => (Clause, "MANDATORY-GROUPS", [], "MANDATORY_GROUPS");
        KwGroup => (Clause, "GROUP", [], "GROUP");
        KwWriteSyntax => (Clause, "WRITE-SYNTAX", [], "WRITE_SYNTAX");
        KwProductRelease => (Clause, "PRODUCT-RELEASE", [], "PRODUCT_RELEASE");
        KwSupports => (Clause, "SUPPORTS", [], "SUPPORTS");
        KwIncludes => (Clause, "INCLUDES", [], "INCLUDES");
        KwVariation => (Clause, "VARIATION", [], "VARIATION");
        KwCreationRequires => (Clause, "CREATION-REQUIRES", [], "CREATION_REQUIRES");
        KwRevision => (Clause, "REVISION", [], "REVISION");
        KwLastUpdated => (Clause, "LAST-UPDATED", [], "LAST_UPDATED");
        KwOrganization => (Clause, "ORGANIZATION", [], "ORGANIZATION");
        KwContactInfo => (Clause, "CONTACT-INFO", [], "CONTACT_INFO");
        KwImplied => (Clause, "IMPLIED", [], "IMPLIED");
        KwSize => (Clause, "SIZE", [], "SIZE");
        KwEnterprise => (Clause, "ENTERPRISE", [], "ENTERPRISE");
        KwVariables => (Clause, "VARIABLES", [], "VARIABLES");

        KwModuleIdentity => (Macro, "MODULE-IDENTITY", [], "MODULE_IDENTITY");
        KwModuleCompliance => (Macro, "MODULE-COMPLIANCE", [], "MODULE_COMPLIANCE");
        KwObjectGroup => (Macro, "OBJECT-GROUP", [], "OBJECT_GROUP");
        KwNotificationGroup => (Macro, "NOTIFICATION-GROUP", [], "NOTIFICATION_GROUP");
        KwAgentCapabilities => (Macro, "AGENT-CAPABILITIES", [], "AGENT_CAPABILITIES");
        KwObjectType => (Macro, "OBJECT-TYPE", [], "OBJECT_TYPE");
        KwObjectIdentity => (Macro, "OBJECT-IDENTITY", [], "OBJECT_IDENTITY");
        KwNotificationType => (Macro, "NOTIFICATION-TYPE", [], "NOTIFICATION_TYPE");
        KwTextualConvention => (Macro, "TEXTUAL-CONVENTION", [], "TEXTUAL_CONVENTION");
        KwTrapType => (Macro, "TRAP-TYPE", [], "TRAP_TYPE");

        KwInteger => (Type, "INTEGER", ["Integer"], "INTEGER");
        KwUnsigned32 => (Type, "Unsigned32", [], "UNSIGNED32");
        KwCounter32 => (Type, "Counter32", [], "COUNTER32");
        KwCounter64 => (Type, "Counter64", [], "COUNTER64");
        KwGauge32 => (Type, "Gauge32", [], "GAUGE32");
        KwIpAddress => (Type, "IpAddress", [], "IPADDRESS");
        KwOpaque => (Type, "Opaque", [], "OPAQUE");
        KwTimeTicks => (Type, "TimeTicks", [], "TIMETICKS");
        KwBits => (Type, "BITS", [], "BITS");
        KwOctet => (Type, "OCTET", [], "OCTET");
        KwString => (Type, "STRING", [], "STRING");
        KwCounter => (Type, "Counter", [], "COUNTER");
        KwGauge => (Type, "Gauge", [], "GAUGE");
        KwNetworkAddress => (Type, "NetworkAddress", [], "NETWORKADDRESS");

        KwApplication => (Tag, "APPLICATION", [], "APPLICATION");
        KwImplicit => (Tag, "IMPLICIT", [], "IMPLICIT");
        KwUniversal => (Tag, "UNIVERSAL", [], "UNIVERSAL");

        KwCurrent => (StatusAccess, "current", [], "CURRENT");
        KwDeprecated => (StatusAccess, "deprecated", [], "DEPRECATED");
        KwObsolete => (StatusAccess, "obsolete", [], "OBSOLETE");
        KwMandatory => (StatusAccess, "mandatory", [], "MANDATORY");
        KwOptional => (StatusAccess, "optional", [], "OPTIONAL");
        KwReadOnly => (StatusAccess, "read-only", [], "READ_ONLY");
        KwReadWrite => (StatusAccess, "read-write", [], "READ_WRITE");
        KwReadCreate => (StatusAccess, "read-create", [], "READ_CREATE");
        KwWriteOnly => (StatusAccess, "write-only", [], "WRITE_ONLY");
        KwNotAccessible => (StatusAccess, "not-accessible", [], "NOT_ACCESSIBLE");
        KwAccessibleForNotify => (StatusAccess, "accessible-for-notify", [], "ACCESSIBLE_FOR_NOTIFY");
        KwNotImplemented => (StatusAccess, "not-implemented", [], "NOT_IMPLEMENTED");
    }
    nodes {
        SourceFile => ("SOURCE_FILE", "source file");
        Module => ("MODULE", "module");
        ModuleHeader => ("MODULE_HEADER", "module header");
        Imports => ("IMPORTS_NODE", "imports");
        ImportGroup => ("IMPORT_GROUP", "import group");
        UnparsedRegion => ("UNPARSED_REGION", "unparsed region");

        ValueAssignment => ("VALUE_ASSIGNMENT", "value assignment");
        TypeAssignment => ("TYPE_ASSIGNMENT", "type assignment");
        TextualConventionDefinition => ("TEXTUAL_CONVENTION_DEFINITION", "TEXTUAL-CONVENTION definition");
        ObjectTypeDefinition => ("OBJECT_TYPE_DEFINITION", "OBJECT-TYPE definition");
        ModuleIdentityDefinition => ("MODULE_IDENTITY_DEFINITION", "MODULE-IDENTITY definition");
        ObjectIdentityDefinition => ("OBJECT_IDENTITY_DEFINITION", "OBJECT-IDENTITY definition");
        NotificationTypeDefinition => ("NOTIFICATION_TYPE_DEFINITION", "NOTIFICATION-TYPE definition");
        TrapTypeDefinition => ("TRAP_TYPE_DEFINITION", "TRAP-TYPE definition");
        MacroDefinition => ("MACRO_DEFINITION", "MACRO definition");
        ObjectGroupDefinition => ("OBJECT_GROUP_DEFINITION", "OBJECT-GROUP definition");
        NotificationGroupDefinition => ("NOTIFICATION_GROUP_DEFINITION", "NOTIFICATION-GROUP definition");
        ModuleComplianceDefinition => ("MODULE_COMPLIANCE_DEFINITION", "MODULE-COMPLIANCE definition");
        AgentCapabilitiesDefinition => ("AGENT_CAPABILITIES_DEFINITION", "AGENT-CAPABILITIES definition");

        ComplianceModule => ("COMPLIANCE_MODULE", "MODULE compliance section");
        MandatoryGroupsClause => ("MANDATORY_GROUPS_CLAUSE", "MANDATORY-GROUPS clause");
        ComplianceGroup => ("COMPLIANCE_GROUP", "GROUP compliance refinement");
        ComplianceObject => ("COMPLIANCE_OBJECT", "OBJECT compliance refinement");
        WriteSyntaxClause => ("WRITE_SYNTAX_CLAUSE", "WRITE-SYNTAX clause");
        SupportsModule => ("SUPPORTS_MODULE", "SUPPORTS capability section");
        IncludesClause => ("INCLUDES_CLAUSE", "INCLUDES clause");
        VariationClause => ("VARIATION_CLAUSE", "VARIATION clause");
        CreationRequiresClause => ("CREATION_REQUIRES_CLAUSE", "CREATION-REQUIRES clause");

        SyntaxClause => ("SYNTAX_CLAUSE", "SYNTAX clause");
        AccessClause => ("ACCESS_CLAUSE", "access clause");
        StatusClause => ("STATUS_CLAUSE", "STATUS clause");
        DescriptionClause => ("DESCRIPTION_CLAUSE", "DESCRIPTION clause");
        ReferenceClause => ("REFERENCE_CLAUSE", "REFERENCE clause");
        UnitsClause => ("UNITS_CLAUSE", "UNITS clause");
        DisplayHintClause => ("DISPLAY_HINT_CLAUSE", "DISPLAY-HINT clause");
        IndexClause => ("INDEX_CLAUSE", "INDEX clause");
        IndexItem => ("INDEX_ITEM", "index item");
        AugmentsClause => ("AUGMENTS_CLAUSE", "AUGMENTS clause");
        DefvalClause => ("DEFVAL_CLAUSE", "DEFVAL clause");
        DefvalContent => ("DEFVAL_CONTENT", "DEFVAL content");
        ObjectsClause => ("OBJECTS_CLAUSE", "OBJECTS clause");
        NotificationsClause => ("NOTIFICATIONS_CLAUSE", "NOTIFICATIONS clause");
        RevisionClause => ("REVISION_CLAUSE", "REVISION clause");
        LastUpdatedClause => ("LAST_UPDATED_CLAUSE", "LAST-UPDATED clause");
        OrganizationClause => ("ORGANIZATION_CLAUSE", "ORGANIZATION clause");
        ContactInfoClause => ("CONTACT_INFO_CLAUSE", "CONTACT-INFO clause");
        EnterpriseClause => ("ENTERPRISE_CLAUSE", "ENTERPRISE clause");
        VariablesClause => ("VARIABLES_CLAUSE", "VARIABLES clause");
        ProductReleaseClause => ("PRODUCT_RELEASE_CLAUSE", "PRODUCT-RELEASE clause");

        OidAssignment => ("OID_ASSIGNMENT", "OID assignment");
        OidComponent => ("OID_COMPONENT", "OID component");

        TypeRefSyntax => ("TYPE_REF_SYNTAX", "type reference syntax");
        IntegerEnumSyntax => ("INTEGER_ENUM_SYNTAX", "integer enumeration syntax");
        BitsSyntax => ("BITS_SYNTAX", "BITS syntax");
        ConstrainedSyntax => ("CONSTRAINED_SYNTAX", "constrained type syntax");
        Constraint => ("CONSTRAINT", "constraint");
        Range => ("RANGE", "constraint range");
        NamedNumber => ("NAMED_NUMBER", "named number");
        SequenceOfSyntax => ("SEQUENCE_OF_SYNTAX", "SEQUENCE OF syntax");
        SequenceSyntax => ("SEQUENCE_SYNTAX", "SEQUENCE syntax");
        SequenceField => ("SEQUENCE_FIELD", "SEQUENCE or CHOICE field");
        ChoiceSyntax => ("CHOICE_SYNTAX", "CHOICE syntax");
        TaggedSyntax => ("TAGGED_SYNTAX", "tagged syntax");
        OctetStringSyntax => ("OCTET_STRING_SYNTAX", "OCTET STRING syntax");
        ObjectIdentifierSyntax => ("OBJECT_IDENTIFIER_SYNTAX", "OBJECT IDENTIFIER syntax");
        Error => ("ERROR_NODE", "error node");
    }
    forbidden {
        "ABSENT", "ANY", "BIT", "BOOLEAN", "BY", "COMPONENT", "COMPONENTS",
        "DEFAULT", "DEFINED", "ENUMERATED", "EXPLICIT", "EXTERNAL", "FALSE",
        "MAX", "MIN", "MINUS-INFINITY", "NULL", "OPTIONAL", "PLUS-INFINITY",
        "PRESENT", "PRIVATE", "REAL", "SET", "TAGS", "TRUE", "WITH",
    }
}

impl fmt::Display for SyntaxKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.libsmi_name())
    }
}

#[cfg(test)]
mod tests {
    use std::collections::{HashMap, HashSet};

    use super::*;

    #[test]
    fn raw_discriminants_round_trip_exhaustively() {
        for (raw, kind) in SyntaxKind::ALL.iter().copied().enumerate() {
            assert_eq!(usize::from(kind.to_raw()), raw);
            assert_eq!(SyntaxKind::from_raw(kind.to_raw()), Some(kind));
        }
        assert_eq!(
            SyntaxKind::from_raw(u16::try_from(SyntaxKind::ALL.len()).unwrap()),
            None
        );
        assert_eq!(SyntaxKind::from_raw(u16::MAX), None);
    }

    #[test]
    fn inventory_and_categories_are_exhaustive() {
        assert_eq!(SyntaxKind::ALL.len(), 175);
        assert_eq!(
            SyntaxKind::ALL.iter().filter(|kind| kind.is_node()).count(),
            66
        );
        assert_eq!(
            SyntaxKind::ALL
                .iter()
                .filter(|kind| kind.is_token())
                .count(),
            109
        );
        assert_eq!(
            SyntaxKind::ALL
                .iter()
                .filter(|kind| kind.is_trivia())
                .count(),
            2
        );
        assert_eq!(
            SyntaxKind::ALL
                .iter()
                .filter(|kind| kind.is_identifier())
                .count(),
            2
        );
        assert_eq!(
            SyntaxKind::ALL
                .iter()
                .filter(|kind| kind.is_literal())
                .count(),
            5
        );
        assert_eq!(
            SyntaxKind::ALL
                .iter()
                .filter(|kind| kind.is_punctuation())
                .count(),
            14
        );
        assert_eq!(
            SyntaxKind::ALL
                .iter()
                .filter(|kind| kind.is_keyword())
                .count(),
            82
        );
        assert_eq!(SyntaxKind::SourceFile.category(), SyntaxCategory::Node);
        assert_eq!(SyntaxKind::Error.category(), SyntaxCategory::Node);
        assert!(SyntaxKind::Whitespace.is_trivia());
        assert!(SyntaxKind::Comment.is_trivia());
        assert!(!SyntaxKind::OpaqueText.is_trivia());
    }

    #[test]
    fn keyword_and_fixed_spelling_round_trips_are_exhaustive() {
        for kind in SyntaxKind::ALL.iter().copied() {
            for spelling in kind.keyword_spellings() {
                assert_eq!(SyntaxKind::from_keyword(spelling), Some(kind));
            }
            if let Some(text) = kind.fixed_text() {
                assert_eq!(SyntaxKind::from_fixed_text(text), Some(kind));
            }
        }
        assert_eq!(SyntaxKind::from_keyword("integer"), None);
        assert_eq!(SyntaxKind::from_fixed_text("Integer"), None);
    }

    #[test]
    fn single_byte_punctuation_round_trips_exhaustively() {
        for kind in SyntaxKind::ALL
            .iter()
            .copied()
            .filter(|kind| kind.is_punctuation())
        {
            let spelling = kind.fixed_text().unwrap();
            if let [byte] = spelling.as_bytes() {
                assert_eq!(SyntaxKind::from_punctuation_byte(*byte), Some(kind));
            } else {
                assert!(matches!(
                    kind,
                    SyntaxKind::DotDot | SyntaxKind::ColonColonEqual
                ));
            }
        }
    }

    #[test]
    fn declared_spellings_are_unique() {
        let mut keywords = HashMap::new();
        let mut fixed = HashMap::new();
        for kind in SyntaxKind::ALL.iter().copied() {
            for spelling in kind.keyword_spellings() {
                assert_eq!(
                    keywords.insert(*spelling, kind),
                    None,
                    "duplicate {spelling}"
                );
            }
            if let Some(spelling) = kind.fixed_text() {
                assert_eq!(fixed.insert(spelling, kind), None, "duplicate {spelling}");
            }
        }
        let forbidden: HashSet<_> = FORBIDDEN_KEYWORDS.iter().copied().collect();
        assert_eq!(forbidden.len(), FORBIDDEN_KEYWORDS.len());
        assert!(forbidden.is_disjoint(&keywords.keys().copied().collect()));
    }

    #[test]
    fn forbidden_keyword_lookup_matches_the_declared_inventory() {
        for keyword in FORBIDDEN_KEYWORDS {
            assert!(is_forbidden_keyword(keyword));
            assert_eq!(SyntaxKind::from_keyword(keyword), None);
        }
        assert!(!is_forbidden_keyword("optional"));
        assert_eq!(
            SyntaxKind::from_keyword("optional"),
            Some(SyntaxKind::KwOptional)
        );
    }

    #[test]
    fn keyword_subcategories_cover_exactly_all_keywords() {
        let counts = SyntaxKind::ALL.iter().copied().fold(
            HashMap::<KeywordCategory, usize>::new(),
            |mut counts, kind| {
                if let Some(category) = kind.keyword_category() {
                    *counts.entry(category).or_default() += 1;
                }
                counts
            },
        );
        assert_eq!(counts.values().sum::<usize>(), 82);
        assert_eq!(counts[&KeywordCategory::Structural], 12);
        assert_eq!(counts[&KeywordCategory::Clause], 31);
        assert_eq!(counts[&KeywordCategory::Macro], 10);
        assert_eq!(counts[&KeywordCategory::Type], 14);
        assert_eq!(counts[&KeywordCategory::Tag], 3);
        assert_eq!(counts[&KeywordCategory::StatusAccess], 12);
    }

    #[test]
    fn legacy_display_and_libsmi_names_are_preserved() {
        assert_eq!(SyntaxKind::EofToken.display_name(), "end of file");
        assert_eq!(SyntaxKind::LBrace.display_name(), "'{'");
        assert_eq!(SyntaxKind::KwObjectType.display_name(), "OBJECT_TYPE");
        assert_eq!(SyntaxKind::KwObjectType.libsmi_name(), "OBJECT_TYPE");
        assert_eq!(SyntaxKind::NegativeNumber.libsmi_name(), "NEGATIVENUMBER");
        assert_eq!(SyntaxKind::ColonColonEqual.fixed_text(), Some("::="));
    }
}