dyaml/source/dyaml/exception.d

107 lines
3.2 KiB
D
Raw Normal View History

2011-08-16 12:53:13 +00:00
// 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.
2011-08-16 12:53:13 +00:00
module dyaml.exception;
import std.algorithm;
import std.array;
import std.string;
import std.conv;
2011-08-16 12:53:13 +00:00
2012-12-11 16:06:20 +00:00
alias to!string str;
2011-08-16 12:53:13 +00:00
///Base class for all exceptions thrown by D:YAML.
class YAMLException : Exception
{
///Construct a YAMLException with specified message and position where it was thrown.
public this(string msg, string file = __FILE__, int line = __LINE__)
2013-12-17 14:16:43 +00:00
@trusted pure nothrow
{
super(msg, file, line);
}
2011-08-16 12:53:13 +00:00
}
///Position in a YAML stream, used for error messages.
struct Mark
2011-08-16 12:53:13 +00:00
{
private:
///Line number.
ushort line_;
///Column number.
ushort column_;
public:
///Construct a Mark with specified line and column in the file.
this(const uint line, const uint column) pure @safe nothrow
2011-08-16 12:53:13 +00:00
{
line_ = cast(ushort)min(ushort.max, line);
column_ = cast(ushort)min(ushort.max, column);
}
///Get a string representation of the mark.
string toString() const @trusted
2011-08-16 12:53:13 +00:00
{
//Line/column numbers start at zero internally, make them start at 1.
2012-12-11 16:06:20 +00:00
string clamped(ushort v){return str(v + 1) ~ (v == ushort.max ? " or higher" : "");}
return "line " ~ clamped(line_) ~ ",column " ~ clamped(column_);
2011-08-16 12:53:13 +00:00
}
}
static assert(Mark.sizeof == 4, "Unexpected Mark size");
2011-08-16 12:53:13 +00:00
package:
//Base class of YAML exceptions with marked positions of the problem.
abstract class MarkedYAMLException : YAMLException
{
//Construct a MarkedYAMLException with specified context and problem.
this(string context, Mark contextMark, string problem, Mark problemMark,
2013-12-17 14:16:43 +00:00
string file = __FILE__, int line = __LINE__) @safe pure
2011-08-16 12:53:13 +00:00
{
const msg = context ~ '\n' ~
(contextMark != problemMark ? contextMark.toString() ~ '\n' : "") ~
problem ~ '\n' ~ problemMark.toString() ~ '\n';
super(msg, file, line);
2011-08-16 12:53:13 +00:00
}
//Construct a MarkedYAMLException with specified problem.
this(string problem, Mark problemMark, string file = __FILE__, int line = __LINE__)
2013-12-17 14:16:43 +00:00
@safe pure
2011-08-16 12:53:13 +00:00
{
super(problem ~ '\n' ~ problemMark.toString(), file, line);
2011-08-16 12:53:13 +00:00
}
}
//Constructors of YAML exceptions are mostly the same, so we use a mixin.
template ExceptionCtors()
{
public this(string msg, string file = __FILE__, int line = __LINE__)
@safe nothrow
{
super(msg, file, line);
}
}
//Constructors of marked YAML exceptions are mostly the same, so we use a mixin.
template MarkedExceptionCtors()
{
public:
this(string context, Mark contextMark, string problem, Mark problemMark,
2012-12-27 19:56:15 +00:00
string file = __FILE__, int line = __LINE__) @safe
{
super(context, contextMark, problem, problemMark,
file, line);
}
this(string problem, Mark problemMark, string file = __FILE__, int line = __LINE__)
2012-12-27 19:56:15 +00:00
@safe
{
super(problem, problemMark, file, line);
}
}