aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/error.h
blob: c9cad00e390d629af00b42225b4e2bfe096c58d3 (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
191
192
193
194
195
196
197
198
/*
 * Copyright (C) 2012-2015 Robin Haberkorn
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __ERROR_H
#define __ERROR_H

#include <exception>
#include <typeinfo>

#include <glib.h>
#include <glib/gprintf.h>

#include "sciteco.h"
#include "string-utils.h"

namespace SciTECO {

/*
 * Thrown as exception to signify that program
 * should be terminated.
 */
class Quit {};

class Error {
	gchar *description;
	GSList *frames;

public:
	gint pos;
	gint line, column;

	class Frame {
	public:
		gint pos;
		gint line, column;

		virtual Frame *copy() const = 0;
		virtual ~Frame() {}

		virtual void display(gint nr) = 0;
	};

	class QRegFrame : public Frame {
		gchar *name;

	public:
		QRegFrame(const gchar *_name)
			 : name(g_strdup(_name)) {}

		Frame *copy() const;

		~QRegFrame()
		{
			g_free(name);
		}

		void display(gint nr);
	};

	class FileFrame : public Frame {
		gchar *name;

	public:
		FileFrame(const gchar *_name)
			 : name(g_strdup(_name)) {}

		Frame *copy() const;

		~FileFrame()
		{
			g_free(name);
		}

		void display(gint nr);
	};

	class EDHookFrame : public Frame {
		gchar *type;

	public:
		EDHookFrame(const gchar *_type)
		           : type(g_strdup(_type)) {}

		Frame *copy() const;

		~EDHookFrame()
		{
			g_free(type);
		}

		void display(gint nr);
	};

	class ToplevelFrame : public Frame {
	public:
		Frame *copy() const;

		void display(gint nr);
	};

	Error(const gchar *fmt, ...) G_GNUC_PRINTF(2, 3);
	Error(const Error &inst);
	~Error();

	inline void
	set_coord(const gchar *str, gint _pos)
	{
		pos = _pos;
		String::get_coord(str, pos, line, column);
	}

	void add_frame(Frame *frame);

	void display_short(void);
	void display_full(void);
};

class StdError : public Error {
public:
	StdError(const gchar *type, const std::exception &error)
		: Error("%s: %s", type, error.what()) {}
	StdError(const std::exception &error)
		: Error("%s: %s", typeid(error).name(), error.what()) {}
};

class GlibError : public Error {
public:
	/**
	 * Construct error for glib's GError.
	 * Ownership of the error's resources is passed
	 * the GlibError object.
	 */
	GlibError(GError *gerror)
	         : Error("%s", gerror->message)
	{
		g_error_free(gerror);
	}
};

class SyntaxError : public Error {
public:
	SyntaxError(gchar chr)
		   : Error("Syntax error \"%c\" (%d)", chr, chr) {}
};

class ArgExpectedError : public Error {
public:
	ArgExpectedError(const gchar *cmd)
		        : Error("Argument expected for <%s>", cmd) {}
	ArgExpectedError(gchar cmd)
		        : Error("Argument expected for <%c>", cmd) {}
};

class MoveError : public Error {
public:
	MoveError(const gchar *cmd)
		 : Error("Attempt to move pointer off page with <%s>",
			 cmd) {}
	MoveError(gchar cmd)
		 : Error("Attempt to move pointer off page with <%c>",
			 cmd) {}
};

class RangeError : public Error {
public:
	RangeError(const gchar *cmd)
		  : Error("Invalid range specified for <%s>", cmd) {}
	RangeError(gchar cmd)
		  : Error("Invalid range specified for <%c>", cmd) {}
};

class InvalidQRegError : public Error {
public:
	InvalidQRegError(const gchar *name, bool local = false)
			: Error("Invalid Q-Register \"%s%s\"",
				local ? "." : "", name) {}
	InvalidQRegError(gchar name, bool local = false)
			: Error("Invalid Q-Register \"%s%c\"",
				local ? "." : "", name) {}
};

} /* namespace SciTECO */

#endif