<<< Alias for std::bitset | Lambda Home | Text insert by cttl::node >>> |
Common Text Transformation Library http://cttl.sourceforge.net/
During grammar evaluation, lambda expression can modify content of input by inserting and/or replacing arbitrary fragments of text referenced by cttl::node objects and CTTL substrings. This technique realizes an inline text transformation. The types of text manipulation include read/write access to individual characters, text insertion at arbitrary positions, and substring replacement.
None of the operations impose restrictions on CTTL grammar definition, providing that a correct type of parseable substring is being used. All replacements/insertions modifying logical positions of parsed symbols are safe with cttl::edge class of the parseable substring.
Single character manipulation is safe with both cttl::edge and cttl::const_edge substring classes. (See CTTL_STATIC_ACTION closure helper section for an example of individual character conversion to uppercase.)
The following sections present a few syntax-only (that is, meaningless) programs designed to demonstrate lambda syntax for various text transformation techniques.
First example, string_2_edge_assign.cpp, updates parsed substring "target" with new content "source". The expression uses overloaded assignment operator= for the text replacement. The expression then verifies the result by debugging macro CTTL_LAMBDA_ASSERT(). Lambda expression is connected to CTTL grammar by Kleene star adaptor.
Keywords: string_2_edge_assign.cpp, edge, std::string, alias::text, CTTL_STATIC_RULE, match
// string_2_edge_assign.cpp // Program demonstrates text transformation using cttl::edge class // via overloaded assignment operator for std::string. #define CTTL_TRACE_RULES // automatically turns lambda tracing on #include "cttl/cttl.h" #include "lambda/lambda.h" using namespace cttl; template< typename SubstrT > size_t match_and_replace( SubstrT& substr_ ) { return ( *( symbol( "target" ) & *( scalar( &substr_ ) = scalar( std::string( "source" ) ) , CTTL_LAMBDA_ASSERT( alias::text( scalar( &substr_ ) ) == scalar( std::string( "source" ) ) ) ) ) ).match( substr_ ); } int main(/*int argc, char* argv[]*/) { std::string inp = "target"; typedef edge<> SubstrT; SubstrT substring( inp ); size_t result = ( CTTL_STATIC_RULE( match_and_replace< SubstrT > ) ).match( substring ); assert( result != std::string::npos ); return 0; }
A slightly modified example, string_2_edge_text.cpp program, updates parsed substring "target" with the new content "source" via cttl::alias::text() helper function:
Keywords: string_2_edge_text.cpp, substring replacement, const_edge, scalar_reference, alias::edge::text
// edge_2_edge_text.cpp // Program demonstrates substring replacement // by using alias::text() helper. #define CTTL_TRACE_RULES // automatically turns lambda tracing on #include "cttl/cttl.h" #include "lambda/lambda.h" using namespace cttl; int main(/*int argc, char* argv[]*/) { std::string inp( "XYZABC" ); const_edge<> edge_source( inp, 3, inp.length() ); edge<> edge_target( edge_source.first, edge_source.second, 0, 3 ); lambda< const_edge<> >::scalar_reference source( &edge_source ); lambda< edge<> >::scalar target( &edge_target ); ( CTTL_LAMBDA_ASSERT( alias::text( target ) == scalar( std::string( "XYZ" ) ) ), alias::edge::text( &target, edge_source ), CTTL_LAMBDA_ASSERT( alias::text( target ) == scalar( std::string( "ABC" ) ) ) ).evaluate(); assert( inp == "ABCABC" ); return 0; }
Copyright © 1997-2009 Igor Kholodov mailto:cttl@users.sourceforge.net.
Permission to copy and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.
<<< Alias for std::bitset | Lambda Home | Text insert by cttl::node >>> |