File: doc_parser.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (516 lines) | stat: -rw-r--r-- 14,766 bytes parent folder | download | duplicates (2)
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#include "doc_parser.h"

#include "parse/parselo.h"
#include "libs/antlr/ErrorListener.h"

#undef TRUE
#undef FALSE

PUSH_SUPPRESS_WARNINGS
#include "arg_parser/generated/ArgumentListLexer.h"
#include "arg_parser/generated/ArgumentListParser.h"
#include "arg_parser/generated/ArgumentListVisitor.h"
POP_SUPPRESS_WARNINGS

namespace scripting {

namespace {

ade_type_info merge_alternatives(const ade_type_info& left, const ade_type_info& right)
{
	SCP_vector<ade_type_info> elements;
	if (left.isSimple()) {
		elements.push_back(left);
	} else {
		elements.insert(elements.end(), left.elements().begin(), left.elements().end());
	}
	if (right.isSimple()) {
		elements.push_back(right);
	} else {
		elements.insert(elements.end(), right.elements().begin(), right.elements().end());
	}

	return ade_type_info(ade_type_alternative(elements));
}

class BaseVisitor : public ArgumentListVisitor {
	static void failure()
	{
		throw std::runtime_error("Unhandled node encountered!");
	}

  public:
	antlrcpp::Any visitArg_list(ArgumentListParser::Arg_listContext* /*context*/) override
	{
		failure();
		return antlrcpp::Any();
	}
	antlrcpp::Any visitStandalone_type(ArgumentListParser::Standalone_typeContext* /*context*/) override
	{
		failure();
		return antlrcpp::Any();
	}
	antlrcpp::Any visitMap_type(ArgumentListParser::Map_typeContext* /*context*/) override
	{
		failure();
		return antlrcpp::Any();
	}
	antlrcpp::Any visitIterator_type(ArgumentListParser::Iterator_typeContext* /*context*/) override
	{
		failure();
		return antlrcpp::Any();
	}
	antlrcpp::Any visitSimple_type(ArgumentListParser::Simple_typeContext* /*context*/) override
	{
		failure();
		return antlrcpp::Any();
	}
	antlrcpp::Any visitType(ArgumentListParser::TypeContext* /*context*/) override
	{
		failure();
		return antlrcpp::Any();
	}
	antlrcpp::Any visitBoolean(ArgumentListParser::BooleanContext* /*context*/) override
	{
		failure();
		return antlrcpp::Any();
	}
	antlrcpp::Any visitValue(ArgumentListParser::ValueContext* /*context*/) override
	{
		failure();
		return antlrcpp::Any();
	}
	antlrcpp::Any visitActual_argument(ArgumentListParser::Actual_argumentContext* /*context*/) override
	{
		failure();
		return antlrcpp::Any();
	}
	antlrcpp::Any visitOptional_argument(ArgumentListParser::Optional_argumentContext* /*context*/) override
	{
		failure();
		return antlrcpp::Any();
	}
	antlrcpp::Any visitArgument(ArgumentListParser::ArgumentContext* /*context*/) override
	{
		failure();
		return antlrcpp::Any();
	}
	antlrcpp::Any visitFunc_arg(ArgumentListParser::Func_argContext* /*context*/) override
	{
		failure();
		return antlrcpp::Any();
	}
	antlrcpp::Any visitFunc_arglist(ArgumentListParser::Func_arglistContext* /*context*/) override
	{
		failure();
		return antlrcpp::Any();
	}
	antlrcpp::Any visitFunction_type(ArgumentListParser::Function_typeContext* /*context*/) override
	{
		failure();
		return antlrcpp::Any();
	}
	antlrcpp::Any visitVarargs_or_simple_type(ArgumentListParser::Varargs_or_simple_typeContext* /*context*/) override
	{
		failure();
		return antlrcpp::Any();
	}
};

class ValueVisitor : public BaseVisitor {
  public:
	antlrcpp::Any visitBoolean(ArgumentListParser::BooleanContext* context) override { return visitChildren(context); }
	antlrcpp::Any visitValue(ArgumentListParser::ValueContext* context) override { return visitChildren(context); }
	antlrcpp::Any visitTerminal(antlr4::tree::TerminalNode* node) override { return node->getText(); }
};

class ArglistVisitor : public BaseVisitor {
  public:
	antlrcpp::Any visitFunc_arg(ArgumentListParser::Func_argContext* context) override;
	antlrcpp::Any visitFunc_arglist(ArgumentListParser::Func_arglistContext* context) override
	{
		SCP_vector<ade_type_info> argTypes;

		size_t n = context->children.size();
		for (size_t i = 0; i < n; i++) {
			antlrcpp::Any childResult = context->children[i]->accept(this);

			if (childResult.isNotNull()) {
				argTypes.push_back(childResult.as<ade_type_info>());
			}
		}

		return argTypes;
	}

