summaryrefslogtreecommitdiff
path: root/src/plugins/xml/XmlParser.cpp
blob: 17bc4706a7322b9a9cab28d9134e72a120d94cb1 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
    Ousía
    Copyright (C) 2014, 2015  Benjamin Paaßen, Andreas Stöckel

    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/>.
*/

#include <iostream>
#include <vector>

#include <expat.h>

#include <core/common/CharReader.hpp>
#include <core/common/Utils.hpp>
#include <core/common/VariantReader.hpp>
#include <core/parser/ParserStack.hpp>
#include <core/parser/ParserScope.hpp>
#include <core/model/Typesystem.hpp>

#include "XmlParser.hpp"

namespace ousia {

/* Document structure */
static const State STATE_DOCUMENT = 0;
static const State STATE_HEAD = 1;
static const State STATE_BODY = 2;

/* Special commands */
static const State STATE_USE = 100;
static const State STATE_INCLUDE = 101;
static const State STATE_INLINE = 102;

/* Type system definitions */
static const State STATE_TYPESYSTEM = 200;
static const State STATE_TYPES = 201;
static const State STATE_CONSTANTS = 202;
static const State STATE_CONSTANT = 203;
static const State STATE_ENUM = 204;
static const State STATE_STRUCT = 205;
static const State STATE_FIELD = 206;

class TypesystemHandler : public Handler {
public:
	using Handler::Handler;

	void start(Variant::mapType &args) override
	{
		scope().push(project()->createTypesystem(args["name"].asString()));
	}

	void end() override
	{
		scope().performDeferredResolution(logger());
		// TODO: Automatically call validate in "pop"?
		scope().getLeaf()->validate(logger());
		scope().pop();
	}

	static Handler *create(const HandlerData &handlerData)
	{
		return new TypesystemHandler{handlerData};
	}
};

class StructHandler : public Handler {
public:
	using Handler::Handler;

	void start(Variant::mapType &args) override
	{
		// Fetch the arguments used for creating this type
		const std::string &name = args["name"].asString();
		const std::string &parent = args["parent"].asString();

		// Fetch the current typesystem and create the struct node
		Rooted<Typesystem> typesystem = scope().getLeaf().cast<Typesystem>();
		Rooted<StructType> structType = typesystem->createStructType(name);

		// Try to resolve the parent type and set it as parent structure
		if (!parent.empty()) {
			scope().resolve<StructType>(parent, logger(),
			                            [structType](Handle<StructType> parent,
			                                         Logger &logger) mutable {
				                            structType->setParentStructure(
				                                parent, logger);
				                        },
			                            location());
		}

		// Descend into the struct type
		scope().push(structType);
	}

	void end() override
	{
		// Descend from the struct type
		scope().pop();
	}

	static Handler *create(const HandlerData &handlerData)
	{
		return new StructHandler{handlerData};
	}
};

class StructFieldHandler : public Handler {
public:
	using Handler::Handler;

	void start(Variant::mapType &args) override
	{
		// Read the argument values
		const std::string &name = args["name"].asString();
		const std::string &type = args["type"].asString();
		const Variant &defaultValue = args["default"];
		const bool optional =
		    !(defaultValue.isObject() && defaultValue.asObject() == nullptr);

		Rooted<StructType> structType = scope().getLeaf().cast<StructType>();
		Rooted<Attribute> attribute =
		    structType->createAttribute(name, defaultValue, optional, logger());

		// Try to resolve the type
		scope().resolve<Type>(
		    type, logger(),
		    [attribute](Handle<Type> type, Logger &logger) mutable {
			    attribute->setType(type, logger);
			},
		    location());
	}

	void end() override {}

	static Handler *create(const HandlerData &handlerData)
	{
		return new StructFieldHandler{handlerData};
	}
};

static const std::multimap<std::string, HandlerDescriptor> XML_HANDLERS{
    /* Document tags */
    {"document", {{STATE_NONE}, nullptr, STATE_DOCUMENT}},
    {"head", {{STATE_DOCUMENT}, nullptr, STATE_HEAD}},
    {"body", {{STATE_DOCUMENT}, nullptr, STATE_BODY, true}},

    /* Special commands */
    {"use", {{STATE_HEAD}, nullptr, STATE_USE}},
    {"include", {{STATE_ALL}, nullptr, STATE_INCLUDE}},
    {"inline", {{STATE_ALL}, nullptr, STATE_INLINE}},

    /* Typesystem */
    {"typesystem",
     {{STATE_NONE, STATE_HEAD},
      TypesystemHandler::create,
      STATE_TYPESYSTEM,
      false,
      {Argument::String("name")}}},
    {"types", {{STATE_TYPESYSTEM}, nullptr, STATE_TYPES}},
    {"enum", {{STATE_TYPES}, nullptr, STATE_ENUM}},
    {"struct",
     {{STATE_TYPES},
      StructHandler::create,
      STATE_STRUCT,
      false,
      {Argument::String("name"), Argument::String("parent", "")}}},
    {"field",
     {{{STATE_STRUCT}},
      StructFieldHandler::create,
      STATE_FIELD,
      false,
      {Argument::String("name"), Argument::String("type"),
       Argument::Any("default", Variant::fromObject(nullptr))}}},
    {"constants", {{STATE_TYPESYSTEM}, nullptr, STATE_CONSTANTS}},
    {"constant", {{STATE_CONSTANTS}, nullptr, STATE_CONSTANT}}};

/**
 * Wrapper class around the XML_Parser pointer which safely frees it whenever
 * the scope is left (e.g. because an exception was thrown).
 */
class ScopedExpatXmlParser {
private:
	/**
	 * Internal pointer to the XML_Parser instance.
	 */
	XML_Parser parser;

public:
	/**
	 * Constructor of the ScopedExpatXmlParser class. Calls XML_ParserCreateNS
	 * from the expat library. Throws a parser exception if the XML parser
	 * cannot be initialized.
	 *
	 * @param encoding is the protocol-defined encoding passed to expat (or
	 * nullptr if expat should determine the encoding by itself).
	 */
	ScopedExpatXmlParser(const XML_Char *encoding) : parser(nullptr)
	{
		parser = XML_ParserCreate(encoding);
		if (!parser) {
			throw LoggableException{
			    "Internal error: Could not create expat XML parser!"};
		}
	}

