Skip to content

Instantly share code, notes, and snippets.

@thequux
Created July 19, 2013 01:35
Show Gist options
  • Save thequux/6034448 to your computer and use it in GitHub Desktop.
Save thequux/6034448 to your computer and use it in GitHub Desktop.
Putative C++ bindings for hammer
// Bindings MUST have a consistent mapping from the base Hammer API to
// the binding, suitable for mechanical translation. The binding MAY
// also support additional convenience methods. (see +type+ for an
// example)
const hammer::Parser& init_parser() {
// The default constructor gives you an +h_indirect()+ parser.
static hammer::Parser ret;
hammer::Parser &domain = init_domain();
hammer::Parser &hdzero = hammer::Bits(3, false);
hammer::Parser &header =
hammer::Bits(16, false)
+ hammer::Bits(1, false)
+ // ...
+ hammer::Uint16();
// This could also be written +hammer::IntRange(hammer::Uint16(), 1, 16)+
hammer::Parser &type = hammer::Uint16().in_range(1,16);
hammer::Parser &qtype = type | hammer::Uint16().in_range(252,255);
hammer::Parser &qclass = hammer::Uint16().in_range(1,4);
// ...
hammer::Parser &len = hammer::Uint8().in_range(1,255);
// mirrors array creation; +[4]+ would also work to indicate a
// constant repetition count. +.many(len)+ would also work for this
// case, and +.many(4)+ would work for the constant repeat count
// case.
hammer::Parser &label = hammer::Uint8()[len];
hammer::Parser &qname = label.many() + '\x00';
// Could also be implemented as +hammer::BindIndirect(message)+;
ret = message;
}
// figure out ignore, optional, sepBy1
// When in doubt with operator overloads, leave them out
// ignore: .ignore(), <-x>
// h_not: !
// optional: .optional(), ~x
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment