OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 class ProtobufField { | 7 class ProtobufField { |
8 static final RegExp HEX_LITERAL_REGEX = | 8 static final RegExp HEX_LITERAL_REGEX = |
9 new RegExp(r'^0x[0-9a-f]+$', multiLine: false, caseSensitive: false); | 9 new RegExp(r'^0x[0-9a-f]+$', multiLine: false, caseSensitive: false); |
10 static final RegExp INTEGER_LITERAL_REGEX = new RegExp(r'^[+-]?[0-9]+$'); | 10 static final RegExp INTEGER_LITERAL_REGEX = new RegExp(r'^[+-]?[0-9]+$'); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 String initialization; | 132 String initialization; |
133 String prefixedInitialization; | 133 String prefixedInitialization; |
134 switch (field.type) { | 134 switch (field.type) { |
135 case FieldDescriptorProto_Type.TYPE_BOOL: | 135 case FieldDescriptorProto_Type.TYPE_BOOL: |
136 baseType = 'bool'; | 136 baseType = 'bool'; |
137 typeString = write('bool'); | 137 typeString = write('bool'); |
138 packable = true; | 138 packable = true; |
139 codedStreamType = 'Bool'; | 139 codedStreamType = 'Bool'; |
140 if (!repeats) { | 140 if (!repeats) { |
141 if (field.hasDefaultValue() && 'false' != field.defaultValue) { | 141 if (field.hasDefaultValue() && 'false' != field.defaultValue) { |
142 initialization = '()${SP}=>${SP}${field.defaultValue}'; | 142 initialization = '${field.defaultValue}'; |
143 } | 143 } |
144 } | 144 } |
145 break; | 145 break; |
146 case FieldDescriptorProto_Type.TYPE_FLOAT: | 146 case FieldDescriptorProto_Type.TYPE_FLOAT: |
147 case FieldDescriptorProto_Type.TYPE_DOUBLE: | 147 case FieldDescriptorProto_Type.TYPE_DOUBLE: |
148 baseType = 'double'; | 148 baseType = 'double'; |
149 typeString = write('double'); | 149 typeString = write('double'); |
150 packable = true; | 150 packable = true; |
151 codedStreamType = | 151 codedStreamType = |
152 (field.type == FieldDescriptorProto_Type.TYPE_FLOAT) ? | 152 (field.type == FieldDescriptorProto_Type.TYPE_FLOAT) ? |
153 'Float' : 'Double'; | 153 'Float' : 'Double'; |
154 if (!repeats) { | 154 if (!repeats) { |
155 if (field.hasDefaultValue() && | 155 if (field.hasDefaultValue() && |
156 ('0.0' != field.defaultValue || '0' != field.defaultValue)) { | 156 ('0.0' != field.defaultValue || '0' != field.defaultValue)) { |
157 if (field.defaultValue == 'inf') { | 157 if (field.defaultValue == 'inf') { |
158 initialization = '()${SP}=>${SP}double.INFINITY'; | 158 initialization = 'double.INFINITY'; |
159 } else if (field.defaultValue == '-inf') { | 159 } else if (field.defaultValue == '-inf') { |
160 initialization = '()${SP}=>${SP}double.NEGATIVE_INFINITY'; | 160 initialization = 'double.NEGATIVE_INFINITY'; |
161 } else if (field.defaultValue == 'nan') { | 161 } else if (field.defaultValue == 'nan') { |
162 initialization = '()${SP}=>${SP}double.NAN'; | 162 initialization = 'double.NAN'; |
163 } else if (HEX_LITERAL_REGEX.hasMatch(field.defaultValue)) { | 163 } else if (HEX_LITERAL_REGEX.hasMatch(field.defaultValue)) { |
164 initialization = '()${SP}=>${SP}(${field.defaultValue})' | 164 initialization = '(${field.defaultValue}).toDouble()'; |
165 '.toDouble()'; | |
166 } else if (INTEGER_LITERAL_REGEX.hasMatch(field.defaultValue)) { | 165 } else if (INTEGER_LITERAL_REGEX.hasMatch(field.defaultValue)) { |
167 initialization = '()${SP}=>${SP}${field.defaultValue}.0'; | 166 initialization = '${field.defaultValue}.0'; |
168 } else if (DECIMAL_LITERAL_REGEX_A.hasMatch(field.defaultValue) | 167 } else if (DECIMAL_LITERAL_REGEX_A.hasMatch(field.defaultValue) |
169 || DECIMAL_LITERAL_REGEX_B.hasMatch(field.defaultValue)) { | 168 || DECIMAL_LITERAL_REGEX_B.hasMatch(field.defaultValue)) { |
170 initialization = '()${SP}=>${SP}${field.defaultValue}'; | 169 initialization = '${field.defaultValue}'; |
171 } else { | 170 } else { |
172 throw new InvalidDefaultValue.double( | 171 throw new InvalidDefaultValue.double( |
173 field.name, field.defaultValue); | 172 field.name, field.defaultValue); |
174 } | 173 } |
175 } | 174 } |
176 } | 175 } |
177 break; | 176 break; |
178 case FieldDescriptorProto_Type.TYPE_INT32: | 177 case FieldDescriptorProto_Type.TYPE_INT32: |
179 case FieldDescriptorProto_Type.TYPE_UINT32: | 178 case FieldDescriptorProto_Type.TYPE_UINT32: |
180 case FieldDescriptorProto_Type.TYPE_SINT32: | 179 case FieldDescriptorProto_Type.TYPE_SINT32: |
(...skipping 14 matching lines...) Expand all Loading... |
195 break; | 194 break; |
196 case FieldDescriptorProto_Type.TYPE_FIXED32: | 195 case FieldDescriptorProto_Type.TYPE_FIXED32: |
197 codedStreamType = 'Fixed32'; | 196 codedStreamType = 'Fixed32'; |
198 break; | 197 break; |
199 case FieldDescriptorProto_Type.TYPE_SFIXED32: | 198 case FieldDescriptorProto_Type.TYPE_SFIXED32: |
200 codedStreamType = 'Sfixed32'; | 199 codedStreamType = 'Sfixed32'; |
201 break; | 200 break; |
202 } | 201 } |
203 if (!repeats) { | 202 if (!repeats) { |
204 if (field.hasDefaultValue() && '0' != field.defaultValue) { | 203 if (field.hasDefaultValue() && '0' != field.defaultValue) { |
205 initialization = '()${SP}=>${SP}${field.defaultValue}'; | 204 initialization = '${field.defaultValue}'; |
206 } | 205 } |
207 } | 206 } |
208 break; | 207 break; |
209 case FieldDescriptorProto_Type.TYPE_INT64: | 208 case FieldDescriptorProto_Type.TYPE_INT64: |
210 case FieldDescriptorProto_Type.TYPE_UINT64: | 209 case FieldDescriptorProto_Type.TYPE_UINT64: |
211 case FieldDescriptorProto_Type.TYPE_SINT64: | 210 case FieldDescriptorProto_Type.TYPE_SINT64: |
212 case FieldDescriptorProto_Type.TYPE_FIXED64: | 211 case FieldDescriptorProto_Type.TYPE_FIXED64: |
213 case FieldDescriptorProto_Type.TYPE_SFIXED64: | 212 case FieldDescriptorProto_Type.TYPE_SFIXED64: |
214 baseType = 'Int64'; | 213 baseType = 'Int64'; |
215 typeString = write('Int64'); | 214 typeString = write('Int64'); |
(...skipping 11 matching lines...) Expand all Loading... |
227 case FieldDescriptorProto_Type.TYPE_FIXED64: | 226 case FieldDescriptorProto_Type.TYPE_FIXED64: |
228 codedStreamType = 'Fixed64'; | 227 codedStreamType = 'Fixed64'; |
229 break; | 228 break; |
230 case FieldDescriptorProto_Type.TYPE_SFIXED64: | 229 case FieldDescriptorProto_Type.TYPE_SFIXED64: |
231 codedStreamType = 'Sfixed64'; | 230 codedStreamType = 'Sfixed64'; |
232 break; | 231 break; |
233 } | 232 } |
234 if (!repeats) { | 233 if (!repeats) { |
235 final defaultValue = field.hasDefaultValue() ? | 234 final defaultValue = field.hasDefaultValue() ? |
236 field.defaultValue : '0'; | 235 field.defaultValue : '0'; |
237 initialization = '()${SP}=>${SP}makeLongInt($defaultValue)'; | 236 if (defaultValue == '0') { |
| 237 initialization = 'Int64.ZERO'; |
| 238 } else { |
| 239 initialization = "parseLongInt('$defaultValue')"; |
| 240 } |
238 } | 241 } |
239 break; | 242 break; |
240 case FieldDescriptorProto_Type.TYPE_STRING: | 243 case FieldDescriptorProto_Type.TYPE_STRING: |
241 baseType = 'String'; | 244 baseType = 'String'; |
242 typeString = write('String'); | 245 typeString = write('String'); |
243 codedStreamType = 'String'; | 246 codedStreamType = 'String'; |
244 if (!repeats) { | 247 if (!repeats) { |
245 if (field.hasDefaultValue() && !field.defaultValue.isEmpty) { | 248 if (field.hasDefaultValue() && !field.defaultValue.isEmpty) { |
246 String defaultValue = field.defaultValue.replaceAll(r'$', r'\$'); | 249 String defaultValue = field.defaultValue.replaceAll(r'$', r'\$'); |
247 initialization = '()${SP}=>${SP}\'$defaultValue\''; | 250 initialization = '\'$defaultValue\''; |
248 } | 251 } |
249 } | 252 } |
250 break; | 253 break; |
251 case FieldDescriptorProto_Type.TYPE_BYTES: | 254 case FieldDescriptorProto_Type.TYPE_BYTES: |
252 baseType = 'List<int>'; | 255 baseType = 'List<int>'; |
253 typeString = write('List<int>'); | 256 typeString = write('List<int>'); |
254 codedStreamType = 'Bytes'; | 257 codedStreamType = 'Bytes'; |
255 if (!repeats) { | 258 if (!repeats) { |
256 if (field.hasDefaultValue() && !field.defaultValue.isEmpty) { | 259 if (field.hasDefaultValue() && !field.defaultValue.isEmpty) { |
257 String byteList = field.defaultValue.codeUnits | 260 String byteList = field.defaultValue.codeUnits |
(...skipping 12 matching lines...) Expand all Loading... |
270 if (groupType.packageImportPrefix.isNotEmpty) { | 273 if (groupType.packageImportPrefix.isNotEmpty) { |
271 prefixedBaseType = groupType.packageImportPrefix + '.' + baseType; | 274 prefixedBaseType = groupType.packageImportPrefix + '.' + baseType; |
272 } else { | 275 } else { |
273 prefixedBaseType = baseType; | 276 prefixedBaseType = baseType; |
274 } | 277 } |
275 prefixedTypeString = write(prefixedBaseType); | 278 prefixedTypeString = write(prefixedBaseType); |
276 codedStreamType = 'Group'; | 279 codedStreamType = 'Group'; |
277 } else { | 280 } else { |
278 throw 'FAILURE: Unknown group type reference ${field.typeName}'; | 281 throw 'FAILURE: Unknown group type reference ${field.typeName}'; |
279 } | 282 } |
280 initialization = '()${SP}=>${SP}new ${baseType}()'; | 283 initialization = '${baseType}.create'; |
281 prefixedInitialization = '()${SP}=>${SP}new ${prefixedBaseType}()'; | 284 prefixedInitialization = '${prefixedBaseType}.create'; |
282 break; | 285 break; |
283 case FieldDescriptorProto_Type.TYPE_MESSAGE: | 286 case FieldDescriptorProto_Type.TYPE_MESSAGE: |
284 ProtobufContainer messageType = context[field.typeName]; | 287 ProtobufContainer messageType = context[field.typeName]; |
285 if (messageType != null) { | 288 if (messageType != null) { |
286 typePackage = messageType.package; | 289 typePackage = messageType.package; |
287 baseType = messageType.classname; | 290 baseType = messageType.classname; |
288 typeString = write(baseType); | 291 typeString = write(baseType); |
289 if (messageType.packageImportPrefix.isNotEmpty) { | 292 if (messageType.packageImportPrefix.isNotEmpty) { |
290 prefixedBaseType = messageType.packageImportPrefix + '.' + baseType; | 293 prefixedBaseType = messageType.packageImportPrefix + '.' + baseType; |
291 } else { | 294 } else { |
292 prefixedBaseType = baseType; | 295 prefixedBaseType = baseType; |
293 } | 296 } |
294 prefixedTypeString = write(prefixedBaseType); | 297 prefixedTypeString = write(prefixedBaseType); |
295 codedStreamType = 'Message'; | 298 codedStreamType = 'Message'; |
296 } else { | 299 } else { |
297 throw 'FAILURE: Unknown message type reference ${field.typeName}'; | 300 throw 'FAILURE: Unknown message type reference ${field.typeName}'; |
298 } | 301 } |
299 initialization = '()${SP}=>${SP}new ${baseType}()'; | 302 initialization = '${baseType}.create'; |
300 prefixedInitialization = '()${SP}=>${SP}new ${prefixedBaseType}()'; | 303 prefixedInitialization = '${prefixedBaseType}.create'; |
301 break; | 304 break; |
302 case FieldDescriptorProto_Type.TYPE_ENUM: | 305 case FieldDescriptorProto_Type.TYPE_ENUM: |
303 EnumGenerator enumType = context[field.typeName]; | 306 EnumGenerator enumType = context[field.typeName]; |
304 if (enumType != null) { | 307 if (enumType != null) { |
305 typePackage = enumType.package; | 308 typePackage = enumType.package; |
306 baseType = enumType.classname; | 309 baseType = enumType.classname; |
307 typeString = write(enumType.classname); | 310 typeString = write(enumType.classname); |
308 codedStreamType = 'Enum'; | 311 codedStreamType = 'Enum'; |
309 if (enumType.packageImportPrefix.isNotEmpty) { | 312 if (enumType.packageImportPrefix.isNotEmpty) { |
310 prefixedBaseType = enumType.packageImportPrefix + '.' + baseType; | 313 prefixedBaseType = enumType.packageImportPrefix + '.' + baseType; |
311 } else { | 314 } else { |
312 prefixedBaseType = baseType; | 315 prefixedBaseType = baseType; |
313 } | 316 } |
314 prefixedTypeString = write(prefixedBaseType); | 317 prefixedTypeString = write(prefixedBaseType); |
315 packable = true; | 318 packable = true; |
316 if (!repeats) { | 319 if (!repeats) { |
317 if (field.hasDefaultValue() && !field.defaultValue.isEmpty) { | 320 if (field.hasDefaultValue() && !field.defaultValue.isEmpty) { |
318 initialization = | 321 initialization = |
319 '()${SP}=>${SP}${baseType}.${field.defaultValue}'; | 322 '${baseType}.${field.defaultValue}'; |
320 prefixedInitialization = | 323 prefixedInitialization = |
321 '()${SP}=>${SP}${prefixedBaseType}.${field.defaultValue}'; | 324 '${prefixedBaseType}.${field.defaultValue}'; |
322 } else if (!enumType._canonicalValues.isEmpty) { | 325 } else if (!enumType._canonicalValues.isEmpty) { |
323 initialization = | 326 initialization = |
324 '()${SP}=>${SP}${baseType}.' | 327 '${baseType}.${enumType._canonicalValues[0].name}'; |
325 '${enumType._canonicalValues[0].name}'; | |
326 prefixedInitialization = | 328 prefixedInitialization = |
327 '()${SP}=>${SP}${prefixedBaseType}.' | 329 '${prefixedBaseType}.${enumType._canonicalValues[0].name}'; |
328 '${enumType._canonicalValues[0].name}'; | |
329 } | 330 } |
330 } | 331 } |
331 } else { | 332 } else { |
332 throw 'FAILURE: Unknown enum type reference ${field.typeName}'; | 333 throw 'FAILURE: Unknown enum type reference ${field.typeName}'; |
333 } | 334 } |
334 break; | 335 break; |
335 default: | 336 default: |
336 throw 'Unknown type ${field.type.name}'; | 337 throw 'Unknown type ${field.type.name}'; |
337 // No default -- should be an error. | 338 // No default -- should be an error. |
338 } | 339 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 return 2; // Length-delimited | 398 return 2; // Length-delimited |
398 case FieldDescriptorProto_Type.TYPE_GROUP: | 399 case FieldDescriptorProto_Type.TYPE_GROUP: |
399 return 3; // Start group | 400 return 3; // Start group |
400 case FieldDescriptorProto_Type.TYPE_FLOAT: | 401 case FieldDescriptorProto_Type.TYPE_FLOAT: |
401 case FieldDescriptorProto_Type.TYPE_FIXED32: | 402 case FieldDescriptorProto_Type.TYPE_FIXED32: |
402 case FieldDescriptorProto_Type.TYPE_SFIXED32: | 403 case FieldDescriptorProto_Type.TYPE_SFIXED32: |
403 return 5; // 32-bit | 404 return 5; // 32-bit |
404 } | 405 } |
405 } | 406 } |
406 } | 407 } |
OLD | NEW |