Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Side by Side Diff: lib/protobuf/plugin/DartFile.dart

Issue 10595002: Protocol Buffer runtime library and 'protoc' plugin (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Work around http://code.google.com/p/dart/issues/detail?id=3806 Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « lib/protobuf/plugin/CodeGenerator.dart ('k') | lib/protobuf/plugin/DartHelpers.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 class FileGenerator implements ProtobufContainer {
6 GoogleProtobuf_FileDescriptorProto _fileDescriptor;
7 ProtobufContainer _parent;
8 GenerationContext _context;
9 GoogleProtobuf_FileOptions_OptimizeMode _optimizeFor;
10
11 List<EnumGenerator> enumGenerators;
12 List<MessageGenerator> messageGenerators;
13 List<ExtensionGenerator> extensionGenerators;
14
15 FileGenerator(GoogleProtobuf_FileDescriptorProto this._fileDescriptor,
16 ProtobufContainer this._parent, GenerationContext this._context) {
17 _context.register(this);
18 enumGenerators = new List<EnumGenerator>();
19 messageGenerators = new List<MessageGenerator>();
20 extensionGenerators = new List<ExtensionGenerator>();
21
22 _classname = _fileDescriptor.package === null ? "" :
23 "${dotsToCamelCase(_fileDescriptor.package, true)}";
24
25 _optimizeFor = _parent.optimizeFor;
26 if (_optimizeFor == null && _fileDescriptor.hasOptions() &&
27 _fileDescriptor.options.hasOptimizeFor()) {
28 _optimizeFor = _fileDescriptor.options.optimizeFor;
29 }
30 if (_optimizeFor == null) {
31 _optimizeFor = GoogleProtobuf_FileOptions_OptimizeMode.SPEED;
32 }
33
34 // Load and register all enum and message types
35 for (GoogleProtobuf_EnumDescriptorProto enumType in _fileDescriptor.enumType ) {
36 enumGenerators.add(new EnumGenerator(enumType, this, _context));
37 }
38 for (GoogleProtobuf_DescriptorProto messageType in _fileDescriptor.messageTy pe) {
39 messageGenerators.add(new MessageGenerator(messageType, this, _context));
40 }
41 for (GoogleProtobuf_FieldDescriptorProto extension in _fileDescriptor.extens ion) {
42 extensionGenerators.add(new ExtensionGenerator(extension,
43 this, _context));
44 }
45 }
46
47 String get classname() => _classname;
48 String get fqname() => _fileDescriptor.package === null
49 ? "" : ".${_fileDescriptor.package}";
50 GoogleProtobuf_FileOptions_OptimizeMode get optimizeFor() => _optimizeFor;
51
52 GoogleProtobufCompiler_CodeGeneratorResponse_File generate() {
53 MemoryWriter writer = new MemoryWriter();
54 IndentingWriter out = new IndentingWriter(" ", writer);
55
56 List<ExtensionGenerator> allExtensions = new List<ExtensionGenerator>();
57
58 out.println("///");
59 out.println("// Generated code. Do not modify.");
60 out.println("///");
61
62 // Initialize Field
63 for (MessageGenerator m in messageGenerators) {
64 m.initializeFields();
65 }
66
67 // Generate code
68 for (EnumGenerator e in enumGenerators) {
69 e.generate(out);
70 }
71 for (MessageGenerator m in messageGenerators) {
72 m.generate(out, allExtensions);
73 }
74
75 // Generate code for extensions defined at top-level using a class
76 // name derived from the file name
77 if (!extensionGenerators.isEmpty() || !allExtensions.isEmpty()) {
78 String filename = _fileDescriptor.name;
79 if (filename.endsWith(".proto")) {
80 filename = filename.substring(0, filename.length - 6);
81 }
82 filename = filename.replaceAll("/", "_");
83 filename = "${filename[0].toUpperCase()}${filename.substring(1)}";
84
85 out.addBlock("class ${filename} {", "}\n", () {
86 for (ExtensionGenerator x in extensionGenerators) {
87 allExtensions.add(x);
88 x.generate(out);
89 }
90 out.println("static void registerAllExtensions(ExtensionRegistry "
91 "registry) {");
92 for (ExtensionGenerator x in allExtensions) {
93 out.println(" registry.add(${x.name});");
94 }
95 out.println("}");
96 });
97 }
98
99 GoogleProtobufCompiler_CodeGeneratorResponse_File_Builder file_builder =
100 new GoogleProtobufCompiler_CodeGeneratorResponse_File_Builder();
101 String name = _fileDescriptor.name;
102 if (name.endsWith(".proto")) {
103 name = name.substring(0, name.length - 6);
104 }
105 file_builder.name = "$name.pb.dart";
106 file_builder.content = writer.toString();
107 return file_builder.build();
108 }
109
110 String _classname;
111 }
112
113 class GenerationContext {
114 GenerationContext(Writer this.err) : _registry = {};
115
116 void register(ProtobufContainer container) {
117 _registry[container.fqname] = container;
118 }
119
120 Writer err;
121 ProtobufContainer operator [](String fqname) => _registry[fqname];
122 Map<String, ProtobufContainer> _registry;
123 }
OLDNEW
« no previous file with comments | « lib/protobuf/plugin/CodeGenerator.dart ('k') | lib/protobuf/plugin/DartHelpers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698