#ifndef HAMMER_HAMMER__HXX #define HAMMER_HAMMER__HXX #include "hammer.h" #include #include #include #include using std::list; using std::string; using std::vector; using boost::variant; namespace hammer { template class ParseResult { public: typedef T result_type; protected: ParseResult() { } }; class BytesResult : public ParseResult > { public: typedef vector result_type; BytesResult(const vector res) : _bytes(res) { } const result_type result(); private: BytesResult() { } result_type _bytes; }; class UintResult : public ParseResult { public: typedef uint64_t result_type; UintResult(const uint64_t res) : _uint(res) { } const result_type result(); private: UintResult() { } result_type _uint; }; class IntResult : public ParseResult { public: typedef int64_t result_type; IntResult(const int64_t res) : _sint(res) { } const result_type result(); private: IntResult() { } result_type _sint; }; class NullResult: public ParseResult { public: NullResult() { } typedef void* result_type; const result_type result() { return NULL; } }; class SequenceResult : public ParseResult > > { public: typedef vector > result_type; SequenceResult(result_type res) : _seq(res) { } const result_type result(); private: SequenceResult() { } result_type _seq; }; /* forward declarations */ template class Many; class Many1; template class Optional; class RepeatN; template class IntRange; template class Parser { public: typedef T result_type; result_type parse(const string &input); result_type parse(const uint8_t *input, size_t length); Many many(); RepeatN many(size_t n); Optional optional(); RepeatN operator[](size_t n); HParser* parser() { return _parser; } protected: HParser* _parser; Parser() { } // Parser(const Parser &p) : _parser(p.parser()) { } // hopefully we don't need a copy constructor... }; class Token : public Parser { public: Token(string &str) : _tok(str) { _parser = h_token(reinterpret_cast(str.c_str()), str.size()); } Token(const uint8_t *str, size_t length) : _tok(reinterpret_cast(str), length) { _parser = h_token(str, length); } private: string _tok; }; class Ch : public Parser { public: friend class Parser; Ch(const uint8_t c) : _c(c) { _parser = h_ch(c); } private: uint8_t _c; }; class ChRange : public Parser { public: ChRange(const uint8_t lower, const uint8_t upper) : _lower(lower), _upper(upper) { _parser = h_ch_range(lower, upper); } private: uint8_t _lower, _upper; }; class SignedBits : public Parser { public: SignedBits(size_t len) : _len(len) { _parser = h_bits(len, true); } private: size_t _len; }; class UnsignedBits : public Parser { public: UnsignedBits(size_t len) : _len(len) { _parser = h_bits(len, false); } private: size_t _len; }; class Int64 : public Parser { public: Int64() { _parser = h_int64(); } IntRange in_range(const int64_t lower, const int64_t upper); }; class Int32 : public Parser { public: Int32() { _parser = h_int32(); } IntRange in_range(const int32_t lower, const int32_t upper); }; class Int16 : public Parser { public: Int16() { _parser = h_int16(); } IntRange in_range(const int16_t lower, const int16_t upper); }; class Int8 : public Parser { public: Int8() { _parser = h_int8(); } IntRange in_range(const int8_t lower, const int8_t upper); }; class Uint64 : public Parser { public: Uint64() { _parser = h_uint64(); } IntRange in_range(const uint64_t lower, const uint64_t upper); }; class Uint32 : public Parser { public: Uint32() { _parser = h_uint32(); } IntRange in_range(const uint32_t lower, const uint32_t upper); }; class Uint16 : public Parser { public: Uint16() { _parser = h_uint16(); } IntRange in_range(const uint16_t lower, const uint16_t upper); }; class Uint8 : public Parser { public: Uint8() { _parser = h_uint8(); } IntRange in_range(const uint8_t lower, const uint8_t upper); }; template class IntRange : public Parser { public: IntRange(Parser &p, const int64_t lower, const int64_t upper) : _p(p), _lower(lower), _upper(upper) { this->_parser = h_int_range(p.parser(), lower, upper); } private: Parser _p; int64_t _lower, _upper; }; template class Whitespace : public Parser { public: typedef typename T::result_type result_type; Whitespace(Parser &p) : _p(p) { this->_parser = h_whitespace(p.parser()); } private: Parser _p; }; template class Left : public Parser { public: typedef typename T::result_type result_type; Left(Parser &p, Parser &q) : _p(p), _q(q) { this->_parser = h_left(p.parser(), q.parser()); } private: Parser _p; Parser _q; }; template class Right : public Parser { public: typedef typename U::result_type result_type; Right(Parser &p, Parser &q) : _p(p), _q(q) { this->_parser = h_right(p.parser(), q.parser()); } private: Parser _p; Parser _q; }; template class Middle : public Parser { public: typedef typename U::result_type result_type; Middle(Parser &p, Parser &x, Parser &q) : _p(p), _x(x), _q(q) { this->_parser = h_middle(p.parser(), x.parser(), q.parser()); } private: Parser _p; Parser _x; Parser _q; }; /* what are we doing about h_action? */ class In : public Parser { public: In(string &charset) : _charset(charset) { _parser = h_in(reinterpret_cast(charset.c_str()), charset.size()); } In(const uint8_t *charset, size_t length) : _charset(reinterpret_cast(charset), length) { _parser = h_in(charset, length); } private: string _charset; }; class NotIn : public Parser { public: NotIn(string &charset) : _charset(charset) { _parser = h_not_in(reinterpret_cast(charset.c_str()), charset.size()); } NotIn(const uint8_t *charset, size_t length) : _charset(reinterpret_cast(charset), length) { _parser = h_not_in(charset, length); } private: string _charset; }; class End : public Parser { public: End() { _parser = h_end_p(); } }; class Nothing : public Parser { public: Nothing() { _parser = h_nothing_p(); } }; class Sequence : public Parser { friend class Parser; public: Sequence(list &ps) : _ps(ps) { void *parsers[ps.size()]; size_t i = 0; for (list::iterator it=ps.begin(); it != ps.end(); ++it, ++i) { parsers[i] = const_cast(it->parser()); } _parser = h_sequence__a(parsers); } // maybe also a begin and end iterator version private: list _ps; }; class Choice : public Parser { public: Choice(list > &ps) : _ps(ps) { void *parsers[ps.size()]; size_t i = 0; for (list >::iterator it=ps.begin(); it != ps.end(); ++it, ++i) { parsers[i] = const_cast(it->parser()); } _parser = h_choice__a(parsers); } private: list > _ps; }; template class ButNot : public Parser { public: typedef typename T::result_type result_type; ButNot(Parser &p, Parser &q) : _p(p), _q(q) { this->_parser = h_butnot(p.parser(), q.parser()); } private: Parser _p; Parser _q; }; template class Difference : public Parser { public: typedef typename T::result_type result_type; Difference(Parser &p, Parser &q) : _p(p), _q(q) { this->_parser = h_difference(p.parser(), q.parser()); } private: Parser _p; Parser _q; }; template class Xor : public Parser > { public: typedef variant result_type; Xor(Parser &p, Parser &q) : _p(p), _q(q) { this->_parser = h_xor(p.parser(), q.parser()); } private: Parser _p; Parser _q; }; template class Many : public Parser { public: Many(Parser &p) : _p(p) { _parser = h_many(p.parser()); } private: Parser _p; }; class Many1 : public Parser { public: Many1(Parser &p) : _p(p) { _parser = h_many1(p.parser()); } private: Parser _p; }; class RepeatN: public Parser { public: RepeatN(Parser &p, const size_t n) : _p(p), _n(n) { _parser = h_repeat_n(p.parser(), n); } private: Parser _p; size_t _n; }; template class Optional : public Parser { public: typedef typename T::result_type result_type; Optional(Parser &p) : _p(p) { this->_parser = h_optional(p.parser()); } private: Parser _p; }; class Ignore : public Parser { public: Ignore(Parser &p) : _p(p) { _parser = h_ignore(p.parser()); } private: Parser _p; }; class SepBy : public Parser { public: SepBy(Parser &p, Parser &sep) : _p(p), _sep(sep) { _parser = h_sepBy(p.parser(), sep.parser()); } private: Parser _p, _sep; }; class SepBy1 : public Parser { public: SepBy1(Parser &p, Parser &sep) : _p(p), _sep(sep) { _parser = h_sepBy1(p.parser(), sep.parser()); } private: Parser _p, _sep; }; class Epsilon : public Parser { public: Epsilon() { _parser = h_epsilon_p(); } }; template class LengthValue : public Parser { public: LengthValue(Parser &length, Parser &value) : _length(length), _value(value) { _parser = h_length_value(length.parser(), value.parser()); } private: Parser _length; Parser _value; }; /* FIXME attr_bool */ class And : public Parser { public: And(Parser &p) : _p(p) { _parser = h_and(p.parser()); } private: Parser _p; }; class Not : public Parser { public: Not(Parser &p) : _p(p) { _parser = h_not(p.parser()); } private: Parser _p; }; template class Indirect : public Parser { public: typedef typename T::result_type result_type; Indirect(Parser &p) : _p(p) { this->_parser = h_indirect(); h_bind_indirect(this->_parser, p.parser()); } private: Parser _p; }; } #endif