dyaml/source/dyaml/exception.d

141 lines
4.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
2014-07-23 16:15:13 +00:00
/// Base class for all exceptions thrown by D:YAML.
2011-08-16 12:53:13 +00:00
class YAMLException : Exception
{
2014-07-23 16:15:13 +00:00
/// Construct a YAMLException with specified message and position where it was thrown.
public this(string msg, string file = __FILE__, size_t line = __LINE__)
2014-07-23 16:15:37 +00:00
@safe pure nothrow
{
super(msg, file, line);
}
2011-08-16 12:53:13 +00:00
}
2014-08-01 00:52:14 +00:00
package:
// Position in a YAML stream, used for error messages.
struct Mark
2011-08-16 12:53:13 +00:00
{
package:
2014-07-21 23:22:56 +00:00
/// Line number.
2011-08-16 12:53:13 +00:00
ushort line_;
2014-07-21 23:22:56 +00:00
/// Column number.
2011-08-16 12:53:13 +00:00
ushort column_;
public:
2014-07-21 23:22:56 +00:00
/// Construct a Mark with specified line and column in the file.
this(const uint line, const uint column) @safe pure nothrow @nogc
2011-08-16 12:53:13 +00:00
{
line_ = cast(ushort)min(ushort.max, line);
// This *will* overflow on extremely wide files but saves CPU time
// (mark ctor takes ~5% of time)
column_ = cast(ushort)column;
2011-08-16 12:53:13 +00:00
}
2014-07-21 23:22:56 +00:00
/// Get a string representation of the mark.
string toString() @safe pure nothrow const
2011-08-16 12:53:13 +00:00
{
2014-08-01 00:52:14 +00:00
// Line/column numbers start at zero internally, make them start at 1.
2014-07-21 23:22:56 +00:00
static string clamped(ushort v) @safe pure nothrow
2014-07-23 16:15:13 +00:00
{
2018-06-13 08:17:20 +00:00
return text(v + 1, v == ushort.max ? " or higher" : "");
2014-07-21 23:22:56 +00:00
}
return "line " ~ clamped(line_) ~ ",column " ~ clamped(column_);
2011-08-16 12:53:13 +00:00
}
}
static assert(Mark.sizeof == 4, "Unexpected Mark size");
// A struct storing parameters to the MarkedYAMLException constructor.
struct MarkedYAMLExceptionData
{
// Context of the error.
string context;
// Position of the context in a YAML buffer.
Mark contextMark;
// The error itself.
string problem;
// Position if the error.
Mark problemMark;
}
// Base class of YAML exceptions with marked positions of the problem.
2011-08-16 12:53:13 +00:00
abstract class MarkedYAMLException : YAMLException
{
2014-07-23 16:15:13 +00:00
// Construct a MarkedYAMLException with specified context and problem.
2014-07-24 16:41:55 +00:00
this(string context, const Mark contextMark, string problem, const Mark problemMark,
string file = __FILE__, size_t line = __LINE__) @safe pure nothrow
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
}
2014-07-23 16:15:13 +00:00
// Construct a MarkedYAMLException with specified problem.
2014-07-24 16:41:55 +00:00
this(string problem, const Mark problemMark,
string file = __FILE__, size_t line = __LINE__)
2014-07-23 16:15:37 +00:00
@safe pure nothrow
2011-08-16 12:53:13 +00:00
{
super(problem ~ '\n' ~ problemMark.toString(), file, line);
2011-08-16 12:53:13 +00:00
}
/// Construct a MarkedYAMLException from a struct storing constructor parameters.
this(ref const(MarkedYAMLExceptionData) data) @safe pure nothrow
{
with(data) this(context, contextMark, problem, problemMark);
}
2011-08-16 12:53:13 +00:00
}
2014-07-23 16:15:13 +00:00
// Constructors of YAML exceptions are mostly the same, so we use a mixin.
//
// See_Also: YAMLException
template ExceptionCtors()
{
public this(string msg, string file = __FILE__, size_t line = __LINE__)
2013-12-17 14:18:03 +00:00
@safe pure nothrow
{
super(msg, file, line);
}
}
2014-07-23 16:15:13 +00:00
// Constructors of marked YAML exceptions are mostly the same, so we use a mixin.
//
// See_Also: MarkedYAMLException
template MarkedExceptionCtors()
{
public:
2018-04-10 01:02:30 +00:00
this(string context, const Mark contextMark, string problem,
const Mark problemMark, string file = __FILE__, size_t line = __LINE__)
2014-07-24 16:41:55 +00:00
@safe pure nothrow
{
2014-07-23 16:15:13 +00:00
super(context, contextMark, problem, problemMark,
file, line);
}
2014-07-24 16:41:55 +00:00
this(string problem, const Mark problemMark,
string file = __FILE__, size_t line = __LINE__)
2014-07-24 16:41:55 +00:00
@safe pure nothrow
{
super(problem, problemMark, file, line);
}
2014-07-23 16:15:13 +00:00
this(ref const(MarkedYAMLExceptionData) data) @safe pure nothrow
{
super(data);
}
}