  protected:
	antlrcpp::Any aggregateResult(antlrcpp::Any any, const antlrcpp::Any& nextResult) override
	{
		if (any.isNull()) {
		}
		return AbstractParseTreeVisitor::aggregateResult(any, nextResult);
	}
};

class TypeVisitor : public BaseVisitor {
  public:
	antlrcpp::Any visitSimple_type(ArgumentListParser::Simple_typeContext* context) override
	{
		if (context->NIL() != nullptr) {
			return ade_type_info("nil");
		} else {
			return ade_type_info(context->ID()->getText());
		}
	}
	antlrcpp::Any visitVarargs_or_simple_type(ArgumentListParser::Varargs_or_simple_typeContext* context) override
	{
		auto retType = visit(context->simple_type()).as<ade_type_info>();
		if (context->VARARGS_SPECIFIER() != nullptr) {
			return ade_type_info(ade_type_varargs(std::move(retType)));
		}
		return retType;
	}

	antlrcpp::Any visitType(ArgumentListParser::TypeContext* context) override
	{
		return visitChildren(context);
	}
	antlrcpp::Any visitStandalone_type(ArgumentListParser::Standalone_typeContext* context) override
	{
		const auto typeValue = visitChildren(context).as<ade_type_info>();

		// We can't properly distinguish between alternate types and tuple types in aggregateResult so instead we do
		// that here. For a single type, we just return that type
		if (!context->COMMA().empty()) {
			// We saw a comma in our rule so this is a tuple type
			return ade_type_info(ade_type_tuple(typeValue.elements()));
		}

		// Otherwise just pass through the originally parsed type
		return typeValue;
	}

	antlrcpp::Any visitFunction_type(ArgumentListParser::Function_typeContext* context) override
	{
		auto retType = visit(context->type()).as<ade_type_info>();

		ArglistVisitor arglistVisitor;
		auto argTypes = context->func_arglist()->accept(&arglistVisitor).as<SCP_vector<ade_type_info>>();

		return ade_type_info(ade_type_function(std::move(retType), std::move(argTypes)));
	}

	antlrcpp::Any visitMap_type(ArgumentListParser::Map_typeContext* context) override
	{
		const auto keyType = visit(context->type(0)).as<ade_type_info>();
		const auto valueType = visit(context->type(1)).as<ade_type_info>();

		return ade_type_info(ade_type_map(keyType, valueType));
	}

	antlrcpp::Any visitIterator_type(ArgumentListParser::Iterator_typeContext* context) override
	{
		const auto valueType = visit(context->type()).as<ade_type_info>();

		return ade_type_info(ade_type_iterator(valueType));
	}

	antlrcpp::Any visitErrorNode(antlr4::tree::ErrorNode* /*node*/) override { return ade_type_info("<error type>"); }

  protected:
	antlrcpp::Any aggregateResult(antlrcpp::Any previous, const antlrcpp::Any& nextResult) override
	{
		// This happens while visiting terminals, ignore those
		if (nextResult.isNull()) {
			return previous;
		}

		if (previous.isNotNull()) {
			const auto& previousType = previous.as<ade_type_info>();
			const auto& nextType     = nextResult.as<ade_type_info>();
			return merge_alternatives(previousType, nextType);
		} else {
			return nextResult.as<ade_type_info>();
		}
	}
};

antlrcpp::Any scripting::ArglistVisitor::visitFunc_arg(ArgumentListParser::Func_argContext* context)
{
	TypeVisitor typeVisit;
	auto argType = context->type()->accept(&typeVisit).as<ade_type_info>();
	argType.setName(context->ID()->getText());

	return argType;
}

SCP_string getCommentContent(const SCP_string& content) {
	auto base = content.substr(2, content.length() - 4); // Strip leading and trailing delimiters

	drop_white_space(base);

	return base;
}

class ArgumentCollectorVisitor : public BaseVisitor {
	bool saw_optional = false;

