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

Side by Side Diff: pkg/analyzer/lib/dart/ast/visitor.dart

Issue 2623453003: Add visitors that throw by default (Closed)
Patch Set: rework implementation Created 3 years, 11 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
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 /** 5 /**
6 * Defines AST visitors that support useful patterns for visiting the nodes in 6 * Defines AST visitors that support useful patterns for visiting the nodes in
7 * an [AST structure](ast.dart). 7 * an [AST structure](ast.dart).
8 * 8 *
9 * Dart is an evolving language, and the AST structure must evolved with it. 9 * Dart is an evolving language, and the AST structure must evolved with it.
10 * When the AST structure changes, the visitor interface will sometimes change 10 * When the AST structure changes, the visitor interface will sometimes change
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 * children (either directly or indirectly). Failure to do will break the order 43 * children (either directly or indirectly). Failure to do will break the order
44 * in which nodes are visited. 44 * in which nodes are visited.
45 * 45 *
46 * Note that, unlike other visitors that begin to visit a structure of nodes by 46 * Note that, unlike other visitors that begin to visit a structure of nodes by
47 * asking the root node in the structure to accept the visitor, this visitor 47 * asking the root node in the structure to accept the visitor, this visitor
48 * requires that clients start the visit by invoking the method [visitAllNodes] 48 * requires that clients start the visit by invoking the method [visitAllNodes]
49 * defined on the visitor with the root node as the argument: 49 * defined on the visitor with the root node as the argument:
50 * 50 *
51 * visitor.visitAllNodes(rootNode); 51 * visitor.visitAllNodes(rootNode);
52 * 52 *
53 * Clients may extend or implement this class. 53 * Clients may extend this class.
54 */ 54 */
55 class BreadthFirstVisitor<R> extends GeneralizingAstVisitor<R> { 55 class BreadthFirstVisitor<R> extends GeneralizingAstVisitor<R> {
56 /** 56 /**
57 * A queue holding the nodes that have not yet been visited in the order in 57 * A queue holding the nodes that have not yet been visited in the order in
58 * which they ought to be visited. 58 * which they ought to be visited.
59 */ 59 */
60 Queue<AstNode> _queue = new Queue<AstNode>(); 60 Queue<AstNode> _queue = new Queue<AstNode>();
61 61
62 /** 62 /**
63 * A visitor, used to visit the children of the current node, that will add 63 * A visitor, used to visit the children of the current node, that will add
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 * to be invoked but will also cause the methods [visitStatement] and 140 * to be invoked but will also cause the methods [visitStatement] and
141 * [visitNode] to be subsequently invoked. This allows visitors to be written 141 * [visitNode] to be subsequently invoked. This allows visitors to be written
142 * that visit all statements without needing to override the visit method for 142 * that visit all statements without needing to override the visit method for
143 * each of the specific subclasses of [Statement]. 143 * each of the specific subclasses of [Statement].
144 * 144 *
145 * Subclasses that override a visit method must either invoke the overridden 145 * Subclasses that override a visit method must either invoke the overridden
146 * visit method or explicitly invoke the more general visit method. Failure to 146 * visit method or explicitly invoke the more general visit method. Failure to
147 * do so will cause the visit methods for superclasses of the node to not be 147 * do so will cause the visit methods for superclasses of the node to not be
148 * invoked and will cause the children of the visited node to not be visited. 148 * invoked and will cause the children of the visited node to not be visited.
149 * 149 *
150 * Clients may extend or implement this class. 150 * Clients may extend this class.
151 */ 151 */
152 class GeneralizingAstVisitor<R> implements AstVisitor<R> { 152 class GeneralizingAstVisitor<R> implements AstVisitor<R> {
153 @override 153 @override
154 R visitAdjacentStrings(AdjacentStrings node) => visitStringLiteral(node); 154 R visitAdjacentStrings(AdjacentStrings node) => visitStringLiteral(node);
155 155
156 R visitAnnotatedNode(AnnotatedNode node) => visitNode(node); 156 R visitAnnotatedNode(AnnotatedNode node) => visitNode(node);
157 157
158 @override 158 @override
159 R visitAnnotation(Annotation node) => visitNode(node); 159 R visitAnnotation(Annotation node) => visitNode(node);
160 160
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 /** 569 /**
570 * An AST visitor that will recursively visit all of the nodes in an AST 570 * An AST visitor that will recursively visit all of the nodes in an AST
571 * structure. For example, using an instance of this class to visit a [Block] 571 * structure. For example, using an instance of this class to visit a [Block]
572 * will also cause all of the statements in the block to be visited. 572 * will also cause all of the statements in the block to be visited.
573 * 573 *
574 * Subclasses that override a visit method must either invoke the overridden 574 * Subclasses that override a visit method must either invoke the overridden
575 * visit method or must explicitly ask the visited node to visit its children. 575 * visit method or must explicitly ask the visited node to visit its children.
576 * Failure to do so will cause the children of the visited node to not be 576 * Failure to do so will cause the children of the visited node to not be
577 * visited. 577 * visited.
578 * 578 *
579 * Clients may extend or implement this class. 579 * Clients may extend this class.
580 */ 580 */
581 class RecursiveAstVisitor<R> implements AstVisitor<R> { 581 class RecursiveAstVisitor<R> implements AstVisitor<R> {
582 @override 582 @override
583 R visitAdjacentStrings(AdjacentStrings node) { 583 R visitAdjacentStrings(AdjacentStrings node) {
584 node.visitChildren(this); 584 node.visitChildren(this);
585 return null; 585 return null;
586 } 586 }
587 587
588 @override 588 @override
589 R visitAnnotation(Annotation node) { 589 R visitAnnotation(Annotation node) {
(...skipping 644 matching lines...) Expand 10 before | Expand all | Expand 10 after
1234 return null; 1234 return null;
1235 } 1235 }
1236 } 1236 }
1237 1237
1238 /** 1238 /**
1239 * An AST visitor that will do nothing when visiting an AST node. It is intended 1239 * An AST visitor that will do nothing when visiting an AST node. It is intended
1240 * to be a superclass for classes that use the visitor pattern primarily as a 1240 * to be a superclass for classes that use the visitor pattern primarily as a
1241 * dispatch mechanism (and hence don't need to recursively visit a whole 1241 * dispatch mechanism (and hence don't need to recursively visit a whole
1242 * structure) and that only need to visit a small number of node types. 1242 * structure) and that only need to visit a small number of node types.
1243 * 1243 *
1244 * Clients may extend or implement this class. 1244 * Clients may extend this class.
1245 */ 1245 */
1246 class SimpleAstVisitor<R> implements AstVisitor<R> { 1246 class SimpleAstVisitor<R> implements AstVisitor<R> {
1247 @override 1247 @override
1248 R visitAdjacentStrings(AdjacentStrings node) => null; 1248 R visitAdjacentStrings(AdjacentStrings node) => null;
1249 1249
1250 @override 1250 @override
1251 R visitAnnotation(Annotation node) => null; 1251 R visitAnnotation(Annotation node) => null;
1252 1252
1253 @override 1253 @override
1254 R visitArgumentList(ArgumentList node) => null; 1254 R visitArgumentList(ArgumentList node) => null;
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
1572 R visitWhileStatement(WhileStatement node) => null; 1572 R visitWhileStatement(WhileStatement node) => null;
1573 1573
1574 @override 1574 @override
1575 R visitWithClause(WithClause node) => null; 1575 R visitWithClause(WithClause node) => null;
1576 1576
1577 @override 1577 @override
1578 R visitYieldStatement(YieldStatement node) => null; 1578 R visitYieldStatement(YieldStatement node) => null;
1579 } 1579 }
1580 1580
1581 /** 1581 /**
1582 * An AST Visitor that captures visit call timings. 1582 * An AST visitor that will throw an exception if any of the visit methods that
1583 * are invoked have not been overridden. It is intended to be a superclass for
1584 * classes that implement the visitor pattern and need to override all of the
1585 * visit methods.
Paul Berry 2017/01/09 17:36:51 It would also be useful for classes that implement
Brian Wilkerson 2017/01/09 18:08:07 Updated comment.
1586 *
1587 * Clients may extend this class.
1588 */
1589 class ThrowingAstVisitor<R> implements AstVisitor<R> {
1590 @override
1591 R visitAdjacentStrings(AdjacentStrings node) => _throw(node);
1592
1593 @override
1594 R visitAnnotation(Annotation node) => _throw(node);
1595
1596 @override
1597 R visitArgumentList(ArgumentList node) => _throw(node);
1598
1599 @override
1600 R visitAsExpression(AsExpression node) => _throw(node);
1601
1602 @override
1603 R visitAssertInitializer(AssertInitializer node) => _throw(node);
1604
1605 @override
1606 R visitAssertStatement(AssertStatement node) => _throw(node);
1607
1608 @override
1609 R visitAssignmentExpression(AssignmentExpression node) => _throw(node);
1610
1611 @override
1612 R visitAwaitExpression(AwaitExpression node) => _throw(node);
1613
1614 @override
1615 R visitBinaryExpression(BinaryExpression node) => _throw(node);
1616
1617 @override
1618 R visitBlock(Block node) => _throw(node);
1619
1620 @override
1621 R visitBlockFunctionBody(BlockFunctionBody node) => _throw(node);
1622
1623 @override
1624 R visitBooleanLiteral(BooleanLiteral node) => _throw(node);
1625
1626 @override
1627 R visitBreakStatement(BreakStatement node) => _throw(node);
1628
1629 @override
1630 R visitCascadeExpression(CascadeExpression node) => _throw(node);
1631
1632 @override
1633 R visitCatchClause(CatchClause node) => _throw(node);
1634
1635 @override
1636 R visitClassDeclaration(ClassDeclaration node) => _throw(node);
1637
1638 @override
1639 R visitClassTypeAlias(ClassTypeAlias node) => _throw(node);
1640
1641 @override
1642 R visitComment(Comment node) => _throw(node);
1643
1644 @override
1645 R visitCommentReference(CommentReference node) => _throw(node);
1646
1647 @override
1648 R visitCompilationUnit(CompilationUnit node) => _throw(node);
1649
1650 @override
1651 R visitConditionalExpression(ConditionalExpression node) => _throw(node);
1652
1653 @override
1654 R visitConfiguration(Configuration node) => _throw(node);
1655
1656 @override
1657 R visitConstructorDeclaration(ConstructorDeclaration node) => _throw(node);
1658
1659 @override
1660 R visitConstructorFieldInitializer(ConstructorFieldInitializer node) =>
1661 _throw(node);
1662
1663 @override
1664 R visitConstructorName(ConstructorName node) => _throw(node);
1665
1666 @override
1667 R visitContinueStatement(ContinueStatement node) => _throw(node);
1668
1669 @override
1670 R visitDeclaredIdentifier(DeclaredIdentifier node) => _throw(node);
1671
1672 @override
1673 R visitDefaultFormalParameter(DefaultFormalParameter node) => _throw(node);
1674
1675 @override
1676 R visitDoStatement(DoStatement node) => _throw(node);
1677
1678 @override
1679 R visitDottedName(DottedName node) => _throw(node);
1680
1681 @override
1682 R visitDoubleLiteral(DoubleLiteral node) => _throw(node);
1683
1684 @override
1685 R visitEmptyFunctionBody(EmptyFunctionBody node) => _throw(node);
1686
1687 @override
1688 R visitEmptyStatement(EmptyStatement node) => _throw(node);
1689
1690 @override
1691 R visitEnumConstantDeclaration(EnumConstantDeclaration node) => _throw(node);
1692
1693 @override
1694 R visitEnumDeclaration(EnumDeclaration node) => _throw(node);
1695
1696 @override
1697 R visitExportDirective(ExportDirective node) => _throw(node);
1698
1699 @override
1700 R visitExpressionFunctionBody(ExpressionFunctionBody node) => _throw(node);
1701
1702 @override
1703 R visitExpressionStatement(ExpressionStatement node) => _throw(node);
1704
1705 @override
1706 R visitExtendsClause(ExtendsClause node) => _throw(node);
1707
1708 @override
1709 R visitFieldDeclaration(FieldDeclaration node) => _throw(node);
1710
1711 @override
1712 R visitFieldFormalParameter(FieldFormalParameter node) => _throw(node);
1713
1714 @override
1715 R visitForEachStatement(ForEachStatement node) => _throw(node);
1716
1717 @override
1718 R visitFormalParameterList(FormalParameterList node) => _throw(node);
1719
1720 @override
1721 R visitForStatement(ForStatement node) => _throw(node);
1722
1723 @override
1724 R visitFunctionDeclaration(FunctionDeclaration node) => _throw(node);
1725
1726 @override
1727 R visitFunctionDeclarationStatement(FunctionDeclarationStatement node) =>
1728 _throw(node);
1729
1730 @override
1731 R visitFunctionExpression(FunctionExpression node) => _throw(node);
1732
1733 @override
1734 R visitFunctionExpressionInvocation(FunctionExpressionInvocation node) =>
1735 _throw(node);
1736
1737 @override
1738 R visitFunctionTypeAlias(FunctionTypeAlias node) => _throw(node);
1739
1740 @override
1741 R visitFunctionTypedFormalParameter(FunctionTypedFormalParameter node) =>
1742 _throw(node);
1743
1744 @override
1745 R visitHideCombinator(HideCombinator node) => _throw(node);
1746
1747 @override
1748 R visitIfStatement(IfStatement node) => _throw(node);
1749
1750 @override
1751 R visitImplementsClause(ImplementsClause node) => _throw(node);
1752
1753 @override
1754 R visitImportDirective(ImportDirective node) => _throw(node);
1755
1756 @override
1757 R visitIndexExpression(IndexExpression node) => _throw(node);
1758
1759 @override
1760 R visitInstanceCreationExpression(InstanceCreationExpression node) =>
1761 _throw(node);
1762
1763 @override
1764 R visitIntegerLiteral(IntegerLiteral node) => _throw(node);
1765
1766 @override
1767 R visitInterpolationExpression(InterpolationExpression node) => _throw(node);
1768
1769 @override
1770 R visitInterpolationString(InterpolationString node) => _throw(node);
1771
1772 @override
1773 R visitIsExpression(IsExpression node) => _throw(node);
1774
1775 @override
1776 R visitLabel(Label node) => _throw(node);
1777
1778 @override
1779 R visitLabeledStatement(LabeledStatement node) => _throw(node);
1780
1781 @override
1782 R visitLibraryDirective(LibraryDirective node) => _throw(node);
1783
1784 @override
1785 R visitLibraryIdentifier(LibraryIdentifier node) => _throw(node);
1786
1787 @override
1788 R visitListLiteral(ListLiteral node) => _throw(node);
1789
1790 @override
1791 R visitMapLiteral(MapLiteral node) => _throw(node);
1792
1793 @override
1794 R visitMapLiteralEntry(MapLiteralEntry node) => _throw(node);
1795
1796 @override
1797 R visitMethodDeclaration(MethodDeclaration node) => _throw(node);
1798
1799 @override
1800 R visitMethodInvocation(MethodInvocation node) => _throw(node);
1801
1802 @override
1803 R visitNamedExpression(NamedExpression node) => _throw(node);
1804
1805 @override
1806 R visitNativeClause(NativeClause node) => _throw(node);
1807
1808 @override
1809 R visitNativeFunctionBody(NativeFunctionBody node) => _throw(node);
1810
1811 @override
1812 R visitNullLiteral(NullLiteral node) => _throw(node);
1813
1814 @override
1815 R visitParenthesizedExpression(ParenthesizedExpression node) => _throw(node);
1816
1817 @override
1818 R visitPartDirective(PartDirective node) => _throw(node);
1819
1820 @override
1821 R visitPartOfDirective(PartOfDirective node) => _throw(node);
1822
1823 @override
1824 R visitPostfixExpression(PostfixExpression node) => _throw(node);
1825
1826 @override
1827 R visitPrefixedIdentifier(PrefixedIdentifier node) => _throw(node);
1828
1829 @override
1830 R visitPrefixExpression(PrefixExpression node) => _throw(node);
1831
1832 @override
1833 R visitPropertyAccess(PropertyAccess node) => _throw(node);
1834
1835 @override
1836 R visitRedirectingConstructorInvocation(
1837 RedirectingConstructorInvocation node) =>
1838 _throw(node);
1839
1840 @override
1841 R visitRethrowExpression(RethrowExpression node) => _throw(node);
1842
1843 @override
1844 R visitReturnStatement(ReturnStatement node) => _throw(node);
1845
1846 @override
1847 R visitScriptTag(ScriptTag node) => _throw(node);
1848
1849 @override
1850 R visitShowCombinator(ShowCombinator node) => _throw(node);
1851
1852 @override
1853 R visitSimpleFormalParameter(SimpleFormalParameter node) => _throw(node);
1854
1855 @override
1856 R visitSimpleIdentifier(SimpleIdentifier node) => _throw(node);
1857
1858 @override
1859 R visitSimpleStringLiteral(SimpleStringLiteral node) => _throw(node);
1860
1861 @override
1862 R visitStringInterpolation(StringInterpolation node) => _throw(node);
1863
1864 @override
1865 R visitSuperConstructorInvocation(SuperConstructorInvocation node) =>
1866 _throw(node);
1867
1868 @override
1869 R visitSuperExpression(SuperExpression node) => _throw(node);
1870
1871 @override
1872 R visitSwitchCase(SwitchCase node) => _throw(node);
1873
1874 @override
1875 R visitSwitchDefault(SwitchDefault node) => _throw(node);
1876
1877 @override
1878 R visitSwitchStatement(SwitchStatement node) => _throw(node);
1879
1880 @override
1881 R visitSymbolLiteral(SymbolLiteral node) => _throw(node);
1882
1883 @override
1884 R visitThisExpression(ThisExpression node) => _throw(node);
1885
1886 @override
1887 R visitThrowExpression(ThrowExpression node) => _throw(node);
1888
1889 @override
1890 R visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) =>
1891 _throw(node);
1892
1893 @override
1894 R visitTryStatement(TryStatement node) => _throw(node);
1895
1896 @override
1897 R visitTypeArgumentList(TypeArgumentList node) => _throw(node);
1898
1899 @override
1900 R visitTypeName(TypeName node) => _throw(node);
1901
1902 @override
1903 R visitTypeParameter(TypeParameter node) => _throw(node);
1904
1905 @override
1906 R visitTypeParameterList(TypeParameterList node) => _throw(node);
1907
1908 @override
1909 R visitVariableDeclaration(VariableDeclaration node) => _throw(node);
1910
1911 @override
1912 R visitVariableDeclarationList(VariableDeclarationList node) => _throw(node);
1913
1914 @override
1915 R visitVariableDeclarationStatement(VariableDeclarationStatement node) =>
1916 _throw(node);
1917
1918 @override
1919 R visitWhileStatement(WhileStatement node) => _throw(node);
1920
1921 @override
1922 R visitWithClause(WithClause node) => _throw(node);
1923
1924 @override
1925 R visitYieldStatement(YieldStatement node) => _throw(node);
1926
1927 R _throw(AstNode node) {
1928 throw new Exception('Missing implementation of visit${node.runtimeType}');
1929 }
1930 }
1931
1932 /**
1933 * An AST visitor that captures visit call timings.
1934 *
1935 * Clients may not extend, implement or mix-in this class.
1583 */ 1936 */
1584 class TimedAstVisitor<T> implements AstVisitor<T> { 1937 class TimedAstVisitor<T> implements AstVisitor<T> {
1585 /** 1938 /**
1586 * The base visitor whose visit methods will be timed. 1939 * The base visitor whose visit methods will be timed.
1587 */ 1940 */
1588 final AstVisitor<T> _baseVisitor; 1941 final AstVisitor<T> _baseVisitor;
1589 1942
1590 /** 1943 /**
1591 * Collects elapsed time for visit calls. 1944 * Collects elapsed time for visit calls.
1592 */ 1945 */
(...skipping 883 matching lines...) Expand 10 before | Expand all | Expand 10 after
2476 /** 2829 /**
2477 * An AST visitor that will recursively visit all of the nodes in an AST 2830 * An AST visitor that will recursively visit all of the nodes in an AST
2478 * structure (like instances of the class [RecursiveAstVisitor]). In addition, 2831 * structure (like instances of the class [RecursiveAstVisitor]). In addition,
2479 * every node will also be visited by using a single unified [visitNode] method. 2832 * every node will also be visited by using a single unified [visitNode] method.
2480 * 2833 *
2481 * Subclasses that override a visit method must either invoke the overridden 2834 * Subclasses that override a visit method must either invoke the overridden
2482 * visit method or explicitly invoke the more general [visitNode] method. 2835 * visit method or explicitly invoke the more general [visitNode] method.
2483 * Failure to do so will cause the children of the visited node to not be 2836 * Failure to do so will cause the children of the visited node to not be
2484 * visited. 2837 * visited.
2485 * 2838 *
2486 * Clients may extend or implement this class. 2839 * Clients may extend this class.
2487 */ 2840 */
2488 class UnifyingAstVisitor<R> implements AstVisitor<R> { 2841 class UnifyingAstVisitor<R> implements AstVisitor<R> {
2489 @override 2842 @override
2490 R visitAdjacentStrings(AdjacentStrings node) => visitNode(node); 2843 R visitAdjacentStrings(AdjacentStrings node) => visitNode(node);
2491 2844
2492 @override 2845 @override
2493 R visitAnnotation(Annotation node) => visitNode(node); 2846 R visitAnnotation(Annotation node) => visitNode(node);
2494 2847
2495 @override 2848 @override
2496 R visitArgumentList(ArgumentList node) => visitNode(node); 2849 R visitArgumentList(ArgumentList node) => visitNode(node);
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
2847 * Initialize a newly created visitor to help the [outerVisitor]. 3200 * Initialize a newly created visitor to help the [outerVisitor].
2848 */ 3201 */
2849 _BreadthFirstChildVisitor(this.outerVisitor); 3202 _BreadthFirstChildVisitor(this.outerVisitor);
2850 3203
2851 @override 3204 @override
2852 Object visitNode(AstNode node) { 3205 Object visitNode(AstNode node) {
2853 outerVisitor._queue.add(node); 3206 outerVisitor._queue.add(node);
2854 return null; 3207 return null;
2855 } 3208 }
2856 } 3209 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698