| 1 | // Copyright (C) 2024 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #include "qmlpropertyarguments.h" |
| 5 | #include "location.h" |
| 6 | |
| 7 | using namespace Qt::Literals::StringLiterals; |
| 8 | |
| 9 | QT_BEGIN_NAMESPACE |
| 10 | |
| 11 | /*! |
| 12 | \class QmlPropertyArguments |
| 13 | \brief Helper class for parsing QML property arguments. |
| 14 | */ |
| 15 | |
| 16 | /*! |
| 17 | \enum QmlPropertyArguments::ParsingOptions |
| 18 | |
| 19 | \value None |
| 20 | No options specified. |
| 21 | \value RequireQualifiedPath |
| 22 | Require the path segment to be qualified with a |
| 23 | QML type name. |
| 24 | \value IgnoreType Don’t require a type segment when parsing. |
| 25 | |
| 26 | \sa parse() |
| 27 | */ |
| 28 | |
| 29 | /*! |
| 30 | Parses a QML property from the input string \a str, with parsing options |
| 31 | \a opts. \a str contains the argument string passed to e.g. the |
| 32 | \\qmlproperty command. |
| 33 | |
| 34 | A valid QML property has the following syntax: |
| 35 | |
| 36 | \badcode |
| 37 | <type> <QML-type>::<name> |
| 38 | <type> <QML-module>::<QML-type>::<name> |
| 39 | \endcode |
| 40 | |
| 41 | In addition, if parsing option RequireQualifiedPath is \b not set, the |
| 42 | following is also accepted: |
| 43 | |
| 44 | \badcode |
| 45 | <type> <name> |
| 46 | \endcode |
| 47 | |
| 48 | \e {<type>} can be omitted if \c IgnoreType option is set. |
| 49 | |
| 50 | This syntax is accepted in QmlDocVisitor where the associated QML type |
| 51 | is already known. |
| 52 | |
| 53 | Returns a populated QmlPropertyArguments container with the property type |
| 54 | (m_type), whether the type is a list type (m_isList), property name |
| 55 | (m_name), and optionally, the parent QML type name (m_qmltype) and QML |
| 56 | module name (m_module) if those were present in the argument string. |
| 57 | |
| 58 | If the argument string is incorrect, outputs a warning using \a loc and |
| 59 | returns \c nullopt. |
| 60 | */ |
| 61 | std::optional<QmlPropertyArguments> |
| 62 | QmlPropertyArguments::parse(const QString &str, const Location &loc, ParsingOptions opts) |
| 63 | { |
| 64 | Q_ASSERT(!str.isEmpty()); |
| 65 | |
| 66 | auto input{str.trimmed()}; |
| 67 | QmlPropertyArguments args; |
| 68 | auto offset = input.indexOf(ch: ' '_L1); |
| 69 | if (offset == -1) { |
| 70 | if ((opts & ParsingOptions::IgnoreType) == ParsingOptions::None) { |
| 71 | loc.warning(message: "Missing property type for %1."_L1 .arg(args: str)); |
| 72 | return std::nullopt; |
| 73 | } |
| 74 | } else { |
| 75 | args.m_type = input.first(n: offset); |
| 76 | if ((args.m_isList = args.m_type.startsWith(s: "list<"_L1 ))) |
| 77 | args.m_type.slice(pos: 5).chop(n: 1); |
| 78 | } |
| 79 | |
| 80 | auto segments = input.slice(pos: ++offset).split(sep: "::"_L1 ); |
| 81 | if (segments.size() > 3 || (segments.size() == 1 && |
| 82 | (opts & ParsingOptions::RequireQualifiedPath) != ParsingOptions::None)) { |
| 83 | loc.warning(message: "Unrecognizable QML module/type qualifier for %1."_L1 .arg(args: str)); |
| 84 | return std::nullopt; |
| 85 | } |
| 86 | args.m_name = segments.takeLast(); |
| 87 | if (segments.size() > 0) |
| 88 | args.m_qmltype = segments.takeLast(); |
| 89 | if (segments.size() > 0) |
| 90 | args.m_module = segments[0]; |
| 91 | |
| 92 | return std::optional(args); |
| 93 | } |
| 94 | |
| 95 | QT_END_NAMESPACE |
| 96 | |