OLD | NEW |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import argparse | 5 import argparse |
6 import copy | 6 import copy |
7 from datetime import datetime | 7 from datetime import datetime |
8 import os | 8 import os |
9 | 9 |
10 from code import Code | 10 from code import Code |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 """Parses the feature from the given json value.""" | 350 """Parses the feature from the given json value.""" |
351 for key in parsed_json.keys(): | 351 for key in parsed_json.keys(): |
352 if key not in FEATURE_GRAMMAR: | 352 if key not in FEATURE_GRAMMAR: |
353 self._AddError(key, 'Unrecognized key') | 353 self._AddError(key, 'Unrecognized key') |
354 for key, key_grammar in FEATURE_GRAMMAR.iteritems(): | 354 for key, key_grammar in FEATURE_GRAMMAR.iteritems(): |
355 self._ParseKey(key, parsed_json, key_grammar) | 355 self._ParseKey(key, parsed_json, key_grammar) |
356 | 356 |
357 def GetCode(self, feature_class): | 357 def GetCode(self, feature_class): |
358 """Returns the Code object for generating this feature.""" | 358 """Returns the Code object for generating this feature.""" |
359 c = Code() | 359 c = Code() |
360 c.Append('std::unique_ptr<%s> feature(new %s());' % | 360 c.Append('%s* feature = new %s();' % (feature_class, feature_class)) |
361 (feature_class, feature_class)) | |
362 c.Append('feature->set_name("%s");' % self.name) | 361 c.Append('feature->set_name("%s");' % self.name) |
363 for key in sorted(self.feature_values.keys()): | 362 for key in sorted(self.feature_values.keys()): |
364 if key in IGNORED_KEYS: | 363 if key in IGNORED_KEYS: |
365 continue; | 364 continue; |
366 c.Append('feature->set_%s(%s);' % (key, self.feature_values[key])) | 365 c.Append('feature->set_%s(%s);' % (key, self.feature_values[key])) |
367 return c | 366 return c |
368 | 367 |
369 class FeatureCompiler(object): | 368 class FeatureCompiler(object): |
370 """A compiler to load, parse, and generate C++ code for a number of | 369 """A compiler to load, parse, and generate C++ code for a number of |
371 features.json files.""" | 370 features.json files.""" |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 def Render(self): | 478 def Render(self): |
480 """Returns the Code object for the body of the .cc file, which handles the | 479 """Returns the Code object for the body of the .cc file, which handles the |
481 initialization of all features.""" | 480 initialization of all features.""" |
482 c = Code() | 481 c = Code() |
483 c.Append('%s::%s() {' % (self._provider_class, self._provider_class)) | 482 c.Append('%s::%s() {' % (self._provider_class, self._provider_class)) |
484 c.Sblock() | 483 c.Sblock() |
485 for k in sorted(self._features.keys()): | 484 for k in sorted(self._features.keys()): |
486 c.Sblock('{') | 485 c.Sblock('{') |
487 feature = self._features[k] | 486 feature = self._features[k] |
488 if type(feature) is list: | 487 if type(feature) is list: |
489 c.Append('std::unique_ptr<ComplexFeature::FeatureList> features(') | 488 c.Append('std::vector<Feature*> features;') |
490 c.Append(' new ComplexFeature::FeatureList());') | |
491 for f in feature: | 489 for f in feature: |
492 c.Sblock('{') | 490 c.Sblock('{') |
493 c.Concat(f.GetCode(self._feature_class)) | 491 c.Concat(f.GetCode(self._feature_class)) |
494 c.Append('features->push_back(std::move(feature));') | 492 c.Append('features.push_back(feature);') |
495 c.Eblock('}') | 493 c.Eblock('}') |
496 c.Append('std::unique_ptr<ComplexFeature> feature(') | 494 c.Append('ComplexFeature* feature(new ComplexFeature(&features));') |
497 c.Append(' new ComplexFeature(std::move(features)));') | |
498 c.Append('feature->set_name("%s");' % k) | 495 c.Append('feature->set_name("%s");' % k) |
499 else: | 496 else: |
500 c.Concat(feature.GetCode(self._feature_class)) | 497 c.Concat(feature.GetCode(self._feature_class)) |
501 c.Append('AddFeature("%s", std::move(feature));' % k) | 498 c.Append('AddFeature("%s", feature);' % k) |
502 c.Eblock('}') | 499 c.Eblock('}') |
503 c.Eblock('}') | 500 c.Eblock('}') |
504 return c | 501 return c |
505 | 502 |
506 def Write(self): | 503 def Write(self): |
507 """Writes the output.""" | 504 """Writes the output.""" |
508 header_file_path = self._out_base_filename + '.h' | 505 header_file_path = self._out_base_filename + '.h' |
509 cc_file_path = self._out_base_filename + '.cc' | 506 cc_file_path = self._out_base_filename + '.cc' |
510 substitutions = ({ | 507 substitutions = ({ |
511 'header_file_path': header_file_path, | 508 'header_file_path': header_file_path, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
551 'out_base_filename', type=str, | 548 'out_base_filename', type=str, |
552 help='The base filename for the C++ files (.h and .cc will be appended)') | 549 help='The base filename for the C++ files (.h and .cc will be appended)') |
553 parser.add_argument('source_files', type=str, nargs='+', | 550 parser.add_argument('source_files', type=str, nargs='+', |
554 help='The source features.json files') | 551 help='The source features.json files') |
555 args = parser.parse_args() | 552 args = parser.parse_args() |
556 c = FeatureCompiler(args.chrome_root, args.source_files, args.feature_class, | 553 c = FeatureCompiler(args.chrome_root, args.source_files, args.feature_class, |
557 args.provider_class, args.out_root, | 554 args.provider_class, args.out_root, |
558 args.out_base_filename) | 555 args.out_base_filename) |
559 c.Compile() | 556 c.Compile() |
560 c.Write() | 557 c.Write() |
OLD | NEW |