Qt 4.8
Public Types | Public Functions | Static Public Functions | Protected Functions | Private Types | Private Functions | Static Private Functions | Properties | List of all members
QPatternist::PatternPlatform Class Reference

Contains functionality for functions and expressions that uses regular expressions. More...

#include <qpatternplatform_p.h>

Inheritance diagram for QPatternist::PatternPlatform:
QPatternist::FunctionCall QPatternist::UnlimitedContainer QPatternist::Expression QSharedData QPatternist::CppCastingHelper< Expression > QPatternist::SourceLocationReflection QPatternist::MatchesFN QPatternist::ReplaceFN QPatternist::TokenizeFN

Public Types

enum  Flag {
  NoFlags = 0, DotAllMode = 1, MultiLineMode = 2, CaseInsensitive = 4,
  SimplifyWhitespace = 8
}
 
typedef QFlags< FlagFlags
 
- Public Types inherited from QPatternist::FunctionCall
typedef QExplicitlySharedDataPointer< FunctionCallPtr
 
- Public Types inherited from QPatternist::UnlimitedContainer
enum  CardinalityComputation { ProductOfCardinality, UnionOfCardinality }
 
- Public Types inherited from QPatternist::Expression
typedef QExplicitlySharedDataPointer< const ExpressionConstPtr
 A smart pointer wrapping const Expression instances. More...
 
enum  ID {
  IDBooleanValue = 1, IDCountFN, IDEmptyFN, IDExistsFN,
  IDExpressionSequence, IDGeneralComparison, IDIfThenClause, IDIgnorableExpression,
  IDIntegerValue, IDPositionFN, IDStringValue, IDValueComparison,
  IDRangeVariableReference, IDContextItem, IDUserFunctionCallsite, IDExpressionVariableReference,
  IDAttributeConstructor, IDUpperCaseFN, IDLowerCaseFN, IDFirstItemPredicate,
  IDEmptySequence, IDReturnOrderBy, IDLetClause, IDForClause,
  IDPath, IDNamespaceConstructor, IDArgumentReference, IDGenericPredicate,
  IDAxisStep, IDFloat, IDCombineNodes, IDUnresolvedVariableReference,
  IDCardinalityVerifier
}
 
typedef QList< Expression::PtrList
 
typedef QFlags< PropertyProperties
 
enum  Property {
  UseContextItem = 1, DisableElimination = 1 << 1, IsEvaluated = 1 << 2, DisableTypingDeduction = 1 << 3,
  EmptynessFollowsChild = 1 << 4, RewriteToEmptyOnEmpty = 1 << 5, RequiresFocus = 1 << 6, AffectsOrderOnly = 1 << 7,
  RequiresContextItem = (1 << 8) | RequiresFocus, CreatesFocusForLast = 1 << 9, LastOperandIsCollation = 1 << 10, DependsOnLocalVariable = (1 << 11) | DisableElimination,
  EvaluationCacheRedundant = (1 << 12), IsNodeConstructor = 1 << 13, RequiresCurrentItem = 1 << 14
}
 
typedef QExplicitlySharedDataPointer< ExpressionPtr
 A smart pointer wrapping mutable Expression instances. More...
 
typedef ::QAbstractXmlForwardIterator< Expression::PtrQAbstractXmlForwardIterator
 
typedef QVector< Expression::PtrVector
 

Public Functions

int captureCount () const
 
virtual Expression::Ptr compress (const StaticContext::Ptr &context)
 
const QRegExp pattern (const DynamicContext::Ptr &context) const
 
- Public Functions inherited from QPatternist::FunctionCall
virtual ExpressionVisitorResult::Ptr accept (const ExpressionVisitor::Ptr &visitor) const
 
virtual SequenceType::List expectedOperandTypes () const
 
virtual ID id () const
 
virtual Expression::Properties properties () const
 
virtual void setSignature (const FunctionSignature::Ptr &sign)
 
virtual FunctionSignature::Ptr signature () const
 
virtual SequenceType::Ptr staticType () const
 
