node text insertion
|
// insert digit names in a binary number, e.g. "01" -> "0zero1one"
#define CTTL_TRACE_EVERYTHING
#include "cttl/cttl.h"
#include "lambda/lambda.h"
using namespace cttl;
struct digit_parser {
std::vector< std::string > vect_digit_names;
lambda< char >::scalar sdigit;
digit_parser()
: vect_digit_names( 2 )
{
vect_digit_names[ 0 ] = "zero";
vect_digit_names[ 1 ] = "one";
}
std::string get_digit_name( char digit_ ) const
{
assert( digit_ == 0 || digit_ == 1 );
return vect_digit_names[ digit_ ];
}
size_t grammar( edge<>& edge_ )
{
return (
*(
first( isdigit )
+
*(
// get digit:
sdigit = scalar( edge_.first )[ -1 ] - '0',
// insert digit name:
scalar( edge_.first )
+= // += insert after, -= before
*scalar( CTTL_MEMBER_ACTION(
*this,
std::mem_fun( &digit_parser::get_digit_name ),
sdigit.top()
))
)
)
).match( edge_ );
}
};
int main(int argc, char* argv[])
{
if ( argc == 1 ) {
std::cout
<< "usage: specify binary number, for example:"
<< std::endl
<< argv[ 0 ] << "01101"
;
return 1;
}
input<> inp( argv[ 1 ] );
assert( inp.length() );
edge<> universe( new_edge< std::string >( inp ) );
digit_parser parser;
if ( parser.grammar( universe ) != std::string::npos ) {
std::cout << inp.text();
return 0;
}
std::cout << "*** parser failed ***" << std::endl;
return 1;
}
|