lrlex

Function ct_token_map

source
pub fn ct_token_map<StorageT: Display>(
    mod_name: &str,
    token_map: &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_. 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;
}

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;
}