Formatting

This commit is contained in:
Eelco Dolstra 2020-10-07 16:33:19 +02:00
parent 5257a2559e
commit 27ca87c46a
3 changed files with 32 additions and 28 deletions

View file

@ -11,13 +11,13 @@ const std::string nativeSystem = SYSTEM;
BaseError & BaseError::addTrace(std::optional<ErrPos> e, hintformat hint) BaseError & BaseError::addTrace(std::optional<ErrPos> e, hintformat hint)
{ {
err.traces.push_front(Trace { .pos = e, .hint = hint}); err.traces.push_front(Trace { .pos = e, .hint = hint });
return *this; return *this;
} }
// c++ std::exception descendants must have a 'const char* what()' function. // c++ std::exception descendants must have a 'const char* what()' function.
// This stringifies the error and caches it for use by what(), or similarly by msg(). // This stringifies the error and caches it for use by what(), or similarly by msg().
const string& BaseError::calcWhat() const const string & BaseError::calcWhat() const
{ {
if (what_.has_value()) if (what_.has_value())
return *what_; return *what_;
@ -34,12 +34,12 @@ const string& BaseError::calcWhat() const
std::optional<string> ErrorInfo::programName = std::nullopt; std::optional<string> ErrorInfo::programName = std::nullopt;
std::ostream& operator<<(std::ostream &os, const hintformat &hf) std::ostream & operator<<(std::ostream & os, const hintformat & hf)
{ {
return os << hf.str(); return os << hf.str();
} }
string showErrPos(const ErrPos &errPos) string showErrPos(const ErrPos & errPos)
{ {
if (errPos.line > 0) { if (errPos.line > 0) {
if (errPos.column > 0) { if (errPos.column > 0) {
@ -53,7 +53,7 @@ string showErrPos(const ErrPos &errPos)
} }
} }
std::optional<LinesOfCode> getCodeLines(const ErrPos &errPos) std::optional<LinesOfCode> getCodeLines(const ErrPos & errPos)
{ {
if (errPos.line <= 0) if (errPos.line <= 0)
return std::nullopt; return std::nullopt;
@ -92,13 +92,13 @@ std::optional<LinesOfCode> getCodeLines(const ErrPos &errPos)
return loc; return loc;
} }
} }
catch (EndOfFile &eof) { catch (EndOfFile & eof) {
if (loc.errLineOfCode.has_value()) if (loc.errLineOfCode.has_value())
return loc; return loc;
else else
return std::nullopt; return std::nullopt;
} }
catch (std::exception &e) { catch (std::exception & e) {
printError("error reading nix file: %s\n%s", errPos.file, e.what()); printError("error reading nix file: %s\n%s", errPos.file, e.what());
return std::nullopt; return std::nullopt;
} }
@ -137,10 +137,10 @@ std::optional<LinesOfCode> getCodeLines(const ErrPos &errPos)
} }
// print lines of code to the ostream, indicating the error column. // print lines of code to the ostream, indicating the error column.
void printCodeLines(std::ostream &out, void printCodeLines(std::ostream & out,
const string &prefix, const string & prefix,
const ErrPos &errPos, const ErrPos & errPos,
const LinesOfCode &loc) const LinesOfCode & loc)
{ {
// previous line of code. // previous line of code.
if (loc.prevLineOfCode.has_value()) { if (loc.prevLineOfCode.has_value()) {
@ -186,7 +186,7 @@ void printCodeLines(std::ostream &out,
} }
} }
void printAtPos(const string &prefix, const ErrPos &pos, std::ostream &out) void printAtPos(const string & prefix, const ErrPos & pos, std::ostream & out)
{ {
if (pos) if (pos)
{ {
@ -212,7 +212,7 @@ void printAtPos(const string &prefix, const ErrPos &pos, std::ostream &out)
} }
} }
std::ostream& showErrorInfo(std::ostream &out, const ErrorInfo &einfo, bool showTrace) std::ostream & showErrorInfo(std::ostream & out, const ErrorInfo & einfo, bool showTrace)
{ {
auto errwidth = std::max<size_t>(getWindowSize().second, 20); auto errwidth = std::max<size_t>(getWindowSize().second, 20);
string prefix = ""; string prefix = "";

View file

@ -107,7 +107,7 @@ struct Trace {
struct ErrorInfo { struct ErrorInfo {
Verbosity level; Verbosity level;
string name; string name;
string description; string description; // FIXME: remove? it seems to be barely used
std::optional<hintformat> hint; std::optional<hintformat> hint;
std::optional<ErrPos> errPos; std::optional<ErrPos> errPos;
std::list<Trace> traces; std::list<Trace> traces;
@ -169,7 +169,7 @@ public:
#endif #endif
const string & msg() const { return calcWhat(); } const string & msg() const { return calcWhat(); }
const ErrorInfo & info() { calcWhat(); return err; } const ErrorInfo & info() const { calcWhat(); return err; }
template<typename... Args> template<typename... Args>
BaseError & addTrace(std::optional<ErrPos> e, const string &fs, const Args & ... args) BaseError & addTrace(std::optional<ErrPos> e, const string &fs, const Args & ... args)

View file

@ -76,11 +76,11 @@ template <class T>
struct yellowtxt struct yellowtxt
{ {
yellowtxt(const T &s) : value(s) {} yellowtxt(const T &s) : value(s) {}
const T &value; const T & value;
}; };
template <class T> template <class T>
std::ostream& operator<<(std::ostream &out, const yellowtxt<T> &y) std::ostream & operator<<(std::ostream & out, const yellowtxt<T> & y)
{ {
return out << ANSI_YELLOW << y.value << ANSI_NORMAL; return out << ANSI_YELLOW << y.value << ANSI_NORMAL;
} }
@ -88,12 +88,12 @@ std::ostream& operator<<(std::ostream &out, const yellowtxt<T> &y)
template <class T> template <class T>
struct normaltxt struct normaltxt
{ {
normaltxt(const T &s) : value(s) {} normaltxt(const T & s) : value(s) {}
const T &value; const T & value;
}; };
template <class T> template <class T>
std::ostream& operator<<(std::ostream &out, const normaltxt<T> &y) std::ostream & operator<<(std::ostream & out, const normaltxt<T> & y)
{ {
return out << ANSI_NORMAL << y.value; return out << ANSI_NORMAL << y.value;
} }
@ -101,26 +101,30 @@ std::ostream& operator<<(std::ostream &out, const normaltxt<T> &y)
class hintformat class hintformat
{ {
public: public:
hintformat(const string &format) :fmt(format) hintformat(const string & format) : fmt(format)
{ {
fmt.exceptions(boost::io::all_error_bits ^ fmt.exceptions(boost::io::all_error_bits ^
boost::io::too_many_args_bit ^ boost::io::too_many_args_bit ^
boost::io::too_few_args_bit); boost::io::too_few_args_bit);
} }
hintformat(const hintformat &hf) hintformat(const hintformat & hf)
: fmt(hf.fmt) : fmt(hf.fmt)
{} { }
hintformat(format && fmt)
: fmt(std::move(fmt))
{ }
template<class T> template<class T>
hintformat& operator%(const T &value) hintformat & operator%(const T & value)
{ {
fmt % yellowtxt(value); fmt % yellowtxt(value);
return *this; return *this;
} }
template<class T> template<class T>
hintformat& operator%(const normaltxt<T> &value) hintformat & operator%(const normaltxt<T> & value)
{ {
fmt % value.value; fmt % value.value;
return *this; return *this;
@ -135,7 +139,7 @@ private:
format fmt; format fmt;
}; };
std::ostream& operator<<(std::ostream &os, const hintformat &hf); std::ostream & operator<<(std::ostream & os, const hintformat & hf);
template<typename... Args> template<typename... Args>
inline hintformat hintfmt(const std::string & fs, const Args & ... args) inline hintformat hintfmt(const std::string & fs, const Args & ... args)