Skip to content

Commit

Permalink
Disable scanning of &&= and ||= assignment operators
Browse files Browse the repository at this point in the history
Fix #30340

Change-Id: Id27f37af9805ca09deaa3c6252817e3d9a926216
Reviewed-on: https://dart-review.googlesource.com/17361
Reviewed-by: Brian Wilkerson <[email protected]>
  • Loading branch information
Dan Rubel committed Oct 31, 2017
1 parent 542083e commit 2b681b2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
17 changes: 10 additions & 7 deletions pkg/analyzer/test/generated/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:analyzer/src/generated/parser.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/generated/testing/token_factory.dart';
import 'package:analyzer/src/generated/utilities_dart.dart';
import 'package:front_end/src/fasta/scanner/abstract_scanner.dart';
import 'package:front_end/src/scanner/scanner.dart' as fe;
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
Expand Down Expand Up @@ -5628,13 +5629,15 @@ abstract class ExpressionParserTestMixin implements AbstractParserTestCase {
}

void test_parseExpression_assign_compound() {
enableLazyAssignmentOperators = true;
Expression expression = parseExpression('x ||= y');
var assignmentExpression = expression as AssignmentExpression;
expect(assignmentExpression.leftHandSide, isNotNull);
expect(assignmentExpression.operator, isNotNull);
expect(assignmentExpression.operator.type, TokenType.BAR_BAR_EQ);
expect(assignmentExpression.rightHandSide, isNotNull);
if (usingFastaParser && AbstractScanner.LAZY_ASSIGNMENT_ENABLED) {
enableLazyAssignmentOperators = true;
Expression expression = parseExpression('x ||= y');
var assignmentExpression = expression as AssignmentExpression;
expect(assignmentExpression.leftHandSide, isNotNull);
expect(assignmentExpression.operator, isNotNull);
expect(assignmentExpression.operator.type, TokenType.BAR_BAR_EQ);
expect(assignmentExpression.rightHandSide, isNotNull);
}
}

void test_parseExpression_comparison() {
Expand Down
12 changes: 10 additions & 2 deletions pkg/front_end/lib/src/fasta/scanner/abstract_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ import 'token_constants.dart';
import 'characters.dart';

abstract class AbstractScanner implements Scanner {
/**
* A flag indicating whether character sequences `&&=` and `||=`
* should be tokenized as the assignment operators
* [AMPERSAND_AMPERSAND_EQ_TOKEN] and [BAR_BAR_EQ_TOKEN] respectively.
* See issue https://github.com/dart-lang/sdk/issues/30340
*/
static const bool LAZY_ASSIGNMENT_ENABLED = false;

final bool includeComments;

/**
Expand Down Expand Up @@ -510,7 +518,7 @@ abstract class AbstractScanner implements Scanner {
next = advance();
if (identical(next, $BAR)) {
next = advance();
if (identical(next, $EQ)) {
if (LAZY_ASSIGNMENT_ENABLED && identical(next, $EQ)) {
appendPrecedenceToken(TokenType.BAR_BAR_EQ);
return advance();
}
Expand All @@ -530,7 +538,7 @@ abstract class AbstractScanner implements Scanner {
next = advance();
if (identical(next, $AMPERSAND)) {
next = advance();
if (identical(next, $EQ)) {
if (LAZY_ASSIGNMENT_ENABLED && identical(next, $EQ)) {
appendPrecedenceToken(TokenType.AMPERSAND_AMPERSAND_EQ);
return advance();
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/front_end/test/precedence_info_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:front_end/src/fasta/scanner/abstract_scanner.dart'
show AbstractScanner;
import 'package:front_end/src/fasta/scanner/string_scanner.dart';
import 'package:front_end/src/fasta/scanner/token.dart' as fasta;
import 'package:front_end/src/scanner/token.dart';
Expand Down Expand Up @@ -35,8 +37,10 @@ class PrecedenceInfoTest {
assertLexeme('#!/'); // SCRIPT_TAG
assertLexeme('"foo"'); // STRING
assertLexeme('bar'); // IDENTIFIER
assertLexeme('&&=');
assertLexeme('||=');
if (AbstractScanner.LAZY_ASSIGNMENT_ENABLED) {
assertLexeme('&&=');
assertLexeme('||=');
}
}

void test_isOperator() {
Expand Down
12 changes: 9 additions & 3 deletions pkg/front_end/test/scanner_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import 'package:front_end/src/base/errors.dart';
import 'package:front_end/src/base/jenkins_smi_hash.dart';
import 'package:front_end/src/fasta/scanner/abstract_scanner.dart'
show AbstractScanner;
import 'package:front_end/src/scanner/errors.dart';
import 'package:front_end/src/scanner/reader.dart';
import 'package:front_end/src/scanner/scanner.dart';
Expand Down Expand Up @@ -153,8 +155,10 @@ abstract class ScannerTestBase {
}

void test_ampersand_ampersand_eq() {
_assertToken(TokenType.AMPERSAND_AMPERSAND_EQ, "&&=",
lazyAssignmentOperators: true);
if (AbstractScanner.LAZY_ASSIGNMENT_ENABLED) {
_assertToken(TokenType.AMPERSAND_AMPERSAND_EQ, "&&=",
lazyAssignmentOperators: true);
}
}

void test_ampersand_eq() {
Expand Down Expand Up @@ -211,7 +215,9 @@ abstract class ScannerTestBase {
}

void test_bar_bar_eq() {
_assertToken(TokenType.BAR_BAR_EQ, "||=", lazyAssignmentOperators: true);
if (AbstractScanner.LAZY_ASSIGNMENT_ENABLED) {
_assertToken(TokenType.BAR_BAR_EQ, "||=", lazyAssignmentOperators: true);
}
}

void test_bar_eq() {
Expand Down

0 comments on commit 2b681b2

Please sign in to comment.