OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 | 7 |
8 import 'package:front_end/src/scanner/token.dart' show Token; | 8 import 'package:front_end/src/scanner/token.dart' show Token; |
9 import 'package:kernel/ast.dart'; | 9 import 'package:kernel/ast.dart'; |
10 import 'package:kernel/binary/ast_to_binary.dart'; | 10 import 'package:kernel/binary/ast_to_binary.dart'; |
11 import 'package:kernel/binary/limited_ast_to_binary.dart'; | |
12 import 'package:kernel/text/ast_to_text.dart'; | 11 import 'package:kernel/text/ast_to_text.dart'; |
13 | 12 |
14 /// A null-aware alternative to `token.offset`. If [token] is `null`, returns | 13 /// A null-aware alternative to `token.offset`. If [token] is `null`, returns |
15 /// `TreeNode.noOffset`. | 14 /// `TreeNode.noOffset`. |
16 int offsetForToken(Token token) => | 15 int offsetForToken(Token token) => |
17 token == null ? TreeNode.noOffset : token.offset; | 16 token == null ? TreeNode.noOffset : token.offset; |
18 | 17 |
19 /// Print the given [program]. Do nothing if it is `null`. If the | 18 /// Print the given [program]. Do nothing if it is `null`. If the |
20 /// [libraryFilter] is provided, then only libraries that satisfy it are | 19 /// [libraryFilter] is provided, then only libraries that satisfy it are |
21 /// printed. | 20 /// printed. |
(...skipping 11 matching lines...) Expand all Loading... |
33 Future<Null> writeProgramToFile(Program program, Uri uri) async { | 32 Future<Null> writeProgramToFile(Program program, Uri uri) async { |
34 File output = new File.fromUri(uri); | 33 File output = new File.fromUri(uri); |
35 IOSink sink = output.openWrite(); | 34 IOSink sink = output.openWrite(); |
36 try { | 35 try { |
37 new BinaryPrinter(sink).writeProgramFile(program); | 36 new BinaryPrinter(sink).writeProgramFile(program); |
38 program.unbindCanonicalNames(); | 37 program.unbindCanonicalNames(); |
39 } finally { | 38 } finally { |
40 await sink.close(); | 39 await sink.close(); |
41 } | 40 } |
42 } | 41 } |
43 | |
44 /// Serialize the libraries in [program] that match [filter]. | |
45 List<int> serializeProgram(Program program, | |
46 {bool filter(Library library), bool excludeUriToSource: false}) { | |
47 ByteSink byteSink = new ByteSink(); | |
48 BinaryPrinter printer = filter == null && !excludeUriToSource | |
49 ? new BinaryPrinter(byteSink) | |
50 : new LimitedBinaryPrinter( | |
51 byteSink, filter ?? (_) => true, excludeUriToSource); | |
52 printer.writeProgramFile(program); | |
53 return byteSink.builder.takeBytes(); | |
54 } | |
55 | |
56 /// A [Sink] that directly writes data into a byte builder. | |
57 class ByteSink implements Sink<List<int>> { | |
58 final BytesBuilder builder = new BytesBuilder(); | |
59 | |
60 void add(List<int> data) { | |
61 builder.add(data); | |
62 } | |
63 | |
64 void close() {} | |
65 } | |
OLD | NEW |