aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sdlang/exception.d
blob: 188991e28bb31c788eca24a2580ea26e4b8d0a55 (plain)
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
// SDLang-D
// Written in the D programming language.

module sdlang.exception;

import std.array;
import std.exception;
import std.range;
import std.stdio;
import std.string;

import sdlang.ast;
import sdlang.util;

/// Abstract parent class of all SDLang-D defined exceptions.
abstract class SDLangException : Exception
{
	this(string msg, string file = __FILE__, size_t line = __LINE__)
	{
		super(msg, file, line);
	}
}

/// Thrown when a syntax error is encounterd while parsing.
class ParseException : SDLangException
{
	Location location;
	bool hasLocation;

	this(string msg, string file = __FILE__, size_t line = __LINE__)
	{
		hasLocation = false;
		super(msg, file, line);
	}

	this(Location location, string msg, string file = __FILE__, size_t line = __LINE__)
	{
		hasLocation = true;
		super("%s: %s".format(location.toString(), msg), file, line);
	}
}

/// Compatibility alias
deprecated("The new name is ParseException")
alias SDLangParseException = ParseException;

/++
Thrown when attempting to do something in the DOM that's unsupported, such as:

$(UL
$(LI Adding the same instance of a tag or attribute to more than one parent.)
$(LI Writing SDLang where:
	$(UL
	$(LI The root tag has values, attributes or a namespace. )
	$(LI An anonymous tag has a namespace. )
	$(LI An anonymous tag has no values. )
	$(LI A floating point value is infinity or NaN. )
	)
))
+/
class ValidationException : SDLangException
{
	this(string msg, string file = __FILE__, size_t line = __LINE__)
	{
		super(msg, file, line);
	}
}

/// Compatibility alias
deprecated("The new name is ValidationException")
alias SDLangValidationException = ValidationException;

/// Thrown when someting is wrong with the provided arguments to a function.
class ArgumentException : SDLangException
{
	this(string msg, string file = __FILE__, size_t line = __LINE__)
	{
		super(msg, file, line);
	}
}

/// Thrown by the DOM on empty range and out-of-range conditions.
abstract class DOMException : SDLangException
{
	Tag base; /// The tag searched from

	this(Tag base, string msg, string file = __FILE__, size_t line = __LINE__)
	{
		this.base = base;
		super(msg, file, line);
	}

	/// Prefixes a message with file/line information from the tag (if tag exists).
	/// Optionally takes output range as a sink.
	string customMsg(string msg)
	{
		if(!base)
			return msg;

		Appender!string sink;
		this.customMsg(sink, msg);
		return sink.data;
	}

	///ditto
	void customMsg(Sink)(ref Sink sink, string msg) if(isOutputRange!(Sink,char))
	{
		if(base)
		{
			sink.put(base.location.toString());
			sink.put(": ");
			sink.put(msg);
		}
		else
			sink.put(msg);
	}

	/// Outputs a message to stderr, prefixed with file/line information
	void writeCustomMsg(string msg)
	{
		stderr.writeln( customMsg(msg) );
	}
}

/// Thrown by the DOM on empty range and out-of-range conditions.
class DOMRangeException : DOMException
{
	this(Tag base, string msg, string file = __FILE__, size_t line = __LINE__)
	{
		super(base, msg, file, line);
	}
}

/// Compatibility alias
deprecated("The new name is DOMRangeException")
alias SDLangRangeException = DOMRangeException;

/// Abstract parent class of `TagNotFoundException`, `ValueNotFoundException`
/// and `AttributeNotFoundException`.
///
/// Thrown by the DOM's `sdlang.ast.Tag.expectTag`, etc. functions if a matching element isn't found.
abstract class DOMNotFoundException : DOMException
{
	FullName tagName; /// The tag searched for

	this(Tag base, FullName tagName, string msg, string file = __FILE__, size_t line = __LINE__)
	{
		this.tagName = tagName;
		super(base, msg, file, line);
	}
}

/// Thrown by the DOM's `sdlang.ast.Tag.expectTag`, etc. functions if a Tag isn't found.
class TagNotFoundException : DOMNotFoundException
{
	this(Tag base, FullName tagName, string msg, string file = __FILE__, size_t line = __LINE__)
	{
		super(base, tagName, msg, file, line);
	}
}

/// Thrown by the DOM's `sdlang.ast.Tag.expectValue`, etc. functions if a Value isn't found.
class ValueNotFoundException : DOMNotFoundException
{
	/// Expected type for the not-found value.
	TypeInfo valueType;

	this(Tag base, FullName tagName, TypeInfo valueType, string msg, string file = __FILE__, size_t line = __LINE__)
	{
		this.valueType = valueType;
		super(base, tagName, msg, file, line);
	}
}

/// Thrown by the DOM's `sdlang.ast.Tag.expectAttribute`, etc. functions if an Attribute isn't found.
class AttributeNotFoundException : DOMNotFoundException
{
	FullName attributeName; /// The attribute searched for

	/// Expected type for the not-found attribute's value.
	TypeInfo valueType;

	this(Tag base, FullName tagName, FullName attributeName, TypeInfo valueType, string msg,
		string file = __FILE__, size_t line = __LINE__)
	{
		this.valueType = valueType;
		this.attributeName = attributeName;
		super(base, tagName, msg, file, line);
	}
}