aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/error.hh
blob: 7260dfd5bf67ea2bf8c11d7019b94d5845d08118 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#pragma once

#include "types.hh"
#include <string>
#include <optional>
#include <iostream>

using std::string;
using std::optional;

namespace nix {

enum ErrLevel 
  { elWarning
  , elError
  };

class ColumnRange { 
  public:
    unsigned int start;
    unsigned int len;
};

class ErrorInfo;

class ErrLine { 
  public:
    int lineNumber;
    optional<ColumnRange> columnRange;
    optional<string> prevLineOfCode;
    string errLineOfCode;
    optional<string> nextLineOfCode;

};

class NixCode { 
  public:
    optional<string> nixFile;
    optional<ErrLine> errLine;

    ErrLine& ensureErrLine() 
    { 
      if (!this->errLine.has_value())
         this->errLine = optional(ErrLine());
      return *this->errLine; 
    }
};

// -------------------------------------------------
// ErrorInfo.

// Forward friend class declarations.  "builder classes"
template <class T>
class AddName;

template <class T>
class AddDescription;

template <class T>
class AddNixCode;

template <class T>
class AddNixFile;

template <class T>
class AddErrLine;

template <class T> 
class AddLineNumber;

template <class T> 
class AddColumnRange;

template <class T> 
class AddLOC;

// The error info class itself.
class ErrorInfo { 
  public:
    ErrLevel level;
    string name;
    string description;
    optional<NixCode> nixCode;
    string hint;
    ErrorInfo& GetEI() { return *this; }

    static optional<string> programName;

    // give these access to the private constructor, 
    // when they are direct descendants.
    friend AddName<ErrorInfo>;
    friend AddDescription<ErrorInfo>;
    friend AddNixCode<ErrorInfo>;
    friend AddNixFile<ErrorInfo>;
    friend AddErrLine<ErrorInfo>;
    friend AddLineNumber<ErrorInfo>;
    friend AddColumnRange<ErrorInfo>;
    friend AddLOC<ErrorInfo>;
    
    NixCode& ensureNixCode() 
    { 
      if (!this->nixCode.has_value())
         this->nixCode = optional(NixCode());
      return *this->nixCode; 
    }
  protected:
    // constructor is protected, so only the builder classes can create an ErrorInfo.
    ErrorInfo(ErrLevel level) { this->level = level; }


};

// Init as error
class EIError : public ErrorInfo
{
  protected:
    EIError() : ErrorInfo(elError) {}
};

// Init as warning
class EIWarning : public ErrorInfo
{
  protected:
    EIWarning() : ErrorInfo(elWarning) {}
};

// Builder class definitions.
template <class T>
class AddName : private T
{
  public:
    T& name(const std::string &name){
      GetEI().name = name;
      return *this;
    }
  protected:
    ErrorInfo& GetEI() { return T::GetEI(); }
};

template <class T>
class AddDescription : private T 
{
  public:
    T& description(const std::string &description){
      GetEI().description = description;
      return *this;
    }
  protected:
    ErrorInfo& GetEI() { return T::GetEI(); }
};

template <class T> 
class AddNixFile : public T
{
  public:
    T& nixFile(string filename) { 
     GetEI().ensureNixCode().nixFile = filename;
     return *this;
    }
  protected:
    ErrorInfo& GetEI() { return T::GetEI(); }
};

template <class T> 
class AddLineNumber : public T
{
  public:
    T& lineNumber(int lineNumber) { 
      GetEI().ensureNixCode().ensureErrLine().lineNumber = lineNumber;
      return *this;
    }
  protected:
    ErrorInfo& GetEI() { return T::GetEI(); }
};

template <class T> 
class AddColumnRange : public T
{
  public:
    T& columnRange(unsigned int start, unsigned int len) { 
      GetEI().ensureNixCode().ensureErrLine().columnRange = { start, len };
      return *this;
    }
  protected:
    ErrorInfo& GetEI() { return T::GetEI(); }
};

template <class T> 
class AddLOC : public T
{
  public:
    T& linesOfCode(optional<string> prevloc, string loc, optional<string> nextloc) { 
      GetEI().ensureNixCode().ensureErrLine().prevLineOfCode = prevloc;
      GetEI().ensureNixCode().ensureErrLine().errLineOfCode = loc;
      GetEI().ensureNixCode().ensureErrLine().nextLineOfCode = nextloc;
      return *this;
    }
  protected:
    ErrorInfo& GetEI() { return T::GetEI(); }
};

typedef AddNixFile<AddErrLine<NixCode>> MkNixCode;

typedef AddName<AddDescription<EIError>> StandardError;
typedef AddName<AddDescription<EIWarning>> StandardWarning;

typedef AddName<
        AddDescription<
        AddNixFile<
        AddLineNumber<
        AddColumnRange<
        AddLOC<EIError>>>>>> MkNixError;
typedef AddName<
        AddDescription<
        AddNixFile<
        AddLineNumber<
        AddColumnRange<
        AddLOC<EIWarning>>>>>> MkNixWarning;

string showErrLine(ErrLine &errLine);

void print_code_lines(string &prefix, NixCode &nix_code); 

void print_error(ErrorInfo &einfo);
}