Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Side by Side Diff: pkg/analyzer/lib/src/dart/ast/utilities.dart

Issue 1805123002: Check whether we have a result instead of throwing an exception in NodeLocator. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 3672 matching lines...) Expand 10 before | Expand all | Expand 10 after
3683 * Search within the given AST [node] for an identifier representing an 3683 * Search within the given AST [node] for an identifier representing an
3684 * element in the specified source range. Return the element that was found, 3684 * element in the specified source range. Return the element that was found,
3685 * or `null` if no element was found. 3685 * or `null` if no element was found.
3686 */ 3686 */
3687 AstNode searchWithin(AstNode node) { 3687 AstNode searchWithin(AstNode node) {
3688 if (node == null) { 3688 if (node == null) {
3689 return null; 3689 return null;
3690 } 3690 }
3691 try { 3691 try {
3692 node.accept(this); 3692 node.accept(this);
3693 } on NodeLocator_NodeFoundException {
3694 // A node with the right source position was found.
3695 } catch (exception, stackTrace) { 3693 } catch (exception, stackTrace) {
3696 AnalysisEngine.instance.logger.logInformation( 3694 AnalysisEngine.instance.logger.logInformation(
3697 "Unable to locate element at offset ($_startOffset - $_endOffset)", 3695 "Unable to locate element at offset ($_startOffset - $_endOffset)",
3698 new CaughtException(exception, stackTrace)); 3696 new CaughtException(exception, stackTrace));
3699 return null; 3697 return null;
3700 } 3698 }
3701 return _foundNode; 3699 return _foundNode;
3702 } 3700 }
3703 3701
3704 @override 3702 @override
3705 Object visitNode(AstNode node) { 3703 Object visitNode(AstNode node) {
3704 // Don't visit a new tree if the result has been already found.
3705 if (_foundNode != null) {
3706 return null;
3707 }
3708 // Check whether the current node covers the selection.
3706 Token beginToken = node.beginToken; 3709 Token beginToken = node.beginToken;
3707 Token endToken = node.endToken; 3710 Token endToken = node.endToken;
3708 // Don't include synthetic tokens. 3711 // Don't include synthetic tokens.
3709 while (endToken != beginToken) { 3712 while (endToken != beginToken) {
3710 if (endToken.type == TokenType.EOF || !endToken.isSynthetic) { 3713 if (endToken.type == TokenType.EOF || !endToken.isSynthetic) {
3711 break; 3714 break;
3712 } 3715 }
3713 endToken = endToken.previous; 3716 endToken = endToken.previous;
3714 } 3717 }
3715 int end = endToken.end; 3718 int end = endToken.end;
3716 int start = node.offset; 3719 int start = node.offset;
3717 if (end < _startOffset) { 3720 if (end < _startOffset) {
3718 return null; 3721 return null;
3719 } 3722 }
3720 if (start > _endOffset) { 3723 if (start > _endOffset) {
3721 return null; 3724 return null;
3722 } 3725 }
3726 // Check children.
3723 try { 3727 try {
3724 node.visitChildren(this); 3728 node.visitChildren(this);
3725 } on NodeLocator_NodeFoundException {
3726 rethrow;
3727 } catch (exception, stackTrace) { 3729 } catch (exception, stackTrace) {
3728 // Ignore the exception and proceed in order to visit the rest of the 3730 // Ignore the exception and proceed in order to visit the rest of the
3729 // structure. 3731 // structure.
3730 AnalysisEngine.instance.logger.logInformation( 3732 AnalysisEngine.instance.logger.logInformation(
3731 "Exception caught while traversing an AST structure.", 3733 "Exception caught while traversing an AST structure.",
3732 new CaughtException(exception, stackTrace)); 3734 new CaughtException(exception, stackTrace));
3733 } 3735 }
3736 // Found a child.
3737 if (_foundNode != null) {
3738 return null;
3739 }
3740 // Check this node.
3734 if (start <= _startOffset && _endOffset <= end) { 3741 if (start <= _startOffset && _endOffset <= end) {
3735 _foundNode = node; 3742 _foundNode = node;
3736 throw new NodeLocator_NodeFoundException();
3737 } 3743 }
3738 return null; 3744 return null;
3739 } 3745 }
3740 } 3746 }
3741 3747
3742 /** 3748 /**
3743 * An object used to locate the [AstNode] associated with a source range. 3749 * An object used to locate the [AstNode] associated with a source range.
3744 * More specifically, they will return the deepest [AstNode] which completely 3750 * More specifically, they will return the deepest [AstNode] which completely
3745 * encompasses the specified range. 3751 * encompasses the specified range.
3746 */ 3752 */
(...skipping 27 matching lines...) Expand all
3774 /** 3780 /**
3775 * Search within the given AST [node] and return the node that was found, 3781 * Search within the given AST [node] and return the node that was found,
3776 * or `null` if no node was found. 3782 * or `null` if no node was found.
3777 */ 3783 */
3778 AstNode searchWithin(AstNode node) { 3784 AstNode searchWithin(AstNode node) {
3779 if (node == null) { 3785 if (node == null) {
3780 return null; 3786 return null;
3781 } 3787 }
3782 try { 3788 try {
3783 node.accept(this); 3789 node.accept(this);
3784 } on NodeLocator_NodeFoundException {} catch (exception, stackTrace) { 3790 } catch (exception, stackTrace) {
3785 AnalysisEngine.instance.logger.logInformation( 3791 AnalysisEngine.instance.logger.logInformation(
3786 "Unable to locate element at offset ($_startOffset - $_endOffset)", 3792 "Unable to locate element at offset ($_startOffset - $_endOffset)",
3787 new CaughtException(exception, stackTrace)); 3793 new CaughtException(exception, stackTrace));
3788 return null; 3794 return null;
3789 } 3795 }
3790 return _foundNode; 3796 return _foundNode;
3791 } 3797 }
3792 3798
3793 @override 3799 @override
3794 Object visitNode(AstNode node) { 3800 Object visitNode(AstNode node) {
3801 // Don't visit a new tree if the result has been already found.
3802 if (_foundNode != null) {
3803 return null;
3804 }
3805 // Check whether the current node covers the selection.
3795 Token beginToken = node.beginToken; 3806 Token beginToken = node.beginToken;
3796 Token endToken = node.endToken; 3807 Token endToken = node.endToken;
3797 // Don't include synthetic tokens. 3808 // Don't include synthetic tokens.
3798 while (endToken != beginToken) { 3809 while (endToken != beginToken) {
3799 if (endToken.type == TokenType.EOF || !endToken.isSynthetic) { 3810 if (endToken.type == TokenType.EOF || !endToken.isSynthetic) {
3800 break; 3811 break;
3801 } 3812 }
3802 endToken = endToken.previous; 3813 endToken = endToken.previous;
3803 } 3814 }
3804 int end = endToken.end; 3815 int end = endToken.end;
3805 int start = node.offset; 3816 int start = node.offset;
3806 if (end <= _startOffset) { 3817 if (end <= _startOffset) {
3807 return null; 3818 return null;
3808 } 3819 }
3809 if (start > _endOffset) { 3820 if (start > _endOffset) {
3810 return null; 3821 return null;
3811 } 3822 }
3823 // Check children.
3812 try { 3824 try {
3813 node.visitChildren(this); 3825 node.visitChildren(this);
3814 } on NodeLocator_NodeFoundException {
3815 rethrow;
3816 } catch (exception, stackTrace) { 3826 } catch (exception, stackTrace) {
3817 // Ignore the exception and proceed in order to visit the rest of the 3827 // Ignore the exception and proceed in order to visit the rest of the
3818 // structure. 3828 // structure.
3819 AnalysisEngine.instance.logger.logInformation( 3829 AnalysisEngine.instance.logger.logInformation(
3820 "Exception caught while traversing an AST structure.", 3830 "Exception caught while traversing an AST structure.",
3821 new CaughtException(exception, stackTrace)); 3831 new CaughtException(exception, stackTrace));
3822 } 3832 }
3833 // Found a child.
3834 if (_foundNode != null) {
3835 return null;
3836 }
3837 // Check this node.
3823 if (start <= _startOffset && _endOffset < end) { 3838 if (start <= _startOffset && _endOffset < end) {
3824 _foundNode = node; 3839 _foundNode = node;
3825 throw new NodeLocator_NodeFoundException();
3826 } 3840 }
3827 return null; 3841 return null;
3828 } 3842 }
3829 } 3843 }
3830 3844
3831 /** 3845 /**
3832 * An exception used by [NodeLocator] to cancel visiting after a node has been
3833 * found.
3834 */
3835 class NodeLocator_NodeFoundException extends RuntimeException {}
3836
3837 /**
3838 * An object that will replace one child node in an AST node with another node. 3846 * An object that will replace one child node in an AST node with another node.
3839 */ 3847 */
3840 class NodeReplacer implements AstVisitor<bool> { 3848 class NodeReplacer implements AstVisitor<bool> {
3841 /** 3849 /**
3842 * The node being replaced. 3850 * The node being replaced.
3843 */ 3851 */
3844 final AstNode _oldNode; 3852 final AstNode _oldNode;
3845 3853
3846 /** 3854 /**
3847 * The node that is replacing the old node. 3855 * The node that is replacing the old node.
(...skipping 3919 matching lines...) Expand 10 before | Expand all | Expand 10 after
7767 * Safely visit the given [token], printing the [suffix] after the token if it 7775 * Safely visit the given [token], printing the [suffix] after the token if it
7768 * is non-`null`. 7776 * is non-`null`.
7769 */ 7777 */
7770 void _visitTokenWithSuffix(Token token, String suffix) { 7778 void _visitTokenWithSuffix(Token token, String suffix) {
7771 if (token != null) { 7779 if (token != null) {
7772 _writer.print(token.lexeme); 7780 _writer.print(token.lexeme);
7773 _writer.print(suffix); 7781 _writer.print(suffix);
7774 } 7782 }
7775 } 7783 }
7776 } 7784 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698