OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | |
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.md file. | |
4 | |
5 library rasta.kernel.fall_through_visitor; | |
6 | |
7 import 'package:kernel/ast.dart' show | |
8 AssertStatement, | |
9 Block, | |
10 BreakStatement, | |
11 Catch, | |
12 ContinueSwitchStatement, | |
13 DoStatement, | |
14 EmptyStatement, | |
15 ExpressionStatement, | |
16 ForInStatement, | |
17 ForStatement, | |
18 FunctionDeclaration, | |
19 IfStatement, | |
20 InvalidStatement, | |
21 LabeledStatement, | |
22 ReturnStatement, | |
23 Statement, | |
24 StatementVisitor, | |
25 SwitchStatement, | |
26 Throw, | |
27 TryCatch, | |
28 TryFinally, | |
29 VariableDeclaration, | |
30 WhileStatement, | |
31 YieldStatement; | |
32 | |
33 /// Returns true if [node] would let execution reach the next node (aka | |
34 /// fall-through in switch cases). | |
35 bool fallsThrough(Statement node) => node.accept(const FallThroughVisitor()); | |
36 | |
37 /// Visitor implementing [computeFallThrough]. | |
38 class FallThroughVisitor implements StatementVisitor<bool> { | |
39 const FallThroughVisitor(); | |
40 | |
41 bool defaultStatement(Statement node) => throw "Not implemented."; | |
42 | |
43 bool visitInvalidStatement(InvalidStatement node) => false; | |
44 | |
45 bool visitExpressionStatement(ExpressionStatement node) { | |
46 return node.expression is! Throw; | |
47 } | |
48 | |
49 bool visitBlock(Block node) { | |
50 return node.statements.isEmpty || node.statements.last.accept(this); | |
kasperl
2016/08/04 11:20:38
Wouldn't it be better to loop through the statemen
ahe
2016/08/04 12:05:45
Thank you, good idea. I'll do that in a follow-up.
ahe
2016/08/04 12:27:27
Addressed in CL 2213933002.
| |
51 } | |
52 | |
53 bool visitEmptyStatement(EmptyStatement node) => true; | |
54 | |
55 bool visitAssertStatement(AssertStatement node) => true; | |
56 | |
57 bool visitLabeledStatement(LabeledStatement node) => true; | |
58 | |
59 bool visitBreakStatement(BreakStatement node) => false; | |
60 | |
61 bool visitWhileStatement(WhileStatement node) => true; | |
62 | |
63 bool visitDoStatement(DoStatement node) => node.body.accept(this); | |
64 | |
65 bool visitForStatement(ForStatement node) => true; | |
66 | |
67 bool visitForInStatement(ForInStatement node) => true; | |
68 | |
69 bool visitSwitchStatement(SwitchStatement node) => true; | |
70 | |
71 bool visitContinueSwitchStatement(ContinueSwitchStatement node) => false; | |
72 | |
73 bool visitIfStatement(IfStatement node) { | |
74 if (node.then == null || node.otherwise == null) return true; | |
75 return node.then.accept(this) || node.otherwise.accept(this); | |
76 } | |
77 | |
78 bool visitReturnStatement(ReturnStatement node) => false; | |
79 | |
80 bool visitTryCatch(TryCatch node) { | |
81 if (node.body.accept(this)) return true; | |
82 for (Catch catchNode in node.catches) { | |
83 if (catchNode.body.accept(this)) return true; | |
84 } | |
85 return false; | |
86 } | |
87 | |
88 bool visitTryFinally(TryFinally node) { | |
89 return node.body.accept(this) || node.finalizer.accept(this); | |
90 } | |
91 | |
92 bool visitYieldStatement(YieldStatement node) => true; | |
93 | |
94 bool visitVariableDeclaration(VariableDeclaration node) => true; | |
95 | |
96 bool visitFunctionDeclaration(FunctionDeclaration node) => true; | |
97 } | |
OLD | NEW |