|
| 1 | +#include <pl/pattern_language.hpp> |
| 2 | +#include <pl/formatters.hpp> |
| 3 | +#include <wolv/io/file.hpp> |
| 4 | + |
| 5 | +#include <pl/cli/helpers/utils.hpp> |
| 6 | + |
| 7 | +#include <CLI/CLI.hpp> |
| 8 | +#include <CLI/App.hpp> |
| 9 | +#include <fmt/format.h> |
| 10 | + |
| 11 | +#include <nlohmann/json.hpp> |
| 12 | + |
| 13 | +namespace pl::cli::sub { |
| 14 | + |
| 15 | + void addLintSubcommand(CLI::App *app) { |
| 16 | + static std::fs::path patternFilePath; |
| 17 | + static std::vector<std::fs::path> includePaths; |
| 18 | + static std::vector<std::string> defines; |
| 19 | + |
| 20 | + static bool allowDangerousFunctions = false; |
| 21 | + static bool outputJson = false; |
| 22 | + |
| 23 | + auto subcommand = app->add_subcommand("lint", "Compiles the given pattern"); |
| 24 | + |
| 25 | + // Add command line arguments |
| 26 | + subcommand->add_option("-p,--pattern,PATTERN_FILE", patternFilePath, "Pattern file")->required()->check(CLI::ExistingFile); |
| 27 | + subcommand->add_option("-I,--includes", includePaths, "Include file paths")->take_all()->check(CLI::ExistingDirectory); |
| 28 | + subcommand->add_option("-D,--define", defines, "Define a preprocessor macro")->take_all(); |
| 29 | + subcommand->add_flag("-d,--dangerous", allowDangerousFunctions, "Allow dangerous functions")->default_val(false); |
| 30 | + subcommand->add_flag("-j,--json", outputJson, "Output JSON instead of text")->default_val(false); |
| 31 | + |
| 32 | + subcommand->callback([] { |
| 33 | + // Open pattern file |
| 34 | + wolv::io::File patternFile(patternFilePath, wolv::io::File::Mode::Read); |
| 35 | + if (!patternFile.isValid()) { |
| 36 | + ::fmt::print("Failed to open file '{}'\n", patternFilePath.string()); |
| 37 | + std::exit(EXIT_FAILURE); |
| 38 | + } |
| 39 | + |
| 40 | + // Create and configure Pattern Language runtime |
| 41 | + pl::PatternLanguage runtime; |
| 42 | + |
| 43 | + pl::cli::configureRuntime(runtime, includePaths, defines, allowDangerousFunctions); |
| 44 | + |
| 45 | + const auto code = patternFile.readString(); |
| 46 | + const auto source = wolv::util::toUTF8String(patternFile.getPath()); |
| 47 | + auto _ = runtime.parseString(code, source); |
| 48 | + |
| 49 | + auto compileErrors = runtime.getCompileErrors(); |
| 50 | + ::nlohmann::json errors = ::nlohmann::json::array(); |
| 51 | + if (compileErrors.size() > 0) { |
| 52 | + for (const auto &error : compileErrors) { |
| 53 | + if (outputJson) { |
| 54 | + ::nlohmann::json obj; |
| 55 | + obj["message"] = error.getMessage(); |
| 56 | + obj["description"] = error.getDescription(); |
| 57 | + |
| 58 | + ::nlohmann::json loc; |
| 59 | + const auto &location = error.getLocation(); |
| 60 | + loc["line"] = location.line; |
| 61 | + loc["column"] = location.column; |
| 62 | + loc["source"] = location.source->source; |
| 63 | + obj["location"] = loc; |
| 64 | + |
| 65 | + errors.emplace_back(obj); |
| 66 | + } else { |
| 67 | + ::fmt::print("{}\n", error.format()); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if(outputJson) { |
| 73 | + ::fmt::print("{}\n", errors.dump()); |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments