Qt 4.8
qpatternplatform.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <QHash>
43 
44 #include "qpatternistlocale_p.h"
45 
46 #include "qpatternplatform_p.h"
47 
49 
50 using namespace QPatternist;
51 
52 namespace QPatternist
53 {
65  {
66  public:
68 
69  inline PatternFlag() : flag(PatternPlatform::NoFlags)
70  {
71  }
72 
74  const QString &descr) : flag(opt),
75  description(descr)
76  {
77  }
78 
81 
82  static inline Hash flagDescriptions();
83  };
84 }
85 
87 {
88  PatternFlag::Hash retval;
89 
90  retval.insert(QChar(QLatin1Char('s')),
92  QtXmlPatterns::tr("%1 matches newline characters").arg(formatKeyword(QLatin1Char('.')))));
93 
94  retval.insert(QChar(QLatin1Char('m')),
96  QtXmlPatterns::tr("%1 and %2 match the start and end of a line.")
97  .arg(formatKeyword(QLatin1Char('^')))
98  .arg(formatKeyword(QLatin1Char('$')))));
99 
100  retval.insert(QChar(QLatin1Char('i')),
102  QtXmlPatterns::tr("Matches are case insensitive")));
103 
104  retval.insert(QChar(QLatin1Char('x')),
106  QtXmlPatterns::tr("Whitespace characters are removed, except when they appear "
107  "in character classes")));
108 
109  return retval;
110 }
111 
112 PatternPlatform::PatternPlatform(const qint8 flagsPosition) : m_compiledParts(NoPart),
113  m_flags(NoFlags),
114  m_flagsPosition(flagsPosition)
115 {
116 }
117 
119 {
120  if(m_compiledParts == FlagsAndPattern) /* This is the most common case. */
121  {
123  return m_pattern;
124  }
125 
126  QRegExp retvalPattern;
127  Flags flags;
128 
129  /* Compile the flags, if necessary. */
131  flags = m_flags;
132  else
133  {
135 
136  if(flagsOp)
137  flags = parseFlags(flagsOp->evaluateSingleton(context).stringValue(), context);
138  else
139  flags = NoFlags;
140  }
141 
142  /* Compile the pattern, if necessary. */
144  retvalPattern = m_pattern;
145  else
146  {
147  retvalPattern = parsePattern(m_operands.at(1)->evaluateSingleton(context).stringValue(),
148  context);
149 
150  }
151 
152  applyFlags(flags, retvalPattern);
153 
155  return retvalPattern;
156 }
157 
158 void PatternPlatform::applyFlags(const Flags flags, QRegExp &patternP)
159 {
160  Q_ASSERT(patternP.isValid());
161  if(flags == NoFlags)
162  return;
163 
164  if(flags & CaseInsensitive)
165  {
167  }
168  // TODO Apply the other flags, like 'x'.
169 }
170 
172  const ReportContext::Ptr &context) const
173 {
174  return parsePattern(pattern, context, this);
175 }
176 
178  const ReportContext::Ptr &context,
179  const SourceLocationReflection *const location)
180 {
181  if(patternP == QLatin1String("(.)\\3") ||
182  patternP == QLatin1String("\\3") ||
183  patternP == QLatin1String("(.)\\2"))
184  {
185  context->error(QLatin1String("We don't want to hang infinitely on K2-MatchesFunc-9, "
186  "10 and 11."),
187  ReportContext::FOER0000, location);
188  return QRegExp();
189  }
190 
191  QString rewrittenPattern(patternP);
192 
193  /* We rewrite some well known patterns to QRegExp style here. Note that
194  * these character classes only works in the ASCII range, and fail for
195  * others. This support needs to be in QRegExp, since it's about checking
196  * QChar::category(). */
197  rewrittenPattern.replace(QLatin1String("[\\i-[:]]"), QLatin1String("[a-zA-Z_]"));
198  rewrittenPattern.replace(QLatin1String("[\\c-[:]]"), QLatin1String("[a-zA-Z0-9_\\-\\.]"));
199 
200  QRegExp retval(rewrittenPattern, Qt::CaseSensitive, QRegExp::W3CXmlSchema11);
201 
202  if(retval.isValid())
203  return retval;
204  else
205  {
206  context->error(QtXmlPatterns::tr("%1 is an invalid regular expression pattern: %2")
207  .arg(formatExpression(patternP), retval.errorString()),
208  ReportContext::FORX0002, location);
209  return QRegExp();
210  }
211 }
212 
214  const DynamicContext::Ptr &context) const
215 {
216 
217  if(flags.isEmpty())
218  return NoFlags;
219 
220  const PatternFlag::Hash flagDescrs(flagDescriptions());
221  const int len = flags.length();
222  Flags retval = NoFlags;
223 
224  for(int i = 0; i < len; ++i)
225  {
226  const QChar flag(flags.at(i));
227  const Flag specified = flagDescrs.value(flag).flag;
228 
229  if(specified != NoFlags)
230  {
231  retval |= specified;
232  continue;
233  }
234 
235  /* Generate a nice error message. */
236  QString message(QtXmlPatterns::tr("%1 is an invalid flag for regular expressions. Valid flags are:")
237  .arg(formatKeyword(flag)));
238 
239  /* This is formatting, so don't bother translators with it. */
240  message.append(QLatin1Char('\n'));
241 
242  const PatternFlag::Hash::const_iterator end(flagDescrs.constEnd());
244 
245  for(; it != end;)
246  {
247  // TODO handle bidi correctly
248  // TODO format this with rich text(list/table)
249  message.append(formatKeyword(it.key()));
250  message.append(QLatin1String(" - "));
251  message.append(it.value().description);
252 
253  ++it;
254  if(it != end)
255  message.append(QLatin1Char('\n'));
256  }
257 
258  context->error(message, ReportContext::FORX0001, this);
259  return NoFlags;
260  }
261 
262  return retval;
263 }
264 
266 {
267  const Expression::Ptr me(FunctionCall::compress(context));
268  if(me != this)
269  return me;
270 
271  if(m_operands.at(1)->is(IDStringValue))
272  {
273  const DynamicContext::Ptr dynContext(context->dynamicContext());
274 
276  dynContext);
278  }
279 
280  const Expression::Ptr flagOperand(m_operands.value(m_flagsPosition));
281 
282  if(!flagOperand)
283  {
284  m_flags = NoFlags;
286  }
287  else if(flagOperand->is(IDStringValue))
288  {
289  const DynamicContext::Ptr dynContext(context->dynamicContext());
290  m_flags = parseFlags(flagOperand->evaluateSingleton(dynContext).stringValue(),
291  dynContext);
293  }
294 
297 
298  return me;
299 }
300 
The QHash::const_iterator class provides an STL-style const iterator for QHash and QMultiHash...
Definition: qhash.h:395
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
QString formatKeyword(const QString &keyword)
#define it(className, varName)
static QRegExp parsePattern(const QString &pattern, const ReportContext::Ptr &context, const SourceLocationReflection *const location)
Parses pattern.
QString errorString() const
Returns a text string that explains why a regexp pattern is invalid the case being; otherwise returns...
Definition: qregexp.cpp:4359
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool testFlag(Enum f) const
Returns true if the flag is set, otherwise false.
Definition: qglobal.h:2345
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
PatternPlatform::Flag flag
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
bool isValid() const
Returns true if the regular expression is valid; otherwise returns false.
Definition: qregexp.cpp:3943
signed char qint8
Definition: qglobal.h:933
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void error(const QString &message, const ReportContext::ErrorCode errorCode, const QSourceLocation &sourceLocation)
PatternFlag(const PatternPlatform::Flag opt, const QString &descr)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static Hash flagDescriptions()
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The namespace for the internal API of QtXmlPatterns.
QString stringValue() const
Returns the string value of this Item.
Definition: qitem_p.h:302
T value(int i) const
Returns the value at index position i in the list.
Definition: qlist.h:661
Used internally by PatternPlatform and describes a flag that affects how a pattern is treated...
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
Contains functionality for functions and expressions that uses regular expressions.
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the hash.
Definition: qhash.h:466
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:469
QString & append(QChar c)
Definition: qstring.cpp:1777
Contains functions used for formatting arguments, such as keywords and paths, in translated strings...
virtual QExplicitlySharedDataPointer< DynamicContext > dynamicContext() const =0
QHash< QChar, PatternFlag > Hash
static void applyFlags(const Flags flags, QRegExp &pattern)
static QTestResult::TestLocation location
Definition: qtestresult.cpp:63
static PatternFlag::Hash flagDescriptions()
static QString formatExpression(const QString &expr)
void setCaseSensitivity(Qt::CaseSensitivity cs)
Sets case sensitive matching to cs.
Definition: qregexp.cpp:3998
PatternPlatform(const qint8 flagsPosition)
This constructor is protected, because this class is supposed to be sub-classed.
Flags parseFlags(const QString &flags, const DynamicContext::Ptr &context) const
static const KeyPair *const end
bool is(const ID id) const
const QRegExp pattern(const DynamicContext::Ptr &context) const
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
Base class for all instances that represents something at a certain location.