-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSyntaxParser.java
More file actions
734 lines (696 loc) · 37.9 KB
/
SyntaxParser.java
File metadata and controls
734 lines (696 loc) · 37.9 KB
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
package io.github.syst3ms.skriptparser.parsing;
import io.github.syst3ms.skriptparser.expressions.ExprBooleanOperators;
import io.github.syst3ms.skriptparser.file.FileSection;
import io.github.syst3ms.skriptparser.lang.*;
import io.github.syst3ms.skriptparser.lang.base.ConditionalExpression;
import io.github.syst3ms.skriptparser.lang.base.ContextExpression;
import io.github.syst3ms.skriptparser.log.ErrorContext;
import io.github.syst3ms.skriptparser.log.ErrorType;
import io.github.syst3ms.skriptparser.log.SkriptLogger;
import io.github.syst3ms.skriptparser.pattern.PatternElement;
import io.github.syst3ms.skriptparser.pattern.PatternParser;
import io.github.syst3ms.skriptparser.registration.ExpressionInfo;
import io.github.syst3ms.skriptparser.registration.SkriptEventInfo;
import io.github.syst3ms.skriptparser.registration.SyntaxInfo;
import io.github.syst3ms.skriptparser.registration.SyntaxManager;
import io.github.syst3ms.skriptparser.registration.context.ContextValue;
import io.github.syst3ms.skriptparser.registration.context.ContextValue.State;
import io.github.syst3ms.skriptparser.registration.context.ContextValues;
import io.github.syst3ms.skriptparser.types.PatternType;
import io.github.syst3ms.skriptparser.types.Type;
import io.github.syst3ms.skriptparser.types.TypeManager;
import io.github.syst3ms.skriptparser.types.conversions.Converters;
import io.github.syst3ms.skriptparser.util.ClassUtils;
import io.github.syst3ms.skriptparser.util.RecentElementList;
import io.github.syst3ms.skriptparser.util.StringUtils;
import io.github.syst3ms.skriptparser.variables.Variables;
import org.intellij.lang.annotations.MagicConstant;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.regex.Pattern;
/**
* Contains the logic for parsing and interpreting single statements, sections and expressions inside of a script.
*/
@SuppressWarnings("unchecked")
public class SyntaxParser {
/**
* Tells {@link #parseBooleanExpression(String, int, ParserState, SkriptLogger)} to only return expressions that are not conditional
* @see #parseBooleanExpression(String, int, ParserState, SkriptLogger)
*/
public static final int NOT_CONDITIONAL = 0;
/**
* Tells {@link #parseBooleanExpression(String, int, ParserState, SkriptLogger)} to return any expressions, conditional or not
* @see #parseBooleanExpression(String, int, ParserState, SkriptLogger)
*/
public static final int MAYBE_CONDITIONAL = 1;
/**
* Tells {@link #parseBooleanExpression(String, int, ParserState, SkriptLogger)} to only return conditional expressions
* @see #parseBooleanExpression(String, int, ParserState, SkriptLogger)
*/
public static final int CONDITIONAL = 2;
public static final Pattern LIST_SPLIT_PATTERN = Pattern.compile("\\s*(,)\\s*|\\s+(and|n?or)\\s+", Pattern.CASE_INSENSITIVE);
/**
* The pattern type representing {@link Boolean}
*/
public static final PatternType<Boolean> BOOLEAN_PATTERN_TYPE = new PatternType<>((Type<Boolean>) TypeManager.getByClass(Boolean.class).orElseThrow(AssertionError::new), true);
/**
* The pattern type representing {@link Object}
*/
// Gradle requires the cast, but IntelliJ considers it redundant
public static final PatternType<Object> OBJECT_PATTERN_TYPE = new PatternType<>((Type<Object>) TypeManager.getByClass(Object.class).orElseThrow(AssertionError::new), true);
// Gradle requires the cast, but IntelliJ considers it redundant
public static final PatternType<Object> OBJECTS_PATTERN_TYPE = new PatternType<>((Type<Object>) TypeManager.getByClass(Object.class).orElseThrow(AssertionError::new), false);
// We control input, so new SkriptLogger instance is fine
public static final PatternElement CONTEXT_VALUE_PATTERN = PatternParser.parsePattern("[the] (0:(past|previous)|1:|2:(future|next)) [ctx:context-]<.+>", new SkriptLogger()).orElseThrow();
public static final ExpressionInfo<ExprBooleanOperators, Boolean> EXPRESSION_BOOLEAN_OPERATORS = SyntaxManager.getAllExpressions().stream()
.filter(i -> i.getSyntaxClass() == ExprBooleanOperators.class)
.findFirst()
.map(val -> (ExpressionInfo<ExprBooleanOperators, Boolean>) val)
.orElseThrow();
/**
* All {@link Effect effects} that are successfully parsed during parsing, in order of last successful parsing
*/
private static final RecentElementList<SyntaxInfo<? extends Effect>> recentEffects = new RecentElementList<>();
/**
* All {@link CodeSection sections} that are successfully parsed during parsing, in order of last successful parsing
*/
private static final RecentElementList<SyntaxInfo<? extends CodeSection>> recentSections = new RecentElementList<>();
/**
* All {@link SkriptEvent events} that are successfully parsed during parsing, in order of last successful parsing
*/
private static final RecentElementList<SkriptEventInfo<?>> recentEvents = new RecentElementList<>();
/**
* All {@link Expression expressions} that are successfully parsed during parsing, in order of last successful parsing
*/
private static final RecentElementList<ExpressionInfo<?, ?>> recentExpressions = new RecentElementList<>();
/**
* All {@link ConditionalExpression conditions} that are successfully parsed during parsing, in order of last successful parsing
*/
private static final RecentElementList<ExpressionInfo<? extends ConditionalExpression, ? extends Boolean>> recentConditions = new RecentElementList<>();
/**
* All {@link ContextValue context values} that are successfully parsed during parsing, in order of last successful parsing
*/
private static final RecentElementList<ContextValue<?, ?>> recentContextValues = new RecentElementList<>();
/**
* Parses an {@link Expression} from the given {@linkplain String} and {@link PatternType expected return type}
* @param <T> the type of the expression
* @param s the string to be parsed as an expression
* @param expectedType the expected return type
* @param parserState the current parser state
* @param logger the logger
* @return an expression that was successfully parsed, or {@literal null} if the string is empty,
* no match was found
* or for another reason detailed in an error message.
*/
public static <T> Optional<? extends Expression<? extends T>> parseExpression(String s, PatternType<T> expectedType, ParserState parserState, SkriptLogger logger) {
if (s.isEmpty())
return Optional.empty();
if (s.startsWith("(") && s.endsWith(")") && StringUtils.findClosingIndex(s, '(', ')', 0) == s.length() - 1) {
s = s.substring(1, s.length() - 1);
}
var literal = parseLiteral(s, expectedType, parserState, logger);
if (literal.isPresent()) {
logger.clearErrors();
return literal;
}
var variable = (Optional<? extends Variable<? extends T>>) Variables.parseVariable(s, expectedType.getType().getTypeClass(), parserState, logger);
if (variable.isPresent()) {
if (variable.filter(v -> !v.isSingle() && expectedType.isSingle()).isPresent()) {
logger.error(
"A single value was expected, but " +
s +
" represents multiple values.",
ErrorType.SEMANTIC_ERROR,
"Use a loop/map to divert each element of this list into single elements"
);
return Optional.empty();
} else {
return variable;
}
}
// This is to prevent us from exchanging boolean operators with lists.
if (s.toLowerCase().startsWith("list ")) {
s = s.substring("list ".length());
} else {
// We parse boolean operators first to prevent clutter while parsing.
var booleanOperator = matchExpressionInfo(s, EXPRESSION_BOOLEAN_OPERATORS, expectedType, parserState, logger);
if (booleanOperator.isPresent()) {
recentExpressions.acknowledge(EXPRESSION_BOOLEAN_OPERATORS);
logger.clearErrors();
return booleanOperator;
}
}
if (!expectedType.isSingle()) {
var listLiteral = parseListLiteral(s, expectedType, parserState, logger);
if (listLiteral.isPresent()) {
logger.clearErrors();
return listLiteral;
}
}
for (var info : recentExpressions.mergeWith(SyntaxManager.getAllExpressions())) {
var expr = matchExpressionInfo(s, info, expectedType, parserState, logger);
if (expr.isPresent()) {
if (parserState.isRestrictingExpressions() && parserState.forbidsSyntax(expr.get().getClass())) {
logger.setContext(ErrorContext.RESTRICTED_SYNTAXES);
logger.error("The enclosing code section does not allow the use of this expression: " + expr.get().toString(TriggerContext.DUMMY, logger.isDebug()), ErrorType.SEMANTIC_ERROR);
return Optional.empty();
}
recentExpressions.acknowledge(info);
logger.clearErrors();
return expr;
}
logger.forgetError();
}
// Parsing of standalone context values
var contextValue = parseContextValue(s, expectedType, parserState, logger);
if (contextValue.isPresent()) {
logger.clearErrors();
return contextValue;
}
logger.setContext(ErrorContext.NO_MATCH);
logger.error("No expression matching '" + s + "' was found", ErrorType.NO_MATCH);
return Optional.empty();
}
/**
* Parses a {@link Expression boolean expression} from the given {@linkplain String}
* @param s the string to be parsed as an expression
* @param conditional a constant describing whether the result can be a {@link ConditionalExpression condition}
* @param parserState the current parser state
* @param logger the logger
* @see SyntaxParser#NOT_CONDITIONAL
* @see SyntaxParser#MAYBE_CONDITIONAL
* @see SyntaxParser#CONDITIONAL
* @return a boolean expression that was successfully parsed, or {@literal null} if the string is empty,
* no match was found
* or for another reason detailed in an error message.
*/
public static Optional<? extends Expression<Boolean>> parseBooleanExpression(String s, @MagicConstant(intValues = {NOT_CONDITIONAL, MAYBE_CONDITIONAL, CONDITIONAL}) int conditional, ParserState parserState, SkriptLogger logger) {
// I swear this is the cleanest way to do it
if (s.startsWith("(") && s.endsWith(")") && StringUtils.findClosingIndex(s, '(', ')', 0) == s.length() - 1) {
s = s.substring(1, s.length() - 1);
}
if (s.equalsIgnoreCase("true")) {
return Optional.of(new SimpleLiteral<>(Boolean.class, true));
} else if (s.equalsIgnoreCase("false")) {
return Optional.of(new SimpleLiteral<>(Boolean.class, false));
}
var variable = (Optional<? extends Variable<Boolean>>) Variables.parseVariable(s, Boolean.class, parserState, logger);
if (variable.isPresent()) {
if (variable.filter(v -> !v.isSingle()).isPresent()) {
logger.error(
"A single value was expected, but " +
s +
" represents multiple values.",
ErrorType.SEMANTIC_ERROR,
"Use a loop/map to divert each element of this list into single elements"
);
return Optional.empty();
} else {
return variable;
}
}
for (var info : recentExpressions.mergeWith(SyntaxManager.getAllExpressions())) {
if (info.getReturnType().getType().getTypeClass() != Boolean.class)
continue;
var expr = (Optional<? extends Expression<Boolean>>) matchExpressionInfo(s, info, BOOLEAN_PATTERN_TYPE, parserState, logger);
if (expr.isPresent()) {
switch (conditional) {
case NOT_CONDITIONAL: // Can't be conditional
if (ConditionalExpression.class.isAssignableFrom(expr.get().getClass())) {
logger.error(
"The boolean expression must not be conditional",
ErrorType.SEMANTIC_ERROR,
"Rather than a condition, use a simple boolean here. Use 'whether %=boolean%' to convert a condition into a simple boolean"
);
return Optional.empty();
}
break;
case MAYBE_CONDITIONAL: // Can be conditional
if (ConditionalExpression.class.isAssignableFrom(expr.get().getClass())) {
recentConditions.acknowledge((ExpressionInfo<? extends ConditionalExpression, ? extends Boolean>) info);
}
case CONDITIONAL: // Has to be conditional
if (!ConditionalExpression.class.isAssignableFrom(expr.get().getClass())) {
logger.error(
"The boolean expression must be conditional",
ErrorType.SEMANTIC_ERROR,
"Rather than a simple boolean, use a condition here, like '{x} is more than 10'"
);
return Optional.empty();
}
default: // You just want me dead, don't you ?
break;
}
recentExpressions.acknowledge(info);
logger.clearErrors();
return expr;
}
logger.forgetError();
}
logger.setContext(ErrorContext.NO_MATCH);
logger.error("No expression matching '" + s + "' was found", ErrorType.NO_MATCH);
return Optional.empty();
}
/**
* Parses a {@link ContextValue} from the given {@linkplain String} and {@link PatternType expected return type}
* @param <T> the type of the context value
* @param toParse the string to be parsed as a context value
* @param expectedType the expected return type
* @param parserState the current parser state
* @param logger the logger
* @return an expression that was successfully parsed, or {@literal null} if the string is empty,
* no match was found
* or for another reason detailed in an error message.
*/
public static <T> Optional<? extends ContextExpression<?, ? extends T>> parseContextValue(String toParse, PatternType<T> expectedType, ParserState parserState, SkriptLogger logger) {
var matchContext = new MatchContext(CONTEXT_VALUE_PATTERN, parserState, logger);
CONTEXT_VALUE_PATTERN.match(toParse, 0, matchContext);
var parseContext = matchContext.toParseResult();
var state = State.values()[parseContext.getNumericMark()];
boolean alone = !parseContext.hasMark("ctx");
var value = parseContext.getMatches().get(0).group();
for (Class<? extends TriggerContext> ctx : parseContext.getParserState().getCurrentContexts()) {
for (var info : recentContextValues.mergeWith(ContextValues.getContextValues(ctx))) {
matchContext = new MatchContext(info.getPattern(), parserState, logger);
// Checking all conditions, so no false results slip through.
if (info.getPattern().match(value, 0, matchContext) == -1 || !expectedType.getType().getTypeClass().isAssignableFrom(info.getReturnType().getType().getTypeClass())) {
continue;
} else if (!info.getUsage().isCorrect(alone)) {
if (alone) {
logger.error(
"The context value matching '" + toParse + "' cannot be used alone",
ErrorType.SEMANTIC_ERROR,
"Use 'context-something' instead of using this context value alone"
);
} else {
logger.error(
"The context value matching '" + toParse + "' must be used alone",
ErrorType.SEMANTIC_ERROR,
"Instead of 'context-something', use this context value as 'something'"
);
}
return Optional.empty();
} else if (state != info.getState()) {
logger.error("The time state of this context value (" + state.toString().toLowerCase() + ") is incorrect", ErrorType.SEMANTIC_ERROR);
continue;
} else if (expectedType.isSingle() && !info.getReturnType().isSingle()) {
logger.error(
"A single value was expected, but " + toParse + " represents multiple values.",
ErrorType.SEMANTIC_ERROR,
"Use a loop/map to divert each element of this list into single elements"
);
return Optional.empty();
} else if (parserState.isRestrictingExpressions() && parserState.forbidsSyntax(ContextExpression.class)) {
logger.setContext(ErrorContext.RESTRICTED_SYNTAXES);
logger.error("The enclosing code section does not allow the use of context expressions.", ErrorType.SEMANTIC_ERROR);
return Optional.empty();
}
recentContextValues.acknowledge(info);
return Optional.of(new ContextExpression<>((ContextValue<?, T>) info, value, alone));
}
}
return Optional.empty();
}
private static <T> Optional<? extends Expression<? extends T>> matchExpressionInfo(String s, ExpressionInfo<?, ?> info, PatternType<T> expectedType, ParserState parserState, SkriptLogger logger) {
var patterns = info.getPatterns();
var infoType = info.getReturnType();
var infoTypeClass = infoType.getType().getTypeClass();
var expectedTypeClass = expectedType.getType().getTypeClass();
if (!expectedTypeClass.isAssignableFrom(infoTypeClass) && !Converters.converterExists(infoTypeClass, expectedTypeClass))
return Optional.empty();
for (var i = 0; i < patterns.size(); i++) {
var element = patterns.get(i);
logger.setContext(ErrorContext.MATCHING);
var parser = new MatchContext(element, parserState, logger);
if (element.match(s, 0, parser) == s.length()) {
try {
var expression = (Expression<? extends T>) info.getSyntaxClass()
.getDeclaredConstructor()
.newInstance();
logger.setContext(ErrorContext.INITIALIZATION);
if (!expression.init(
parser.getParsedExpressions().toArray(new Expression[0]),
i,
parser.toParseResult()
)) {
continue;
}
logger.setContext(ErrorContext.CONSTRAINT_CHECKING);
Class<?> expressionReturnType = expression.getReturnType();
if (!expectedTypeClass.isAssignableFrom(expressionReturnType)) { // Would only screw up in case of bad dynamic type usage
var converted = expression.convertExpression(expectedTypeClass);
if (converted.isPresent()) {
return converted;
} else {
var type = TypeManager.getByClass(expressionReturnType);
assert type.isPresent();
logger.error(StringUtils.withIndefiniteArticle(expectedType.toString(), false) +
" was expected, but " +
StringUtils.withIndefiniteArticle(type.get().toString(), false) +
" was found", ErrorType.SEMANTIC_ERROR);
return Optional.empty();
}
}
if (!expression.isSingle() &&
expectedType.isSingle()) {
logger.error(
"A single value was expected, but '" + s + "' represents multiple values.",
ErrorType.SEMANTIC_ERROR,
"Use a loop/map to divert each element of this list into single elements"
);
continue;
}
if (parserState.isRestrictingExpressions() && parserState.forbidsSyntax(expression.getClass())) {
logger.setContext(ErrorContext.RESTRICTED_SYNTAXES);
logger.error(
"The enclosing code section does not allow the use of this expression: "
+ expression.toString(TriggerContext.DUMMY, logger.isDebug()),
ErrorType.SEMANTIC_ERROR,
"The current section limits the usage of syntax. This means that certain syntax cannot be used here, which was the case. Remove this expression entirely and refer to the documentation for the correct usage of this section"
);
continue;
}
return Optional.of(expression);
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
logger.error("Couldn't instantiate class '" + info.getSyntaxClass().getName() + "'", ErrorType.EXCEPTION);
}
}
}
return Optional.empty();
}
/**
* Parses a list literal expression (of the form {@code ..., ... and ...}) from the given {@linkplain String} and {@link PatternType expected return type}
* @param <T> the type of the list literal
* @param s the string to be parsed as a list literal
* @param expectedType the expected return type (must be plural)
* @param parserState the current parser state
* @param logger the logger
* @return a list literal that was successfully parsed, or {@literal null} if the string is empty,
* no match was found
* or for another reason detailed in an error message.
*/
public static <T> Optional<? extends Expression<? extends T>> parseListLiteral(String s, PatternType<T> expectedType, ParserState parserState, SkriptLogger logger) {
assert !expectedType.isSingle();
if (!s.contains(",") && !s.contains("and") && !s.contains("nor") && !s.contains("or"))
return Optional.empty();
List<String> parts = new ArrayList<>();
var m = LIST_SPLIT_PATTERN.matcher(s);
var lastIndex = 0;
for (var i = 0; i < s.length(); i = StringUtils.nextSimpleCharacterIndex(s, i + 1)) {
if (i == -1) {
return Optional.empty();
} else if (StringUtils.nextSimpleCharacterIndex(s, i) > i) { // We are currently at the start of something we need to skip over
i = StringUtils.nextSimpleCharacterIndex(s, i) - 1;
continue;
}
var c = s.charAt(i);
if (c == ' ' || c == ',') {
m.region(i, s.length());
if (m.lookingAt()) {
if (i == lastIndex)
return Optional.empty();
parts.add(s.substring(lastIndex, i));
parts.add(m.group());
i = m.end() - 1;
lastIndex = i;
}
} else if (c == '(') {
var closing = StringUtils.getEnclosedText(s, '(', ')', i);
if (closing.isPresent()) {
var finalI = i; // Lambdas require it
i = closing.map(cl -> finalI + cl.length() + 1).orElse(i);
}
}
}
if (lastIndex < s.length() - 1)
parts.add(s.substring(lastIndex));
if (parts.size() == 1)
return Optional.empty();
Boolean isAndList = null; // Hello nullable booleans, it had been a pleasure NOT using you
for (var i = 0; i < parts.size(); i++) {
if ((i & 1) == 1) { // Odd index == separator
var separator = parts.get(i).strip();
if (separator.equalsIgnoreCase("and") || separator.equalsIgnoreCase("nor")) {
isAndList = true;
} else if (separator.equalsIgnoreCase("or")) {
isAndList = isAndList != null && isAndList;
}
}
}
isAndList = isAndList == null || isAndList; // Defaults to true
List<Expression<? extends T>> expressions = new ArrayList<>();
var isLiteralList = true;
for (var i = 0; i < parts.size(); i++) {
if ((i & 1) == 0) { // Even index == element
var part = parts.get(i).strip();
logger.recurse();
var expression = parseExpression(part, expectedType, parserState, logger);
logger.callback();
if (expression.isEmpty())
return Optional.empty();
isLiteralList &= Literal.isLiteral(expression.get());
expressions.add(expression.get());
}
}
if (expressions.size() == 1)
return Optional.of(expressions.get(0));
if (isLiteralList) {
Literal<?>[] literals = new Literal[expressions.size()];
for (var i = 0; i < expressions.size(); i++) {
var exp = expressions.get(i);
if (exp instanceof Literal) {
literals[i] = (Literal<?>) exp;
} else {
assert exp instanceof VariableString;
literals[i] = new SimpleLiteral<>(String.class, (String) exp.getSingle(TriggerContext.DUMMY).orElseThrow(AssertionError::new));
}
}
var returnType = ClassUtils.getCommonSuperclass(false, Arrays.stream(literals).map(Literal::getReturnType).toArray(Class[]::new));
return Optional.of(new LiteralList<>(
(Literal<? extends T>[]) literals,
(Class<T>) returnType,
isAndList
));
} else {
Expression<?>[] exprs = expressions.toArray(new Expression[0]);
var returnType = ClassUtils.getCommonSuperclass(false, Arrays.stream(exprs).map(Expression::getReturnType).toArray(Class[]::new));
return Optional.of(new ExpressionList<>(
(Expression<? extends T>[]) exprs,
(Class<T>) returnType,
isAndList
));
}
}
/**
* Parses a literal of a given {@link PatternType type} from the given {@linkplain String}
* @param <T> the type of the literal
* @param s the string to be parsed as a literal
* @param expectedType the expected return type
* @param parserState the current parser state
* @param logger the logger
* @return a literal that was successfully parsed, or {@literal null} if the string is empty,
* no match was found
* or for another reason detailed in an error message.
*/
public static <T> Optional<? extends Expression<? extends T>> parseLiteral(String s, PatternType<T> expectedType, ParserState parserState, SkriptLogger logger) {
var classToTypeMap = TypeManager.getClassToTypeMap();
for (var c : classToTypeMap.keySet()) {
Class<? extends T> expectedClass = expectedType.getType().getTypeClass();
if (expectedClass.isAssignableFrom(c) || Converters.converterExists(c, expectedClass)) {
Optional<? extends Function<String, ?>> literalParser = classToTypeMap.get(c).getLiteralParser();
if (literalParser.isPresent()) {
var literal = literalParser.map(l -> (T) l.apply(s));
if (literal.isPresent() && expectedClass.isAssignableFrom(c)) {
return Optional.of(new SimpleLiteral<>((Class<T>) literal.get().getClass(), literal.get()));
} else if (literal.isPresent()) {
return new SimpleLiteral<>((Class<T>) c, literal.get()).convertExpression(expectedType.getType().getTypeClass());
}
} else if (expectedClass == String.class || c == String.class) {
var vs = VariableString.newInstanceWithQuotes(s, parserState, logger)
.map(v -> (Expression<? extends T>) v);
if (vs.isPresent()) {
return vs;
}
}
}
}
return Optional.empty();
}
/**
* Parses a line of code as an {@link Effect}
* @param s the line to be parsed
* @param parserState the current parser state
* @param logger the logger
* @return an effect that was successfully parsed, or {@literal null} if the string is empty,
* no match was found
* or for another reason detailed in an error message
*/
public static Optional<? extends Effect> parseEffect(String s, ParserState parserState, SkriptLogger logger) {
if (s.isEmpty())
return Optional.empty();
for (var recentEffect : recentEffects.mergeWith(SyntaxManager.getEffects())) {
var eff = matchEffectInfo(s, recentEffect, parserState, logger);
if (eff.isPresent()) {
if (parserState.forbidsSyntax(eff.get().getClass())) {
logger.setContext(ErrorContext.RESTRICTED_SYNTAXES);
logger.error("The enclosing code section does not allow the use of this effect: " + eff.get().toString(TriggerContext.DUMMY, logger.isDebug()), ErrorType.SEMANTIC_ERROR);
return Optional.empty();
}
recentEffects.acknowledge(recentEffect);
logger.clearErrors();
return eff;
}
logger.forgetError();
}
logger.setContext(ErrorContext.NO_MATCH);
logger.error("No effect matching '" + s + "' was found", ErrorType.NO_MATCH);
return Optional.empty();
}
private static Optional<? extends Effect> matchEffectInfo(String s, SyntaxInfo<? extends Effect> info, ParserState parserState, SkriptLogger logger) {
var patterns = info.getPatterns();
for (var i = 0; i < patterns.size(); i++) {
var element = patterns.get(i);
logger.setContext(ErrorContext.MATCHING);
var parser = new MatchContext(element, parserState, logger);
if (element.match(s, 0, parser) == s.length()) {
try {
var eff = info.getSyntaxClass()
.getDeclaredConstructor()
.newInstance();
logger.setContext(ErrorContext.INITIALIZATION);
if (!eff.init(
parser.getParsedExpressions().toArray(new Expression[0]),
i,
parser.toParseResult()
)) {
continue;
}
return Optional.of(eff);
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
logger.error("Couldn't instantiate class " + info.getSyntaxClass(), ErrorType.EXCEPTION);
}
}
}
return Optional.empty();
}
/**
* Parses a section of a file as a {@link CodeSection}
* @param section the section to be parsed
* @param parserState the current parser state
* @param logger the logger
* @return a section that was successfully parsed, or {@literal null} if the section is empty,
* no match was found or for another reason detailed in an error message
*/
public static Optional<? extends CodeSection> parseSection(FileSection section, ParserState parserState, SkriptLogger logger) {
var content = section.getLineContent();
if (content.isEmpty())
return Optional.empty();
for (var toParse : recentSections.mergeWith(SyntaxManager.getSections())) {
var sec = matchSectionInfo(section, toParse, parserState, logger);
if (sec.isPresent()) {
if (parserState.forbidsSyntax(sec.get().getClass())) {
logger.setContext(ErrorContext.RESTRICTED_SYNTAXES);
logger.error("The enclosing code section does not allow the use of this section: " + sec.get().toString(TriggerContext.DUMMY, logger.isDebug()), ErrorType.SEMANTIC_ERROR);
return Optional.empty();
}
recentSections.acknowledge(toParse);
logger.clearErrors();
return sec;
}
logger.forgetError();
}
logger.setContext(ErrorContext.NO_MATCH);
logger.error("No section matching '" + content + "' was found", ErrorType.NO_MATCH);
return Optional.empty();
}
private static Optional<? extends CodeSection> matchSectionInfo(FileSection section, SyntaxInfo<? extends CodeSection> info, ParserState parserState, SkriptLogger logger) {
var patterns = info.getPatterns();
for (var i = 0; i < patterns.size(); i++) {
var element = patterns.get(i);
logger.setContext(ErrorContext.MATCHING);
var parser = new MatchContext(element, parserState, logger);
if (element.match(section.getLineContent(), 0, parser) != -1) {
try {
var sec = info.getSyntaxClass()
.getDeclaredConstructor()
.newInstance();
logger.setContext(ErrorContext.INITIALIZATION);
if (!sec.init(
parser.getParsedExpressions().toArray(Expression[]::new),
i,
parser.toParseResult())) {
continue;
}
if (!sec.loadSection(section, parserState, logger)) {
continue;
}
return Optional.of(sec);
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
logger.error("Couldn't instantiate class " + info.getSyntaxClass(), ErrorType.EXCEPTION);
}
}
}
return Optional.empty();
}
/**
* Parses a section of a file as a {@link Trigger}
* @param section the section to be parsed
* @param logger the logger
* @return a trigger that was successfully parsed, or {@literal null} if the section is empty,
* no match was found
* or for another reason detailed in an error message
*/
public static Optional<? extends UnloadedTrigger> parseTrigger(FileSection section, SkriptLogger logger) {
if (section.getLineContent().isEmpty())
return Optional.empty();
for (var info : recentEvents.mergeWith(SyntaxManager.getEvents())) {
var trigger = matchEventInfo(section, info, logger);
if (trigger.isPresent()) {
recentEvents.acknowledge(info);
logger.clearErrors();
return trigger;
}
logger.forgetError();
}
logger.setContext(ErrorContext.NO_MATCH);
logger.error("No trigger matching '" + section.getLineContent() + "' was found", ErrorType.NO_MATCH);
return Optional.empty();
}
private static Optional<? extends UnloadedTrigger> matchEventInfo(FileSection section, SkriptEventInfo<?> info, SkriptLogger logger) {
var patterns = info.getPatterns();
for (var i = 0; i < patterns.size(); i++) {
var element = patterns.get(i);
var parserState = new ParserState();
logger.setContext(ErrorContext.MATCHING);
var parser = new MatchContext(element, parserState, logger);
if (element.match(section.getLineContent(), 0, parser) != -1) {
try {
var event = info.getSyntaxClass()
.getDeclaredConstructor()
.newInstance();
logger.setContext(ErrorContext.INITIALIZATION);
if (!event.init(
parser.getParsedExpressions().toArray(new Expression[0]),
i,
parser.toParseResult()
)) {
continue;
}
var trig = new Trigger(event);
parserState.setCurrentContexts(info.getContexts());
/*
* We don't actually load the trigger here, that will be left to the loading priority system
*/
return Optional.of(new UnloadedTrigger(trig, section, logger.getLine(), info, parserState));
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
logger.error("Couldn't instantiate class " + info.getSyntaxClass(), ErrorType.EXCEPTION);
}
}
}
return Optional.empty();
}
}