dyaml/dyaml/exception.d
Ferdinand Majerech 8ad650e089 Cleaned up exceptions and their messages.
Some minor dog fixes.
2011-10-18 16:12:22 +02:00

98 lines
3 KiB
D

// 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.string;
///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__)
{
super(msg, file, line);
}
}
///Position in a YAML stream, used for error messages.
align(1) struct Mark
{
private:
///Line number.
ushort line_;
///Column number.
ushort column_;
public:
///Construct a Mark with specified line and column in the file.
this(in uint line, in uint column)
{
line_ = cast(ushort)min(ushort.max, line);
column_ = cast(ushort)min(ushort.max, column);
}
///Get a string representation of the mark.
string toString() const
{
//Line/column numbers start at zero internally, make them start at 1.
string clamped(ushort v){return format(v + 1, v == ushort.max ? " or higher" : "");}
return format("line ", clamped(line_), ",column ", clamped(column_));
}
}
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,
string file = __FILE__, int line = __LINE__)
{
const msg = context ~ '\n' ~
(contextMark != problemMark ? contextMark.toString() ~ '\n' : "") ~
problem ~ '\n' ~ problemMark.toString() ~ '\n';
super(msg, file, line);
}
//Construct a MarkedYAMLException with specified problem.
this(string problem, Mark problemMark, string file = __FILE__, int line = __LINE__)
{
super(problem ~ '\n' ~ problemMark.toString(), file, line);
}
}
//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__)
{
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,
string file = __FILE__, int line = __LINE__)
{
super(context, contextMark, problem, problemMark,
file, line);
}
this(string problem, Mark problemMark, string file = __FILE__, int line = __LINE__)
{
super(problem, problemMark, file, line);
}
}