|
17 | 17 | public final class Parser { |
18 | 18 |
|
19 | 19 | private static final Token EOF = new Token(TokenType.EOF, "", -1, -1); |
| 20 | + |
| 21 | + private static final Map<TokenType, BinaryExpression.Operator> assignOperator; |
| 22 | + static { |
| 23 | + assignOperator = new HashMap<>(BinaryExpression.Operator.values().length + 1); |
| 24 | + assignOperator.put(TokenType.EQ, null); |
| 25 | + assignOperator.put(TokenType.PLUSEQ, BinaryExpression.Operator.ADD); |
| 26 | + assignOperator.put(TokenType.MINUSEQ, BinaryExpression.Operator.SUBTRACT); |
| 27 | + assignOperator.put(TokenType.STAREQ, BinaryExpression.Operator.MULTIPLY); |
| 28 | + assignOperator.put(TokenType.SLASHEQ, BinaryExpression.Operator.DIVIDE); |
| 29 | + assignOperator.put(TokenType.PERCENTEQ, BinaryExpression.Operator.REMAINDER); |
| 30 | + assignOperator.put(TokenType.AMPEQ, BinaryExpression.Operator.AND); |
| 31 | + assignOperator.put(TokenType.CARETEQ, BinaryExpression.Operator.XOR); |
| 32 | + assignOperator.put(TokenType.BAREQ, BinaryExpression.Operator.OR); |
| 33 | + assignOperator.put(TokenType.COLONCOLONEQ, BinaryExpression.Operator.PUSH); |
| 34 | + assignOperator.put(TokenType.LTLTEQ, BinaryExpression.Operator.LSHIFT); |
| 35 | + assignOperator.put(TokenType.GTGTEQ, BinaryExpression.Operator.RSHIFT); |
| 36 | + assignOperator.put(TokenType.GTGTGTEQ, BinaryExpression.Operator.URSHIFT); |
| 37 | + } |
20 | 38 |
|
21 | 39 | private final List<Token> tokens; |
22 | 40 | private final int size; |
@@ -334,22 +352,24 @@ private Expression assignment() { |
334 | 352 | } |
335 | 353 |
|
336 | 354 | private Expression assignmentStrict() { |
337 | | - if (lookMatch(0, TokenType.WORD) && lookMatch(1, TokenType.EQ)) { |
338 | | - final String variable = consume(TokenType.WORD).getText(); |
339 | | - consume(TokenType.EQ); |
340 | | - return new AssignmentExpression(variable, expression()); |
| 355 | + final int position = pos; |
| 356 | + final Expression targetExpr = qualifiedName(); |
| 357 | + if ((targetExpr == null) || !(targetExpr instanceof Accessible)) { |
| 358 | + pos = position; |
| 359 | + return null; |
341 | 360 | } |
342 | 361 |
|
343 | | - final int position = pos; |
344 | | - final Expression qualifiedNameExpr = qualifiedName(); |
345 | | - if (lookMatch(0, TokenType.EQ) && (qualifiedNameExpr instanceof ContainerAccessExpression)) { |
346 | | - consume(TokenType.EQ); |
347 | | - final ContainerAccessExpression containerExpr = (ContainerAccessExpression) qualifiedNameExpr; |
348 | | - return new ContainerAssignmentExpression(containerExpr, expression()); |
| 362 | + final TokenType currentType = get(0).getType(); |
| 363 | + if (!assignOperator.containsKey(currentType)) { |
| 364 | + pos = position; |
| 365 | + return null; |
349 | 366 | } |
350 | | - pos = position; |
| 367 | + match(currentType); |
| 368 | + |
| 369 | + final BinaryExpression.Operator op = assignOperator.get(currentType); |
| 370 | + final Expression expression = expression(); |
351 | 371 |
|
352 | | - return null; |
| 372 | + return new AssignmentExpression(op, (Accessible) targetExpr, expression); |
353 | 373 | } |
354 | 374 |
|
355 | 375 | private Expression ternary() { |
|
0 commit comments