OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, 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 file. | |
4 | |
5 #library('parser_helper'); | |
6 | |
7 #import("dart:uri"); | |
8 | |
9 #import("../../../lib/compiler/implementation/elements/elements.dart"); | |
10 #import("../../../lib/compiler/implementation/tree/tree.dart"); | |
11 #import('../../../lib/compiler/implementation/scanner/scannerlib.dart'); | |
12 #import("../../../lib/compiler/implementation/leg.dart"); | |
13 #import("../../../lib/compiler/implementation/util/util.dart"); | |
14 | |
15 class LoggerCanceler implements DiagnosticListener { | |
16 void cancel([String reason, node, token, instruction, element]) { | |
17 throw new CompilerCancelledException(reason); | |
18 } | |
19 | |
20 void log(message) { | |
21 print(message); | |
22 } | |
23 } | |
24 | |
25 Token scan(String text) => new StringScanner(text).tokenize(); | |
26 | |
27 Node parseBodyCode(String text, Function parseMethod) { | |
28 Token tokens = scan(text); | |
29 LoggerCanceler lc = new LoggerCanceler(); | |
30 NodeListener listener = new NodeListener(lc, null); | |
31 Parser parser = new Parser(listener); | |
32 Token endToken = parseMethod(parser, tokens); | |
33 assert(endToken.kind == EOF_TOKEN); | |
34 Node node = listener.popNode(); | |
35 Expect.isNotNull(node); | |
36 Expect.isTrue(listener.nodes.isEmpty(), 'Not empty: ${listener.nodes}'); | |
37 return node; | |
38 } | |
39 | |
40 Node parseStatement(String text) => | |
41 parseBodyCode(text, (parser, tokens) => parser.parseStatement(tokens)); | |
42 | |
43 Node parseFunction(String text, Compiler compiler) { | |
44 Element element = parseUnit(text, compiler, compiler.mainApp).head; | |
45 Expect.isNotNull(element); | |
46 Expect.equals(ElementKind.FUNCTION, element.kind); | |
47 return element.parseNode(compiler); | |
48 } | |
49 | |
50 class MockFile { | |
51 final filename = '<string>'; | |
52 final text; | |
53 MockFile(this.text); | |
54 } | |
55 | |
56 Link<Element> parseUnit(String text, Compiler compiler, | |
57 LibraryElement library) { | |
58 Token tokens = scan(text); | |
59 Uri uri = new Uri(scheme: "source"); | |
60 var script = new Script(uri, new MockFile(text)); | |
61 var unit = new CompilationUnitElement(script, library); | |
62 int id = 0; | |
63 ElementListener listener = new ElementListener(compiler, unit, () => id++); | |
64 PartialParser parser = new PartialParser(listener); | |
65 compiler.withCurrentElement(unit, () => parser.parseUnit(tokens)); | |
66 return unit.topLevelElements; | |
67 } | |
68 | |
69 // TODO(ahe): We define this method to avoid having to import | |
70 // the scanner in the tests. We should move SourceString to another | |
71 // location instead. | |
72 SourceString buildSourceString(String name) { | |
73 return new SourceString(name); | |
74 } | |
OLD | NEW |