Listing 2: The parser class and some trivial subclass examples
// Copyright (c) 2001, Joel de Guzman and Dan Nuffer
// Permission is granted to use this code without restriction as
// long as this copyright notice appears in all source files.
template <typename DerivedT>
struct parser
{
DerivedT&
derived()
{ return *static_cast<DerivedT*>(this); }
DerivedT const&
derived() const
{ return *static_cast<DerivedT const*>(this); }
};
template <typename DerivedT>
struct char_parser : public parser<DerivedT>
{
template <typename IteratorT>
match
parse(IteratorT& first, IteratorT const& last) const
{
if (first != last)
if (bool r = this->derived().test(*first))
{
++first;
return match(1);
}
return match();
}
...
};
template <typename CharT = char>
class chlit : public char_parser<chlit<CharT> >
{
public:
...
template <typename T>
bool test(T ch_) const
{ return T(ch) == ch_; }
private:
CharT ch;
};