OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class TreeValidatorTask extends CompilerTask { | 5 class TreeValidatorTask extends CompilerTask { |
6 TreeValidatorTask(Compiler compiler) : super(compiler); | 6 TreeValidatorTask(Compiler compiler) : super(compiler); |
7 | 7 |
8 void validate(Node tree) { | 8 void validate(Node tree) { |
9 assert(check(tree)); | 9 assert(check(tree)); |
10 } | 10 } |
11 | 11 |
12 bool check(Node tree) { | 12 bool check(Node tree) { |
13 List<InvalidNodeError> errors = []; | 13 List<InvalidNodeError> errors = []; |
14 void report(node, message) { | 14 void report(node, message) { |
15 final error = new InvalidNodeError(node, message); | 15 final error = new InvalidNodeError(node, message); |
16 errors.add(error); | 16 errors.add(error); |
17 compiler.reportWarning(node, message); | 17 compiler.reportWarning(node, message); |
18 }; | 18 }; |
19 final validator = new ValidatorVisitor(report); | 19 final validator = new ValidatorVisitor(report); |
20 tree.accept(new TraversingVisitor(validator)); | 20 tree.accept(new TraversingVisitor(validator)); |
21 | 21 |
22 return errors.isEmpty(); | 22 return errors.isEmpty; |
23 } | 23 } |
24 } | 24 } |
25 | 25 |
26 class ValidatorVisitor extends Visitor { | 26 class ValidatorVisitor extends Visitor { |
27 final Function reportInvalidNode; | 27 final Function reportInvalidNode; |
28 | 28 |
29 ValidatorVisitor(Function this.reportInvalidNode); | 29 ValidatorVisitor(Function this.reportInvalidNode); |
30 | 30 |
31 expect(Node node, bool test, [message]) { | 31 expect(Node node, bool test, [message]) { |
32 if (!test) reportInvalidNode(node, message); | 32 if (!test) reportInvalidNode(node, message); |
33 } | 33 } |
34 | 34 |
35 visitNode(Node node) {} | 35 visitNode(Node node) {} |
36 | 36 |
37 visitSendSet(SendSet node) { | 37 visitSendSet(SendSet node) { |
38 final selector = node.selector; | 38 final selector = node.selector; |
39 final name = node.assignmentOperator.source.stringValue; | 39 final name = node.assignmentOperator.source.stringValue; |
40 final arguments = node.arguments; | 40 final arguments = node.arguments; |
41 | 41 |
42 expect(node, arguments != null); | 42 expect(node, arguments != null); |
43 expect(node, selector is Identifier, 'selector is not assignable'); | 43 expect(node, selector is Identifier, 'selector is not assignable'); |
44 if (identical(name, '++') || identical(name, '--')) { | 44 if (identical(name, '++') || identical(name, '--')) { |
45 expect(node, node.assignmentOperator is Operator); | 45 expect(node, node.assignmentOperator is Operator); |
46 if (node.isIndex) { | 46 if (node.isIndex) { |
47 expect(node.arguments.tail.head, node.arguments.tail.isEmpty()); | 47 expect(node.arguments.tail.head, node.arguments.tail.isEmpty); |
48 } else { | 48 } else { |
49 expect(node.arguments.head, node.arguments.isEmpty()); | 49 expect(node.arguments.head, node.arguments.isEmpty); |
50 } | 50 } |
51 } else { | 51 } else { |
52 expect(node, !node.arguments.isEmpty()); | 52 expect(node, !node.arguments.isEmpty); |
53 } | 53 } |
54 } | 54 } |
55 | 55 |
56 visitReturn(Return node) { | 56 visitReturn(Return node) { |
57 if (!node.isRedirectingFactoryBody && node.hasExpression) { | 57 if (!node.isRedirectingFactoryBody && node.hasExpression) { |
58 // We allow non-expression expressions in Return nodes, but only when | 58 // We allow non-expression expressions in Return nodes, but only when |
59 // using them to hold redirecting factory constructors. | 59 // using them to hold redirecting factory constructors. |
60 expect(node, node.expression.asExpression() != null); | 60 expect(node, node.expression.asExpression() != null); |
61 } | 61 } |
62 } | 62 } |
63 } | 63 } |
64 | 64 |
65 class InvalidNodeError { | 65 class InvalidNodeError { |
66 final Node node; | 66 final Node node; |
67 final String message; | 67 final String message; |
68 InvalidNodeError(this.node, [this.message]); | 68 InvalidNodeError(this.node, [this.message]); |
69 | 69 |
70 toString() { | 70 toString() { |
71 String nodeString = node.toDebugString(); | 71 String nodeString = node.toDebugString(); |
72 String result = 'invalid node: $nodeString'; | 72 String result = 'invalid node: $nodeString'; |
73 if (message != null) result = '$result ($message)'; | 73 if (message != null) result = '$result ($message)'; |
74 return result; | 74 return result; |
75 } | 75 } |
76 } | 76 } |
OLD | NEW |