summaryrefslogtreecommitdiff
path: root/src/plugins/plain/PlainFormatStreamReader.cpp
blob: 44695361206af7abedc17a5919c193b505cfa8f3 (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
/*
    Ousía
    Copyright (C) 2014  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 <core/common/CharReader.hpp>
#include <core/common/Logger.hpp>
#include <core/common/Utils.hpp>
#include <core/common/VariantReader.hpp>

#include "PlainFormatStreamReader.hpp"

namespace ousia {

namespace {

/**
 * Class used internally to collect data issued via "DATA" event.
 */
class DataHandler {
private:
	/**
	 * Internal character buffer.
	 */
	std::vector<char> buf;

	/**
	 * Start location of the character data.
	 */
	SourceOffset start;

	/**
	 * End location of the character data.
	 */
	SourceOffset end;

public:
	/**
	 * Default constructor, initializes start and end with zeros.
	 */
	DataHandler() : start(0), end(0) {}

	/**
	 * Returns true if the internal buffer is empty.
	 *
	 * @return true if no characters were added to the internal buffer, false
	 * otherwise.
	 */
	bool isEmpty() { return buf.empty(); }

	/**
	 * Appends a single character to the internal buffer.
	 *
	 * @param c is the character that should be added to the internal buffer.
	 * @param charStart is the start position of the character.
	 * @param charEnd is the end position of the character.
	 */
	void append(char c, SourceOffset charStart, SourceOffset charEnd)
	{
		if (isEmpty()) {
			start = charStart;
		}
		buf.push_back(c);
		end = charEnd;
	}

	/**
	 * Appends a string to the internal buffer.
	 *
	 * @param s is the string that should be added to the internal buffer.
	 * @param stringStart is the start position of the string.
	 * @param stringEnd is the end position of the string.
	 */
	void append(const std::string &s, SourceOffset stringStart,
	            SourceOffset stringEnd)
	{
		if (isEmpty()) {
			start = stringStart;
		}
		std::copy(s.c_str(), s.c_str() + s.size(), back_inserter(buf));
		end = stringEnd;
	}

	/**
	 * Converts the internal buffer to a variant with attached location
	 * information.
	 *
	 * @param sourceId is the source id which is needed for building the
	 * location information.
	 * @return a Variant with the internal buffer content as string and
	 * the correct start and end location.
	 */
	Variant toVariant(SourceId sourceId)
	{
		Variant res = Variant::fromString(std::string(buf.data(), buf.size()));
		res.setLocation({sourceId, start, end});
		return res;
	}
};
}

PlainFormatStreamReader::PlainFormatStreamReader(CharReader &reader,
                                                 Logger &logger)
    : reader(reader), logger(logger), fieldIdx(0)
{
	tokenBackslash = tokenizer.registerToken("\\");
	tokenLinebreak = tokenizer.registerToken("\n");
	tokenLineComment = tokenizer.registerToken("%");
	tokenBlockCommentStart = tokenizer.registerToken("%{");
	tokenBlockCommentEnd = tokenizer.registerToken("}%");
}

Variant PlainFormatStreamReader::parseIdentifier(size_t start)
{
	bool first = true;
	std::vector<char> identifier;
	size_t end = reader.getPeekOffset();
	char c;
	while (reader.peek(c)) {
		// Abort if this character is not a valid identifer character
		if ((first && Utils::isIdentifierStartCharacter(c)) ||
		    (!first && Utils::isIdentifierCharacter(c))) {
			identifier.push_back(c);
		} else {
			reader.resetPeek();
			break;
		}

		// This is no longer the first character
		first = false;
		end = reader.getPeekOffset();
		reader.consumePeek();
	}

	// Return the identifier at its location
	Variant res =
	    Variant::fromString(std::string(identifier.data(), identifier.size()));
	res.setLocation({reader.getSourceId(), start, end});
	return res;
}