virtual Expression::Ptr typeCheck (const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
 
- Public Functions inherited from QPatternist::UnlimitedContainer
virtual bool compressOperands (const StaticContext::Ptr &)
 
virtual Expression::List operands () const
 
virtual void setOperands (const Expression::List &list)
 
 UnlimitedContainer (const Expression::List &operands=Expression::List())
 
- Public Functions inherited from QPatternist::Expression
virtual const SourceLocationReflectionactualReflection () const
 Returns this. More...
 
virtual void announceFocusType (const ItemType::Ptr &itemType)
 
Properties deepProperties () const
 Computes the union of properties for this Expression and all its descending children. More...
 
virtual Properties dependencies () const
 
virtual QString description () const
 
virtual bool evaluateEBV (const DynamicContext::Ptr &context) const
 
virtual Item::Iterator::Ptr evaluateSequence (const DynamicContext::Ptr &context) const
 
virtual Item evaluateSingleton (const DynamicContext::Ptr &context) const
 
virtual void evaluateToSequenceReceiver (const DynamicContext::Ptr &context) const
 
virtual ItemType::Ptr expectedContextItemType () const
 
 Expression ()
 
bool has (const Property prop) const
 
bool hasDependency (const Property prop) const
 
bool is (const ID id) const
 
bool isEvaluated () const
 
virtual ItemType::Ptr newFocusType () const
 
virtual QList< QExplicitlySharedDataPointer< OptimizationPass > > optimizationPasses () const
 
virtual PatternPriority patternPriority () const
 
const Expression::Ptrrewrite (const Expression::Ptr &to, const StaticContext::Ptr &context) const
 Rewrites this Expression to to, and return to. More...
 
virtual ~Expression ()
 
- Public Functions inherited from QSharedData
 QSharedData ()
 Constructs a QSharedData object with a reference count of 0. More...
 
 QSharedData (const QSharedData &)
 Constructs a QSharedData object with reference count 0. More...
 
- Public Functions inherited from QPatternist::CppCastingHelper< Expression >
const TCastTarget * as () const
 
TCastTarget * as ()
 
- Public Functions inherited from QPatternist::SourceLocationReflection
virtual QSourceLocation sourceLocation () const
 
 SourceLocationReflection ()
 
virtual ~SourceLocationReflection ()
 

Static Public Functions

static QRegExp parsePattern (const QString &pattern, const ReportContext::Ptr &context, const SourceLocationReflection *const location)
 Parses pattern. More...
 
- Static Public Functions inherited from QPatternist::Expression
static void rewrite (Expression::Ptr &old, const Expression::Ptr &New, const StaticContext::Ptr &context)
 

Protected Functions

 PatternPlatform (const qint8 flagsPosition)
 This constructor is protected, because this class is supposed to be sub-classed. More...
 
- Protected Functions inherited from QPatternist::UnlimitedContainer
template<CardinalityComputation suppliedCard>
SequenceType::Ptr operandsUnionType () const
 
- Protected Functions inherited from QPatternist::Expression
void typeCheckOperands (const StaticContext::Ptr &context)
 
- Protected Functions inherited from QPatternist::CppCastingHelper< Expression >
 CppCastingHelper ()
 

Private Types

enum  PreCompiledPart { NoPart = 0, PatternPrecompiled = 1, FlagsPrecompiled = 2, FlagsAndPattern = PatternPrecompiled | FlagsPrecompiled }
 
typedef QFlags< PreCompiledPartPreCompiledParts
 

Private Functions

Flags parseFlags (const QString &flags, const DynamicContext::Ptr &context) const
 
QRegExp parsePattern (const QString &pattern, const ReportContext::Ptr &context) const
 Calls the public parsePattern() function and passes in this as the location. More...
 

Static Private Functions

static void applyFlags (const Flags flags, QRegExp &pattern)
 

Properties

PreCompiledParts m_compiledParts
 
Flags m_flags
 
const qint8 m_flagsPosition
 
QRegExp m_pattern
 

Additional Inherited Members

- Public Variables inherited from QSharedData
QAtomicInt ref
 
- Protected Variables inherited from QPatternist::UnlimitedContainer
Expression::List m_operands
 

Detailed Description

Contains functionality for functions and expressions that uses regular expressions.

Author
Frans Englich frans.nosp@m..eng.nosp@m.lich@.nosp@m.noki.nosp@m.a.com

Definition at line 73 of file qpatternplatform_p.h.

Typedefs

◆ Flags

Definition at line 107 of file qpatternplatform_p.h.

◆ PreCompiledParts

Definition at line 155 of file qpatternplatform_p.h.

Enumerations

◆ Flag

See also
XQuery 1.0 and XPath 2.0 Functions and Operators, 7.6.1.1 Flags
Enumerator
NoFlags 

No flags are set. Default behavior is used.

DotAllMode 

Flag s

MultiLineMode 

Flag m

CaseInsensitive 

Flag i

SimplifyWhitespace 

Flag x

Definition at line 80 of file qpatternplatform_p.h.

◆ PreCompiledPart

Enum telling whether the flags, pattern, or both have been compiled at compile time.

Enumerator
NoPart 
PatternPrecompiled 
FlagsPrecompiled 
FlagsAndPattern 

Definition at line 147 of file qpatternplatform_p.h.

Constructors and Destructors

◆ PatternPlatform()

PatternPlatform::PatternPlatform ( const qint8  flagsPosition)
protected

This constructor is protected, because this class is supposed to be sub-classed.

Parameters
flagsPositionan index position specifying the operand containing the pattern flags.

Definition at line 112 of file qpatternplatform.cpp.

Functions

◆ applyFlags()

void PatternPlatform::applyFlags ( const Flags  flags,
QRegExp pattern 
)
staticprivate

Definition at line 158 of file qpatternplatform.cpp.

Referenced by compress(), and pattern().

159 {
160  Q_ASSERT(patternP.isValid());
161  if(flags == NoFlags)
162  return;
163 
164  if(flags & CaseInsensitive)
165  {
166  patternP.setCaseSensitivity(Qt::CaseInsensitive);
167  }
168  // TODO Apply the other flags, like 'x'.
169 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823

◆ captureCount()

int QPatternist::PatternPlatform::captureCount ( ) const
inline
Returns
the number of captures, also called parenthesized sub-expressions, the pattern has.

If the pattern isn't precompiled, -1 is returned.

Definition at line 180 of file qpatternplatform_p.h.

Referenced by QPatternist::ReplaceFN::compress().

181  {
183  return m_pattern.captureCount();
184  else
185  return -1;
186  }
bool testFlag(Enum f) const
Returns true if the flag is set, otherwise false.
Definition: qglobal.h:2345
int captureCount() const
Returns the number of captures contained in the regular expression.
Definition: qregexp.cpp:4223

◆ compress()

Expression::Ptr PatternPlatform::compress ( const StaticContext::Ptr context)
virtual

compress() is the last stage performs in compiling an expression, done after the initial AST build and calling typeCheck(). compress() performs crucial simplifications, either by having drastic performance implications or that some expressions depend on it for proper behavior.

The default implementation performs a sparse conditional constant propagation. In short, a recursive process is performed in the AST which examines if the Expression's operands are constant values, and if so, performs a const fold(AST rewrite) into the result of evaluating the expression in question. This default behavior can be disabled by letting properties() return DisableElimination.

This compress() stage can be relative effective due to the design of XPath, in part because intrinsic functions are heavily used. Many Expressions override compress() and do optimizations specific to what they do. Also, many Expressions performs optimizations in their typeCheck().

Parameters
contextthe static context. Supplies compile time information, and is the channel for communicating error messages.
See also
Wikipedia, the free encyclopedia, Sparse conditional constant propagation
Wikipedia, the free encyclopedia, Intrinsic function
Wikipedia, the free encyclopedia, Compiler optimization

Reimplemented from QPatternist::Expression.

Reimplemented in QPatternist::ReplaceFN.

Definition at line 265 of file qpatternplatform.cpp.

Referenced by QPatternist::ReplaceFN::compress().

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 
296  applyFlags(m_flags, m_pattern);
297 
298  return me;
299 }
static QRegExp parsePattern(const QString &pattern, const ReportContext::Ptr &context, const SourceLocationReflection *const location)
Parses pattern.
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
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
virtual QExplicitlySharedDataPointer< DynamicContext > dynamicContext() const =0
static void applyFlags(const Flags flags, QRegExp &pattern)
Flags parseFlags(const QString &flags, const DynamicContext::Ptr &context) const
bool is(const ID id) const
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const

◆ parseFlags()

PatternPlatform::Flags PatternPlatform::parseFlags ( const QString flags,
const DynamicContext::Ptr context 
) const
private

Definition at line 213 of file qpatternplatform.cpp.

Referenced by compress(), and pattern().

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());
243  PatternFlag::Hash::const_iterator it(flagDescrs.constBegin());
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 }
The QHash::const_iterator class provides an STL-style const iterator for QHash and QMultiHash...
Definition: qhash.h:395
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
QString formatKeyword(const QString &keyword)
#define it(className, varName)
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
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
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
void error(const QString &message, const ReportContext::ErrorCode errorCode, const QSourceLocation &sourceLocation)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static PatternFlag::Hash flagDescriptions()
static const KeyPair *const end
Flags
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ parsePattern() [1/2]

