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
|
// Copyright Ferdinand Majerech 2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
///Exceptions thrown by D:YAML and _exception related code.
module dyaml.exception;
import std.algorithm;
import std.array;
import std.conv;
import std.exception;
import std.format;
import std.range;
import std.string;
import std.typecons;
/// Base class for all exceptions thrown by D:YAML.
class YAMLException : Exception
{
mixin basicExceptionCtors;
}
/// Position in a YAML stream, used for error messages.
struct Mark
{
/// File name.
string name = "<unknown>";
/// Line number.
ushort line;
/// Column number.
ushort column;
public:
/// Construct a Mark with specified line and column in the file.
this(string name, const uint line, const uint column) @safe pure nothrow @nogc
{
this.name = name;
this.line = cast(ushort)min(ushort.max, line);
// This *will* overflow on extremely wide files but saves CPU time
// (mark ctor takes ~5% of time)
this.column = cast(ushort)column;
}
/// Get a string representation of the mark.
void toString(W)(ref W writer) const scope
{
// Line/column numbers start at zero internally, make them start at 1.
void writeClamped(ushort v)
{
writer.formattedWrite!"%s"(v + 1);
if (v == ushort.max)
{
put(writer, "or higher");
}
}
put(writer, name);
put(writer, ":");
writeClamped(line);
put(writer, ",");
writeClamped(column);
}
}
/// Base class of YAML exceptions with marked positions of the problem.
abstract class MarkedYAMLException : YAMLException
{
/// Position of the error.
Mark mark;
/// Additional position information, usually the start of a token or scalar
Nullable!Mark mark2;
/// A label for the extra information
string mark2Label;
// Construct a MarkedYAMLException with two marks
this(string context, const Mark mark, string mark2Label, const Nullable!Mark mark2,
string file = __FILE__, size_t line = __LINE__) @safe pure nothrow
{
super(context, file, line);
this.mark = mark;
this.mark2 = mark2;
this.mark2Label = mark2Label;
}
// Construct a MarkedYAMLException with specified problem.
this(string msg, const Mark mark,
string file = __FILE__, size_t line = __LINE__)
@safe pure nothrow
{
super(msg, file, line);
this.mark = mark;
}
/// Custom toString to add context without requiring allocation up-front
void toString(W)(ref W sink) const
{
sink.formattedWrite!"%s@%s(%s): "(typeid(this).name, file, line);
put(sink, msg);
put(sink, "\n");
mark.toString(sink);
if (!mark2.isNull)
{
put(sink, "\n");
put(sink, mark2Label);
put(sink, ":");
mark2.get.toString(sink);
}
put(sink, "\n");
put(sink, info.toString());
}
/// Ditto
override void toString(scope void delegate(in char[]) sink) const
{
toString!(typeof(sink))(sink);
}
/// An override of message
override const(char)[] message() const @safe nothrow
{
if (mark2.isNull)
{
return assertNotThrown(text(msg, "\n", mark));
}
else
{
return assertNotThrown(text(msg, "\n", mark, "\n", mark2Label, ": ", mark2.get));
}
}
}
/// Exception thrown on composer errors.
class ComposerException : MarkedYAMLException
{
mixin MarkedExceptionCtors;
}
/// Exception thrown on constructor errors.
class ConstructorException : MarkedYAMLException
{
mixin MarkedExceptionCtors;
}
/// Exception thrown on loader errors.
class LoaderException : MarkedYAMLException
{
mixin MarkedExceptionCtors;
}
/// Exception thrown on node related errors.
class NodeException : MarkedYAMLException
{
mixin MarkedExceptionCtors;
}
/// Exception thrown on parser errors.
class ParserException : MarkedYAMLException
{
mixin MarkedExceptionCtors;
}
/// Exception thrown on Reader errors.
class ReaderException : MarkedYAMLException
{
mixin MarkedExceptionCtors;
}
/// Exception thrown on Representer errors.
class RepresenterException : YAMLException
{
mixin basicExceptionCtors;
}
/// Exception thrown on scanner errors.
class ScannerException : MarkedYAMLException
{
mixin MarkedExceptionCtors;
}
private:
/// Constructors of marked YAML exceptions are identical, so we use a mixin.
///
/// See_Also: MarkedYAMLException
template MarkedExceptionCtors()
{
public:
this(string msg, const Mark mark1, string mark2Label,
const Mark mark2, string file = __FILE__, size_t line = __LINE__)
@safe pure nothrow
{
super(msg, mark1, mark2Label, Nullable!Mark(mark2), file, line);
}
this(string msg, const Mark mark,
string file = __FILE__, size_t line = __LINE__)
@safe pure nothrow
{
super(msg, mark, file, line);
}
this(string msg, const Mark mark1, string mark2Label,
const Nullable!Mark mark2, string file = __FILE__, size_t line = __LINE__)
@safe pure nothrow
{
super(msg, mark1, mark2Label, mark2, file, line);
}
}
|