Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmldompathparser.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <QtQmlDom/private/qqmldomerrormessage_p.h>
6#include <QtQmlDom/private/qqmldomitem_p.h>
7
8#include <QtCore/QChar>
9#include <QtCore/QMetaEnum>
10
12
13namespace QQmlJS {
14namespace Dom {
15
16// Error group and translation context for parse errors.
18{
19 Q_DECLARE_TR_FUNCTIONS(PathParsing)
20public:
22 {
23 static ErrorGroups res = { { NewErrorGroup("PathParsing") } };
24 return res;
25 }
26};
27
28namespace {
29
30enum class ParserState {
31 Start,
32 IndexOrKey,
33 End
34};
35
36template<typename Enum>
37bool lookupEnumValue(QStringView name, Enum &out)
38{
39 QMetaEnum metaEnum = QMetaEnum::fromType<Enum>();
40 for (int j = 0; j < metaEnum.keyCount(); ++j) {
41 if (name.compare(QString::fromUtf8(metaEnum.key(j)), Qt::CaseInsensitive) == 0) {
42 out = Enum(metaEnum.value(j));
43 return true;
44 }
45 }
46 return false;
47}
48
49} // namespace
50
51Path parsePath(QStringView s, const ErrorHandler &errorHandler)
52{
53 using Component = Path::Component;
54
55 if (s.isEmpty())
56 return Path();
57 int len = 1;
58 const QChar dot = QChar::fromLatin1('.');
59 const QChar lsBrace = QChar::fromLatin1('[');
60 const QChar rsBrace = QChar::fromLatin1(']');
61 const QChar dollar = QChar::fromLatin1('$');
62 const QChar at = QChar::fromLatin1('@');
63 const QChar quote = QChar::fromLatin1('"');
64 const QChar backslash = QChar::fromLatin1('\\');
65 const QChar underscore = QChar::fromLatin1('_');
66 const QChar tilda = QChar::fromLatin1('~');
67 for (int i = 0; i < s.size(); ++i)
68 if (s.at(i) == lsBrace || s.at(i) == dot)
69 ++len;
70 QList<Component> components;
71 components.reserve(len);
72 int i = 0;
73 int i0 = 0;
74 ParserState state = ParserState::Start;
75 while (i < s.size()) {
76 // skip space
77 while (i < s.size() && s.at(i).isSpace())
78 ++i;
79 if (i >= s.size())
80 break;
81 QChar c = s.at(i++);
82 switch (state) {
83 case ParserState::Start:
84 if (c == dollar) {
85 i0 = i;
86 while (i < s.size() && s.at(i).isLetterOrNumber()) {
87 ++i;
88 }
89 QStringView name = s.mid(i0, i - i0);
90 PathRoot rootKind;
91 if (!lookupEnumValue(name, rootKind)) {
92 PathParseMessages::group().error(PathParseMessages::tr("Unknown root context '$%1'.").arg(name))
93 .handle(errorHandler);
94 return Path();
95 }
96 components.append(Component(rootKind));
97 state = ParserState::End;
98 } else if (c == at) {
99 i0 = i;
100 while (i < s.size() && s.at(i).isLetterOrNumber()) {
101 ++i;
102 }
103 QStringView name = s.mid(i0, i - i0);
104 PathCurrent currentKind;
105 if (!lookupEnumValue(name, currentKind)) {
106 PathParseMessages::group().error(PathParseMessages::tr("Unknown current context '@%1'.").arg(name))
107 .handle(errorHandler);
108 return Path();
109 }
110 components.append(Component(currentKind));
111 state = ParserState::End;
112 } else if (c.isLetter()) {
113 PathParseMessages::group().warning(
114 PathParseMessages::tr("Field expressions should start with a dot, even when at the "
115 "start of the path %1.")
116 .arg(s)).handle(errorHandler);
117 return Path();
118 } else {
119 --i;
120 state = ParserState::End;
121 }
122 break;
123 case ParserState::IndexOrKey:
124 if (c.isDigit()) {
125 i0 = i - 1;
126 while (i < s.size() && s.at(i).isDigit())
127 ++i;
128 bool ok;
129 components.append(Component(static_cast<index_type>(
130 s.mid(i0, i - i0).toString().toLongLong(&ok))));
131 if (!ok) {
132 PathParseMessages::group().warning(PathParseMessages::tr("Error extracting integer from '%1' at char %2.")
133 .arg(s.mid(i0, i - i0))
134 .arg(QString::number(i0)))
135 .handle(errorHandler);
136 }
137 } else if (c.isLetter() || c == tilda || c == underscore) {
138 i0 = i - 1;
139 while (i < s.size() && (s.at(i).isLetterOrNumber() || s.at(i) == underscore || s.at(i) == tilda))
140 ++i;
141 components.append(Component(PathEls::Key(s.mid(i0, i - i0).toString())));
142 } else if (c == quote) {
143 i0 = i;
144 QString strVal;
145 bool properEnd = false;
146 while (i < s.size()) {
147 c = s.at(i);
148 if (c == quote) {
149 properEnd = true;
150 break;
151 } else if (c == backslash) {
152 strVal.append(s.mid(i0, i - i0).toString());
153 c = s.at(++i);
154 i0 = i + 1;
155 if (c == QChar::fromLatin1('n'))
156 strVal.append(QChar::fromLatin1('\n'));
157 else if (c == QChar::fromLatin1('r'))
158 strVal.append(QChar::fromLatin1('\r'));
159 else if (c == QChar::fromLatin1('t'))
160 strVal.append(QChar::fromLatin1('\t'));
161 else
162 strVal.append(c);
163 }
164 ++i;
165 }
166 if (properEnd) {
167 strVal.append(s.mid(i0, i - i0).toString());
168 ++i;
169 } else {
170 PathParseMessages::group().error(PathParseMessages::tr("Unclosed quoted string at char %1.")
171 .arg(QString::number(i - 1))).handle(errorHandler);
172 return Path();
173 }
174 components.append(PathEls::Key(strVal));
175 } else {
176 PathParseMessages::group().error(PathParseMessages::tr("Unexpected character '%1' after square bracket at %2.")
177 .arg(c).arg(i - 1)).handle(errorHandler);
178 return Path();
179 }
180 while (i < s.size() && s.at(i).isSpace())
181 ++i;
182 if (i >= s.size() || s.at(i) != rsBrace) {
183 PathParseMessages::group().error(PathParseMessages::tr("square braces misses closing brace at char %1.")
184 .arg(QString::number(i))).handle(errorHandler);
185 return Path();
186 } else {
187 ++i;
188 }
189 state = ParserState::End;
190 break;
191 case ParserState::End:
192 if (c == dot) {
193 while (i < s.size() && s.at(i).isSpace())
194 ++i;
195 if (i == s.size()) {
196 components.append(Component());
197 state = ParserState::End;
198 } else if (s.at(i).isLetter() || s.at(i) == underscore || s.at(i) == tilda) {
199 i0 = i;
200 while (i < s.size() && (s.at(i).isLetterOrNumber() || s.at(i) == underscore || s.at(i) == tilda)) {
201 ++i;
202 }
203 const QStringView ident = s.mid(i0, i - i0);
204 if (auto fn = fieldNameFromString(ident))
205 components.append(Component(*fn));
206 else
207 components.append(Component(PathEls::Key(ident.toString())));
208 state = ParserState::End;
209 } else if (s.at(i).isDigit()) {
210 i0 = i;
211 while (i < s.size() && s.at(i).isDigit()) {
212 ++i;
213 }
214 bool ok;
215 components.append(Component(static_cast<index_type>(
216 s.mid(i0, i - i0).toString().toLongLong(&ok))));
217 if (!ok) {
218 PathParseMessages::group().warning(PathParseMessages::tr("Error extracting integer from '%1' at char %2.")
219 .arg(s.mid(i0, i - i0))
220 .arg(QString::number(i0)))
221 .handle(errorHandler);
222 return Path();
223 } else {
224 PathParseMessages::group().info(PathParseMessages::tr("Index should use square brackets and not a dot (at char %1).")
225 .arg(QString::number(i0)))
226 .handle(errorHandler);
227 }
228 state = ParserState::End;
229 } else if (s.at(i) == dot || s.at(i) == lsBrace) {
230 components.append(Component());
231 state = ParserState::End;
232 } else if (s.at(i) == at) {
233 i0 = ++i;
234 while (i < s.size() && s.at(i).isLetterOrNumber()) {
235 ++i;
236 }
237 QStringView name = s.mid(i0, i - i0);
238 PathCurrent currentKind;
239 if (!lookupEnumValue(name, currentKind)) {
240 PathParseMessages::group().error(PathParseMessages::tr("Unknown current context '@%1'.").arg(name))
241 .handle(errorHandler);
242 return Path();
243 }
244 components.append(Component(currentKind));
245 state = ParserState::End;
246 } else if (s.at(i) == dollar) {
247 i0 = ++i;
248 while (i < s.size() && s.at(i).isLetterOrNumber()) {
249 ++i;
250 }
251 QStringView name = s.mid(i0, i - i0);
252 PathRoot rootKind;
253 if (!lookupEnumValue(name, rootKind)) {
254 PathParseMessages::group().error(PathParseMessages::tr("Unknown root context '$%1'.").arg(name))
255 .handle(errorHandler);
256 return Path();
257 }
258 components.append(Component(rootKind));
259 state = ParserState::End;
260 } else {
261 c = s.at(i);
262 PathParseMessages::group().error(PathParseMessages::tr("Unexpected character '%1' after dot (at char %2).")
263 .arg(QStringView(&c, 1))
264 .arg(QString::number(i - 1)))
265 .handle(errorHandler);
266 return Path();
267 }
268 } else if (c == lsBrace) {
269 state = ParserState::IndexOrKey;
270 } else {
271 PathParseMessages::group().error(PathParseMessages::tr("Unexpected character '%1' after end of component (char %2).")
272 .arg(QStringView(&c, 1))
273 .arg(QString::number(i - 1)))
274 .handle(errorHandler);
275 return Path();
276 }
277 break;
278 }
279 }
280 switch (state) {
281 case ParserState::Start:
282 return Path();
283 case ParserState::IndexOrKey:
284 errorHandler(PathParseMessages::group().error(PathParseMessages::tr("unclosed square brace at end.")));
285 return Path();
286 case ParserState::End: {
287 Path result;
288 for (auto &c : components)
289 result = std::move(result).withComponent(std::move(c));
290 return result;
291 }
292 }
293 Q_ASSERT(false && "Unexpected state in parsePath");
294 return Path();
295}
296
297} // namespace Dom
298} // namespace QQmlJS
299
300QT_END_NAMESPACE
Path parsePath(QStringView s, const ErrorHandler &errorHandler)
Combined button and popup list for selecting options.