|
@@ -0,0 +1,124 @@
|
|
1
|
+/*
|
|
2
|
+ * To change this license header, choose License Headers in Project Properties.
|
|
3
|
+ * To change this template file, choose Tools | Templates
|
|
4
|
+ * and open the template in the editor.
|
|
5
|
+ */
|
|
6
|
+package org.openzen.zenscript.parser;
|
|
7
|
+
|
|
8
|
+import org.openzen.zencode.shared.CodePosition;
|
|
9
|
+import org.openzen.zencode.shared.CompileException;
|
|
10
|
+import org.openzen.zenscript.codemodel.OperatorType;
|
|
11
|
+import org.openzen.zenscript.codemodel.expression.CallArguments;
|
|
12
|
+import org.openzen.zenscript.codemodel.expression.CallStaticExpression;
|
|
13
|
+import org.openzen.zenscript.codemodel.expression.Expression;
|
|
14
|
+import org.openzen.zenscript.codemodel.member.ref.FunctionalMemberRef;
|
|
15
|
+import org.openzen.zenscript.codemodel.partial.IPartialExpression;
|
|
16
|
+import org.openzen.zenscript.codemodel.scope.ExpressionScope;
|
|
17
|
+import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
|
|
18
|
+import org.openzen.zenscript.codemodel.type.StringTypeID;
|
|
19
|
+import org.openzen.zenscript.codemodel.type.TypeID;
|
|
20
|
+import org.openzen.zenscript.lexer.ParseException;
|
|
21
|
+import org.openzen.zenscript.lexer.ZSToken;
|
|
22
|
+import org.openzen.zenscript.lexer.ZSTokenParser;
|
|
23
|
+import org.openzen.zenscript.lexer.ZSTokenType;
|
|
24
|
+import org.openzen.zenscript.parser.expression.ParsedExpression;
|
|
25
|
+import org.openzen.zenscript.parser.expression.ParsedExpressionBinary;
|
|
26
|
+import org.openzen.zenscript.parser.expression.ParsedExpressionString;
|
|
27
|
+
|
|
28
|
+import java.util.ArrayList;
|
|
29
|
+import java.util.List;
|
|
30
|
+
|
|
31
|
+/**
|
|
32
|
+ * @author Hoofdgebruiker and kindlich
|
|
33
|
+ */
|
|
34
|
+public class EscapableBracketParser implements BracketExpressionParser {
|
|
35
|
+ private final FunctionalMemberRef method;
|
|
36
|
+ private final TypeID targetType;
|
|
37
|
+
|
|
38
|
+ public EscapableBracketParser(GlobalTypeRegistry registry, FunctionalMemberRef method) {
|
|
39
|
+ if (!method.isStatic())
|
|
40
|
+ throw new IllegalArgumentException("Method must be static");
|
|
41
|
+ if (method.getHeader().getNumberOfTypeParameters() > 0)
|
|
42
|
+ throw new IllegalArgumentException("Method cannot have type parameters");
|
|
43
|
+
|
|
44
|
+ this.method = method;
|
|
45
|
+ this.targetType = registry.getForDefinition(method.getTarget().definition);
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ @Override
|
|
49
|
+ public ParsedExpression parse(CodePosition position, ZSTokenParser tokens) throws ParseException {
|
|
50
|
+ StringBuilder string = new StringBuilder();
|
|
51
|
+
|
|
52
|
+ //This list will contain the BEP calls
|
|
53
|
+ //If this is only a normal BEP, then it will contain exactly one String ParsedExpression.
|
|
54
|
+ final List<ParsedExpression> expressionList = new ArrayList<>();
|
|
55
|
+
|
|
56
|
+ while (tokens.optional(ZSTokenType.T_GREATER) == null) {
|
|
57
|
+ ZSToken next = tokens.next();
|
|
58
|
+
|
|
59
|
+ if (next.type != ZSTokenType.T_DOLLAR) {
|
|
60
|
+ string.append(next.content);
|
|
61
|
+ string.append(tokens.getLastWhitespace());
|
|
62
|
+ continue;
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ //We found a $, now check that it has a { directly after it.
|
|
66
|
+ final String ws = tokens.getLastWhitespace();
|
|
67
|
+ if (!ws.isEmpty()) {
|
|
68
|
+ //$ {..} is not ${..} so we print it as literal
|
|
69
|
+ string.append(next.content).append(ws);
|
|
70
|
+ continue;
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ next = tokens.next();
|
|
74
|
+ //Now we check if it is a {
|
|
75
|
+ if (next.type == ZSTokenType.T_AOPEN) {
|
|
76
|
+ if (string.length() != 0) {
|
|
77
|
+ expressionList.add(new ParsedExpressionString(position, string.toString(), false));
|
|
78
|
+ string = new StringBuilder();
|
|
79
|
+ }
|
|
80
|
+ expressionList.add(ParsedExpression.parse(tokens));
|
|
81
|
+ tokens.required(ZSTokenType.T_ACLOSE, "} expected.");
|
|
82
|
+ string.append(tokens.getLastWhitespace());
|
|
83
|
+ } else {
|
|
84
|
+ //No { after the $, so we treat them both as literal
|
|
85
|
+ string.append("$").append(ws); //Technically, the whitespace here is empty, but let's be sure
|
|
86
|
+ string.append(next.content).append(tokens.getLastWhitespace());
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+ if (string.length() != 0) {
|
|
92
|
+ expressionList.add(new ParsedExpressionString(position, string.toString(), false));
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ return new StaticMethodCallExpression(position, expressionList);
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ private class StaticMethodCallExpression extends ParsedExpression {
|
|
99
|
+
|
|
100
|
+ private final ParsedExpression call;
|
|
101
|
+
|
|
102
|
+ public StaticMethodCallExpression(CodePosition position, List<ParsedExpression> expressions) {
|
|
103
|
+ super(position);
|
|
104
|
+ ParsedExpression p = null;
|
|
105
|
+ for (ParsedExpression expression : expressions) {
|
|
106
|
+ p = p == null ? expression : new ParsedExpressionBinary(expression.position, p, expression, OperatorType.ADD);
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+ this.call = p;
|
|
110
|
+ }
|
|
111
|
+
|
|
112
|
+ @Override
|
|
113
|
+ public IPartialExpression compile(ExpressionScope scope) throws CompileException {
|
|
114
|
+ final Expression methodCall = call.compile(scope.withHint(StringTypeID.AUTO)).eval();
|
|
115
|
+ final CallArguments arguments = new CallArguments(methodCall);
|
|
116
|
+ return new CallStaticExpression(position, targetType, method, method.getHeader(), arguments);
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ @Override
|
|
120
|
+ public boolean hasStrongType() {
|
|
121
|
+ return true;
|
|
122
|
+ }
|
|
123
|
+ }
|
|
124
|
+}
|