53 using Component = Path::Component;
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)
70 QList<Component> components;
71 components.reserve(len);
74 ParserState state = ParserState::Start;
75 while (i < s.size()) {
77 while (i < s.size() && s.at(i).isSpace())
83 case ParserState::Start:
86 while (i < s.size() && s.at(i).isLetterOrNumber()) {
89 QStringView name = s.mid(i0, i - i0);
91 if (!lookupEnumValue(name, rootKind)) {
92 PathParseMessages::group().error(PathParseMessages::tr(
"Unknown root context '$%1'.").arg(name))
93 .handle(errorHandler);
96 components.append(Component(rootKind));
97 state = ParserState::End;
100 while (i < s.size() && s.at(i).isLetterOrNumber()) {
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);
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);
120 state = ParserState::End;
123 case ParserState::IndexOrKey:
126 while (i < s.size() && s.at(i).isDigit())
129 components.append(Component(
static_cast<index_type>(
130 s.mid(i0, i - i0).toString().toLongLong(&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);
137 }
else if (c.isLetter() || c == tilda || c == underscore) {
139 while (i < s.size() && (s.at(i).isLetterOrNumber() || s.at(i) == underscore || s.at(i) == tilda))
141 components.append(Component(PathEls::Key(s.mid(i0, i - i0).toString())));
142 }
else if (c == quote) {
145 bool properEnd =
false;
146 while (i < s.size()) {
151 }
else if (c == backslash) {
152 strVal.append(s.mid(i0, i - i0).toString());
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'));
167 strVal.append(s.mid(i0, i - i0).toString());
170 PathParseMessages::group().error(PathParseMessages::tr(
"Unclosed quoted string at char %1.")
171 .arg(QString::number(i - 1))).handle(errorHandler);
174 components.append(PathEls::Key(strVal));
176 PathParseMessages::group().error(PathParseMessages::tr(
"Unexpected character '%1' after square bracket at %2.")
177 .arg(c).arg(i - 1)).handle(errorHandler);
180 while (i < s.size() && s.at(i).isSpace())
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);
189 state = ParserState::End;
191 case ParserState::End:
193 while (i < s.size() && s.at(i).isSpace())
196 components.append(Component());
197 state = ParserState::End;
198 }
else if (s.at(i).isLetter() || s.at(i) == underscore || s.at(i) == tilda) {
200 while (i < s.size() && (s.at(i).isLetterOrNumber() || s.at(i) == underscore || s.at(i) == tilda)) {
203 const QStringView ident = s.mid(i0, i - i0);
204 if (
auto fn = fieldNameFromString(ident))
205 components.append(Component(*fn));
207 components.append(Component(PathEls::Key(ident.toString())));
208 state = ParserState::End;
209 }
else if (s.at(i).isDigit()) {
211 while (i < s.size() && s.at(i).isDigit()) {
215 components.append(Component(
static_cast<index_type>(
216 s.mid(i0, i - i0).toString().toLongLong(&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);
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);
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) {
234 while (i < s.size() && s.at(i).isLetterOrNumber()) {
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);
244 components.append(Component(currentKind));
245 state = ParserState::End;
246 }
else if (s.at(i) == dollar) {
248 while (i < s.size() && s.at(i).isLetterOrNumber()) {
251 QStringView name = s.mid(i0, i - i0);
253 if (!lookupEnumValue(name, rootKind)) {
254 PathParseMessages::group().error(PathParseMessages::tr(
"Unknown root context '$%1'.").arg(name))
255 .handle(errorHandler);
258 components.append(Component(rootKind));
259 state = ParserState::End;
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);
268 }
else if (c == lsBrace) {
269 state = ParserState::IndexOrKey;
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);
281 case ParserState::Start:
283 case ParserState::IndexOrKey:
284 errorHandler(PathParseMessages::group().error(PathParseMessages::tr(
"unclosed square brace at end.")));
286 case ParserState::End: {
288 for (
auto &c : components)
289 result = std::move(result).withComponent(std::move(c));
293 Q_ASSERT(
false &&
"Unexpected state in parsePath");