branch

Macro branch 

Source
macro_rules! branch {
    ($( $($($a1:literal)?$($a2:ident)? $(-$($b1:literal)?$($b2:ident)?)?),+ => $value:expr ),*) => { ... };
    ($( $([$($($a1:literal)?$($a2:ident)? $(-$($b1:literal)?$($b2:ident)?)?),+])? $(~[$($($c1:literal)?$($c2:ident)? $(-$($d1:literal)?$($d2:ident)?)?),+])? => $value:expr ),*) => { ... };
}
Expand description

Generates the key-value pairs corresponding to the Segments => int arguments, which can be used to add values to BTreeMap<Segments, StateId> state transitions.

All segments must be with square brackets or without them, but it’s not allowed to mix both formats in the same macro. Negation (~) can only be used with square brackets, and is placed in front of the opening bracket.

Segments are made up of any number of single character or codepoint literals, or inclusive ranges of character / codepoint literals.

A few identifiers can also be used:

  • DOT matches all UTF-8 characters
  • MIN = 0
  • LOW_MAX = 0xd7ff
  • GAP_MIN = 0xd800 (GAP_MIN - GAP_MAX are forbidden UTF-8 codepoint values)
  • GAP_MAX = 0xdfff
  • HIGH_MIN = 0xe000
  • MAX = 0x10ffff

Integer values are UTF-8 codepoint values, not the 1-4 byte representation.

§Example

let transitions = btreemap![
    0 => branch!['a'-'c' => 0],
    1 => branch!['a'-'c', '0'-'2' => 0],
    2 => branch!['a'-'c', '.' => 0],
    3 => branch!['a'-'c', '.' => 0, 'd'-'f' => 1],
    4 => branch![['a'-'c', '.'] => 0, ['d'-'f'] => 1],
    5 => branch![['a'-'c', '.'] => 0, ~['a'-'c', '.'] => 1],
    6 => branch![0 - LOW_MAX, HIGH_MIN - MAX => 0],
    7 => branch!['a' => 0, DOT => 1],
];
assert_eq!(transitions,
    btreemap![
        0 => btreemap![Segments::from([Seg('a' as u32, 'c' as u32)]) => 0],
        1 => btreemap![Segments::from([Seg('a' as u32, 'c' as u32), Seg('0' as u32, '2' as u32)]) => 0],
        2 => btreemap![Segments::from([Seg('a' as u32, 'c' as u32), Seg('.' as u32, '.' as u32)]) => 0],
        3 => btreemap![
            Segments::from([Seg('a' as u32, 'c' as u32), Seg('.' as u32, '.' as u32)]) => 0,
            Segments::from([Seg('d' as u32, 'f' as u32)]) => 1],
        4 => btreemap![
            Segments::from([Seg('a' as u32, 'c' as u32), Seg('.' as u32, '.' as u32)]) => 0,
            Segments::from([Seg('d' as u32, 'f' as u32)]) => 1],
        5 => btreemap![
            Segments::from([Seg('a' as u32, 'c' as u32), Seg('.' as u32, '.' as u32)]) => 0,
            Segments::from([Seg('a' as u32, 'c' as u32), Seg('.' as u32, '.' as u32)]).not() => 1],
        6 => btreemap![Segments::from([Seg(0_u32, 0xd7ff_u32), Seg(0xe000_u32, 0x10ffff_u32)]) => 0],
        7 => btreemap![Segments::from([Seg('a' as u32, 'a' as u32)]) => 0, Segments::dot() => 1]
    ]);