  public:
	SCP_vector<argument_def> args;

	antlrcpp::Any visitArg_list(ArgumentListParser::Arg_listContext* context) override
	{
		visitChildren(context);
		return antlrcpp::Any();
	}
	antlrcpp::Any visitActual_argument(ArgumentListParser::Actual_argumentContext* context) override
	{
		argument_def argdef;
		argdef.optional = saw_optional;

		TypeVisitor typeVisit;
		const auto typeAny = context->type()->accept(&typeVisit);
		argdef.type        = typeAny.as<ade_type_info>();

		if (context->ID() != nullptr) {
			argdef.name = context->ID()->getText();
		}

		if (context->value() != nullptr) {
			ValueVisitor valueVisit;
			const auto valueAny = context->value()->accept(&valueVisit);
			argdef.def_val      = valueAny.as<SCP_string>();
			argdef.optional     = true;
		}

		if (context->ARG_COMMENT() != nullptr) {
			argdef.comment = getCommentContent(context->ARG_COMMENT()->getText());
		}

		args.push_back(std::move(argdef));

		if (context->argument()) {
			context->argument()->accept(this);
		}

		return antlrcpp::Any();
	}
	antlrcpp::Any visitOptional_argument(ArgumentListParser::Optional_argumentContext* context) override
	{
		saw_optional = true;

		visitChildren(context);
		return antlrcpp::Any();
	}
	antlrcpp::Any visitArgument(ArgumentListParser::ArgumentContext* context) override
	{
		visitChildren(context);
		return antlrcpp::Any();
	}
};

class TypeCheckVisitor : public BaseVisitor {
	ArgumentListParser* _parser = nullptr;
	const SCP_unordered_set<SCP_string>& _validTypeNames;

  public:
	TypeCheckVisitor(ArgumentListParser* parser, const SCP_unordered_set<SCP_string>& validTypeNames)
		: _parser(parser), _validTypeNames(validTypeNames)
	{
	}

	antlrcpp::Any visitSimple_type(ArgumentListParser::Simple_typeContext* context) override
	{
		// Nil is always valid
		if (context->NIL() != nullptr) {
			return antlrcpp::Any();
		}

		if (_validTypeNames.find(context->ID()->getText()) == _validTypeNames.end()) {
			_parser->notifyErrorListeners(context->ID()->getSymbol(),
				"Invalid type name <" + context->ID()->getText() + ">",
				nullptr);
		}

		return antlrcpp::Any();
	}
	antlrcpp::Any visitType(ArgumentListParser::TypeContext* context) override
	{
		return visitChildren(context);
	}
	antlrcpp::Any visitVarargs_or_simple_type(ArgumentListParser::Varargs_or_simple_typeContext* context) override
	{
		return visitChildren(context);
	}
	antlrcpp::Any visitStandalone_type(ArgumentListParser::Standalone_typeContext* context) override
	{
		return visitChildren(context);
	}
	antlrcpp::Any visitMap_type(ArgumentListParser::Map_typeContext* context) override
	{
		return visitChildren(context);
	}
	antlrcpp::Any visitIterator_type(ArgumentListParser::Iterator_typeContext* context) override
	{
		return visitChildren(context);
	}

	antlrcpp::Any visitFunc_arg(ArgumentListParser::Func_argContext* context) override
	{
		visit(context->type());
		return antlrcpp::Any();
	}
	antlrcpp::Any visitFunc_arglist(ArgumentListParser::Func_arglistContext* context) override
	{
		visitChildren(context);
		return antlrcpp::Any();
	}

	antlrcpp::Any visitFunction_type(ArgumentListParser::Function_typeContext* context) override
	{
		visitChildren(context);
		return antlrcpp::Any();
	}

	antlrcpp::Any visitActual_argument(ArgumentListParser::Actual_argumentContext* context) override
	{
		visit(context->type());

		// Now also look at the remaining arguments
		if (context->argument() != nullptr)
		{
			visit(context->argument());
		}
		return antlrcpp::Any();
	}

