-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherror.d
More file actions
189 lines (169 loc) · 5.03 KB
/
Copy patherror.d
File metadata and controls
189 lines (169 loc) · 5.03 KB
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
// Written in the D programming language
// License: http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0
module ast.error;
import std.string, std.range, std.array, std.uni, std.algorithm.searching;
import std.conv: to;
import ast.lexer, util;
import util.io;
enum ErrorType {
error,
run_error,
warning,
note,
message,
}
private string prefix(ErrorType ty){
final switch(ty) {
case ErrorType.error: return "error: ";
case ErrorType.run_error: return "error: ";
case ErrorType.warning: return "warning: ";
case ErrorType.note: return "note: ";
case ErrorType.message: return "";
}
}
abstract class ErrorHandler{
//string source;
//string code;
int nerrors=0;
private int tabsize=8;
void report(ErrorType ty, lazy string msg, Location loc) {
if(ty <= ErrorType.run_error) {
nerrors++;
}
}
final void error(lazy string msg, Location loc) {
return report(ErrorType.error, msg, loc);
}
final void run_error(lazy string msg, Location loc) {
return report(ErrorType.run_error, msg, loc);
}
final void warning(lazy string msg, Location loc) {
return report(ErrorType.warning, msg, loc);
}
final void note(lazy string msg, Location loc) {
return report(ErrorType.note, msg, loc);
}
final void message(lazy string msg, Location loc) {
return report(ErrorType.message, msg, loc);
}
bool showsEffect(){ return true; }
int getTabsize(){ return tabsize; }
this(){
tabsize=getTabSize();
}
void finalize(){}
}
class SimpleErrorHandler: ErrorHandler{
override void report(ErrorType ty, lazy string err, Location loc){
super.report(ty, err, loc);
if(loc.line == 0){ // just here for robustness
stderr.writef("(location missing): %s%s\n", ty.prefix(), err);
return;
}
stderr.writef("%s(%d): %s%s\n", loc.source.name, loc.line, ty.prefix(), err);
}
}
enum underlineArrow = "^";
enum underlineStroke = "─";
import std.json;
class JSONErrorHandler: ErrorHandler{
alias JSONValue JS;
JS[] result=[];
File output;
bool close;
this(){
this.output = stderr;
this.close = false;
}
this(File output, bool close){
this.output = output;
this.close = close;
}
override void report(ErrorType ty, lazy string error, Location loc){
super.report(ty, error, loc);
if(loc.line == 0||!loc.rep.length){ // just here for robustness
stderr.writef("(location missing): %s%s\n", ty.prefix(), error);
return;
}
auto li = loc.info(getTabsize());
auto sourceJS = JS(li.source.name);
auto startJS = JS(["byte": li.startByte, "line": li.startLine, "column": li.startColumn]);
auto endJS = JS(["byte": li.endByte, "line": li.endLine, "column": li.endColumn]);
auto messageJS = JS(error);
auto severityJS = JS(ty.to!string());
auto diagnosticJS = JS(["source": sourceJS, "start": startJS, "end": endJS, "message": messageJS, "severity": severityJS]);
if(ty != ErrorType.note){
auto relatedInformationJS = JS((JS[]).init);
diagnosticJS["relatedInformation"] = relatedInformationJS;
result~=diagnosticJS;
} else {
result[$-1]["relatedInformation"]~=[diagnosticJS];
}
}
override void finalize(){
output.writeln(result);
if(close) {
output.close();
}
}
}
// TODO: remove code duplication
class VerboseErrorHandler: ErrorHandler{
override void report(ErrorType ty, lazy string err, Location loc){
super.report(ty, err, loc);
if(loc.line == 0||!loc.rep.length){ // just here for robustness
stderr.writef("(location missing): %s%s\n", ty.prefix(), err);
return;
}
auto li = loc.info(getTabsize());
auto line = li.source.getLineOf(loc.rep);
write(ty, li.source.name, li.startLine, li.startColumn, err);
if(line.length&&line[0]){
display(line);
auto ntabs = line.countUntil!(c => c != '\t');
highlight(li.startColumn, cast(int)(ntabs < 0 ? line.length : ntabs), loc.rep);
}
}
protected:
void write(ErrorType ty, string source, int line, int column, string error){
stderr.writeln(source, ':', line, ':', column, ": ", ty.prefix(), error);
}
void display(string line){
stderr.writeln(line);
}
void writeIndent(int column, int ntabs){
foreach(i;0..ntabs) stderr.write("\t");
foreach(i;0..column-ntabs*getTabsize()) stderr.write(" ");
}
void writeUnderline(string rep, int column){
import std.utf:byChar;
auto n = rep.byChar.countUntil('\n');
if(n >= 0) {
rep = rep[0..n];
}
stderr.write(underlineArrow);
foreach(i; 0..displayWidth(rep, getTabsize(), column) - 1) {
stderr.write(underlineStroke);
}
stderr.writeln();
}
void highlight(int column, int ntabs, string rep){
writeIndent(column, ntabs);
writeUnderline(rep, column);
}
}
import util.terminal;
class FormattingErrorHandler: VerboseErrorHandler{
protected:
override void write(ErrorType ty, string source, int line, int column, string error){
string color = BLACK;
if(ty <= ErrorType.run_error) color = RED;
stderr.writeln(BOLD, source, ':', line, ':', column, ": ", color, ty.prefix(), RESET, BOLD, error, RESET);
}
override void highlight(int column, int ntabs, string rep){
writeIndent(column, ntabs);
stderr.write(BOLD, GREEN);
writeUnderline(rep, column);
stderr.writeln(RESET);
}
}