void PlainFormatStreamReader::parseCommand(size_t start)
{
	// Parse the commandName as a first identifier
	commandName = parseIdentifier(start);

	// Check whether the next character is a '#', indicating the start of the
	// command name
	Variant commandArgName;
	start = reader.getOffset();
	if (reader.expect('#')) {
		commandArgName = parseIdentifier(start);
		if (commandArgName.asString().empty()) {
			logger.error("Expected identifier after '#'", commandArgName);
		}
	}

	// Read the arguments (if they are available), otherwise reset them
	if (reader.expect('[')) {
		auto res = VariantReader::parseObject(reader, logger, ']');
		commandArguments = res.second;
	} else {
		commandArguments = Variant::mapType{};
	}

	// Insert the parsed name, make sure "name" was not specified in the
	// arguments
	if (commandArgName.isString()) {
		auto res = commandArguments.asMap().emplace("name", commandArgName);
		if (!res.second) {
			logger.error("Name argument specified multiple times",
			             SourceLocation{}, MessageMode::NO_CONTEXT);
			logger.note("First occurance is here: ", commandArgName);
			logger.note("Second occurance is here: ", res.first->second);
		}
	}
}

void PlainFormatStreamReader::parseBlockComment()
{
	DynamicToken token;
	size_t depth = 1;
	while (tokenizer.read(reader, token)) {
		if (token.type == tokenBlockCommentEnd) {
			depth--;
			if (depth == 0) {
				return;
			}
		}
		if (token.type == tokenBlockCommentStart) {
			depth++;
		}
	}

	// Issue an error if the file ends while we are in a block comment
	logger.error("File ended while being in a block comment", reader);
}

void PlainFormatStreamReader::parseLineComment()
{
	char c;
	reader.consumePeek();
	while (reader.read(c)) {
		if (c == '\n') {
			return;
		}
	}
}

PlainFormatStreamReader::State PlainFormatStreamReader::parse()
{
// Macro (sorry for that) used for checking whether there is data to issue, and
// if yes, aborting the loop, allowing for a reentry on a later parse call by
// resetting the peek cursor
#define CHECK_ISSUE_DATA()            \
	{                                 \
		if (!dataHandler.isEmpty()) { \
			reader.resetPeek();       \
			abort = true;             \
			break;                    \
		}                             \
	}

	// Handler for incomming data
	DataHandler dataHandler;

	// Variable set to true if the parser loop should be left
	bool abort = false;

	// Read tokens until the outer loop should be left
	DynamicToken token;
	while (!abort && tokenizer.peek(reader, token)) {
		// Check whether this backslash just escaped some special or
		// whitespace character or was the beginning of a command
		if (token.type == tokenBackslash) {
			// Check whether this character could be the start of a command
			char c;
			reader.consumePeek();
			reader.peek(c);
			if (Utils::isIdentifierStartCharacter(c)) {
				CHECK_ISSUE_DATA();
				reader.resetPeek();
				parseCommand(token.location.getStart());
				return State::COMMAND;
			}

			// This was not a special character, just append the given character
			// to the data buffer, use the escape character start as start
			// location and the peek offset as end location
			dataHandler.append(c, token.location.getStart(),
			                   reader.getPeekOffset());
		} else if (token.type == tokenLineComment) {
			CHECK_ISSUE_DATA();
			reader.consumePeek();
			parseLineComment();
		} else if (token.type == tokenBlockCommentStart) {
			CHECK_ISSUE_DATA();
			reader.consumePeek();
			parseBlockComment();
		} else if (token.type == tokenLinebreak) {
			CHECK_ISSUE_DATA();
			reader.consumePeek();
			return State::LINEBREAK;
		} else if (token.type == TextToken) {
			dataHandler.append(token.content, token.location.getStart(),
			                   token.location.getEnd());
		}

		// Consume the peeked character if we did not abort, otherwise abort
		if (!abort) {
			reader.consumePeek();
		}
	}

	// Send out pending output data, otherwise we are at the end of the stream
	if (!dataHandler.isEmpty()) {
		data = dataHandler.toVariant(reader.getSourceId());
		return State::DATA;
	}
	return State::END;
#undef CHECK_ISSUE_DATA
}
}