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 } |
Søren Gjesse
2016/03/29 09:04:13
???
| |
24 ; | |
25 | 25 |
26 var nameValue = option.split('='); | 26 var nameValue = option.split('='); |
27 if (nameValue.length != 1 && nameValue.length != 2) { | 27 if (nameValue.length != 1 && nameValue.length != 2) { |
28 reportError('Options should be a single token, or a name=value pair'); | 28 reportError('Options should be a single token, or a name=value pair'); |
29 continue; | 29 continue; |
30 } | 30 } |
31 var name = nameValue[0].trim(); | 31 var name = nameValue[0].trim(); |
32 var parser = parsers[name]; | 32 var parser = parsers[name]; |
33 if (parser == null) { | 33 if (parser == null) { |
34 reportError('Unknown option ($name).'); | 34 reportError('Unknown option ($name).'); |
(...skipping 16 matching lines...) Expand all Loading... | |
51 /// generate. For example `MyMessage.has_field` to `HasFld`. | 51 /// generate. For example `MyMessage.has_field` to `HasFld`. |
52 final Map<String, String> fieldNameOverrides; | 52 final Map<String, String> fieldNameOverrides; |
53 | 53 |
54 GenerationOptions([this.fieldNameOverrides = const {}]); | 54 GenerationOptions([this.fieldNameOverrides = const {}]); |
55 } | 55 } |
56 | 56 |
57 /// A parser for a name-value pair option. Options parsed in | 57 /// A parser for a name-value pair option. Options parsed in |
58 /// [genericOptionsParser] delegate to instances of this class to | 58 /// [genericOptionsParser] delegate to instances of this class to |
59 /// parse the value of a specific option. | 59 /// parse the value of a specific option. |
60 abstract class SingleOptionParser { | 60 abstract class SingleOptionParser { |
61 | |
62 /// Parse the [name]=[value] value pair and report any errors to [onError]. If | 61 /// 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 | 62 /// the option is a flag, [value] will be null. Note, [name] is commonly |
64 /// unused. It is provided because [SingleOptionParser] can be registered for | 63 /// unused. It is provided because [SingleOptionParser] can be registered for |
65 /// multiple option names in [genericOptionsParser]. | 64 /// multiple option names in [genericOptionsParser]. |
66 void parse(String name, String value, onError(String details)); | 65 void parse(String name, String value, onError(String details)); |
67 } | 66 } |
68 | 67 |
69 /// Parser used by the compiler, which supports the `field_name` option (see | 68 /// Parser used by the compiler, which supports the `field_name` option (see |
70 /// [FieldNameOptionParser]) and any additional option added in [parsers]. If | 69 /// [FieldNameOptionParser]) and any additional option added in [parsers]. If |
71 /// [parsers] has a key for `field_name`, it will be ignored. | 70 /// [parsers] has a key for `field_name`, it will be ignored. |
72 GenerationOptions parseGenerationOptions( | 71 GenerationOptions parseGenerationOptions( |
73 CodeGeneratorRequest request, CodeGeneratorResponse response, | 72 CodeGeneratorRequest request, CodeGeneratorResponse response, |
74 [Map<String, SingleOptionParser> parsers]) { | 73 [Map<String, SingleOptionParser> parsers]) { |
75 var fieldNameOptionParser = new FieldNameOptionParser(); | 74 var fieldNameOptionParser = new FieldNameOptionParser(); |
76 var map = {}; | 75 |
77 if (parsers != null) parsers.forEach((k, v) { map[k] = v; }); | 76 var newParsers = <String, SingleOptionParser>{}; |
78 map['field_name'] = fieldNameOptionParser; | 77 if (parsers != null) newParsers.addAll(parsers); |
79 if (genericOptionsParser(request, response, map)) { | 78 newParsers['field_name'] = fieldNameOptionParser; |
79 | |
80 if (genericOptionsParser(request, response, newParsers)) { | |
80 return new GenerationOptions(fieldNameOptionParser.mappings); | 81 return new GenerationOptions(fieldNameOptionParser.mappings); |
81 } | 82 } |
82 return null; | 83 return null; |
83 } | 84 } |
84 | 85 |
85 /// A [SingleOptionParser] to parse the `field_name` option. This option | 86 /// A [SingleOptionParser] to parse the `field_name` option. This option |
86 /// overrides the default name given to some fields that would otherwise collide | 87 /// overrides the default name given to some fields that would otherwise collide |
87 /// with existing field names in Dart core objects or in [GeneratedMessage]. | 88 /// with existing field names in Dart core objects or in [GeneratedMessage]. |
88 /// (see `README.md` for details). | 89 /// (see `README.md` for details). |
89 class FieldNameOptionParser implements SingleOptionParser { | 90 class FieldNameOptionParser implements SingleOptionParser { |
(...skipping 17 matching lines...) Expand all Loading... | |
107 var toName = fromTo[1].trim(); | 108 var toName = fromTo[1].trim(); |
108 if (fromName.isEmpty || toName.isEmpty) { | 109 if (fromName.isEmpty || toName.isEmpty) { |
109 onError('Invalid field_name option, ' | 110 onError('Invalid field_name option, ' |
110 '"from" and "to" names should not be empty.'); | 111 '"from" and "to" names should not be empty.'); |
111 return; | 112 return; |
112 } | 113 } |
113 | 114 |
114 mappings['.$fromName'] = toName; | 115 mappings['.$fromName'] = toName; |
115 } | 116 } |
116 } | 117 } |
OLD | NEW |