| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 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 analyzer.src.dart.ast.utilities; | 5 library analyzer.src.dart.ast.utilities; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/dart/ast/token.dart'; | 10 import 'package:analyzer/dart/ast/token.dart'; |
| (...skipping 976 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 987 */ | 987 */ |
| 988 class AstComparator implements AstVisitor<bool> { | 988 class AstComparator implements AstVisitor<bool> { |
| 989 /** | 989 /** |
| 990 * The AST node with which the node being visited is to be compared. This is | 990 * The AST node with which the node being visited is to be compared. This is |
| 991 * only valid at the beginning of each visit method (until [isEqualNodes] is | 991 * only valid at the beginning of each visit method (until [isEqualNodes] is |
| 992 * invoked). | 992 * invoked). |
| 993 */ | 993 */ |
| 994 AstNode _other; | 994 AstNode _other; |
| 995 | 995 |
| 996 /** | 996 /** |
| 997 * Notify that [first] and second have different length. |
| 998 * This implementation returns `false`. Subclasses can override and throw. |
| 999 */ |
| 1000 bool failDifferentLength(List first, List second) { |
| 1001 return false; |
| 1002 } |
| 1003 |
| 1004 /** |
| 1005 * Check whether [second] is null. Subclasses can override to throw. |
| 1006 */ |
| 1007 bool failIfNotNull(Object first, Object second) { |
| 1008 return second == null; |
| 1009 } |
| 1010 |
| 1011 /** |
| 1012 * Notify that [first] is not `null` while [second] one is `null`. |
| 1013 * This implementation returns `false`. Subclasses can override and throw. |
| 1014 */ |
| 1015 bool failIsNull(Object first, Object second) { |
| 1016 return false; |
| 1017 } |
| 1018 |
| 1019 /** |
| 1020 * Notify that [first] and [second] have different types. |
| 1021 * This implementation returns `false`. Subclasses can override and throw. |
| 1022 */ |
| 1023 bool failRuntimeType(Object first, Object second) { |
| 1024 return false; |
| 1025 } |
| 1026 |
| 1027 /** |
| 997 * Return `true` if the [first] node and the [second] node have the same | 1028 * Return `true` if the [first] node and the [second] node have the same |
| 998 * structure. | 1029 * structure. |
| 999 * | 1030 * |
| 1000 * *Note:* This method is only visible for testing purposes and should not be | 1031 * *Note:* This method is only visible for testing purposes and should not be |
| 1001 * used by clients. | 1032 * used by clients. |
| 1002 */ | 1033 */ |
| 1003 bool isEqualNodes(AstNode first, AstNode second) { | 1034 bool isEqualNodes(AstNode first, AstNode second) { |
| 1004 if (first == null) { | 1035 if (first == null) { |
| 1005 return second == null; | 1036 return failIfNotNull(first, second); |
| 1006 } else if (second == null) { | 1037 } else if (second == null) { |
| 1007 return false; | 1038 return failIsNull(first, second); |
| 1008 } else if (first.runtimeType != second.runtimeType) { | 1039 } else if (first.runtimeType != second.runtimeType) { |
| 1009 return false; | 1040 return failRuntimeType(first, second); |
| 1010 } | 1041 } |
| 1011 _other = second; | 1042 _other = second; |
| 1012 return first.accept(this); | 1043 return first.accept(this); |
| 1013 } | 1044 } |
| 1014 | 1045 |
| 1015 /** | 1046 /** |
| 1016 * Return `true` if the [first] token and the [second] token have the same | 1047 * Return `true` if the [first] token and the [second] token have the same |
| 1017 * structure. | 1048 * structure. |
| 1018 * | 1049 * |
| 1019 * *Note:* This method is only visible for testing purposes and should not be | 1050 * *Note:* This method is only visible for testing purposes and should not be |
| 1020 * used by clients. | 1051 * used by clients. |
| 1021 */ | 1052 */ |
| 1022 bool isEqualTokens(Token first, Token second) { | 1053 bool isEqualTokens(Token first, Token second) { |
| 1023 if (first == null) { | 1054 if (first == null) { |
| 1024 return second == null; | 1055 return failIfNotNull(first, second); |
| 1025 } else if (second == null) { | 1056 } else if (second == null) { |
| 1026 return false; | 1057 return failIsNull(first, second); |
| 1027 } else if (identical(first, second)) { | 1058 } else if (identical(first, second)) { |
| 1028 return true; | 1059 return true; |
| 1029 } | 1060 } |
| 1030 return first.offset == second.offset && | 1061 return isEqualTokensNotNull(first, second); |
| 1031 first.length == second.length && | |
| 1032 first.lexeme == second.lexeme; | |
| 1033 } | 1062 } |
| 1034 | 1063 |
| 1064 /** |
| 1065 * Return `true` if the [first] token and the [second] token have the same |
| 1066 * structure. Both [first] and [second] are not `null`. |
| 1067 */ |
| 1068 bool isEqualTokensNotNull(Token first, Token second) => |
| 1069 first.offset == second.offset && |
| 1070 first.length == second.length && |
| 1071 first.lexeme == second.lexeme; |
| 1072 |
| 1035 @override | 1073 @override |
| 1036 bool visitAdjacentStrings(AdjacentStrings node) { | 1074 bool visitAdjacentStrings(AdjacentStrings node) { |
| 1037 AdjacentStrings other = _other as AdjacentStrings; | 1075 AdjacentStrings other = _other as AdjacentStrings; |
| 1038 return _isEqualNodeLists(node.strings, other.strings); | 1076 return _isEqualNodeLists(node.strings, other.strings); |
| 1039 } | 1077 } |
| 1040 | 1078 |
| 1041 @override | 1079 @override |
| 1042 bool visitAnnotation(Annotation node) { | 1080 bool visitAnnotation(Annotation node) { |
| 1043 Annotation other = _other as Annotation; | 1081 Annotation other = _other as Annotation; |
| 1044 return isEqualTokens(node.atSign, other.atSign) && | 1082 return isEqualTokens(node.atSign, other.atSign) && |
| (...skipping 964 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2009 isEqualNodes(node.expression, other.expression) && | 2047 isEqualNodes(node.expression, other.expression) && |
| 2010 isEqualTokens(node.semicolon, other.semicolon); | 2048 isEqualTokens(node.semicolon, other.semicolon); |
| 2011 } | 2049 } |
| 2012 | 2050 |
| 2013 /** | 2051 /** |
| 2014 * Return `true` if the [first] and [second] lists of AST nodes have the same | 2052 * Return `true` if the [first] and [second] lists of AST nodes have the same |
| 2015 * size and corresponding elements are equal. | 2053 * size and corresponding elements are equal. |
| 2016 */ | 2054 */ |
| 2017 bool _isEqualNodeLists(NodeList first, NodeList second) { | 2055 bool _isEqualNodeLists(NodeList first, NodeList second) { |
| 2018 if (first == null) { | 2056 if (first == null) { |
| 2019 return second == null; | 2057 return failIfNotNull(first, second); |
| 2020 } else if (second == null) { | 2058 } else if (second == null) { |
| 2021 return false; | 2059 return failIsNull(first, second); |
| 2022 } | 2060 } |
| 2023 int size = first.length; | 2061 int size = first.length; |
| 2024 if (second.length != size) { | 2062 if (second.length != size) { |
| 2025 return false; | 2063 return failDifferentLength(first, second); |
| 2026 } | 2064 } |
| 2027 for (int i = 0; i < size; i++) { | 2065 for (int i = 0; i < size; i++) { |
| 2028 if (!isEqualNodes(first[i], second[i])) { | 2066 if (!isEqualNodes(first[i], second[i])) { |
| 2029 return false; | 2067 return false; |
| 2030 } | 2068 } |
| 2031 } | 2069 } |
| 2032 return true; | 2070 return true; |
| 2033 } | 2071 } |
| 2034 | 2072 |
| 2035 /** | 2073 /** |
| 2036 * Return `true` if the [first] and [second] lists of tokens have the same | 2074 * Return `true` if the [first] and [second] lists of tokens have the same |
| 2037 * length and corresponding elements are equal. | 2075 * length and corresponding elements are equal. |
| 2038 */ | 2076 */ |
| 2039 bool _isEqualTokenLists(List<Token> first, List<Token> second) { | 2077 bool _isEqualTokenLists(List<Token> first, List<Token> second) { |
| 2040 int length = first.length; | 2078 int length = first.length; |
| 2041 if (second.length != length) { | 2079 if (second.length != length) { |
| 2042 return false; | 2080 return failDifferentLength(first, second); |
| 2043 } | 2081 } |
| 2044 for (int i = 0; i < length; i++) { | 2082 for (int i = 0; i < length; i++) { |
| 2045 if (!isEqualTokens(first[i], second[i])) { | 2083 if (!isEqualTokens(first[i], second[i])) { |
| 2046 return false; | 2084 return false; |
| 2047 } | 2085 } |
| 2048 } | 2086 } |
| 2049 return true; | 2087 return true; |
| 2050 } | 2088 } |
| 2051 | 2089 |
| 2052 /** | 2090 /** |
| (...skipping 5733 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7786 * Safely visit the given [token], printing the [suffix] after the token if it | 7824 * Safely visit the given [token], printing the [suffix] after the token if it |
| 7787 * is non-`null`. | 7825 * is non-`null`. |
| 7788 */ | 7826 */ |
| 7789 void _visitTokenWithSuffix(Token token, String suffix) { | 7827 void _visitTokenWithSuffix(Token token, String suffix) { |
| 7790 if (token != null) { | 7828 if (token != null) { |
| 7791 _writer.print(token.lexeme); | 7829 _writer.print(token.lexeme); |
| 7792 _writer.print(suffix); | 7830 _writer.print(suffix); |
| 7793 } | 7831 } |
| 7794 } | 7832 } |
| 7795 } | 7833 } |
| OLD | NEW |