| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /** | 5 /** |
| 6 * This file contains code to generate serialization/deserialization logic for | 6 * This file contains code to generate serialization/deserialization logic for |
| 7 * summaries based on an "IDL" description of the summary format (written in | 7 * summaries based on an "IDL" description of the summary format (written in |
| 8 * stylized Dart). | 8 * stylized Dart). |
| 9 * | 9 * |
| 10 * For each class in the "IDL" input, two corresponding classes are generated: | 10 * For each class in the "IDL" input, two corresponding classes are generated: |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 Scanner scanner = new Scanner(idlSource, idlReader, errorListener); | 55 Scanner scanner = new Scanner(idlSource, idlReader, errorListener); |
| 56 Token tokenStream = scanner.tokenize(); | 56 Token tokenStream = scanner.tokenize(); |
| 57 LineInfo lineInfo = new LineInfo(scanner.lineStarts); | 57 LineInfo lineInfo = new LineInfo(scanner.lineStarts); |
| 58 Parser parser = new Parser(idlSource, new BooleanErrorListener()); | 58 Parser parser = new Parser(idlSource, new BooleanErrorListener()); |
| 59 CompilationUnit idlParsed = parser.parseCompilationUnit(tokenStream); | 59 CompilationUnit idlParsed = parser.parseCompilationUnit(tokenStream); |
| 60 _CodeGenerator codeGenerator = new _CodeGenerator(); | 60 _CodeGenerator codeGenerator = new _CodeGenerator(); |
| 61 codeGenerator.processCompilationUnit(lineInfo, idlParsed); | 61 codeGenerator.processCompilationUnit(lineInfo, idlParsed); |
| 62 return codeGenerator._outBuffer.toString(); | 62 return codeGenerator._outBuffer.toString(); |
| 63 }); | 63 }); |
| 64 | 64 |
| 65 typedef String _StringToString(String s); |
| 66 |
| 65 class _CodeGenerator { | 67 class _CodeGenerator { |
| 66 /** | 68 /** |
| 67 * Buffer in which generated code is accumulated. | 69 * Buffer in which generated code is accumulated. |
| 68 */ | 70 */ |
| 69 final StringBuffer _outBuffer = new StringBuffer(); | 71 final StringBuffer _outBuffer = new StringBuffer(); |
| 70 | 72 |
| 71 /** | 73 /** |
| 72 * Current indentation level. | 74 * Current indentation level. |
| 73 */ | 75 */ |
| 74 String _indentation = ''; | 76 String _indentation = ''; |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 out('// for details. All rights reserved. Use of this source code is governe
d by a'); | 336 out('// for details. All rights reserved. Use of this source code is governe
d by a'); |
| 335 out('// BSD-style license that can be found in the LICENSE file.'); | 337 out('// BSD-style license that can be found in the LICENSE file.'); |
| 336 out('//'); | 338 out('//'); |
| 337 out('// This file has been automatically generated. Please do not edit it m
anually.'); | 339 out('// This file has been automatically generated. Please do not edit it m
anually.'); |
| 338 out('// To regenerate the file, use the script "pkg/analyzer/tool/generate_f
iles".'); | 340 out('// To regenerate the file, use the script "pkg/analyzer/tool/generate_f
iles".'); |
| 339 out(); | 341 out(); |
| 340 out('library analyzer.src.summary.format;'); | 342 out('library analyzer.src.summary.format;'); |
| 341 out(); | 343 out(); |
| 342 out("import 'flat_buffers.dart' as fb;"); | 344 out("import 'flat_buffers.dart' as fb;"); |
| 343 out("import 'idl.dart' as idl;"); | 345 out("import 'idl.dart' as idl;"); |
| 346 out("import 'dart:convert' as convert;"); |
| 344 out(); | 347 out(); |
| 345 for (idlModel.EnumDeclaration enm in _idl.enums.values) { | 348 for (idlModel.EnumDeclaration enm in _idl.enums.values) { |
| 346 _generateEnumReader(enm); | 349 _generateEnumReader(enm); |
| 347 out(); | 350 out(); |
| 348 } | 351 } |
| 349 for (idlModel.ClassDeclaration cls in _idl.classes.values) { | 352 for (idlModel.ClassDeclaration cls in _idl.classes.values) { |
| 350 _generateBuilder(cls); | 353 _generateBuilder(cls); |
| 351 out(); | 354 out(); |
| 352 if (cls.isTopLevel) { | 355 if (cls.isTopLevel) { |
| 353 _generateReadFunction(cls); | 356 _generateReadFunction(cls); |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 } | 627 } |
| 625 }); | 628 }); |
| 626 out('}'); | 629 out('}'); |
| 627 } | 630 } |
| 628 | 631 |
| 629 void _generateMixin(idlModel.ClassDeclaration cls) { | 632 void _generateMixin(idlModel.ClassDeclaration cls) { |
| 630 String name = cls.name; | 633 String name = cls.name; |
| 631 String mixinName = '_${name}Mixin'; | 634 String mixinName = '_${name}Mixin'; |
| 632 out('abstract class $mixinName implements ${idlPrefix(name)} {'); | 635 out('abstract class $mixinName implements ${idlPrefix(name)} {'); |
| 633 indent(() { | 636 indent(() { |
| 637 // Write toJson(). |
| 638 out('@override'); |
| 639 out('Map<String, Object> toJson() {'); |
| 640 indent(() { |
| 641 out('Map<String, Object> _result = <String, Object>{};'); |
| 642 for (idlModel.FieldDeclaration field in cls.fields) { |
| 643 String condition; |
| 644 if (field.type.isList) { |
| 645 condition = '${field.name}.isNotEmpty'; |
| 646 } else { |
| 647 condition = '${field.name} != ${defaultValue(field.type, false)}'; |
| 648 } |
| 649 _StringToString convertItem; |
| 650 if (_idl.classes.containsKey(field.type.typeName)) { |
| 651 convertItem = (String name) => '$name.toJson()'; |
| 652 } else if (_idl.enums.containsKey(field.type.typeName)) { |
| 653 // TODO(paulberry): it would be better to generate a const list of |
| 654 // strings so that we don't have to do this kludge. |
| 655 convertItem = (String name) => "$name.toString().split('.')[1]"; |
| 656 } else if (field.type.typeName == 'double') { |
| 657 convertItem = |
| 658 (String name) => '$name.isFinite ? $name : $name.toString()'; |
| 659 } |
| 660 String convertField; |
| 661 if (convertItem == null) { |
| 662 convertField = field.name; |
| 663 } else if (field.type.isList) { |
| 664 convertField = '${field.name}.map((_value) =>' |
| 665 ' ${convertItem('_value')}).toList()'; |
| 666 } else { |
| 667 convertField = convertItem(field.name); |
| 668 } |
| 669 String storeField = '_result[${quoted(field.name)}] = $convertField'; |
| 670 out('if ($condition) $storeField;'); |
| 671 } |
| 672 out('return _result;'); |
| 673 }); |
| 674 out('}'); |
| 675 out(); |
| 634 // Write toMap(). | 676 // Write toMap(). |
| 635 out('@override'); | 677 out('@override'); |
| 636 out('Map<String, Object> toMap() => {'); | 678 out('Map<String, Object> toMap() => {'); |
| 637 indent(() { | 679 indent(() { |
| 638 for (idlModel.FieldDeclaration field in cls.fields) { | 680 for (idlModel.FieldDeclaration field in cls.fields) { |
| 639 String fieldName = field.name; | 681 String fieldName = field.name; |
| 640 out('${quoted(fieldName)}: $fieldName,'); | 682 out('${quoted(fieldName)}: $fieldName,'); |
| 641 } | 683 } |
| 642 }); | 684 }); |
| 643 out('};'); | 685 out('};'); |
| 686 out(); |
| 687 // Write toString(). |
| 688 out('@override'); |
| 689 out('String toString() => convert.JSON.encode(toJson());'); |
| 644 }); | 690 }); |
| 645 out('}'); | 691 out('}'); |
| 646 } | 692 } |
| 647 | 693 |
| 648 void _generateReader(idlModel.ClassDeclaration cls) { | 694 void _generateReader(idlModel.ClassDeclaration cls) { |
| 649 String name = cls.name; | 695 String name = cls.name; |
| 650 String readerName = '_${name}Reader'; | 696 String readerName = '_${name}Reader'; |
| 651 String implName = '_${name}Impl'; | 697 String implName = '_${name}Impl'; |
| 652 out('class $readerName extends fb.TableReader<$implName> {'); | 698 out('class $readerName extends fb.TableReader<$implName> {'); |
| 653 indent(() { | 699 indent(() { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 685 return token.lexeme.split('\n').map((String line) { | 731 return token.lexeme.split('\n').map((String line) { |
| 686 if (line.startsWith(indent)) { | 732 if (line.startsWith(indent)) { |
| 687 line = line.substring(indent.length); | 733 line = line.substring(indent.length); |
| 688 } | 734 } |
| 689 return line; | 735 return line; |
| 690 }).join('\n'); | 736 }).join('\n'); |
| 691 } | 737 } |
| 692 return null; | 738 return null; |
| 693 } | 739 } |
| 694 } | 740 } |
| OLD | NEW |