rsmmr / hilti

**NOTE**: This is outdated and no longer maintained. There's a new version at https://github.com/zeek/spicy.
Other
40 stars 22 forks source link

Add syntax to test for a range in a switch statement #11

Open blipp opened 9 years ago

blipp commented 9 years ago

Extending the case presented in #10, it would be nice to have the possibility to test for a range in a switch statement.

In my case I have a uint16 with a meaning similar to an EtherType or an IP protocol number. The difference is that there are ranges of values with a similar meaning. For example, if this uint16 has a value from 0xFEFC to 0xFEFF, I want to call one particular unit for further processing, regardless of the exact value.

type ExampleEnum = enum {
  # ...
  Foo1 = 0xFEFC,
  Foo2 = 0xFEFD,
  Foo3 = 0xFEFE,
  Foo4 = 0xFEFF,
  # ...
};

type ExampleUnit = unit() {
  frame_id: uint16 &convert=ExampleEnum($$);

  switch ( self.frame_id ) {
    ExampleEnum::Foo1..ExampleEnum::Foo2 -> : OtherUnit(len - self.offset());
    # ...
    * -> : bytes &length=(len - self.offset());
  };
}

A syntax for defining the range had to be invented, one could chose two dots .. like in the definition of ranges in bitfields. This should not be restricted to enum labels but could work with all types for which > and < are defined.