	antlrcpp::Any visitOptional_argument(ArgumentListParser::Optional_argumentContext* context) override
	{
		visitChildren(context);
		return antlrcpp::Any();
	}
	antlrcpp::Any visitArgument(ArgumentListParser::ArgumentContext* context) override
	{
		visitChildren(context);
		return antlrcpp::Any();
	}
	antlrcpp::Any visitArg_list(ArgumentListParser::Arg_listContext* context) override
	{
		visitChildren(context);
		return antlrcpp::Any();
	}
};

} // namespace

argument_list_parser::argument_list_parser(const SCP_vector<SCP_string>& validTypeNames)
	: _validTypeNames(validTypeNames.begin(), validTypeNames.end())
{
}

bool argument_list_parser::parse(const SCP_string& argumentList)
{
	antlr4::ANTLRInputStream input(argumentList);
	ArgumentListLexer lexer(&input);
	antlr4::CommonTokenStream tokens(&lexer);

	ArgumentListParser parser(&tokens);
	// By default we log to stderr which we do not want
	parser.removeErrorListeners();
	libs::antlr::ErrorListener errListener;
	parser.addErrorListener(&errListener);

	antlr4::tree::ParseTree* tree = parser.arg_list();

	TypeCheckVisitor typeChecker(&parser, _validTypeNames);
	tree->accept(&typeChecker);

	// If there were errors, output them
	if (!errListener.diagnostics.empty()) {
		SCP_stringstream outStream;
		for (const auto& diag : errListener.diagnostics) {
			SCP_string tokenUnderline;
			if (diag.tokenLength > 1) {
				tokenUnderline = SCP_string(diag.tokenLength - 1, '~');
			}
			outStream << argumentList << "\n"
					  << SCP_string(diag.columnInLine, ' ') << "^" << tokenUnderline << "\n"
					  << diag.errorMessage << "\n";
		}

		_errorMessage = outStream.str();
	} else {
		// Only look at the parameters if we know that we have valid input to avoid some of the error handling while
		// traversing the parse tree
		ArgumentCollectorVisitor argCollector;
		tree->accept(&argCollector);

		_argList = argCollector.args;
	}

	return errListener.diagnostics.empty();
}
const SCP_vector<scripting::argument_def>& argument_list_parser::getArgList() const
{
	return _argList;
}
const SCP_string& argument_list_parser::getErrorMessage() const
{
	return _errorMessage;
}

type_parser::type_parser(const SCP_vector<SCP_string>& validTypeNames)
	: _validTypeNames(validTypeNames.begin(), validTypeNames.end())
{
}
bool type_parser::parse(const SCP_string& type)
{
	antlr4::ANTLRInputStream input(type);
	ArgumentListLexer lexer(&input);
	antlr4::CommonTokenStream tokens(&lexer);

	ArgumentListParser parser(&tokens);
	// By default we log to stderr which we do not want
	parser.removeErrorListeners();
	libs::antlr::ErrorListener errListener;
	parser.addErrorListener(&errListener);

	// Tuple type is the entry point for a standalone type
	antlr4::tree::ParseTree* tree = parser.standalone_type();

	TypeCheckVisitor typeChecker(&parser, _validTypeNames);
	tree->accept(&typeChecker);

	// If there were errors, output them
	if (!errListener.diagnostics.empty()) {
		SCP_stringstream outStream;
		for (const auto& diag : errListener.diagnostics) {
			SCP_string tokenUnderline;
			if (diag.tokenLength > 1) {
				tokenUnderline = SCP_string(diag.tokenLength - 1, '~');
			}
			outStream << type << "\n"
					  << SCP_string(diag.columnInLine, ' ') << "^" << tokenUnderline << "\n"
					  << diag.errorMessage << "\n";
		}

		_errorMessage = outStream.str();
	} else {
		TypeVisitor typeVisitor;
		const auto typeAny = tree->accept(&typeVisitor);

		_parsedType = typeAny.as<ade_type_info>();
	}

	return errListener.diagnostics.empty();
}
const ade_type_info& type_parser::getType() const
{
	return _parsedType;
}
const SCP_string& type_parser::getErrorMessage() const
{
	return _errorMessage;
}

} // namespace scripting