• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KHTML

  • khtml
  • xpath
tokenizer_tester.cpp
Go to the documentation of this file.
1/*
2 * tokenizer_tester.cc - Copyright 2005 Frerich Raabe <raabe@kde.org>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25#include "expression.h"
26#include "step.h" //
27#include "path.h" //
28#include "predicate.h" // Order dependency.
29#include "parser.h" //
30
31#include <QStringList>
32#include <QtDebug>
33
34extern int xpathyylex();
35extern void initTokenizer( QString s );
36
37QString tokenAsString( yytokentype token )
38{
39 switch ( token ) {
40 case MULOP: return QString( "MULOP[%1]" ).arg( xpathyylval.num );
41 case MINUS: return "MINUS";
42 case PLUS: return "PLUS";
43 case RELOP: return QString( "RELOP[%1]" ).arg( xpathyylval.num );
44 case EQOP: return QString( "EQOP[%1]" ).arg( xpathyylval.num );
45 case AND: return "AND";
46 case OR: return "OR";
47 case AXISNAME: return QString( "AXISNAME['%1']" ).arg( Step::axisAsString( xpathyylval.axisType ) );
48 case NODETYPE: return QString( "NODETYPE['%1']" ).arg( *xpathyylval.str );
49 case PI: return "PI";
50 case FUNCTIONNAME: return QString( "FUNCTIONNAME['%1']" ).arg( *xpathyylval.str );
51 case LITERAL: return QString( "LITERAL['%1']" ).arg( *xpathyylval.str );
52 case VARIABLEREFERENCE: return QString( "VARIABLEREFERENCE['%1']" ).arg( *xpathyylval.str );
53 case NAMETEST: return QString( "NAMETEST['%1']" ).arg( *xpathyylval.str );
54 case NUMBER: return QString( "NUMBER['%1']" ).arg( *xpathyylval.str );
55 case DOTDOT: return "DOTDOT";
56 case SLASHSLASH: return "SLASHSLASH";
57 case ERROR: return "ERROR";
58 }
59
60 return QString( "'%1'" ).arg( char( token ) );
61}
62
63QString getTokenString( const QString &s )
64{
65 initTokenizer( s );
66
67 QStringList tokenStrings;
68 int token = xpathyylex();
69 while ( token != 0 ) {
70 tokenStrings << tokenAsString( ( yytokentype )token );
71 token = xpathyylex();
72 };
73
74 return tokenStrings.join( " " );
75}
76
77void check( const QString &statement, const QString &expected )
78{
79 QString result = getTokenString( statement );
80 if ( result != expected ) {
81 qDebug() << "ERROR: failed to tokenizer " << statement << " as expected";
82 qDebug() << "Expected: " << expected;
83 qDebug() << "Got : " << result;
84 exit( 1 );
85 }
86}
87
88int main()
89{
90 check( "child ::book",
91 "AXISNAME['child'] NAMETEST['book']" );
92 check( "/this/and/that",
93 "'/' NAMETEST['this'] '/' NAMETEST['and'] '/' NAMETEST['that']" );
94 check( "self::self/book[@and=\"and\" and true( \t\t) ]",
95 "AXISNAME['self'] NAMETEST['self'] '/' NAMETEST['book'] '[' '@' NAMETEST['and'] EQOP[1] LITERAL['and'] AND FUNCTIONNAME['true'] '(' ')' ']'" );
96 check( "123 . .43 31. 3131.22 2.2.2",
97 "NUMBER['123'] '.' NUMBER['.43'] NUMBER['31.'] NUMBER['3131.22'] NUMBER['2.2'] NUMBER['.2']" );
98 check( "/or/and[\"and\" and 'or\"' ]",
99 "'/' NAMETEST['or'] '/' NAMETEST['and'] '[' LITERAL['and'] AND LITERAL['or\"'] ']'" );
100 check( "self ::node ( )[true ( )]",
101 "AXISNAME['self'] NODETYPE['node'] '(' ')' '[' FUNCTIONNAME['true'] '(' ')' ']'" );
102
103 const QChar alpha( 0x03B1 );
104 check( QString( "self::" ) + alpha + alpha + alpha,
105 QString( "AXISNAME['self'] NAMETEST['" ) + alpha + alpha + alpha + "']" );
106
107 check( "this[substring-after(\"Name=Joe\", \"=\" )=\"Joe\"]",
108 "NAMETEST['this'] '[' FUNCTIONNAME['substring-after'] '(' LITERAL['Name=Joe'] ',' LITERAL['='] ')' EQOP[1] LITERAL['Joe'] ']'" );
109 check( "child::processing-instruction()",
110 "AXISNAME['child'] PI '(' ')'" );
111 check( "child::processing-instruction(\"yadda\")",
112 "AXISNAME['child'] PI '(' LITERAL['yadda'] ')'" );
113 check( "*",
114 "NAMETEST['*']" );
115 check( "comment() | text() | processing-instruction() | node()",
116 "NODETYPE['comment'] '(' ')' '|' NODETYPE['text'] '(' ')' '|' PI '(' ')' '|' NODETYPE['node'] '(' ')'" );
117 qDebug( "All OK!" );
118}
119
expression.h
LITERAL
@ LITERAL
Definition: parser.cpp:155
NAMETEST
@ NAMETEST
Definition: parser.cpp:160
NUMBER
@ NUMBER
Definition: parser.cpp:157
ERROR
@ ERROR
Definition: parser.cpp:161
NODETYPE
@ NODETYPE
Definition: parser.cpp:152
VARIABLEREFERENCE
@ VARIABLEREFERENCE
Definition: parser.cpp:156
SLASHSLASH
@ SLASHSLASH
Definition: parser.cpp:159
PI
@ PI
Definition: parser.cpp:153
RELOP
@ RELOP
Definition: parser.cpp:145
EQOP
@ EQOP
Definition: parser.cpp:144
PLUS
@ PLUS
Definition: parser.cpp:148
DOTDOT
@ DOTDOT
Definition: parser.cpp:158
FUNCTIONNAME
@ FUNCTIONNAME
Definition: parser.cpp:154
MULOP
@ MULOP
Definition: parser.cpp:146
AXISNAME
@ AXISNAME
Definition: parser.cpp:151
MINUS
@ MINUS
Definition: parser.cpp:147
parser.h
path.h
predicate.h
step.h
check
void check(const QString &statement, const QString &expected)
Definition: tokenizer_tester.cpp:77
initTokenizer
void initTokenizer(QString s)
getTokenString
QString getTokenString(const QString &s)
Definition: tokenizer_tester.cpp:63
tokenAsString
QString tokenAsString(yytokentype token)
Definition: tokenizer_tester.cpp:37
xpathyylex
int xpathyylex()
main
int main()
Definition: tokenizer_tester.cpp:88
yytokentype
yytokentype
AND
AND
OR
OR
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KHTML

Skip menu "KHTML"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal