OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 part of protoc; | 5 part of protoc; |
6 | 6 |
7 /// Helper function implementing a generic option parser that reads | 7 /// Helper function implementing a generic option parser that reads |
8 /// `request.parameters` and treats each token as either a flag ("name") or a | 8 /// `request.parameters` and treats each token as either a flag ("name") or a |
9 /// key-value pair ("name=value"). For each option "name", it looks up whether a | 9 /// key-value pair ("name=value"). For each option "name", it looks up whether a |
10 /// [SingleOptionParser] exists in [parsers] and delegates the actual parsing of | 10 /// [SingleOptionParser] exists in [parsers] and delegates the actual parsing of |
11 /// the option to it. Returns `true` if no errors were reported. | 11 /// the option to it. Returns `true` if no errors were reported. |
12 bool genericOptionsParser( | 12 bool genericOptionsParser(CodeGeneratorRequest request, |
13 CodeGeneratorRequest request, CodeGeneratorResponse response, | 13 CodeGeneratorResponse response, Map<String, SingleOptionParser> parsers) { |
14 Map<String, SingleOptionParser> parsers) { | |
15 var parameter = request.parameter != null ? request.parameter : ''; | 14 var parameter = request.parameter != null ? request.parameter : ''; |
16 var options = parameter.trim().split(','); | 15 var options = parameter.trim().split(','); |
17 var errors = []; | 16 var errors = []; |
18 | 17 |
19 for (var option in options) { | 18 for (var option in options) { |
20 option = option.trim(); | 19 option = option.trim(); |
21 if (option.isEmpty) continue; | 20 if (option.isEmpty) continue; |
22 var reportError = (details) { | 21 void reportError(String details) { |
23 errors.add('Error found trying to parse the option: $option.\n$details'); | 22 errors.add('Error found trying to parse the option: $option.\n$details'); |
24 }; | 23 } |
25 | 24 |
26 var nameValue = option.split('='); | 25 var nameValue = option.split('='); |
27 if (nameValue.length != 1 && nameValue.length != 2) { | 26 if (nameValue.length != 1 && nameValue.length != 2) { |
28 reportError('Options should be a single token, or a name=value pair'); | 27 reportError('Options should be a single token, or a name=value pair'); |
29 continue; | 28 continue; |
30 } | 29 } |
31 var name = nameValue[0].trim(); | 30 var name = nameValue[0].trim(); |
32 var parser = parsers[name]; | 31 var parser = parsers[name]; |
33 if (parser == null) { | 32 if (parser == null) { |
34 reportError('Unknown option ($name).'); | 33 reportError('Unknown option ($name).'); |
(...skipping 16 matching lines...) Expand all Loading... |
51 /// generate. For example `MyMessage.has_field` to `HasFld`. | 50 /// generate. For example `MyMessage.has_field` to `HasFld`. |
52 final Map<String, String> fieldNameOverrides; | 51 final Map<String, String> fieldNameOverrides; |
53 | 52 |
54 GenerationOptions([this.fieldNameOverrides = const {}]); | 53 GenerationOptions([this.fieldNameOverrides = const {}]); |
55 } | 54 } |
56 | 55 |
57 /// A parser for a name-value pair option. Options parsed in | 56 /// A parser for a name-value pair option. Options parsed in |
58 /// [genericOptionsParser] delegate to instances of this class to | 57 /// [genericOptionsParser] delegate to instances of this class to |
59 /// parse the value of a specific option. | 58 /// parse the value of a specific option. |
60 abstract class SingleOptionParser { | 59 abstract class SingleOptionParser { |
61 | |
62 /// Parse the [name]=[value] value pair and report any errors to [onError]. If | 60 /// Parse the [name]=[value] value pair and report any errors to [onError]. If |
63 /// the option is a flag, [value] will be null. Note, [name] is commonly | 61 /// the option is a flag, [value] will be null. Note, [name] is commonly |
64 /// unused. It is provided because [SingleOptionParser] can be registered for | 62 /// unused. It is provided because [SingleOptionParser] can be registered for |
65 /// multiple option names in [genericOptionsParser]. | 63 /// multiple option names in [genericOptionsParser]. |
66 void parse(String name, String value, onError(String details)); | 64 void parse(String name, String value, onError(String details)); |
67 } | 65 } |
68 | 66 |
69 /// Parser used by the compiler, which supports the `field_name` option (see | 67 /// Parser used by the compiler, which supports the `field_name` option (see |
70 /// [FieldNameOptionParser]) and any additional option added in [parsers]. If | 68 /// [FieldNameOptionParser]) and any additional option added in [parsers]. If |
71 /// [parsers] has a key for `field_name`, it will be ignored. | 69 /// [parsers] has a key for `field_name`, it will be ignored. |
72 GenerationOptions parseGenerationOptions( | 70 GenerationOptions parseGenerationOptions( |
73 CodeGeneratorRequest request, CodeGeneratorResponse response, | 71 CodeGeneratorRequest request, CodeGeneratorResponse response, |
74 [Map<String, SingleOptionParser> parsers]) { | 72 [Map<String, SingleOptionParser> parsers]) { |
75 var fieldNameOptionParser = new FieldNameOptionParser(); | 73 var fieldNameOptionParser = new FieldNameOptionParser(); |
76 var map = {}; | 74 |
77 if (parsers != null) parsers.forEach((k, v) { map[k] = v; }); | 75 var newParsers = <String, SingleOptionParser>{}; |
78 map['field_name'] = fieldNameOptionParser; | 76 if (parsers != null) newParsers.addAll(parsers); |
79 if (genericOptionsParser(request, response, map)) { | 77 newParsers['field_name'] = fieldNameOptionParser; |
| 78 |
| 79 if (genericOptionsParser(request, response, newParsers)) { |
80 return new GenerationOptions(fieldNameOptionParser.mappings); | 80 return new GenerationOptions(fieldNameOptionParser.mappings); |
81 } | 81 } |
82 return null; | 82 return null; |
83 } | 83 } |
84 | 84 |
85 /// A [SingleOptionParser] to parse the `field_name` option. This option | 85 /// A [SingleOptionParser] to parse the `field_name` option. This option |
86 /// overrides the default name given to some fields that would otherwise collide | 86 /// overrides the default name given to some fields that would otherwise collide |
87 /// with existing field names in Dart core objects or in [GeneratedMessage]. | 87 /// with existing field names in Dart core objects or in [GeneratedMessage]. |
88 /// (see `README.md` for details). | 88 /// (see `README.md` for details). |
89 class FieldNameOptionParser implements SingleOptionParser { | 89 class FieldNameOptionParser implements SingleOptionParser { |
(...skipping 17 matching lines...) Expand all Loading... |
107 var toName = fromTo[1].trim(); | 107 var toName = fromTo[1].trim(); |
108 if (fromName.isEmpty || toName.isEmpty) { | 108 if (fromName.isEmpty || toName.isEmpty) { |
109 onError('Invalid field_name option, ' | 109 onError('Invalid field_name option, ' |
110 '"from" and "to" names should not be empty.'); | 110 '"from" and "to" names should not be empty.'); |
111 return; | 111 return; |
112 } | 112 } |
113 | 113 |
114 mappings['.$fromName'] = toName; | 114 mappings['.$fromName'] = toName; |
115 } | 115 } |
116 } | 116 } |
OLD | NEW |