* @brief Compile-only check for a frame parser; surfaces syntax errors without running. */
| 6005 | * @brief Compile-only check for a frame parser; surfaces syntax errors without running. |
| 6006 | */ |
| 6007 | API::CommandResponse API::Handlers::ProjectHandler::frameParserDryCompile(const QString& id, |
| 6008 | const QJsonObject& params) |
| 6009 | { |
| 6010 | if (!params.contains(QStringLiteral("code"))) |
| 6011 | return CommandResponse::makeError( |
| 6012 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: code")); |
| 6013 | |
| 6014 | if (!params.contains(QStringLiteral("language"))) |
| 6015 | return CommandResponse::makeError( |
| 6016 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: language")); |
| 6017 | |
| 6018 | const auto code = params.value(QStringLiteral("code")).toString(); |
| 6019 | const auto language = params.value(QStringLiteral("language")).toInt(); |
| 6020 | |
| 6021 | if (language != SerialStudio::JavaScript && language != SerialStudio::Lua |
| 6022 | && language != SerialStudio::Native) |
| 6023 | return CommandResponse::makeError( |
| 6024 | id, |
| 6025 | ErrorCode::InvalidParam, |
| 6026 | QStringLiteral("Invalid language: must be 0 (JavaScript), 1 (Lua) or 2 (Built-In)")); |
| 6027 | |
| 6028 | auto engine = makeScriptEngine(language); |
| 6029 | const bool ok = engine->loadScript(code, 0, false); |
| 6030 | |
| 6031 | QJsonObject result; |
| 6032 | result[QStringLiteral("ok")] = ok; |
| 6033 | if (language == SerialStudio::Native) |
| 6034 | result[QStringLiteral("language")] = QStringLiteral("native"); |
| 6035 | else |
| 6036 | result[QStringLiteral("language")] = |
| 6037 | (language == SerialStudio::Lua ? QStringLiteral("lua") : QStringLiteral("javascript")); |
| 6038 | |
| 6039 | if (!ok) { |
| 6040 | if (language == SerialStudio::Native) |
| 6041 | result[QStringLiteral("error")] = QStringLiteral( |
| 6042 | "Invalid native parser descriptor: expected {\"template\": id, \"params\": {...}} " |
| 6043 | "with a known template id and valid params."); |
| 6044 | else |
| 6045 | result[QStringLiteral("error")] = |
| 6046 | QStringLiteral("Compile failed or parse(frame) is not defined."); |
| 6047 | |
| 6048 | if (language != SerialStudio::Native) { |
| 6049 | const auto warning = detectLanguageMismatch(code, language); |
| 6050 | if (!warning.isEmpty()) |
| 6051 | result[QStringLiteral("warning")] = warning; |
| 6052 | } |
| 6053 | } |
| 6054 | |
| 6055 | return CommandResponse::makeSuccess(id, result); |
| 6056 | } |
| 6057 | |
| 6058 | /** |
| 6059 | * @brief Transform dry-run: compile + apply transform() to a list of values. |
nothing calls this directly
no test coverage detected