|
@@ -16,6 +16,7 @@ import org.openzen.zenscript.lexer.ZSTokenType;
|
16
|
16
|
import org.openzen.zenscript.codemodel.CompareType;
|
17
|
17
|
import org.openzen.zenscript.codemodel.OperatorType;
|
18
|
18
|
import org.openzen.zenscript.codemodel.expression.Expression;
|
|
19
|
+import org.openzen.zenscript.codemodel.expression.switchvalue.SwitchValue;
|
19
|
20
|
import org.openzen.zenscript.codemodel.partial.IPartialExpression;
|
20
|
21
|
import org.openzen.zenscript.codemodel.type.ITypeID;
|
21
|
22
|
import org.openzen.zenscript.lexer.ZSToken;
|
|
@@ -36,175 +37,189 @@ import static org.openzen.zenscript.shared.StringUtils.unescape;
|
36
|
37
|
* @author Stanneke
|
37
|
38
|
*/
|
38
|
39
|
public abstract class ParsedExpression {
|
|
40
|
+ public static class ParsingOptions {
|
|
41
|
+ public static final ParsingOptions DEFAULT = new ParsingOptions(true);
|
|
42
|
+
|
|
43
|
+ public final boolean allowLambda;
|
|
44
|
+
|
|
45
|
+ public ParsingOptions(boolean allowLambda) {
|
|
46
|
+ this.allowLambda = allowLambda;
|
|
47
|
+ }
|
|
48
|
+ }
|
|
49
|
+
|
39
|
50
|
public static ParsedExpression parse(ZSTokenStream parser) {
|
40
|
|
- return readAssignExpression(parser);
|
|
51
|
+ return readAssignExpression(parser, ParsingOptions.DEFAULT);
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ public static ParsedExpression parse(ZSTokenStream parser, ParsingOptions options) {
|
|
55
|
+ return readAssignExpression(parser, options);
|
41
|
56
|
}
|
42
|
57
|
|
43
|
|
- private static ParsedExpression readAssignExpression(ZSTokenStream parser) {
|
|
58
|
+ private static ParsedExpression readAssignExpression(ZSTokenStream parser, ParsingOptions options) {
|
44
|
59
|
CodePosition position = parser.getPosition();
|
45
|
|
- ParsedExpression left = readConditionalExpression(position, parser);
|
|
60
|
+ ParsedExpression left = readConditionalExpression(position, parser, options);
|
46
|
61
|
|
47
|
62
|
switch (parser.peek().type) {
|
48
|
63
|
case T_ASSIGN:
|
49
|
64
|
parser.next();
|
50
|
|
- return new ParsedExpressionAssign(position, left, readAssignExpression(parser));
|
|
65
|
+ return new ParsedExpressionAssign(position, left, readAssignExpression(parser, options));
|
51
|
66
|
case T_ADDASSIGN:
|
52
|
67
|
parser.next();
|
53
|
|
- return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser), OperatorType.ADDASSIGN);
|
|
68
|
+ return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, options), OperatorType.ADDASSIGN);
|
54
|
69
|
case T_SUBASSIGN:
|
55
|
70
|
parser.next();
|
56
|
|
- return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser), OperatorType.SUBASSIGN);
|
|
71
|
+ return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, options), OperatorType.SUBASSIGN);
|
57
|
72
|
case T_CATASSIGN:
|
58
|
73
|
parser.next();
|
59
|
|
- return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser), OperatorType.CATASSIGN);
|
|
74
|
+ return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, options), OperatorType.CATASSIGN);
|
60
|
75
|
case T_MULASSIGN:
|
61
|
76
|
parser.next();
|
62
|
|
- return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser), OperatorType.MULASSIGN);
|
|
77
|
+ return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, options), OperatorType.MULASSIGN);
|
63
|
78
|
case T_DIVASSIGN:
|
64
|
79
|
parser.next();
|
65
|
|
- return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser), OperatorType.DIVASSIGN);
|
|
80
|
+ return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, options), OperatorType.DIVASSIGN);
|
66
|
81
|
case T_MODASSIGN:
|
67
|
82
|
parser.next();
|
68
|
|
- return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser), OperatorType.MODASSIGN);
|
|
83
|
+ return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, options), OperatorType.MODASSIGN);
|
69
|
84
|
case T_ORASSIGN:
|
70
|
85
|
parser.next();
|
71
|
|
- return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser), OperatorType.ORASSIGN);
|
|
86
|
+ return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, options), OperatorType.ORASSIGN);
|
72
|
87
|
case T_ANDASSIGN:
|
73
|
88
|
parser.next();
|
74
|
|
- return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser), OperatorType.ANDASSIGN);
|
|
89
|
+ return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, options), OperatorType.ANDASSIGN);
|
75
|
90
|
case T_XORASSIGN:
|
76
|
91
|
parser.next();
|
77
|
|
- return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser), OperatorType.XORASSIGN);
|
|
92
|
+ return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, options), OperatorType.XORASSIGN);
|
78
|
93
|
case T_SHLASSIGN:
|
79
|
94
|
parser.next();
|
80
|
|
- return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser), OperatorType.SHLASSIGN);
|
|
95
|
+ return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, options), OperatorType.SHLASSIGN);
|
81
|
96
|
case T_SHRASSIGN:
|
82
|
97
|
parser.next();
|
83
|
|
- return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser), OperatorType.SHRASSIGN);
|
|
98
|
+ return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, options), OperatorType.SHRASSIGN);
|
84
|
99
|
case T_USHRASSIGN:
|
85
|
100
|
parser.next();
|
86
|
|
- return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser), OperatorType.USHRASSIGN);
|
|
101
|
+ return new ParsedExpressionOpAssign(position, left, readAssignExpression(parser, options), OperatorType.USHRASSIGN);
|
87
|
102
|
}
|
88
|
103
|
|
89
|
104
|
return left;
|
90
|
105
|
}
|
91
|
106
|
|
92
|
|
- private static ParsedExpression readConditionalExpression(CodePosition position, ZSTokenStream parser) {
|
93
|
|
- ParsedExpression left = readOrOrExpression(position, parser);
|
|
107
|
+ private static ParsedExpression readConditionalExpression(CodePosition position, ZSTokenStream parser, ParsingOptions options) {
|
|
108
|
+ ParsedExpression left = readOrOrExpression(position, parser, options);
|
94
|
109
|
|
95
|
110
|
if (parser.optional(T_QUEST) != null) {
|
96
|
|
- ParsedExpression onIf = readOrOrExpression(parser.peek().position, parser);
|
|
111
|
+ ParsedExpression onIf = readOrOrExpression(parser.peek().position, parser, options);
|
97
|
112
|
parser.required(T_COLON, ": expected");
|
98
|
|
- ParsedExpression onElse = readConditionalExpression(parser.peek().position, parser);
|
|
113
|
+ ParsedExpression onElse = readConditionalExpression(parser.peek().position, parser, options);
|
99
|
114
|
return new ParsedExpressionConditional(position, left, onIf, onElse);
|
100
|
115
|
}
|
101
|
116
|
|
102
|
117
|
return left;
|
103
|
118
|
}
|
104
|
119
|
|
105
|
|
- private static ParsedExpression readOrOrExpression(CodePosition position, ZSTokenStream parser) {
|
106
|
|
- ParsedExpression left = readAndAndExpression(position, parser);
|
|
120
|
+ private static ParsedExpression readOrOrExpression(CodePosition position, ZSTokenStream parser, ParsingOptions options) {
|
|
121
|
+ ParsedExpression left = readAndAndExpression(position, parser, options);
|
107
|
122
|
|
108
|
123
|
while (parser.optional(T_OROR) != null) {
|
109
|
|
- ParsedExpression right = readAndAndExpression(parser.peek().position, parser);
|
|
124
|
+ ParsedExpression right = readAndAndExpression(parser.peek().position, parser, options);
|
110
|
125
|
left = new ParsedExpressionOrOr(position, left, right);
|
111
|
126
|
}
|
112
|
127
|
|
113
|
128
|
while (parser.optional(T_COALESCE) != null) {
|
114
|
|
- ParsedExpression right = readAndAndExpression(parser.peek().position, parser);
|
|
129
|
+ ParsedExpression right = readAndAndExpression(parser.peek().position, parser, options);
|
115
|
130
|
left = new ParsedExpressionCoalesce(position, left, right);
|
116
|
131
|
}
|
117
|
132
|
|
118
|
133
|
return left;
|
119
|
134
|
}
|
120
|
135
|
|
121
|
|
- private static ParsedExpression readAndAndExpression(CodePosition position, ZSTokenStream parser) {
|
122
|
|
- ParsedExpression left = readOrExpression(position, parser);
|
|
136
|
+ private static ParsedExpression readAndAndExpression(CodePosition position, ZSTokenStream parser, ParsingOptions options) {
|
|
137
|
+ ParsedExpression left = readOrExpression(position, parser, options);
|
123
|
138
|
|
124
|
139
|
while (parser.optional(T_ANDAND) != null) {
|
125
|
|
- ParsedExpression right = readOrExpression(parser.peek().position, parser);
|
|
140
|
+ ParsedExpression right = readOrExpression(parser.peek().position, parser, options);
|
126
|
141
|
left = new ParsedExpressionAndAnd(position, left, right);
|
127
|
142
|
}
|
128
|
143
|
return left;
|
129
|
144
|
}
|
130
|
145
|
|
131
|
|
- private static ParsedExpression readOrExpression(CodePosition position, ZSTokenStream parser) {
|
132
|
|
- ParsedExpression left = readXorExpression(position, parser);
|
|
146
|
+ private static ParsedExpression readOrExpression(CodePosition position, ZSTokenStream parser, ParsingOptions options) {
|
|
147
|
+ ParsedExpression left = readXorExpression(position, parser, options);
|
133
|
148
|
|
134
|
149
|
while (parser.optional(T_OR) != null) {
|
135
|
|
- ParsedExpression right = readXorExpression(parser.peek().position, parser);
|
|
150
|
+ ParsedExpression right = readXorExpression(parser.peek().position, parser, options);
|
136
|
151
|
left = new ParsedExpressionBinary(position, left, right, OperatorType.OR);
|
137
|
152
|
}
|
138
|
153
|
return left;
|
139
|
154
|
}
|
140
|
155
|
|
141
|
|
- private static ParsedExpression readXorExpression(CodePosition position, ZSTokenStream parser) {
|
142
|
|
- ParsedExpression left = readAndExpression(position, parser);
|
|
156
|
+ private static ParsedExpression readXorExpression(CodePosition position, ZSTokenStream parser, ParsingOptions options) {
|
|
157
|
+ ParsedExpression left = readAndExpression(position, parser, options);
|
143
|
158
|
|
144
|
159
|
while (parser.optional(T_XOR) != null) {
|
145
|
|
- ParsedExpression right = readAndExpression(parser.peek().position, parser);
|
|
160
|
+ ParsedExpression right = readAndExpression(parser.peek().position, parser, options);
|
146
|
161
|
left = new ParsedExpressionBinary(position, left, right, OperatorType.XOR);
|
147
|
162
|
}
|
148
|
163
|
return left;
|
149
|
164
|
}
|
150
|
165
|
|
151
|
|
- private static ParsedExpression readAndExpression(CodePosition position, ZSTokenStream parser) {
|
152
|
|
- ParsedExpression left = readCompareExpression(position, parser);
|
|
166
|
+ private static ParsedExpression readAndExpression(CodePosition position, ZSTokenStream parser, ParsingOptions options) {
|
|
167
|
+ ParsedExpression left = readCompareExpression(position, parser, options);
|
153
|
168
|
|
154
|
169
|
while (parser.optional(T_AND) != null) {
|
155
|
|
- ParsedExpression right = readCompareExpression(parser.peek().position, parser);
|
|
170
|
+ ParsedExpression right = readCompareExpression(parser.peek().position, parser, options);
|
156
|
171
|
left = new ParsedExpressionBinary(position, left, right, OperatorType.AND);
|
157
|
172
|
}
|
158
|
173
|
return left;
|
159
|
174
|
}
|
160
|
175
|
|
161
|
|
- private static ParsedExpression readCompareExpression(CodePosition position, ZSTokenStream parser) {
|
162
|
|
- ParsedExpression left = readShiftExpression(position, parser);
|
|
176
|
+ private static ParsedExpression readCompareExpression(CodePosition position, ZSTokenStream parser, ParsingOptions options) {
|
|
177
|
+ ParsedExpression left = readShiftExpression(position, parser, options);
|
163
|
178
|
|
164
|
179
|
switch (parser.peek().getType()) {
|
165
|
180
|
case T_EQUAL2: {
|
166
|
181
|
parser.next();
|
167
|
|
- ParsedExpression right = readShiftExpression(parser.peek().position, parser);
|
|
182
|
+ ParsedExpression right = readShiftExpression(parser.peek().position, parser, options);
|
168
|
183
|
return new ParsedExpressionCompare(position, left, right, CompareType.EQ);
|
169
|
184
|
}
|
170
|
185
|
case T_EQUAL3: {
|
171
|
186
|
parser.next();
|
172
|
|
- ParsedExpression right = readShiftExpression(parser.peek().position, parser);
|
|
187
|
+ ParsedExpression right = readShiftExpression(parser.peek().position, parser, options);
|
173
|
188
|
return new ParsedExpressionCompare(position, left, right, CompareType.SAME);
|
174
|
189
|
}
|
175
|
190
|
case T_NOTEQUAL: {
|
176
|
191
|
parser.next();
|
177
|
|
- ParsedExpression right = readShiftExpression(parser.peek().position, parser);
|
|
192
|
+ ParsedExpression right = readShiftExpression(parser.peek().position, parser, options);
|
178
|
193
|
return new ParsedExpressionCompare(position, left, right, CompareType.NE);
|
179
|
194
|
}
|
180
|
195
|
case T_NOTEQUAL2: {
|
181
|
196
|
parser.next();
|
182
|
|
- ParsedExpression right = readShiftExpression(parser.peek().position, parser);
|
|
197
|
+ ParsedExpression right = readShiftExpression(parser.peek().position, parser, options);
|
183
|
198
|
return new ParsedExpressionCompare(position, left, right, CompareType.NOTSAME);
|
184
|
199
|
}
|
185
|
200
|
case T_LESS: {
|
186
|
201
|
parser.next();
|
187
|
|
- ParsedExpression right = readShiftExpression(parser.peek().position, parser);
|
|
202
|
+ ParsedExpression right = readShiftExpression(parser.peek().position, parser, options);
|
188
|
203
|
return new ParsedExpressionCompare(position, left, right, CompareType.LT);
|
189
|
204
|
}
|
190
|
205
|
case T_LESSEQ: {
|
191
|
206
|
parser.next();
|
192
|
|
- ParsedExpression right = readShiftExpression(parser.peek().position, parser);
|
|
207
|
+ ParsedExpression right = readShiftExpression(parser.peek().position, parser, options);
|
193
|
208
|
return new ParsedExpressionCompare(position, left, right, CompareType.LE);
|
194
|
209
|
}
|
195
|
210
|
case T_GREATER: {
|
196
|
211
|
parser.next();
|
197
|
|
- ParsedExpression right = readShiftExpression(parser.peek().position, parser);
|
|
212
|
+ ParsedExpression right = readShiftExpression(parser.peek().position, parser, options);
|
198
|
213
|
return new ParsedExpressionCompare(position, left, right, CompareType.GT);
|
199
|
214
|
}
|
200
|
215
|
case T_GREATEREQ: {
|
201
|
216
|
parser.next();
|
202
|
|
- ParsedExpression right = readShiftExpression(parser.peek().position, parser);
|
|
217
|
+ ParsedExpression right = readShiftExpression(parser.peek().position, parser, options);
|
203
|
218
|
return new ParsedExpressionCompare(position, left, right, CompareType.GE);
|
204
|
219
|
}
|
205
|
220
|
case K_IN: {
|
206
|
221
|
parser.next();
|
207
|
|
- ParsedExpression right = readShiftExpression(parser.peek().position, parser);
|
|
222
|
+ ParsedExpression right = readShiftExpression(parser.peek().position, parser, options);
|
208
|
223
|
return new ParsedExpressionBinary(position, right, left, OperatorType.CONTAINS);
|
209
|
224
|
}
|
210
|
225
|
case K_IS: {
|
|
@@ -215,7 +230,7 @@ public abstract class ParsedExpression {
|
215
|
230
|
case T_NOT: {
|
216
|
231
|
parser.next();
|
217
|
232
|
if (parser.optional(K_IN) != null) {
|
218
|
|
- ParsedExpression right = readShiftExpression(parser.peek().position, parser);
|
|
233
|
+ ParsedExpression right = readShiftExpression(parser.peek().position, parser, options);
|
219
|
234
|
return new ParsedExpressionUnary(position, new ParsedExpressionBinary(position, right, left, OperatorType.CONTAINS), OperatorType.NOT);
|
220
|
235
|
} else if (parser.optional(K_IS) != null) {
|
221
|
236
|
IParsedType type = IParsedType.parse(parser);
|
|
@@ -229,18 +244,18 @@ public abstract class ParsedExpression {
|
229
|
244
|
return left;
|
230
|
245
|
}
|
231
|
246
|
|
232
|
|
- private static ParsedExpression readShiftExpression(CodePosition position, ZSTokenStream parser) {
|
233
|
|
- ParsedExpression left = readAddExpression(position, parser);
|
|
247
|
+ private static ParsedExpression readShiftExpression(CodePosition position, ZSTokenStream parser, ParsingOptions options) {
|
|
248
|
+ ParsedExpression left = readAddExpression(position, parser, options);
|
234
|
249
|
|
235
|
250
|
while (true) {
|
236
|
251
|
if (parser.optional(T_SHL) != null) {
|
237
|
|
- ParsedExpression right = readAddExpression(parser.peek().position, parser);
|
|
252
|
+ ParsedExpression right = readAddExpression(parser.peek().position, parser, options);
|
238
|
253
|
left = new ParsedExpressionBinary(position, left, right, OperatorType.SHL);
|
239
|
254
|
} else if (parser.optional(T_SHR) != null) {
|
240
|
|
- ParsedExpression right = readAddExpression(parser.peek().position, parser);
|
|
255
|
+ ParsedExpression right = readAddExpression(parser.peek().position, parser, options);
|
241
|
256
|
left = new ParsedExpressionBinary(position, left, right, OperatorType.SHR);
|
242
|
257
|
} else if (parser.optional(T_USHR) != null) {
|
243
|
|
- ParsedExpression right = readAddExpression(parser.peek().position, parser);
|
|
258
|
+ ParsedExpression right = readAddExpression(parser.peek().position, parser, options);
|
244
|
259
|
left = new ParsedExpressionBinary(position, left, right, OperatorType.USHR);
|
245
|
260
|
} else {
|
246
|
261
|
break;
|
|
@@ -250,18 +265,18 @@ public abstract class ParsedExpression {
|
250
|
265
|
return left;
|
251
|
266
|
}
|
252
|
267
|
|
253
|
|
- private static ParsedExpression readAddExpression(CodePosition position, ZSTokenStream parser) {
|
254
|
|
- ParsedExpression left = readMulExpression(position, parser);
|
|
268
|
+ private static ParsedExpression readAddExpression(CodePosition position, ZSTokenStream parser, ParsingOptions options) {
|
|
269
|
+ ParsedExpression left = readMulExpression(position, parser, options);
|
255
|
270
|
|
256
|
271
|
while (true) {
|
257
|
272
|
if (parser.optional(T_ADD) != null) {
|
258
|
|
- ParsedExpression right = readMulExpression(parser.peek().position, parser);
|
|
273
|
+ ParsedExpression right = readMulExpression(parser.peek().position, parser, options);
|
259
|
274
|
left = new ParsedExpressionBinary(position, left, right, OperatorType.ADD);
|
260
|
275
|
} else if (parser.optional(T_SUB) != null) {
|
261
|
|
- ParsedExpression right = readMulExpression(parser.peek().position, parser);
|
|
276
|
+ ParsedExpression right = readMulExpression(parser.peek().position, parser, options);
|
262
|
277
|
left = new ParsedExpressionBinary(position, left, right, OperatorType.SUB);
|
263
|
278
|
} else if (parser.optional(T_CAT) != null) {
|
264
|
|
- ParsedExpression right = readMulExpression(parser.peek().position, parser);
|
|
279
|
+ ParsedExpression right = readMulExpression(parser.peek().position, parser, options);
|
265
|
280
|
left = new ParsedExpressionBinary(position, left, right, OperatorType.CAT);
|
266
|
281
|
} else {
|
267
|
282
|
break;
|
|
@@ -270,18 +285,18 @@ public abstract class ParsedExpression {
|
270
|
285
|
return left;
|
271
|
286
|
}
|
272
|
287
|
|
273
|
|
- private static ParsedExpression readMulExpression(CodePosition position, ZSTokenStream parser) {
|
274
|
|
- ParsedExpression left = readUnaryExpression(position, parser);
|
|
288
|
+ private static ParsedExpression readMulExpression(CodePosition position, ZSTokenStream parser, ParsingOptions options) {
|
|
289
|
+ ParsedExpression left = readUnaryExpression(position, parser, options);
|
275
|
290
|
|
276
|
291
|
while (true) {
|
277
|
292
|
if (parser.optional(T_MUL) != null) {
|
278
|
|
- ParsedExpression right = readUnaryExpression(parser.peek().position, parser);
|
|
293
|
+ ParsedExpression right = readUnaryExpression(parser.peek().position, parser, options);
|
279
|
294
|
left = new ParsedExpressionBinary(position, left, right, OperatorType.MUL);
|
280
|
295
|
} else if (parser.optional(T_DIV) != null) {
|
281
|
|
- ParsedExpression right = readUnaryExpression(parser.peek().position, parser);
|
|
296
|
+ ParsedExpression right = readUnaryExpression(parser.peek().position, parser, options);
|
282
|
297
|
left = new ParsedExpressionBinary(position, left, right, OperatorType.DIV);
|
283
|
298
|
} else if (parser.optional(T_MOD) != null) {
|
284
|
|
- ParsedExpression right = readUnaryExpression(parser.peek().position, parser);
|
|
299
|
+ ParsedExpression right = readUnaryExpression(parser.peek().position, parser, options);
|
285
|
300
|
left = new ParsedExpressionBinary(position, left, right, OperatorType.MOD);
|
286
|
301
|
} else {
|
287
|
302
|
break;
|
|
@@ -291,45 +306,45 @@ public abstract class ParsedExpression {
|
291
|
306
|
return left;
|
292
|
307
|
}
|
293
|
308
|
|
294
|
|
- private static ParsedExpression readUnaryExpression(CodePosition position, ZSTokenStream parser) {
|
|
309
|
+ private static ParsedExpression readUnaryExpression(CodePosition position, ZSTokenStream parser, ParsingOptions options) {
|
295
|
310
|
switch (parser.peek().getType()) {
|
296
|
311
|
case T_NOT:
|
297
|
312
|
parser.next();
|
298
|
313
|
return new ParsedExpressionUnary(
|
299
|
314
|
position,
|
300
|
|
- readUnaryExpression(parser.peek().position, parser),
|
|
315
|
+ readUnaryExpression(parser.peek().position, parser, options),
|
301
|
316
|
OperatorType.NOT);
|
302
|
317
|
case T_SUB:
|
303
|
318
|
parser.next();
|
304
|
319
|
return new ParsedExpressionUnary(
|
305
|
320
|
position,
|
306
|
|
- readUnaryExpression(parser.peek().position, parser),
|
|
321
|
+ readUnaryExpression(parser.peek().position, parser, options),
|
307
|
322
|
OperatorType.NEG);
|
308
|
323
|
case T_CAT:
|
309
|
324
|
parser.next();
|
310
|
325
|
return new ParsedExpressionUnary(
|
311
|
326
|
position,
|
312
|
|
- readUnaryExpression(parser.peek().position, parser),
|
|
327
|
+ readUnaryExpression(parser.peek().position, parser, options),
|
313
|
328
|
OperatorType.CAT);
|
314
|
329
|
case T_INCREMENT:
|
315
|
330
|
parser.next();
|
316
|
331
|
return new ParsedExpressionUnary(
|
317
|
332
|
position,
|
318
|
|
- readUnaryExpression(parser.peek().position, parser),
|
|
333
|
+ readUnaryExpression(parser.peek().position, parser, options),
|
319
|
334
|
OperatorType.INCREMENT);
|
320
|
335
|
case T_DECREMENT:
|
321
|
336
|
parser.next();
|
322
|
337
|
return new ParsedExpressionUnary(
|
323
|
338
|
position,
|
324
|
|
- readUnaryExpression(parser.peek().position, parser),
|
|
339
|
+ readUnaryExpression(parser.peek().position, parser, options),
|
325
|
340
|
OperatorType.DECREMENT);
|
326
|
341
|
default:
|
327
|
|
- return readPostfixExpression(position, parser);
|
|
342
|
+ return readPostfixExpression(position, parser, options);
|
328
|
343
|
}
|
329
|
344
|
}
|
330
|
345
|
|
331
|
|
- private static ParsedExpression readPostfixExpression(CodePosition position, ZSTokenStream parser) {
|
332
|
|
- ParsedExpression base = readPrimaryExpression(position, parser);
|
|
346
|
+ private static ParsedExpression readPostfixExpression(CodePosition position, ZSTokenStream parser, ParsingOptions options) {
|
|
347
|
+ ParsedExpression base = readPrimaryExpression(position, parser, options);
|
333
|
348
|
|
334
|
349
|
while (true) {
|
335
|
350
|
if (parser.optional(T_DOT) != null) {
|
|
@@ -352,12 +367,12 @@ public abstract class ParsedExpression {
|
352
|
367
|
}
|
353
|
368
|
}
|
354
|
369
|
} else if (parser.optional(T_DOT2) != null) {
|
355
|
|
- ParsedExpression to = readAssignExpression(parser);
|
|
370
|
+ ParsedExpression to = readAssignExpression(parser, options);
|
356
|
371
|
return new ParsedExpressionRange(position, base, to);
|
357
|
372
|
} else if (parser.optional(T_SQOPEN) != null) {
|
358
|
373
|
List<ParsedExpression> indexes = new ArrayList<>();
|
359
|
374
|
do {
|
360
|
|
- indexes.add(readAssignExpression(parser));
|
|
375
|
+ indexes.add(readAssignExpression(parser, options));
|
361
|
376
|
} while (parser.optional(ZSTokenType.T_COMMA) != null);
|
362
|
377
|
parser.required(T_SQCLOSE, "] expected");
|
363
|
378
|
base = new ParsedExpressionIndex(position, base, indexes);
|
|
@@ -371,7 +386,7 @@ public abstract class ParsedExpression {
|
371
|
386
|
base = new ParsedExpressionPostCall(position, base, OperatorType.INCREMENT);
|
372
|
387
|
} else if (parser.optional(T_DECREMENT) != null) {
|
373
|
388
|
base = new ParsedExpressionPostCall(position, base, OperatorType.DECREMENT);
|
374
|
|
- } else if (parser.optional(T_LAMBDA) != null) {
|
|
389
|
+ } else if (options.allowLambda && parser.optional(T_LAMBDA) != null) {
|
375
|
390
|
ParsedFunctionBody body = ParsedStatement.parseLambdaBody(parser, true);
|
376
|
391
|
base = new ParsedExpressionFunction(position, base.toLambdaHeader(), body);
|
377
|
392
|
} else {
|
|
@@ -382,7 +397,7 @@ public abstract class ParsedExpression {
|
382
|
397
|
return base;
|
383
|
398
|
}
|
384
|
399
|
|
385
|
|
- private static ParsedExpression readPrimaryExpression(CodePosition position, ZSTokenStream parser) {
|
|
400
|
+ private static ParsedExpression readPrimaryExpression(CodePosition position, ZSTokenStream parser, ParsingOptions options) {
|
386
|
401
|
switch (parser.peek().getType()) {
|
387
|
402
|
case T_INT:
|
388
|
403
|
return new ParsedExpressionInt(position, Long.parseLong(parser.next().content));
|
|
@@ -416,7 +431,7 @@ public abstract class ParsedExpression {
|
416
|
431
|
List<ParsedExpression> contents = new ArrayList<>();
|
417
|
432
|
if (parser.optional(T_SQCLOSE) == null) {
|
418
|
433
|
while (parser.optional(T_SQCLOSE) == null) {
|
419
|
|
- contents.add(readAssignExpression(parser));
|
|
434
|
+ contents.add(readAssignExpression(parser, options));
|
420
|
435
|
if (parser.optional(T_COMMA) == null) {
|
421
|
436
|
parser.required(T_SQCLOSE, "] or , expected");
|
422
|
437
|
break;
|
|
@@ -431,13 +446,13 @@ public abstract class ParsedExpression {
|
431
|
446
|
List<ParsedExpression> keys = new ArrayList<>();
|
432
|
447
|
List<ParsedExpression> values = new ArrayList<>();
|
433
|
448
|
while (parser.optional(T_ACLOSE) == null) {
|
434
|
|
- ParsedExpression expression = readAssignExpression(parser);
|
|
449
|
+ ParsedExpression expression = readAssignExpression(parser, options);
|
435
|
450
|
if (parser.optional(T_COLON) == null) {
|
436
|
451
|
keys.add(null);
|
437
|
452
|
values.add(expression);
|
438
|
453
|
} else {
|
439
|
454
|
keys.add(expression);
|
440
|
|
- values.add(readAssignExpression(parser));
|
|
455
|
+ values.add(readAssignExpression(parser, options));
|
441
|
456
|
}
|
442
|
457
|
|
443
|
458
|
if (parser.optional(T_COMMA) == null) {
|
|
@@ -460,7 +475,7 @@ public abstract class ParsedExpression {
|
460
|
475
|
parser.next();
|
461
|
476
|
List<ParsedExpression> expressions = new ArrayList<>();
|
462
|
477
|
do {
|
463
|
|
- expressions.add(readAssignExpression(parser));
|
|
478
|
+ expressions.add(readAssignExpression(parser, options));
|
464
|
479
|
} while (parser.optional(ZSTokenType.T_COMMA) != null);
|
465
|
480
|
parser.required(T_BRCLOSE, ") expected");
|
466
|
481
|
return new ParsedExpressionBracket(position, expressions);
|
|
@@ -474,6 +489,27 @@ public abstract class ParsedExpression {
|
474
|
489
|
|
475
|
490
|
return new ParsedNewExpression(position, type, newArguments);
|
476
|
491
|
}
|
|
492
|
+ case K_MATCH: {
|
|
493
|
+ parser.next();
|
|
494
|
+ ParsedExpression source = parse(parser);
|
|
495
|
+ parser.required(T_AOPEN, "{ expected");
|
|
496
|
+
|
|
497
|
+ List<ParsedMatchExpression.Case> cases = new ArrayList<>();
|
|
498
|
+ while (parser.optional(T_ACLOSE) == null) {
|
|
499
|
+ ParsedExpression key = null;
|
|
500
|
+ if (parser.optional(K_DEFAULT) == null)
|
|
501
|
+ key = parse(parser, new ParsingOptions(false));
|
|
502
|
+
|
|
503
|
+ parser.required(T_LAMBDA, "=> expected");
|
|
504
|
+ ParsedFunctionBody body = ParsedStatement.parseLambdaBody(parser, true);
|
|
505
|
+ cases.add(new ParsedMatchExpression.Case(key, body));
|
|
506
|
+
|
|
507
|
+ if (parser.optional(T_COMMA) == null)
|
|
508
|
+ break;
|
|
509
|
+ }
|
|
510
|
+ parser.required(T_ACLOSE, "} expected");
|
|
511
|
+ return new ParsedMatchExpression(position, source, cases);
|
|
512
|
+ }
|
477
|
513
|
default: {
|
478
|
514
|
IParsedType type = IParsedType.parse(parser);
|
479
|
515
|
if (type == null) {
|
|
@@ -509,6 +545,10 @@ public abstract class ParsedExpression {
|
509
|
545
|
return compile(scope).eval();
|
510
|
546
|
}
|
511
|
547
|
|
|
548
|
+ public SwitchValue compileToSwitchValue(ITypeID type, ExpressionScope scope) {
|
|
549
|
+ throw new CompileException(position, CompileExceptionCode.INVALID_SWITCH_CASE, "Invalid switch case");
|
|
550
|
+ }
|
|
551
|
+
|
512
|
552
|
public ParsedFunctionHeader toLambdaHeader() {
|
513
|
553
|
throw new ParseException(position, "Not a valid lambda header");
|
514
|
554
|
}
|