Skip to content

Commit

Permalink
Make errors dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnDTill committed Jul 29, 2023
1 parent e340ef8 commit 35ac8b4
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 85 deletions.
45 changes: 38 additions & 7 deletions src/forscape_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,51 @@ Typeset::Model* Error::writeErrors(const std::vector<Error>& errors, Typeset::Vi
}
#endif

Error::Error(Typeset::Selection controller, ErrorCode code) noexcept
: selection(controller), code(code) {}
Error::Error(Typeset::Selection selection, ErrorCode code) noexcept
: selection(selection), code(code), str(getMessage(code)) {
if(shouldQuote(code)) str += selection.str();
}

std::string Error::message() const{
std::string msg = getMessage(code);
if(shouldQuote(code)) msg += selection.str();
Error::Error(Typeset::Selection selection, ErrorCode code, const std::string& str)
: selection(selection), code(code), str(str) {}

return msg;
std::string Error::message() const {
return str;
}

std::string Error::line() const{
std::string Error::line() const {
return std::to_string(selection.getStartLine()->id+1);
}

void ErrorStream::reset() noexcept {
error_out.clear();
errors.clear();
warnings.clear();
}

bool ErrorStream::noErrors() const noexcept {
return errors.empty();
}

void ErrorStream::fail(const Typeset::Selection& selection, const std::string& str, ErrorCode code) noexcept {
error_out += str;
errors.push_back(Error(selection, code, str));

Typeset::Model* model = selection.getModel();
model->errors.push_back(errors.back());
}

void ErrorStream::warn(WarningLevel warning_level, const Typeset::Selection& selection, const std::string& str, ErrorCode code) noexcept {
error_out += str;
Typeset::Model* model = selection.getModel();
const Error error(selection, code, str);
switch (warning_level) {
case ERROR: errors.push_back(error); model->errors.push_back(error); break;
case WARN: warnings.push_back(error); model->warnings.push_back(error); break;
default: break;
}
}

}

}
18 changes: 17 additions & 1 deletion src/forscape_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <typeset_selection.h>
#include <code_error_types.h>
#include <code_settings_constants.h>
#include <vector>

namespace Forscape {
Expand All @@ -17,9 +18,11 @@ struct Error {
Typeset::Selection selection;
ErrorCode code;
size_t flag;
std::string str; //DO THIS: eliminate nested allocation

Error() noexcept = default;
Error(Typeset::Selection controller, ErrorCode code) noexcept;
Error(Typeset::Selection selection, ErrorCode code) noexcept;
Error(Typeset::Selection selection, ErrorCode code, const std::string& str);

#ifndef FORSCAPE_TYPESET_HEADLESS
void writeTo(Typeset::Text* t, Typeset::View* caller) const;
Expand All @@ -30,6 +33,19 @@ struct Error {
std::string line() const;
};

class ErrorStream {
private:
std::string error_out;
std::vector<Error> errors;
std::vector<Error> warnings;

public:
void reset() noexcept;
bool noErrors() const noexcept;
void fail(const Typeset::Selection& selection, const std::string& str, ErrorCode code = ErrorCode::VALUE_NOT_DETERMINED) alloc_except;
void warn(WarningLevel warning_level, const Typeset::Selection& selection, const std::string& str, ErrorCode code = ErrorCode::VALUE_NOT_DETERMINED) alloc_except;
};

}

}
Expand Down
23 changes: 14 additions & 9 deletions src/forscape_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ Program* Program::instance() noexcept {
return singleton_instance;
}

bool Program::noErrors() const noexcept {
return error_stream.noErrors();
}

void Program::clear() noexcept {
source_files.clear();
pending_project_browser_updates.clear();
errors.clear();
warnings.clear();
settings.reset();
error_stream.reset();
}

void Program::setProgramEntryPoint(std::filesystem::path path, Typeset::Model* model) {
Expand Down Expand Up @@ -128,8 +132,7 @@ void Program::runStaticPass() {
if(running) return;
running = true;

errors.clear();
warnings.clear();
error_stream.reset();
settings.reset(); //EVENTUALLY: this should be an assert rather than an action
program_entry_point->performSemanticFormatting();
static_pass.resolve(program_entry_point);
Expand Down Expand Up @@ -203,7 +206,7 @@ Program::ptr_or_code Program::openFromRelativePathAutoExtension(std::filesystem:
}

std::string Program::run(){
assert(errors.empty());
assert(error_stream.noErrors());

interpreter.run(
parse_tree,
Expand Down Expand Up @@ -232,16 +235,18 @@ std::string Program::run(){
}

if(interpreter.error_code != Code::ErrorCode::NO_ERROR_FOUND){
Code::Error error(program_entry_point->parser.parse_tree.getSelection(interpreter.error_node), interpreter.error_code);
str += "\nLine " + error.line() + " - " + error.message();
errors.push_back(error);
const Typeset::Selection& sel = program_entry_point->parser.parse_tree.getSelection(interpreter.error_node);
const std::string& msg = getMessage(interpreter.error_code);

str += "\nLine " + sel.getStartLineAsString() + " - " + msg;
error_stream.fail(sel, msg, interpreter.error_code);
}

return str;
}

void Program::runThread(){
assert(errors.empty());
assert(error_stream.noErrors());
interpreter.runThread(
parse_tree,
static_pass.instantiation_lookup,
Expand Down
8 changes: 4 additions & 4 deletions src/forscape_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Forscape {
class Program {
public:
static Program* instance() noexcept;
bool noErrors() const noexcept;
void clear() noexcept;
void setProgramEntryPoint(std::filesystem::path path, Typeset::Model* model);
typedef size_t ptr_or_code;
Expand All @@ -35,16 +36,15 @@ class Program {
void stop();

Code::Settings settings;
Code::ErrorStream error_stream;
Code::ParseTree parse_tree;
Code::StaticPass static_pass = Code::StaticPass(parse_tree, errors, warnings);
Code::StaticPass static_pass = Code::StaticPass(parse_tree, error_stream);
Code::Interpreter interpreter;

FORSCAPE_UNORDERED_MAP<std::filesystem::path, Typeset::Model*> source_files; //May contain multiple entries per model
std::vector<Code::Error> errors;
std::vector<Code::Error> warnings;
Typeset::Model* program_entry_point = nullptr;

//DESIGN QUAGMIRE (ERRORS): define differences between model errors/warnings and program errors/warnings
//DO THIS (ERRORS): define differences between model errors/warnings and program errors/warnings
//DESIGN QUAGMIRE (AST): define difference between model parse_tree and program parse_tree

private:
Expand Down
Loading

0 comments on commit 35ac8b4

Please sign in to comment.