3#include <clingo/input/parser.hh>
4#include <clingo/input/print.hh>
5#include <clingo/input/program.hh>
7#include <clingo/ground/script.hh>
9#include <clingo/util/enum.hh>
10#include <clingo/util/type_traits.hh>
17namespace CppClingo::Control {
34using ProgramParams = std::pair<CppClingo::Input::Precedence, std::optional<ProgramParamVec>>;
44 : log_{&log}, store_{&store}, exec_{exec}, parser_{log, store, prg_backend, thy_backend}, cb_{std::move(cb)} {}
48 parser_.
init(str, *store_->
string(
"<string>"));
62 for (
auto const &file : files) {
76 if (!processed_stdin_) {
77 processed_stdin_ =
true;
89 auto process_path(std::string_view path) ->
bool {
return process_path_(path,
true); }
94 for (; !includes_.empty(); includes_.pop_front()) {
95 auto const &[parent, include] = includes_.front();
96 if (include.type() == Input::IncludeType::system) {
97 auto path = std::filesystem::path(include.value().c_str());
98 if (path.is_relative() && parent != root_) {
99 if (process_path_(parent / path,
false)) {
103 process_path_(path,
true);
105 if (include.value().view() ==
"incmode") {
125 void process_() { process_(root_); }
131 auto process_path_(std::filesystem::path path,
bool required) ->
bool {
132 if (std::filesystem::exists(path)) {
133 path = std::filesystem::canonical(path);
134 auto rel = path.root_name() == root_.root_name() ? path.lexically_relative(root_) : path;
135 if (!std::filesystem::is_directory(path)) {
136 if (seen_.emplace(path).second) {
138 parser_.
init(fin_, *store_->
string(rel.string()));
139 process_(path.parent_path());
160 void process_(std::filesystem::path
const &dir) {
161 bool ensure_base =
true;
163 auto [stm, res] = parser_.
scan();
164 parse_error_ = parse_error_ || !res;
170 [&]<
class T>(T
const &val) {
171 if constexpr (Util::matches<T, Input::StmInclude>) {
173 includes_.emplace_back(dir, std::move(val));
174 }
else if constexpr (Util::is_among_v<T, Input::StmScript>) {
176 if (exec_ !=
nullptr) {
177 exec_->
exec(val.loc(), *log_, val.type().view(), val.value().view());
179 }
else if constexpr (Util::matches<T, Input::StmProgram>) {
182 is_base = val.name() ==
"base" && val.args().empty();
183 }
else if constexpr (!
Util::is_among_v<T, Input::StmShowNothing, Input::StmShowSig,
184 Input::StmProjectSig, Input::StmDefined, Input::StmConst,
185 Input::StmTheory, Input::StmParts, Input::StmComment>) {
187 if (!is_base && ensure_base) {
193 cb_(*std::move(stm));
202 Ground::ScriptExec *exec_;
204 Input::Parser parser_;
206 std::filesystem::path root_ = std::filesystem::current_path();
207 std::deque<std::pair<std::filesystem::path, Input::StmInclude>> includes_;
208 Util::unordered_set<std::filesystem::path> seen_;
209 bool processed_stdin_ =
false;
210 bool parse_error_ =
false;
211 bool is_base =
false;
A helper for parsing.
Definition parse.hh:39
void check() const
Throws if there was an error during parsing.
Definition parse.hh:117
ParseHelper(Logger &log, SymbolStore &store, std::function< void(Input::Stm)> cb, Ground::ScriptExec *exec=nullptr, ProgramBackend *prg_backend=nullptr, TheoryBackend *thy_backend=nullptr)
Construct the helper.
Definition parse.hh:42
auto process_includes() -> BuiltinIncludes
Process includes encountered while parsing.
Definition parse.hh:92
void process_stdin()
Parse a program from stdin.
Definition parse.hh:75
auto process_files(std::span< std::string_view const > const &files) -> BuiltinIncludes
Parse a program from the given files.
Definition parse.hh:56
auto process_path(std::string_view path) -> bool
Parse a program from the given path.
Definition parse.hh:89
auto process_string(std::string_view str) -> BuiltinIncludes
Parse a program from the given string.
Definition parse.hh:47
Interface to execute code in source files.
Definition script.hh:12
void exec(Location const &loc, Logger &log, std::string_view name, std::string_view code)
Execute the given code for the given script.
Definition script.hh:17
Simple logger to report message to stderr or via a callback.
Definition logger.hh:63
Abstract class connecting grounder and solver.
Definition backend.hh:54
A store for symbols.
Definition symbol.hh:454
auto string(std::string_view str) -> SharedString
Construct a string.
auto string_ref(std::string_view str) -> String
Construct a string.
Abstract class connecting grounder and theory data.
Definition backend.hh:213
Exception to indicate that parsing failed.
Definition logger.hh:49
std::pair< CppClingo::Input::Precedence, std::optional< ProgramParamVec > > ProgramParams
A pair capturing a program #parts directive.
Definition parse.hh:34
BuiltinIncludes
Bitset of enabled builtin includes.
Definition parse.hh:23
@ incmode
Enable the incremental mode.
auto location(T const &x) -> Location const &
Get the location of an expression.
Definition location.hh:123
#define CLINGO_REPORT(p, id)
Report messages of the given type.
Definition logger.hh:218
#define CLINGO_REPORT_LOC(p, id, loc)
Report messages of the given type and location.
Definition logger.hh:223
@ info_file_included
Info message for undefined atoms.
std::span< String const > StringSpan
A span of strings.
Definition symbol.hh:87
std::function< void(MessageCode, std::string_view)> Logger
A callback function type for logging messages.
Definition core.hh:465
#define CLINGO_ENABLE_BITSET_ENUM(E,...)
Opt-in macro for enabling bit operations for a given enum type.
Definition enum.hh:18
constexpr bool is_among_v
Check if the type S is among the types in L.
Definition type_traits.hh:11