QRegExp PatternPlatform::parsePattern ( const QString pattern,
const ReportContext::Ptr context,
const SourceLocationReflection *const  location 
)
static

Parses pattern.

Definition at line 177 of file qpatternplatform.cpp.

Referenced by QPatternist::XsdSchemaChecker::checkConstrainingFacets(), QPatternist::XsdTypeChecker::checkConstrainingFacetsBoolean(), QPatternist::XsdTypeChecker::checkConstrainingFacetsDateTime(), QPatternist::XsdTypeChecker::checkConstrainingFacetsDouble(), QPatternist::XsdTypeChecker::checkConstrainingFacetsDuration(), QPatternist::XsdTypeChecker::checkConstrainingFacetsList(), QPatternist::XsdTypeChecker::checkConstrainingFacetsQName(), QPatternist::XsdTypeChecker::checkConstrainingFacetsSignedInteger(), QPatternist::XsdTypeChecker::checkConstrainingFacetsString(), QPatternist::XsdTypeChecker::checkConstrainingFacetsUnion(), QPatternist::XsdTypeChecker::checkConstrainingFacetsUnsignedInteger(), compress(), parsePattern(), and pattern().

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 }
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString formatExpression(const QString &expr)

◆ parsePattern() [2/2]

QRegExp PatternPlatform::parsePattern ( const QString pattern,
const ReportContext::Ptr context 
) const
inlineprivate

