lix/src/error-demo/error-demo.cc

63 lines
2 KiB
C++
Raw Normal View History

2020-04-03 20:55:26 +00:00
#include "error.hh"
#include "nixexpr.hh"
2020-03-23 21:29:49 +00:00
#include <iostream>
2020-04-02 22:02:40 +00:00
#include <optional>
2020-03-23 21:29:49 +00:00
2020-04-02 22:02:40 +00:00
int main()
2020-03-27 16:03:02 +00:00
{
2020-04-02 22:02:40 +00:00
using namespace nix;
2020-03-23 21:29:49 +00:00
// In each program where errors occur, this has to be set.
2020-04-03 20:55:26 +00:00
ErrorInfo::programName = std::optional("error-demo");
2020-03-25 16:52:03 +00:00
2020-04-07 02:14:48 +00:00
// There are currently four constructor functions:
2020-04-02 22:02:40 +00:00
//
// ProgramError, ProgramWarning, NixLangError, NixLangWarning.
2020-04-02 22:02:40 +00:00
//
2020-03-31 18:42:41 +00:00
// ProgramError takes name, description, and an optional hint.
2020-04-07 01:43:22 +00:00
printErrorInfo(
ErrorInfo::ProgramError("name",
"error description",
std::nullopt));
2020-03-31 18:42:41 +00:00
// ProgramWarning takes name, description, and an optional hint.
2020-04-02 22:02:40 +00:00
// The hint is in the form of a hintfmt class, which wraps boost::format(),
// and makes all the substituted text yellow.
2020-04-07 01:43:22 +00:00
printErrorInfo(
ErrorInfo::ProgramWarning("name",
"warning description",
2020-04-07 02:14:48 +00:00
std::optional(
hintfmt("there was a %1%", "warning"))));
2020-04-01 00:29:41 +00:00
2020-04-02 22:02:40 +00:00
// NixLangWarning adds nix file, line number, column range, and the lines of
// code where a warning occurred.
SymbolTable testTable;
auto problem_symbol = testTable.create("problem");
2020-04-07 02:14:48 +00:00
printErrorInfo(
ErrorInfo::NixLangWarning(
"warning name",
"warning description",
Pos(problem_symbol, 40, 13),
std::nullopt,
"this is the problem line of code",
std::nullopt,
hintfmt("this hint has %1% templated %2%!!", "yellow", "values")));
// NixLangError is just the same as NixLangWarning, except for the Error
// flag.
printErrorInfo(
ErrorInfo::NixLangError(
"error name",
"error description",
Pos(problem_symbol, 40, 13),
std::optional("previous line of code"),
"this is the problem line of code",
std::optional("next line of code"),
hintfmt("this hint has %1% templated %2%!!", "yellow", "values")));
2020-03-24 15:18:23 +00:00
return 0;
2020-03-23 21:29:49 +00:00
}