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 os.path | 6 import os.path |
7 import sys | 7 import sys |
8 import re | 8 import re |
9 try: | 9 try: |
10 import json | 10 import json |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 | 52 |
53 | 53 |
54 def CamelCaseToHackerStyle(name): | 54 def CamelCaseToHackerStyle(name): |
55 # Do two passes to insert '_' chars to deal with overlapping matches (e.g., | 55 # Do two passes to insert '_' chars to deal with overlapping matches (e.g., |
56 # 'LoLoLoL'). | 56 # 'LoLoLoL'). |
57 name = re.sub(r'([^_])([A-Z][a-z]+?)', r'\1_\2', name) | 57 name = re.sub(r'([^_])([A-Z][a-z]+?)', r'\1_\2', name) |
58 name = re.sub(r'([^_])([A-Z][a-z]+?)', r'\1_\2', name) | 58 name = re.sub(r'([^_])([A-Z][a-z]+?)', r'\1_\2', name) |
59 return name.lower() | 59 return name.lower() |
60 | 60 |
61 | 61 |
62 def MangleEnum(value): | 62 def SanitizeLiteral(literal): |
63 # Rename NULL enumeration values to avoid a clash with the actual NULL. | 63 return { |
64 return 'NONE' if value == 'NULL' else value | 64 # Rename null enumeration values to avoid a clash with the NULL macro. |
| 65 'null': 'none', |
| 66 # Rename mathematical constants to avoid colliding with C macros. |
| 67 'Infinity': 'InfinityValue', |
| 68 '-Infinity': 'NegativeInfinityValue', |
| 69 'NaN': 'NaNValue', |
| 70 # Turn negative zero into a safe identifier. |
| 71 '-0': 'NegativeZeroValue', |
| 72 }.get(literal, literal) |
65 | 73 |
66 | 74 |
67 def InitializeJinjaEnv(cache_dir): | 75 def InitializeJinjaEnv(cache_dir): |
68 jinja_env = jinja2.Environment( | 76 jinja_env = jinja2.Environment( |
69 loader=jinja2.FileSystemLoader(templates_dir), | 77 loader=jinja2.FileSystemLoader(templates_dir), |
70 # Bytecode cache is not concurrency-safe unless pre-cached: | 78 # Bytecode cache is not concurrency-safe unless pre-cached: |
71 # if pre-cached this is read-only, but writing creates a race condition. | 79 # if pre-cached this is read-only, but writing creates a race condition. |
72 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), | 80 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), |
73 keep_trailing_newline=True, # Newline-terminate generated files. | 81 keep_trailing_newline=True, # Newline-terminate generated files. |
74 lstrip_blocks=True, # So we can indent control flow tags. | 82 lstrip_blocks=True, # So we can indent control flow tags. |
75 trim_blocks=True) | 83 trim_blocks=True) |
76 jinja_env.filters.update({ | 84 jinja_env.filters.update({ |
77 'to_title_case': ToTitleCase, | 85 'to_title_case': ToTitleCase, |
78 'dash_to_camelcase': DashToCamelCase, | 86 'dash_to_camelcase': DashToCamelCase, |
79 'camelcase_to_hacker_style': CamelCaseToHackerStyle, | 87 'camelcase_to_hacker_style': CamelCaseToHackerStyle, |
80 'mangle_enum': MangleEnum, | 88 'sanitize_literal': SanitizeLiteral, |
81 }) | 89 }) |
82 jinja_env.add_extension('jinja2.ext.loopcontrols') | 90 jinja_env.add_extension('jinja2.ext.loopcontrols') |
83 return jinja_env | 91 return jinja_env |
84 | 92 |
85 | 93 |
86 def PatchFullQualifiedRefs(json_api): | 94 def PatchFullQualifiedRefs(json_api): |
87 def PatchFullQualifiedRefsInDomain(json, domain_name): | 95 def PatchFullQualifiedRefsInDomain(json, domain_name): |
88 if isinstance(json, list): | 96 if isinstance(json, list): |
89 for item in json: | 97 for item in json: |
90 PatchFullQualifiedRefsInDomain(item, domain_name) | 98 PatchFullQualifiedRefsInDomain(item, domain_name) |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 json_api, output_dirname = ParseArguments(sys.argv[1:]) | 421 json_api, output_dirname = ParseArguments(sys.argv[1:]) |
414 jinja_env = InitializeJinjaEnv(output_dirname) | 422 jinja_env = InitializeJinjaEnv(output_dirname) |
415 PatchHiddenCommandsAndEvents(json_api) | 423 PatchHiddenCommandsAndEvents(json_api) |
416 SynthesizeCommandTypes(json_api) | 424 SynthesizeCommandTypes(json_api) |
417 SynthesizeEventTypes(json_api) | 425 SynthesizeEventTypes(json_api) |
418 PatchFullQualifiedRefs(json_api) | 426 PatchFullQualifiedRefs(json_api) |
419 CreateTypeDefinitions(json_api) | 427 CreateTypeDefinitions(json_api) |
420 Generate(jinja_env, output_dirname, json_api, 'types', ['cc', 'h']) | 428 Generate(jinja_env, output_dirname, json_api, 'types', ['cc', 'h']) |
421 Generate(jinja_env, output_dirname, json_api, 'type_conversions', ['h']) | 429 Generate(jinja_env, output_dirname, json_api, 'type_conversions', ['h']) |
422 GenerateDomains(jinja_env, output_dirname, json_api, 'domain', ['cc', 'h']) | 430 GenerateDomains(jinja_env, output_dirname, json_api, 'domain', ['cc', 'h']) |
OLD | NEW |