lang_util/
token.rs

1//! Token derive support definitions
2
3/// Information about a known token
4#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
5#[cfg_attr(feature = "serde", derive(rserde::Serialize))]
6#[cfg_attr(feature = "serde", serde(crate = "rserde"))]
7pub struct TokenDescriptor {
8    /// Variant name
9    pub variant_name: &'static str,
10
11    /// Parser token name
12    pub parser_token: &'static str,
13
14    /// List of kinds this token belongs to
15    pub kinds: &'static [&'static str],
16}
17
18impl TokenDescriptor {
19    /// Create a new token descriptor
20    pub const fn new(
21        variant_name: &'static str,
22        parser_token: &'static str,
23        kinds: &'static [&'static str],
24    ) -> Self {
25        Self {
26            variant_name,
27            parser_token,
28            kinds,
29        }
30    }
31}
32
33/// Trait to implement for a token to be used with `lang_util`'s infrastructure
34pub trait Token: std::fmt::Display {
35    /// Return the variant name of the current token
36    fn variant_name(&self) -> &'static str;
37
38    /// Return the name used by the lalrpop parser for this token
39    fn parser_token(&self) -> &'static str;
40
41    /// Return the token kinds this token belongs to
42    fn kinds(&self) -> &'static [&'static str];
43
44    /// Return the descriptions for all known tokens
45    fn all_tokens() -> &'static [TokenDescriptor];
46}