diff --git a/include/ast.hpp b/include/ast.hpp index fcddf12..0695027 100644 --- a/include/ast.hpp +++ b/include/ast.hpp @@ -218,4 +218,13 @@ namespace broma { return &*it; } }; + + /// @brief Used as a safety mechanism for parsing + // in other languages or on multiple threads. + typedef struct SafeRootResult { + void* result; + bool is_error; + } SafeRootResult; + + } // namespace broma diff --git a/include/broma.hpp b/include/broma.hpp index 510efa7..d4d6ce0 100644 --- a/include/broma.hpp +++ b/include/broma.hpp @@ -9,4 +9,10 @@ namespace broma { /// /// @param fname The path of the file you want to parse, as a string. Root parse_file(std::string const& fname); + + /// @brief Parses a broma file safely by not exiting when the parser throws an error + /// @param fname The filename or path of the file you want to parse as a string. + /// @return A root result with a boolean value to check for errors. + SafeRootResult parse_file_safely(std::string const& fname); + } diff --git a/src/broma.cpp b/src/broma.cpp index 9202da8..54e548b 100644 --- a/src/broma.cpp +++ b/src/broma.cpp @@ -36,4 +36,28 @@ namespace broma { return root; } + + SafeRootResult parse_file_safely(std::string const& fname){ + file_input<> input(fname); + + SafeRootResult result; + + Root root; + ScratchData scratch; + parse, run_action>(input, &root, &scratch); + post_process(root); + + if (scratch.errors.size()) { + std::vectorerr_data; + for (auto&e : scratch.errors){ + err_data.emplace_back(e.what()); + } + result.result = reinterpret_cast(&err_data); + result.is_error = true; + } else { + result.result = reinterpret_cast(&root); + result.is_error = false; + } + return result; + } } // namespace broma