/
ForKeyword.java
63 lines (56 loc) · 2.08 KB
/
ForKeyword.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
package com.laytonsmith.core.compiler.keywords;
import java.util.List;
import com.laytonsmith.PureUtilities.Version;
import com.laytonsmith.core.MSVersion;
import com.laytonsmith.core.ParseTree;
import com.laytonsmith.core.compiler.Keyword;
import com.laytonsmith.core.constructs.CFunction;
import com.laytonsmith.core.constructs.CKeyword;
import com.laytonsmith.core.constructs.Target;
import com.laytonsmith.core.exceptions.ConfigCompileException;
import com.laytonsmith.core.functions.Function;
/**
*
*/
@Keyword.keyword("for")
public class ForKeyword extends Keyword {
@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
ParseTree forTree = list.get(keywordPosition);
Target t = forTree.getTarget();
if(list.size() > keywordPosition + 1) {
// This portion handles the initial code block, i.e. foreach(...){ }
ParseTree codeBlock = list.get(keywordPosition + 1);
if(isCodeBlock(codeBlock)) {
validateCodeBlock(codeBlock, "");
forTree.addChild(getArgumentOrNull(codeBlock));
list.remove(keywordPosition + 1);
}
}
if(list.size() > keywordPosition + 1) {
// This part handles the else keyword, i.e. foreach(...){ } else { }
ParseTree elseKeyword = list.get(keywordPosition + 1);
// If it's not an else keyword, then we'll leave it alone, and be done.
if(elseKeyword.getData() instanceof CKeyword && elseKeyword.getData().val().equals("else")) {
list.remove(keywordPosition + 1);
ParseTree codeBlock = list.get(keywordPosition + 1);
if(isCodeBlock(codeBlock)) {
validateCodeBlock(codeBlock, "");
forTree.addChild(getArgumentOrNull(codeBlock));
}
// We also have to refactor this into a foreachelse, instead of a foreach.
list.get(keywordPosition).setData(new CFunction(Function.FORELSE, t));
list.remove(keywordPosition + 1);
}
}
return keywordPosition;
}
@Override
public String docs() {
return "Creates a block that loops over the given inputs, running the code in the block as appropriate.";
}
@Override
public Version since() {
return MSVersion.V3_3_1;
}
}