<<< rule() function adaptor | Table Of Contents | CTTL_MEMBER_RULE() function adaptor >>> |
Common Text Transformation Library http://cttl.sourceforge.net/
Category:
Format:
CTTL_STATIC_RULE() is a debugging macro:
CTTL_STATIC_RULE( &f ) // invokes global or static member function; or a function object
where f is
and SubstrT is a typename of the CTTL substring class.
Algorithm:
Traceable function adaptor implements a delayed function call. The adapted function is invoked when lexer's algorithm reaches the function adaptor in its path of the grammar expression evaluation.
When grammar tracing mode is enabled, traceable function adaptor captures and displays additional information, including
Space sensitivity:
The space sensitivity of a traceable function adaptor is transparent; the encapsulated grammar rule controls the evaluation of the space grammar:
#include "cttl/cttl.h" using namespace cttl; size_t white_space( const_edge< policy_space<> >& substr_ ) { return ( begin( true ) ^ *entity( isspace ) ).match( substr_ ); } int main() { std::string inp = "123\t\t\tabc ???\n\n\n\nxyz"; const_edge< policy_space<> > substring( inp ); const_edge<> delimiter = substring; const_edge<> token = substring; while( ( token( entity( isgraph ) ) + delimiter( CTTL_STATIC_RULE( &white_space ) ) ).match( substring ) != std::string::npos ) { std::cout << "Matched " << '[' << token << ']' << ", followed by " << delimiter.length() << " white space character(s)" << std::endl ; } return 0; } /*Output: Matched [123], followed by 3 white space character(s) Matched [abc], followed by 1 white space character(s) Matched [???], followed by 4 white space character(s) Matched [xyz], followed by 0 white space character(s) */
Searchability:
Search grammar evaluation algorithms are disabled for traceable function adaptors. The following code does not compile:
#include "cttl/cttl.h"
using namespace cttl;
size_t rule_digit( const_edge<>& substr_ )
{
return entity( isdigit ).match( substr_ );
}
int main()
{
std::string inp = "123";
const_edge<> substring( inp );
const_edge<> token = substring;
size_t result = token( CTTL_STATIC_RULE( rule_digit ) ).find( substring );
assert( result != std::string::npos );
assert( token.length() != 0 );
return 0;
}
*** ERROR 'find' is not a member of 'cttl_impl::xtl_predicate<Static_predicateT>'
Example:
Usage of various C++ function formats is illustrated in production rule function documentation. The following sample demonstrates a simple call of the global function named rule_digit():
#define CTTL_TRACE_EVERYTHING #include "cttl/cttl.h" using namespace cttl; size_t rule_digit( const_edge<>& substr_ ) { return entity( isdigit ).match( substr_ ); } int main() { std::string inp = "123"; const_edge<> substring( inp ); const_edge<> token = substring; size_t result = token( CTTL_STATIC_RULE( rule_digit ) ).match( substring ); assert( result != std::string::npos ); assert( token.length() != 0 ); return 0; }
Trace output format:
The trace generated by CTTL_STATIC_RULE() function adaptor shows events of getting in and out of the C++ function call. A balanced pair of in/out traces includes C++ source line number where the call originated, and the result of grammar evaluation. Refer to CTTL_TRACE_RULES documentation for detailed description of the function adaptor trace output format. The above example generates the following trace:
---------------------@123?{e 0-3 :3:1 ---------------------@123--17->rule_digit@0-3 ---------------------123@| $ 3-3 isdigit ------------------------@++17<-rule_digit@0-3 '123' ---------------------@123| e 0-3 }
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.
<<< rule() function adaptor | Table Of Contents | CTTL_MEMBER_RULE() function adaptor >>> |