Calls the public parsePattern() function and passes in this as the location.

Definition at line 171 of file qpatternplatform.cpp.

173 {
174  return parsePattern(pattern, context, this);
175 }
static QRegExp parsePattern(const QString &pattern, const ReportContext::Ptr &context, const SourceLocationReflection *const location)
Parses pattern.

◆ pattern()

const QRegExp PatternPlatform::pattern ( const DynamicContext::Ptr context) const

Retrieves the pattern supplied in the arguments, taking care of compiling it, settings its flags, and everything else required for getting it ready to use. If an error occurs, an appropriate error is raised via context.

Definition at line 118 of file qpatternplatform.cpp.

Referenced by QPatternist::TokenizeFN::evaluateSequence(), QPatternist::MatchesFN::evaluateSingleton(), and QPatternist::ReplaceFN::evaluateSingleton().

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 }
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
static QRegExp parsePattern(const QString &pattern, const ReportContext::Ptr &context, const SourceLocationReflection *const location)
Parses pattern.
bool testFlag(Enum f) const
Returns true if the flag is set, otherwise false.
Definition: qglobal.h:2345
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
bool isValid() const
Returns true if the regular expression is valid; otherwise returns false.
Definition: qregexp.cpp:3943
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
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
static void applyFlags(const Flags flags, QRegExp &pattern)
Flags parseFlags(const QString &flags, const DynamicContext::Ptr &context) const
Flags
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const

Properties

◆ m_compiledParts

PreCompiledParts QPatternist::PatternPlatform::m_compiledParts
private

The parts that have been pre-compiled at compile time.

Definition at line 174 of file qpatternplatform_p.h.

Referenced by captureCount(), compress(), and pattern().

◆ m_flags

Flags QPatternist::PatternPlatform::m_flags
private

Definition at line 175 of file qpatternplatform_p.h.

Referenced by compress(), and pattern().

◆ m_flagsPosition

const qint8 QPatternist::PatternPlatform::m_flagsPosition
private

Definition at line 177 of file qpatternplatform_p.h.

Referenced by compress(), and pattern().

◆ m_pattern

QRegExp QPatternist::PatternPlatform::m_pattern
private

Definition at line 176 of file qpatternplatform_p.h.

Referenced by captureCount(), compress(), and pattern().


The documentation for this class was generated from the following files: