Function ct_token_map

Source
pub fn ct_token_map<StorageT: Display + ToTokens>(
    mod_name: &str,
    token_map: impl Borrow<HashMap<String, StorageT>>,
    rename_map: Option<&HashMap<&str, &str>>,
) -> Result<(), Box<dyn Error>>
Expand description

Create a Rust module named mod_name that can be imported with lrlex_mod!(mod_name). The module contains one const StorageT per token in token_map, with the token prefixed by T_. In addition, it will contain an array of all token IDs TOK_IDS.

For example with StorageT u8, mod_name x, and token_map HashMap{"ID": 0, "INT": 1} the generated module will look roughly as follows:

mod x {
  pub const T_ID: u8 = 0;
  pub const T_INT: u8 = 1;
  pub const TOK_IDS: &[u8] = &[T_ID, T_INT];
}

You can optionally remap names (for example, because the parser’s token names do not lead to valid Rust identifiers) by specifying the rename_map HashMap. For example, if token_map is HashMap{"+": 0, "ID": 1} and rename_map is HashMap{"+": "PLUS"} then the generated module will look roughly as follows:

mod x {
  pub const T_PLUS: u8 = 0;
  pub const T_ID: u8 = 1;
  pub const TOK_IDS: &[u8] = &[T_PLUS, T_ID];
}