1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40//
41// W A R N I N G
42// -------------
43//
44// This file is not part of the Qt API. It exists purely as an
45// implementation detail. This header file may change from version to
46// version without notice, or even be removed.
47//
48// We mean it.
49
50#ifndef Patternist_PatternPlatform_H
51#define Patternist_PatternPlatform_H
52
53#include <QFlags>
54#include <QRegExp>
55
56#include <private/qfunctioncall_p.h>
57
58QT_BEGIN_NAMESPACE
59
60namespace QPatternist
61{
62 /**
63 * @short Contains functionality for functions and expressions that
64 * uses regular expressions.
65 *
66 * @ingroup Patternist_utils
67 * @author Frans Englich <frans.englich@nokia.com>
68 */
69 class PatternPlatform : public FunctionCall
70 {
71 public:
72 /**
73 * @see <a href="http://www.w3.org/TR/xpath-functions/#flags">XQuery 1.0 and
74 * XPath 2.0 Functions and Operators, 7.6.1.1 Flags</a>
75 */
76 enum Flag
77 {
78 /**
79 * No flags are set. Default behavior is used.
80 */
81 NoFlags = 0,
82
83 /**
84 * Flag @c s
85 */
86 DotAllMode = 1,
87
88 /**
89 * Flag @c m
90 */
91 MultiLineMode = 2,
92
93 /**
94 * Flag @c i
95 */
96 CaseInsensitive = 4,
97
98 /**
99 * Flag @c x
100 */
101 SimplifyWhitespace = 8
102 };
103 typedef QFlags<Flag> Flags;
104
105 virtual Expression::Ptr compress(const StaticContext::Ptr &context);
106
107 /**
108 * Retrieves the pattern supplied in the arguments, taking care of compiling it,
109 * settings its flags, and everything else required for getting it ready to use. If an error
110 * occurs, an appropriate error is raised via @p context.
111 */
112 QRegExp pattern(const DynamicContext::Ptr &context) const;
113
114 /**
115 * @returns the number of captures, also called parenthesized sub-expressions, the pattern has.
116 *
117 * If the pattern isn't precompiled, -1 is returned.
118 */
119 inline int captureCount() const;
120
121 /**
122 * @short Parses pattern
123 */
124 static QRegExp parsePattern(const QString &pattern,
125 const ReportContext::Ptr &context,
126 const SourceLocationReflection *const location);
127
128
129 protected:
130 /**
131 * @short This constructor is protected, because this class is supposed to be sub-classed.
132 *
133 * @param flagsPosition an index position specifying the operand containing the pattern
134 * flags.
135 */
136 PatternPlatform(const qint8 flagsPosition);
137
138 private:
139 /**
140 * Enum telling whether the flags, pattern, or both
141 * have been compiled at compile time.
142 */
143 enum PreCompiledPart
144 {
145 NoPart = 0,
146 PatternPrecompiled = 1,
147 FlagsPrecompiled = 2,
148 FlagsAndPattern = PatternPrecompiled | FlagsPrecompiled
149
150 };
151 typedef QFlags<PreCompiledPart> PreCompiledParts;
152
153 /**
154 * @short Calls the public parsePattern() function and passes in @c
155 * this as the location.
156 */
157 inline QRegExp parsePattern(const QString &pattern,
158 const ReportContext::Ptr &context) const;
159
160 Q_DISABLE_COPY(PatternPlatform)
161
162 Flags parseFlags(const QString &flags,
163 const DynamicContext::Ptr &context) const;
164
165 static void applyFlags(const Flags flags, QRegExp &pattern);
166
167 /**
168 * The parts that have been pre-compiled at compile time.
169 */
170 PreCompiledParts m_compiledParts;
171 Flags m_flags;
172 QRegExp m_pattern;
173 const qint8 m_flagsPosition;
174 };
175
176 inline int PatternPlatform::captureCount() const
177 {
178 if(m_compiledParts.testFlag(flag: PatternPrecompiled))
179 return m_pattern.captureCount();
180 else
181 return -1;
182 }
183
184 Q_DECLARE_OPERATORS_FOR_FLAGS(PatternPlatform::Flags)
185}
186
187QT_END_NAMESPACE
188
189#endif
190

source code of qtxmlpatterns/src/xmlpatterns/functions/qpatternplatform_p.h