OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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 parser_helper; | 5 library parser_helper; |
6 | 6 |
7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 | 8 |
9 import "package:compiler/src/elements/elements.dart"; | 9 import "package:compiler/src/elements/elements.dart"; |
10 import 'package:compiler/src/id_generator.dart'; | 10 import 'package:compiler/src/id_generator.dart'; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 } | 49 } |
50 | 50 |
51 void internalError(node, String message) { | 51 void internalError(node, String message) { |
52 log(message); | 52 log(message); |
53 } | 53 } |
54 | 54 |
55 SourceSpan spanFromSpannable(node) { | 55 SourceSpan spanFromSpannable(node) { |
56 throw 'unsupported operation'; | 56 throw 'unsupported operation'; |
57 } | 57 } |
58 | 58 |
59 void reportError( | 59 void reportError(DiagnosticMessage message, |
60 DiagnosticMessage message, | |
61 [List<DiagnosticMessage> infos = const <DiagnosticMessage>[]]) { | 60 [List<DiagnosticMessage> infos = const <DiagnosticMessage>[]]) { |
62 log(message); | 61 log(message); |
63 infos.forEach(log); | 62 infos.forEach(log); |
64 } | 63 } |
65 | 64 |
66 void reportWarning( | 65 void reportWarning(DiagnosticMessage message, |
67 DiagnosticMessage message, | |
68 [List<DiagnosticMessage> infos = const <DiagnosticMessage>[]]) { | 66 [List<DiagnosticMessage> infos = const <DiagnosticMessage>[]]) { |
69 log(message); | 67 log(message); |
70 infos.forEach(log); | 68 infos.forEach(log); |
71 } | 69 } |
72 | 70 |
73 void reportInfo(Spannable node, MessageKind errorCode, | 71 void reportInfo(Spannable node, MessageKind errorCode, |
74 [Map arguments = const {}]) { | 72 [Map arguments = const {}]) { |
75 log(new Message(MessageTemplate.TEMPLATES[errorCode], arguments, false)); | 73 log(new Message(MessageTemplate.TEMPLATES[errorCode], arguments, false)); |
76 } | 74 } |
77 | 75 |
78 void reportHint( | 76 void reportHint(DiagnosticMessage message, |
79 DiagnosticMessage message, | |
80 [List<DiagnosticMessage> infos = const <DiagnosticMessage>[]]) { | 77 [List<DiagnosticMessage> infos = const <DiagnosticMessage>[]]) { |
81 log(message); | 78 log(message); |
82 infos.forEach(log); | 79 infos.forEach(log); |
83 } | 80 } |
84 | 81 |
85 withCurrentElement(Element element, f()) => f(); | 82 withCurrentElement(Element element, f()) => f(); |
86 | 83 |
87 @override | 84 @override |
88 DiagnosticMessage createMessage( | 85 DiagnosticMessage createMessage(Spannable spannable, MessageKind messageKind, |
89 Spannable spannable, | |
90 MessageKind messageKind, | |
91 [Map arguments = const {}]) { | 86 [Map arguments = const {}]) { |
92 return new DiagnosticMessage( | 87 return new DiagnosticMessage(null, spannable, |
93 null, spannable, | |
94 new Message(MessageTemplate.TEMPLATES[messageKind], arguments, false)); | 88 new Message(MessageTemplate.TEMPLATES[messageKind], arguments, false)); |
95 } | 89 } |
96 | 90 |
97 @override | 91 @override |
98 bool get hasReportedError => false; | 92 bool get hasReportedError => false; |
99 } | 93 } |
100 | 94 |
101 Token scan(String text) => | 95 Token scan(String text) => new StringScanner.fromString(text).tokenize(); |
102 new StringScanner.fromString(text) | |
103 .tokenize(); | |
104 | 96 |
105 Node parseBodyCode(String text, Function parseMethod, | 97 Node parseBodyCode(String text, Function parseMethod, |
106 {DiagnosticReporter reporter}) { | 98 {DiagnosticReporter reporter}) { |
107 Token tokens = scan(text); | 99 Token tokens = scan(text); |
108 if (reporter == null) reporter = new LoggerCanceler(); | 100 if (reporter == null) reporter = new LoggerCanceler(); |
109 Uri uri = new Uri(scheme: "source"); | 101 Uri uri = new Uri(scheme: "source"); |
110 Script script = new Script(uri, uri,new MockFile(text)); | 102 Script script = new Script(uri, uri, new MockFile(text)); |
111 LibraryElement library = new LibraryElementX(script); | 103 LibraryElement library = new LibraryElementX(script); |
112 NodeListener listener = new NodeListener( | 104 NodeListener listener = new NodeListener( |
113 new ScannerOptions(canUseNative: true), | 105 new ScannerOptions(canUseNative: true), |
114 reporter, library.entryCompilationUnit); | 106 reporter, |
| 107 library.entryCompilationUnit); |
115 Parser parser = new Parser(listener, new MockParserOptions()); | 108 Parser parser = new Parser(listener, new MockParserOptions()); |
116 Token endToken = parseMethod(parser, tokens); | 109 Token endToken = parseMethod(parser, tokens); |
117 assert(endToken.kind == EOF_TOKEN); | 110 assert(endToken.kind == EOF_TOKEN); |
118 Node node = listener.popNode(); | 111 Node node = listener.popNode(); |
119 Expect.isNotNull(node); | 112 Expect.isNotNull(node); |
120 Expect.isTrue(listener.nodes.isEmpty, 'Not empty: ${listener.nodes}'); | 113 Expect.isTrue(listener.nodes.isEmpty, 'Not empty: ${listener.nodes}'); |
121 return node; | 114 return node; |
122 } | 115 } |
123 | 116 |
124 Node parseStatement(String text) => | 117 Node parseStatement(String text) => |
125 parseBodyCode(text, (parser, tokens) => parser.parseStatement(tokens)); | 118 parseBodyCode(text, (parser, tokens) => parser.parseStatement(tokens)); |
126 | 119 |
127 Node parseFunction(String text, Compiler compiler) { | 120 Node parseFunction(String text, Compiler compiler) { |
128 ElementX element = parseUnit(text, compiler, compiler.mainApp).head; | 121 ElementX element = parseUnit(text, compiler, compiler.mainApp).head; |
129 Expect.isNotNull(element); | 122 Expect.isNotNull(element); |
130 Expect.equals(ElementKind.FUNCTION, element.kind); | 123 Expect.equals(ElementKind.FUNCTION, element.kind); |
131 return element.parseNode(compiler.parsingContext); | 124 return element.parseNode(compiler.parsingContext); |
132 } | 125 } |
133 | 126 |
134 Node parseMember(String text, {DiagnosticReporter reporter}) { | 127 Node parseMember(String text, {DiagnosticReporter reporter}) { |
135 return parseBodyCode( | 128 return parseBodyCode(text, (parser, tokens) => parser.parseMember(tokens), |
136 text, | |
137 (parser, tokens) => parser.parseMember(tokens), | |
138 reporter: reporter); | 129 reporter: reporter); |
139 } | 130 } |
140 | 131 |
141 class MockFile extends StringSourceFile { | 132 class MockFile extends StringSourceFile { |
142 MockFile(text) | 133 MockFile(text) : super.fromName('<string>', text); |
143 : super.fromName('<string>', text); | |
144 } | 134 } |
145 | 135 |
146 var sourceCounter = 0; | 136 var sourceCounter = 0; |
147 | 137 |
148 Link<Element> parseUnit(String text, Compiler compiler, | 138 Link<Element> parseUnit(String text, Compiler compiler, LibraryElement library, |
149 LibraryElement library, | 139 [void registerSource(Uri uri, String source)]) { |
150 [void registerSource(Uri uri, String source)]) { | |
151 Token tokens = scan(text); | 140 Token tokens = scan(text); |
152 Uri uri = new Uri(scheme: "source", path: '${++sourceCounter}'); | 141 Uri uri = new Uri(scheme: "source", path: '${++sourceCounter}'); |
153 if (registerSource != null) { | 142 if (registerSource != null) { |
154 registerSource(uri, text); | 143 registerSource(uri, text); |
155 } | 144 } |
156 var script = new Script(uri, uri, new MockFile(text)); | 145 var script = new Script(uri, uri, new MockFile(text)); |
157 var unit = new CompilationUnitElementX(script, library); | 146 var unit = new CompilationUnitElementX(script, library); |
158 DiagnosticReporter reporter = compiler.reporter; | 147 DiagnosticReporter reporter = compiler.reporter; |
159 ElementListener listener = new ElementListener( | 148 ElementListener listener = new ElementListener( |
160 compiler.parsingContext.getScannerOptionsFor(library), | 149 compiler.parsingContext.getScannerOptionsFor(library), |
161 reporter, unit, new IdGenerator()); | 150 reporter, |
| 151 unit, |
| 152 new IdGenerator()); |
162 PartialParser parser = new PartialParser(listener, new MockParserOptions()); | 153 PartialParser parser = new PartialParser(listener, new MockParserOptions()); |
163 reporter.withCurrentElement(unit, () => parser.parseUnit(tokens)); | 154 reporter.withCurrentElement(unit, () => parser.parseUnit(tokens)); |
164 return unit.localMembers; | 155 return unit.localMembers; |
165 } | 156 } |
166 | 157 |
167 NodeList fullParseUnit(String source, {DiagnosticReporter reporter}) { | 158 NodeList fullParseUnit(String source, {DiagnosticReporter reporter}) { |
168 return parseBodyCode( | 159 return parseBodyCode(source, (parser, tokens) => parser.parseUnit(tokens), |
169 source, | |
170 (parser, tokens) => parser.parseUnit(tokens), | |
171 reporter: reporter); | 160 reporter: reporter); |
172 } | 161 } |
OLD | NEW |