	/**
	 * Destuctor of the ScopedExpatXmlParser, frees the XML parser instance.
	 */
	~ScopedExpatXmlParser()
	{
		if (parser) {
			XML_ParserFree(parser);
			parser = nullptr;
		}
	}

	/**
	 * Returns the XML_Parser pointer.
	 */
	XML_Parser operator&() { return parser; }
};

/* Adapter Expat -> ParserStack */

static SourceLocation syncLoggerPosition(XML_Parser p)
{
	// Fetch the parser stack and the associated user data
	ParserStack *stack = static_cast<ParserStack *>(XML_GetUserData(p));

	// Fetch the current location in the XML file
	size_t offs = XML_GetCurrentByteIndex(p);

	// Build the source location and update the default location of the current
	// logger instance
	SourceLocation loc{stack->getContext().getSourceId(), offs};
	stack->getContext().getLogger().setDefaultLocation(loc);
	return loc;
}

static void xmlStartElementHandler(void *p, const XML_Char *name,
                                   const XML_Char **attrs)
{
	XML_Parser parser = static_cast<XML_Parser>(p);
	SourceLocation loc = syncLoggerPosition(parser);

	ParserStack *stack = static_cast<ParserStack *>(XML_GetUserData(parser));

	Variant::mapType args;
	const XML_Char **attr = attrs;
	while (*attr) {
		const std::string key{*(attr++)};
		std::pair<bool, Variant> value = VariantReader::parseGenericString(
		    *(attr++), stack->getContext().getLogger());
		args.emplace(std::make_pair(key, value.second));
	}
	stack->start(std::string(name), args, loc);
}

static void xmlEndElementHandler(void *p, const XML_Char *name)
{
	XML_Parser parser = static_cast<XML_Parser>(p);
	ParserStack *stack = static_cast<ParserStack *>(XML_GetUserData(parser));

	syncLoggerPosition(parser);
	stack->end();
}

static void xmlCharacterDataHandler(void *p, const XML_Char *s, int len)
{
	XML_Parser parser = static_cast<XML_Parser>(p);
	ParserStack *stack = static_cast<ParserStack *>(XML_GetUserData(parser));

	const std::string data =
	    Utils::trim(std::string{s, static_cast<size_t>(len)});
	if (!data.empty()) {
		stack->data(data);
	}
}

/* Class XmlParser */

Rooted<Node> XmlParser::doParse(CharReader &reader, ParserContext &ctx)
{
	// Create the parser object
	ScopedExpatXmlParser p{"UTF-8"};

	// Create the parser stack instance and pass the reference to the state
	// machine descriptor
	ParserStack stack{ctx, XML_HANDLERS};
	XML_SetUserData(&p, &stack);
	XML_UseParserAsHandlerArg(&p);

	// Set the callback functions
	XML_SetStartElementHandler(&p, xmlStartElementHandler);
	XML_SetEndElementHandler(&p, xmlEndElementHandler);
	XML_SetCharacterDataHandler(&p, xmlCharacterDataHandler);

	// Feed data into expat while there is data to process
	constexpr size_t BUFFER_SIZE = 64 * 1024;
	while (true) {
		// Fetch a buffer from expat for the input data
		char *buf = static_cast<char *>(XML_GetBuffer(&p, BUFFER_SIZE));
		if (!buf) {
			throw LoggableException{
			    "Internal error: XML parser out of memory!"};
		}

		// Read into the buffer
		size_t bytesRead = reader.readRaw(buf, BUFFER_SIZE);

		// Parse the data and handle any XML error
		if (!XML_ParseBuffer(&p, bytesRead, bytesRead == 0)) {
			// Fetch the xml parser byte offset
			size_t offs = XML_GetCurrentByteIndex(&p);

			// Throw a corresponding exception
			XML_Error code = XML_GetErrorCode(&p);
			std::string msg = std::string{XML_ErrorString(code)};
			throw LoggableException{"XML: " + msg,
			                        SourceLocation{reader.getSourceId(), offs}};
		}

		// Abort once there are no more bytes in the stream
		if (bytesRead == 0) {
			break;
		}
	}
	return nullptr;
}
}