KHtml

tokenizer_tester.cpp
1 /*
2  * tokenizer_tester.cc - Copyright 2005 Frerich Raabe <[email protected]>
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 
34 extern int xpathyylex();
35 extern void initTokenizer(QString s);
36 
37 QString 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 
63 QString 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 
77 void check(const QString &statement, const QString &expected)
78 {
79  QString result = getTokenString(statement);
80  if (result != expected) {
81  qCDebug(KHTML_LOG) << "ERROR: failed to tokenizer " << statement << " as expected";
82  qCDebug(KHTML_LOG) << "Expected: " << expected;
83  qCDebug(KHTML_LOG) << "Got : " << result;
84  exit(1);
85  }
86 }
87 
88 int 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 
QString join(const QString &separator) const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 16 2021 22:48:02 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.