OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 import "dart:uri"; | |
9 | 8 |
10 import "../../../sdk/lib/_internal/compiler/implementation/elements/elements.dar
t"; | 9 import "../../../sdk/lib/_internal/compiler/implementation/elements/elements.dar
t"; |
11 import "../../../sdk/lib/_internal/compiler/implementation/tree/tree.dart"; | 10 import "../../../sdk/lib/_internal/compiler/implementation/tree/tree.dart"; |
12 import "../../../sdk/lib/_internal/compiler/implementation/scanner/scannerlib.da
rt"; | 11 import "../../../sdk/lib/_internal/compiler/implementation/scanner/scannerlib.da
rt"; |
13 import "../../../sdk/lib/_internal/compiler/implementation/source_file.dart"; | 12 import "../../../sdk/lib/_internal/compiler/implementation/source_file.dart"; |
14 import "../../../sdk/lib/_internal/compiler/implementation/util/util.dart"; | 13 import "../../../sdk/lib/_internal/compiler/implementation/util/util.dart"; |
15 | 14 |
16 import "../../../sdk/lib/_internal/compiler/implementation/elements/modelx.dart" | 15 import "../../../sdk/lib/_internal/compiler/implementation/elements/modelx.dart" |
17 show CompilationUnitElementX, | 16 show CompilationUnitElementX, |
18 LibraryElementX; | 17 LibraryElementX; |
(...skipping 17 matching lines...) Loading... |
36 } | 35 } |
37 | 36 |
38 Token scan(String text) => new StringScanner(text).tokenize(); | 37 Token scan(String text) => new StringScanner(text).tokenize(); |
39 | 38 |
40 Node parseBodyCode(String text, Function parseMethod, | 39 Node parseBodyCode(String text, Function parseMethod, |
41 {DiagnosticListener diagnosticHandler}) { | 40 {DiagnosticListener diagnosticHandler}) { |
42 Token tokens = scan(text); | 41 Token tokens = scan(text); |
43 if (diagnosticHandler == null) diagnosticHandler = new LoggerCanceler(); | 42 if (diagnosticHandler == null) diagnosticHandler = new LoggerCanceler(); |
44 Script script = | 43 Script script = |
45 new Script( | 44 new Script( |
46 new Uri.fromComponents(scheme: "source"), | 45 new Uri(scheme: "source"), |
47 new MockFile(text)); | 46 new MockFile(text)); |
48 LibraryElement library = new LibraryElementX(script); | 47 LibraryElement library = new LibraryElementX(script); |
49 library.canUseNative = true; | 48 library.canUseNative = true; |
50 NodeListener listener = | 49 NodeListener listener = |
51 new NodeListener(diagnosticHandler, library.entryCompilationUnit); | 50 new NodeListener(diagnosticHandler, library.entryCompilationUnit); |
52 Parser parser = new Parser(listener); | 51 Parser parser = new Parser(listener); |
53 Token endToken = parseMethod(parser, tokens); | 52 Token endToken = parseMethod(parser, tokens); |
54 assert(endToken.kind == EOF_TOKEN); | 53 assert(endToken.kind == EOF_TOKEN); |
55 Node node = listener.popNode(); | 54 Node node = listener.popNode(); |
56 Expect.isNotNull(node); | 55 Expect.isNotNull(node); |
(...skipping 20 matching lines...) Loading... |
77 MockFile(text) | 76 MockFile(text) |
78 : super('<string>', text); | 77 : super('<string>', text); |
79 } | 78 } |
80 | 79 |
81 var sourceCounter = 0; | 80 var sourceCounter = 0; |
82 | 81 |
83 Link<Element> parseUnit(String text, Compiler compiler, | 82 Link<Element> parseUnit(String text, Compiler compiler, |
84 LibraryElement library, | 83 LibraryElement library, |
85 [void registerSource(Uri uri, String source)]) { | 84 [void registerSource(Uri uri, String source)]) { |
86 Token tokens = scan(text); | 85 Token tokens = scan(text); |
87 Uri uri = | 86 Uri uri = new Uri(scheme: "source", path: '${++sourceCounter}'); |
88 new Uri.fromComponents(scheme: "source", path: '${++sourceCounter}'); | |
89 if (registerSource != null) { | 87 if (registerSource != null) { |
90 registerSource(uri, text); | 88 registerSource(uri, text); |
91 } | 89 } |
92 var script = new Script(uri, new MockFile(text)); | 90 var script = new Script(uri, new MockFile(text)); |
93 var unit = new CompilationUnitElementX(script, library); | 91 var unit = new CompilationUnitElementX(script, library); |
94 int id = 0; | 92 int id = 0; |
95 ElementListener listener = new ElementListener(compiler, unit, () => id++); | 93 ElementListener listener = new ElementListener(compiler, unit, () => id++); |
96 PartialParser parser = new PartialParser(listener); | 94 PartialParser parser = new PartialParser(listener); |
97 compiler.withCurrentElement(unit, () => parser.parseUnit(tokens)); | 95 compiler.withCurrentElement(unit, () => parser.parseUnit(tokens)); |
98 return unit.localMembers; | 96 return unit.localMembers; |
99 } | 97 } |
100 | 98 |
101 NodeList fullParseUnit(String source, {DiagnosticListener diagnosticHandler}) { | 99 NodeList fullParseUnit(String source, {DiagnosticListener diagnosticHandler}) { |
102 return parseBodyCode(source, (parser, tokens) => parser.parseUnit(tokens), | 100 return parseBodyCode(source, (parser, tokens) => parser.parseUnit(tokens), |
103 diagnosticHandler: diagnosticHandler); | 101 diagnosticHandler: diagnosticHandler); |
104 } | 102 } |
105 | 103 |
106 // TODO(ahe): We define this method to avoid having to import | 104 // TODO(ahe): We define this method to avoid having to import |
107 // the scanner in the tests. We should move SourceString to another | 105 // the scanner in the tests. We should move SourceString to another |
108 // location instead. | 106 // location instead. |
109 SourceString buildSourceString(String name) { | 107 SourceString buildSourceString(String name) { |
110 return new SourceString(name); | 108 return new SourceString(name); |
111 } | 109 } |
OLD | NEW |