1#!/usr/bin/env python 2# Copyright 2017 The Chromium Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import gperf 7import json5_generator 8import template_expander 9 10 11class AtRuleNamesWriter(json5_generator.Writer): 12 """ 13 Generates AtRuleNames. This class provides utility methods for parsing 14 @rules (e.g. @font-face, @keyframes, etc) 15 """ 16 17 def __init__(self, json5_file_paths, output_dir): 18 super(AtRuleNamesWriter, self).__init__(json5_file_paths, output_dir) 19 20 self._outputs = { 21 'at_rule_descriptors.h': self.generate_header, 22 'at_rule_descriptors.cc': self.generate_implementation 23 } 24 25 self._descriptors = self.json5_file.name_dictionaries 26 self._character_offsets = [] 27 28 # AtRuleDescriptorID::Invalid is 0. 29 first_descriptor_id = 1 30 # Aliases are resolved immediately at parse time, and thus don't appear 31 # in the enum. 32 self._descriptors_count = len(self._descriptors) + first_descriptor_id 33 chars_used = 0 34 self._longest_name_length = 0 35 for offset, descriptor in enumerate(self._descriptors): 36 descriptor['enum_value'] = first_descriptor_id + offset 37 self._character_offsets.append(chars_used) 38 chars_used += len(descriptor['name'].original) 39 self._longest_name_length = max( 40 len(descriptor['name'].original), len(descriptor['alias']), 41 self._longest_name_length) 42 43 @template_expander.use_jinja( 44 'core/css/parser/templates/at_rule_descriptors.h.tmpl') 45 def generate_header(self): 46 return { 47 'descriptors': self._descriptors, 48 'descriptors_count': self._descriptors_count 49 } 50 51 @gperf.use_jinja_gperf_template( 52 'core/css/parser/templates/at_rule_descriptors.cc.tmpl') 53 def generate_implementation(self): 54 return { 55 'descriptors': self._descriptors, 56 'descriptor_offsets': self._character_offsets, 57 'descriptors_count': len(self._descriptors), 58 'longest_name_length': self._longest_name_length, 59 'gperf_path': self.gperf_path 60 } 61 62 63if __name__ == '__main__': 64 json5_generator.Maker(AtRuleNamesWriter).main() 65