-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathParserState.java
135 lines (118 loc) · 4.71 KB
/
ParserState.java
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
package io.github.syst3ms.skriptparser.parsing;
import io.github.syst3ms.skriptparser.lang.CodeSection;
import io.github.syst3ms.skriptparser.lang.Statement;
import io.github.syst3ms.skriptparser.lang.SyntaxElement;
import io.github.syst3ms.skriptparser.lang.TriggerContext;
import io.github.syst3ms.skriptparser.util.Pair;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
/**
* An object that stores data about the current parsing, on the scale of the entire trigger.
*/
public class ParserState {
private Set<Class<? extends TriggerContext>> currentContexts = new HashSet<>();
private final LinkedList<CodeSection> currentSections = new LinkedList<>();
private final LinkedList<LinkedList<Statement>> currentStatements = new LinkedList<>();
private final LinkedList<Pair<Set<Class<? extends SyntaxElement>>, Boolean>> restrictions = new LinkedList<>();
private boolean isntAllowingSyntax = false;
{
currentStatements.add(new LinkedList<>());
restrictions.add(new Pair<>(Collections.emptySet(), false));
}
/**
* @return the {@link TriggerContext}s handled by the currently parsed event
*/
public Set<Class<? extends TriggerContext>> getCurrentContexts() {
return currentContexts;
}
/**
* Sets the {@link TriggerContext}s handled by the currently parsed event
* @param currentContexts the handled {@link TriggerContext}s
*/
public void setCurrentContexts(Set<Class<? extends TriggerContext>> currentContexts) {
this.currentContexts = currentContexts;
}
/**
* @return a list of all enclosing {@linkplain CodeSection}s, with the closest one first
*/
public LinkedList<CodeSection> getCurrentSections() {
return new LinkedList<>(currentSections);
}
/**
* Adds a new enclosing {@link CodeSection} to the hierarchy
* @param section the enclosing {@link CodeSection}
*/
public void addCurrentSection(CodeSection section) {
currentSections.addFirst(section);
}
/**
* Removes the current section from the hierarchy, after all parsing inside it has been completed.
*/
public void removeCurrentSection() {
currentSections.removeFirst();
}
/**
* Returns a list of all consecutive, successfully parsed {@linkplain Statement}s
* in the enclosing section.
* This is essentially a list with all previously parsed items of this section.
* @return a list of all {@linkplain Statement}s in the enclosing section.
*/
public LinkedList<Statement> getCurrentStatements() {
return currentStatements.getLast();
}
/**
* Adds a new {@link Statement} to the items of the enclosing section.
* @param statement the enclosing {@link Statement}
*/
public void addCurrentStatement(Statement statement) {
currentStatements.getLast().add(statement);
}
/**
* Uses recursion to allow items of a new enclosing section to be added, preserving
* the current items to be used when the {@linkplain #callbackCurrentStatements() callback}
* has been invoked.
*/
public void recurseCurrentStatements() {
currentStatements.addLast(new LinkedList<>());
}
/**
* Clears all stored items of this enclosing section,
* after all parsing inside it has been completed.
*/
public void callbackCurrentStatements() {
currentStatements.removeLast();
}
/**
* Define the syntax restrictions enforced by the current section
* @param allowedSyntaxes all allowed syntaxes
* @param restrictingExpressions whether expressions are also restricted
*/
public void setSyntaxRestrictions(Set<Class<? extends SyntaxElement>> allowedSyntaxes, boolean restrictingExpressions) {
if (allowedSyntaxes == null) isntAllowingSyntax = true;
restrictions.addLast(new Pair<>(allowedSyntaxes, restrictingExpressions));
}
/**
* Clears the previously enforced syntax restrictions
*/
public void clearSyntaxRestrictions() {
isntAllowingSyntax = false;
restrictions.removeLast();
}
/**
* @param c the class of the syntax
* @return whether the current syntax restrictions forbid a given syntax or not
*/
public boolean forbidsSyntax(Class<? extends SyntaxElement> c) {
if (isntAllowingSyntax) return true;
var allowedSyntaxes = restrictions.getLast().getFirst();
return !allowedSyntaxes.isEmpty() && !allowedSyntaxes.contains(c);
}
/**
* @return whether the current syntax restrictions also apply to expressions
*/
public boolean isRestrictingExpressions() {
return restrictions.getLast().getSecond();
}
}