Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
|
Johnni Winther
2017/03/17 12:03:52
Add a comment about when and why we use `identical
ahe
2017/03/17 13:48:27
I'll update the documentation comment of the Parse
| |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library fasta.parser.parser; | 5 library fasta.parser.parser; |
| 6 | 6 |
| 7 import '../scanner.dart' show ErrorToken; | 7 import '../scanner.dart' show ErrorToken; |
| 8 | 8 |
| 9 import '../scanner/recover.dart' show closeBraceFor, skipToEof; | 9 import '../scanner/recover.dart' show closeBraceFor, skipToEof; |
| 10 | 10 |
| 11 import '../scanner/keyword.dart' show Keyword; | 11 import '../scanner/keyword.dart' show Keyword; |
| (...skipping 2675 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2687 return parseSendOrFunctionLiteral(token); | 2687 return parseSendOrFunctionLiteral(token); |
| 2688 } else if (kind == INT_TOKEN || kind == HEXADECIMAL_TOKEN) { | 2688 } else if (kind == INT_TOKEN || kind == HEXADECIMAL_TOKEN) { |
| 2689 return parseLiteralInt(token); | 2689 return parseLiteralInt(token); |
| 2690 } else if (kind == DOUBLE_TOKEN) { | 2690 } else if (kind == DOUBLE_TOKEN) { |
| 2691 return parseLiteralDouble(token); | 2691 return parseLiteralDouble(token); |
| 2692 } else if (kind == STRING_TOKEN) { | 2692 } else if (kind == STRING_TOKEN) { |
| 2693 return parseLiteralString(token); | 2693 return parseLiteralString(token); |
| 2694 } else if (kind == HASH_TOKEN) { | 2694 } else if (kind == HASH_TOKEN) { |
| 2695 return parseLiteralSymbol(token); | 2695 return parseLiteralSymbol(token); |
| 2696 } else if (kind == KEYWORD_TOKEN) { | 2696 } else if (kind == KEYWORD_TOKEN) { |
| 2697 final value = token.stringValue; | 2697 final String value = token.stringValue; |
| 2698 if (value == 'true' || value == 'false') { | 2698 if (identical(value, "true") || identical(value, "false")) { |
| 2699 return parseLiteralBool(token); | 2699 return parseLiteralBool(token); |
| 2700 } else if (value == 'null') { | 2700 } else if (identical(value, "null")) { |
| 2701 return parseLiteralNull(token); | 2701 return parseLiteralNull(token); |
| 2702 } else if (value == 'this') { | 2702 } else if (identical(value, "this")) { |
| 2703 return parseThisExpression(token); | 2703 return parseThisExpression(token); |
| 2704 } else if (value == 'super') { | 2704 } else if (identical(value, "super")) { |
| 2705 return parseSuperExpression(token); | 2705 return parseSuperExpression(token); |
| 2706 } else if (value == 'new') { | 2706 } else if (identical(value, "new")) { |
| 2707 return parseNewExpression(token); | 2707 return parseNewExpression(token); |
| 2708 } else if (value == 'const') { | 2708 } else if (identical(value, "const")) { |
| 2709 return parseConstExpression(token); | 2709 return parseConstExpression(token); |
| 2710 } else if (value == 'void') { | 2710 } else if (identical(value, "void")) { |
| 2711 return parseFunctionExpression(token); | 2711 return parseFunctionExpression(token); |
| 2712 } else if (asyncState != AsyncModifier.Sync && | 2712 } else if (asyncState != AsyncModifier.Sync && |
| 2713 (value == 'yield' || value == 'async')) { | 2713 (identical(value, "yield") || identical(value, "async"))) { |
| 2714 return expressionExpected(token); | 2714 return expressionExpected(token); |
| 2715 } else if (token.isIdentifier()) { | 2715 } else if (token.isIdentifier()) { |
| 2716 return parseSendOrFunctionLiteral(token); | 2716 return parseSendOrFunctionLiteral(token); |
| 2717 } else { | 2717 } else { |
| 2718 return expressionExpected(token); | 2718 return expressionExpected(token); |
| 2719 } | 2719 } |
| 2720 } else if (kind == OPEN_PAREN_TOKEN) { | 2720 } else if (kind == OPEN_PAREN_TOKEN) { |
| 2721 return parseParenthesizedExpressionOrFunctionLiteral(token); | 2721 return parseParenthesizedExpressionOrFunctionLiteral(token); |
| 2722 } else if (kind == OPEN_SQUARE_BRACKET_TOKEN || token.stringValue == '[]') { | 2722 } else if (kind == OPEN_SQUARE_BRACKET_TOKEN || optional('[]', token)) { |
|
Johnni Winther
2017/03/17 12:03:52
Why use `optional`? Is it faster? (It doesn't read
ahe
2017/03/17 13:48:27
For consistency, I prefer to use "optional" unless
| |
| 2723 listener.handleNoTypeArguments(token); | 2723 listener.handleNoTypeArguments(token); |
| 2724 return parseLiteralListSuffix(token, null); | 2724 return parseLiteralListSuffix(token, null); |
| 2725 } else if (kind == OPEN_CURLY_BRACKET_TOKEN) { | 2725 } else if (kind == OPEN_CURLY_BRACKET_TOKEN) { |
| 2726 listener.handleNoTypeArguments(token); | 2726 listener.handleNoTypeArguments(token); |
| 2727 return parseLiteralMapSuffix(token, null); | 2727 return parseLiteralMapSuffix(token, null); |
| 2728 } else if (kind == LT_TOKEN) { | 2728 } else if (kind == LT_TOKEN) { |
| 2729 return parseLiteralListOrMapOrFunction(token, null); | 2729 return parseLiteralListOrMapOrFunction(token, null); |
| 2730 } else { | 2730 } else { |
| 2731 return expressionExpected(token); | 2731 return expressionExpected(token); |
| 2732 } | 2732 } |
| 2733 } | 2733 } |
| 2734 | 2734 |
| 2735 Token expressionExpected(Token token) { | 2735 Token expressionExpected(Token token) { |
| 2736 token = reportUnrecoverableError(token, ErrorKind.ExpectedExpression)?.next; | 2736 token = reportUnrecoverableError(token, ErrorKind.ExpectedExpression)?.next; |
| 2737 listener.handleInvalidExpression(token); | 2737 listener.handleInvalidExpression(token); |
| 2738 return token; | 2738 return token; |
| 2739 } | 2739 } |
| 2740 | 2740 |
| 2741 Token parseParenthesizedExpressionOrFunctionLiteral(Token token) { | 2741 Token parseParenthesizedExpressionOrFunctionLiteral(Token token) { |
| 2742 BeginGroupToken beginGroup = token; | 2742 BeginGroupToken beginGroup = token; |
| 2743 // TODO(eernst): Check for NPE as described in issue 26252. | 2743 // TODO(eernst): Check for NPE as described in issue 26252. |
| 2744 Token nextToken = beginGroup.endGroup.next; | 2744 Token nextToken = beginGroup.endGroup.next; |
| 2745 int kind = nextToken.kind; | 2745 int kind = nextToken.kind; |
| 2746 if (mayParseFunctionExpressions && | 2746 if (mayParseFunctionExpressions && |
| 2747 (identical(kind, FUNCTION_TOKEN) || | 2747 (identical(kind, FUNCTION_TOKEN) || |
| 2748 identical(kind, OPEN_CURLY_BRACKET_TOKEN) || | 2748 identical(kind, OPEN_CURLY_BRACKET_TOKEN) || |
| 2749 (identical(kind, KEYWORD_TOKEN) && | 2749 (identical(kind, KEYWORD_TOKEN) && |
| 2750 (nextToken.lexeme == 'async' || nextToken.lexeme == 'sync')))) { | 2750 (optional('async', nextToken) || |
| 2751 optional('sync', nextToken))))) { | |
| 2751 listener.handleNoTypeVariables(token); | 2752 listener.handleNoTypeVariables(token); |
| 2752 return parseUnnamedFunction(token); | 2753 return parseUnnamedFunction(token); |
| 2753 } else { | 2754 } else { |
| 2754 bool old = mayParseFunctionExpressions; | 2755 bool old = mayParseFunctionExpressions; |
| 2755 mayParseFunctionExpressions = true; | 2756 mayParseFunctionExpressions = true; |
| 2756 token = parseParenthesizedExpression(token); | 2757 token = parseParenthesizedExpression(token); |
| 2757 mayParseFunctionExpressions = old; | 2758 mayParseFunctionExpressions = old; |
| 2758 return token; | 2759 return token; |
| 2759 } | 2760 } |
| 2760 } | 2761 } |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2859 /// been parsed, or `listener.handleNoTypeArguments(..)` has been executed. | 2860 /// been parsed, or `listener.handleNoTypeArguments(..)` has been executed. |
| 2860 Token parseLiteralFunctionSuffix(Token token) { | 2861 Token parseLiteralFunctionSuffix(Token token) { |
| 2861 assert(optional('(', token)); | 2862 assert(optional('(', token)); |
| 2862 BeginGroupToken beginGroup = token; | 2863 BeginGroupToken beginGroup = token; |
| 2863 if (beginGroup.endGroup != null) { | 2864 if (beginGroup.endGroup != null) { |
| 2864 Token nextToken = beginGroup.endGroup.next; | 2865 Token nextToken = beginGroup.endGroup.next; |
| 2865 int kind = nextToken.kind; | 2866 int kind = nextToken.kind; |
| 2866 if (identical(kind, FUNCTION_TOKEN) || | 2867 if (identical(kind, FUNCTION_TOKEN) || |
| 2867 identical(kind, OPEN_CURLY_BRACKET_TOKEN) || | 2868 identical(kind, OPEN_CURLY_BRACKET_TOKEN) || |
| 2868 (identical(kind, KEYWORD_TOKEN) && | 2869 (identical(kind, KEYWORD_TOKEN) && |
| 2869 (nextToken.lexeme == 'async' || nextToken.lexeme == 'sync'))) { | 2870 (optional('async', nextToken) || optional('sync', nextToken)))) { |
| 2870 return parseUnnamedFunction(token); | 2871 return parseUnnamedFunction(token); |
| 2871 } | 2872 } |
| 2872 // Fall through. | 2873 // Fall through. |
| 2873 } | 2874 } |
| 2874 reportUnrecoverableError(token, ErrorKind.UnexpectedToken); | 2875 reportUnrecoverableError(token, ErrorKind.UnexpectedToken); |
| 2875 return null; | 2876 return null; |
| 2876 } | 2877 } |
| 2877 | 2878 |
| 2878 /// genericListLiteral | genericMapLiteral | genericFunctionLiteral. | 2879 /// genericListLiteral | genericMapLiteral | genericFunctionLiteral. |
| 2879 /// | 2880 /// |
| (...skipping 763 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3643 break; | 3644 break; |
| 3644 } | 3645 } |
| 3645 if (isRecoverable) { | 3646 if (isRecoverable) { |
| 3646 listener.handleRecoverableError(token, kind, arguments); | 3647 listener.handleRecoverableError(token, kind, arguments); |
| 3647 return null; | 3648 return null; |
| 3648 } else { | 3649 } else { |
| 3649 return listener.handleUnrecoverableError(token, kind, arguments); | 3650 return listener.handleUnrecoverableError(token, kind, arguments); |
| 3650 } | 3651 } |
| 3651 } | 3652 } |
| 3652 } | 3653 } |
| OLD | NEW |