KHTML
tokenizer_tester.cpp
Go to the documentation of this file.
00001 /* 00002 * tokenizer_tester.cc - Copyright 2005 Frerich Raabe <raabe@kde.org> 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions 00006 * are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 00014 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00015 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00016 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00017 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00018 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00019 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00020 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00021 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00022 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00023 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00024 */ 00025 #include "expression.h" 00026 #include "step.h" // 00027 #include "path.h" // 00028 #include "predicate.h" // Order dependency. 00029 #include "parser.h" // 00030 00031 #include <QStringList> 00032 #include <QtDebug> 00033 00034 extern int xpathyylex(); 00035 extern void initTokenizer( QString s ); 00036 00037 QString tokenAsString( yytokentype token ) 00038 { 00039 switch ( token ) { 00040 case MULOP: return QString( "MULOP[%1]" ).arg( xpathyylval.num ); 00041 case MINUS: return "MINUS"; 00042 case PLUS: return "PLUS"; 00043 case RELOP: return QString( "RELOP[%1]" ).arg( xpathyylval.num ); 00044 case EQOP: return QString( "EQOP[%1]" ).arg( xpathyylval.num ); 00045 case AND: return "AND"; 00046 case OR: return "OR"; 00047 case AXISNAME: return QString( "AXISNAME['%1']" ).arg( Step::axisAsString( xpathyylval.axisType ) ); 00048 case NODETYPE: return QString( "NODETYPE['%1']" ).arg( *xpathyylval.str ); 00049 case PI: return "PI"; 00050 case FUNCTIONNAME: return QString( "FUNCTIONNAME['%1']" ).arg( *xpathyylval.str ); 00051 case LITERAL: return QString( "LITERAL['%1']" ).arg( *xpathyylval.str ); 00052 case VARIABLEREFERENCE: return QString( "VARIABLEREFERENCE['%1']" ).arg( *xpathyylval.str ); 00053 case NAMETEST: return QString( "NAMETEST['%1']" ).arg( *xpathyylval.str ); 00054 case NUMBER: return QString( "NUMBER['%1']" ).arg( *xpathyylval.str ); 00055 case DOTDOT: return "DOTDOT"; 00056 case SLASHSLASH: return "SLASHSLASH"; 00057 case ERROR: return "ERROR"; 00058 } 00059 00060 return QString( "'%1'" ).arg( char( token ) ); 00061 } 00062 00063 QString getTokenString( const QString &s ) 00064 { 00065 initTokenizer( s ); 00066 00067 QStringList tokenStrings; 00068 int token = xpathyylex(); 00069 while ( token != 0 ) { 00070 tokenStrings << tokenAsString( ( yytokentype )token ); 00071 token = xpathyylex(); 00072 }; 00073 00074 return tokenStrings.join( " " ); 00075 } 00076 00077 void check( const QString &statement, const QString &expected ) 00078 { 00079 QString result = getTokenString( statement ); 00080 if ( result != expected ) { 00081 qDebug() << "ERROR: failed to tokenizer " << statement << " as expected"; 00082 qDebug() << "Expected: " << expected; 00083 qDebug() << "Got : " << result; 00084 exit( 1 ); 00085 } 00086 } 00087 00088 int main() 00089 { 00090 check( "child ::book", 00091 "AXISNAME['child'] NAMETEST['book']" ); 00092 check( "/this/and/that", 00093 "'/' NAMETEST['this'] '/' NAMETEST['and'] '/' NAMETEST['that']" ); 00094 check( "self::self/book[@and=\"and\" and true( \t\t) ]", 00095 "AXISNAME['self'] NAMETEST['self'] '/' NAMETEST['book'] '[' '@' NAMETEST['and'] EQOP[1] LITERAL['and'] AND FUNCTIONNAME['true'] '(' ')' ']'" ); 00096 check( "123 . .43 31. 3131.22 2.2.2", 00097 "NUMBER['123'] '.' NUMBER['.43'] NUMBER['31.'] NUMBER['3131.22'] NUMBER['2.2'] NUMBER['.2']" ); 00098 check( "/or/and[\"and\" and 'or\"' ]", 00099 "'/' NAMETEST['or'] '/' NAMETEST['and'] '[' LITERAL['and'] AND LITERAL['or\"'] ']'" ); 00100 check( "self ::node ( )[true ( )]", 00101 "AXISNAME['self'] NODETYPE['node'] '(' ')' '[' FUNCTIONNAME['true'] '(' ')' ']'" ); 00102 00103 const QChar alpha( 0x03B1 ); 00104 check( QString( "self::" ) + alpha + alpha + alpha, 00105 QString( "AXISNAME['self'] NAMETEST['" ) + alpha + alpha + alpha + "']" ); 00106 00107 check( "this[substring-after(\"Name=Joe\", \"=\" )=\"Joe\"]", 00108 "NAMETEST['this'] '[' FUNCTIONNAME['substring-after'] '(' LITERAL['Name=Joe'] ',' LITERAL['='] ')' EQOP[1] LITERAL['Joe'] ']'" ); 00109 check( "child::processing-instruction()", 00110 "AXISNAME['child'] PI '(' ')'" ); 00111 check( "child::processing-instruction(\"yadda\")", 00112 "AXISNAME['child'] PI '(' LITERAL['yadda'] ')'" ); 00113 check( "*", 00114 "NAMETEST['*']" ); 00115 check( "comment() | text() | processing-instruction() | node()", 00116 "NODETYPE['comment'] '(' ')' '|' NODETYPE['text'] '(' ')' '|' PI '(' ')' '|' NODETYPE['node'] '(' ')'" ); 00117 qDebug( "All OK!" ); 00118 } 00119
KDE 4.6 API Reference