/
IClosureKeyword.java
58 lines (53 loc) · 2 KB
/
IClosureKeyword.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
package com.laytonsmith.core.compiler.keywords;
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.CClassType;
import com.laytonsmith.core.constructs.CFunction;
import com.laytonsmith.core.constructs.CIClosure;
import com.laytonsmith.core.exceptions.ConfigCompileException;
import java.util.List;
/**
*
*/
@Keyword.keyword("iclosure")
public class IClosureKeyword extends Keyword {
@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
try {
if(list.get(keywordPosition).getData() instanceof CFunction) {
// It's a function, so do the old processing
SimpleBlockKeywordFunction.doProcess(this, null, true, list, keywordPosition);
// However, if we are proceeded by a ClassType, this is the return type of the closure, and it is
// easiest if we do the conversion here.
try {
if(list.get(keywordPosition - 1).getData() instanceof CClassType) {
ParseTree type = list.remove(keywordPosition - 1);
List<ParseTree> children = list.get(keywordPosition - 1).getChildren();
children.add(0, type);
list.get(keywordPosition - 1).setChildren(children);
return keywordPosition - 1;
}
} catch (IndexOutOfBoundsException ex) {
// Ignore, it's not a typed closure
}
return keywordPosition;
} else {
// Else it's standalone, so this should be treated as the closure ClassType
list.set(keywordPosition, new ParseTree(CIClosure.TYPE, list.get(keywordPosition).getFileOptions()));
return keywordPosition;
}
} catch (IndexOutOfBoundsException ex) {
throw new ConfigCompileException("Unexpected \"iclosure\" reference", list.get(keywordPosition).getTarget());
}
}
@Override
public String docs() {
return "Creates an iclosure (an isolated closure) with the given code.";
}
@Override
public Version since() {
return MSVersion.V3_3_1;
}
}