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)
|
|
|
|
|
2011-10-14 08:34:53 +00:00
|
|
|
///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;
|
2012-12-11 15:32:25 +00:00
|
|
|
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.
|
2014-09-19 16:58:16 +00:00
|
|
|
public this(string msg, string file = __FILE__, size_t line = __LINE__)
|
2014-07-23 16:15:37 +00:00
|
|
|
@safe pure nothrow
|
2011-10-18 14:12:22 +00:00
|
|
|
{
|
|
|
|
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.
|
2012-08-30 13:38:51 +00:00
|
|
|
struct Mark
|
2011-08-16 12:53:13 +00:00
|
|
|
{
|
2014-08-05 11:10:52 +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);
|
2014-08-05 11:10:52 +00:00
|
|
|
// 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
|
|
|
}
|
2012-12-27 20:21:18 +00:00
|
|
|
return "line " ~ clamped(line_) ~ ",column " ~ clamped(column_);
|
2011-08-16 12:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-30 13:38:51 +00:00
|
|
|
static assert(Mark.sizeof == 4, "Unexpected Mark size");
|
|
|
|
|
2014-07-23 16:16:05 +00:00
|
|
|
// 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,
|
2014-09-19 16:58:16 +00:00
|
|
|
string file = __FILE__, size_t line = __LINE__) @safe pure nothrow
|
2011-08-16 12:53:13 +00:00
|
|
|
{
|
2011-10-18 14:12:22 +00:00
|
|
|
const msg = context ~ '\n' ~
|
|
|
|
(contextMark != problemMark ? contextMark.toString() ~ '\n' : "") ~
|
|
|
|
problem ~ '\n' ~ problemMark.toString() ~ '\n';
|
2011-10-13 09:30:14 +00:00
|
|
|
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,
|
2014-09-19 16:58:16 +00:00
|
|
|
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
|
|
|
{
|
2011-10-13 09:30:14 +00:00
|
|
|
super(problem ~ '\n' ~ problemMark.toString(), file, line);
|
2011-08-16 12:53:13 +00:00
|
|
|
}
|
2014-07-23 16:16:05 +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
|
|
|
}
|
2011-10-13 09:30:14 +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
|
2011-10-13 09:30:14 +00:00
|
|
|
template ExceptionCtors()
|
|
|
|
{
|
2014-09-19 16:58:16 +00:00
|
|
|
public this(string msg, string file = __FILE__, size_t line = __LINE__)
|
2013-12-17 14:18:03 +00:00
|
|
|
@safe pure nothrow
|
2011-10-18 14:12:22 +00:00
|
|
|
{
|
|
|
|
super(msg, file, line);
|
|
|
|
}
|
2011-10-13 09:30:14 +00:00
|
|
|
}
|
|
|
|
|
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
|
2011-10-13 09:30:14 +00:00
|
|
|
template MarkedExceptionCtors()
|
|
|
|
{
|
|
|
|
public:
|
2018-04-10 01:02:30 +00:00
|
|
|
this(string context, const Mark contextMark, string problem,
|
2014-09-19 16:58:16 +00:00
|
|
|
const Mark problemMark, string file = __FILE__, size_t line = __LINE__)
|
2014-07-24 16:41:55 +00:00
|
|
|
@safe pure nothrow
|
2011-10-13 09:30:14 +00:00
|
|
|
{
|
2014-07-23 16:15:13 +00:00
|
|
|
super(context, contextMark, problem, problemMark,
|
2011-10-13 09:30:14 +00:00
|
|
|
file, line);
|
|
|
|
}
|
|
|
|
|
2014-07-24 16:41:55 +00:00
|
|
|
this(string problem, const Mark problemMark,
|
2014-09-19 16:58:16 +00:00
|
|
|
string file = __FILE__, size_t line = __LINE__)
|
2014-07-24 16:41:55 +00:00
|
|
|
@safe pure nothrow
|
2011-10-13 09:30:14 +00:00
|
|
|
{
|
|
|
|
super(problem, problemMark, file, line);
|
|
|
|
}
|
2014-07-23 16:15:13 +00:00
|
|
|
|
|
|
|
this(ref const(MarkedYAMLExceptionData) data) @safe pure nothrow
|
|
|
|
{
|
|
|
|
super(data);
|
|
|
|
}
|
2011-10-13 09:30:14 +00:00
|
|
|
}
|