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 collections | 6 import collections |
7 import os.path | 7 import os.path |
8 import re | 8 import re |
9 import sys | 9 import sys |
10 try: | 10 try: |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 # 'LoLoLoL'). | 58 # 'LoLoLoL'). |
59 name = re.sub(r'([^_])([A-Z][a-z]+?)', r'\1_\2', name) | 59 name = re.sub(r'([^_])([A-Z][a-z]+?)', r'\1_\2', name) |
60 name = re.sub(r'([^_])([A-Z][a-z]+?)', r'\1_\2', name) | 60 name = re.sub(r'([^_])([A-Z][a-z]+?)', r'\1_\2', name) |
61 return name.lower() | 61 return name.lower() |
62 | 62 |
63 | 63 |
64 def SanitizeLiteral(literal): | 64 def SanitizeLiteral(literal): |
65 return { | 65 return { |
66 # Rename null enumeration values to avoid a clash with the NULL macro. | 66 # Rename null enumeration values to avoid a clash with the NULL macro. |
67 'null': 'none', | 67 'null': 'none', |
| 68 # Rename literals that clash with Win32 defined macros. |
| 69 'error': 'err', |
| 70 'mouseMoved': 'mouse_ptr_moved', |
| 71 'Strict': 'exact', |
| 72 'getCurrentTime': 'getCurrentAnimationTime', |
68 # Rename mathematical constants to avoid colliding with C macros. | 73 # Rename mathematical constants to avoid colliding with C macros. |
69 'Infinity': 'InfinityValue', | 74 'Infinity': 'InfinityValue', |
70 '-Infinity': 'NegativeInfinityValue', | 75 '-Infinity': 'NegativeInfinityValue', |
71 'NaN': 'NaNValue', | 76 'NaN': 'NaNValue', |
72 # Turn negative zero into a safe identifier. | 77 # Turn negative zero into a safe identifier. |
73 '-0': 'NegativeZeroValue', | 78 '-0': 'NegativeZeroValue', |
74 }.get(literal, literal) | 79 }.get(literal, literal) |
75 | 80 |
76 | 81 |
77 def InitializeJinjaEnv(cache_dir): | 82 def InitializeJinjaEnv(cache_dir): |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 for property in type.get('properties', []): | 319 for property in type.get('properties', []): |
315 if 'enum' in property and not '$ref' in property: | 320 if 'enum' in property and not '$ref' in property: |
316 SynthesizeEnumType(domain, type['id'], property) | 321 SynthesizeEnumType(domain, type['id'], property) |
317 | 322 |
318 for command in domain.get('commands', []): | 323 for command in domain.get('commands', []): |
319 if 'parameters' in command: | 324 if 'parameters' in command: |
320 for parameter in command['parameters']: | 325 for parameter in command['parameters']: |
321 if 'enum' in parameter and not '$ref' in parameter: | 326 if 'enum' in parameter and not '$ref' in parameter: |
322 SynthesizeEnumType(domain, command['name'], parameter) | 327 SynthesizeEnumType(domain, command['name'], parameter) |
323 parameters_type = { | 328 parameters_type = { |
324 'id': ToTitleCase(command['name']) + 'Params', | 329 'id': ToTitleCase(SanitizeLiteral(command['name'])) + 'Params', |
325 'type': 'object', | 330 'type': 'object', |
326 'description': 'Parameters for the %s command.' % ToTitleCase( | 331 'description': 'Parameters for the %s command.' % ToTitleCase( |
327 command['name']), | 332 SanitizeLiteral(command['name'])), |
328 'properties': command['parameters'] | 333 'properties': command['parameters'] |
329 } | 334 } |
330 domain['types'].append(parameters_type) | 335 domain['types'].append(parameters_type) |
331 if 'returns' in command: | 336 if 'returns' in command: |
332 for parameter in command['returns']: | 337 for parameter in command['returns']: |
333 if 'enum' in parameter and not '$ref' in parameter: | 338 if 'enum' in parameter and not '$ref' in parameter: |
334 SynthesizeEnumType(domain, command['name'], parameter) | 339 SynthesizeEnumType(domain, command['name'], parameter) |
335 result_type = { | 340 result_type = { |
336 'id': ToTitleCase(command['name']) + 'Result', | 341 'id': ToTitleCase(SanitizeLiteral(command['name'])) + 'Result', |
337 'type': 'object', | 342 'type': 'object', |
338 'description': 'Result for the %s command.' % ToTitleCase( | 343 'description': 'Result for the %s command.' % ToTitleCase( |
339 command['name']), | 344 SanitizeLiteral(command['name'])), |
340 'properties': command['returns'] | 345 'properties': command['returns'] |
341 } | 346 } |
342 domain['types'].append(result_type) | 347 domain['types'].append(result_type) |
343 | 348 |
344 | 349 |
345 def SynthesizeEventTypes(json_api): | 350 def SynthesizeEventTypes(json_api): |
346 """Generate types for events and their properties. | 351 """Generate types for events and their properties. |
347 | 352 |
348 Note that parameter objects are also created for events without parameters to | 353 Note that parameter objects are also created for events without parameters to |
349 make it easier to introduce parameters later. | 354 make it easier to introduce parameters later. |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 InitializeDomainDependencies(json_api) | 532 InitializeDomainDependencies(json_api) |
528 PatchExperimentalCommandsAndEvents(json_api) | 533 PatchExperimentalCommandsAndEvents(json_api) |
529 EnsureCommandsHaveParametersAndReturnTypes(json_api) | 534 EnsureCommandsHaveParametersAndReturnTypes(json_api) |
530 SynthesizeCommandTypes(json_api) | 535 SynthesizeCommandTypes(json_api) |
531 SynthesizeEventTypes(json_api) | 536 SynthesizeEventTypes(json_api) |
532 PatchFullQualifiedRefs(json_api) | 537 PatchFullQualifiedRefs(json_api) |
533 CreateTypeDefinitions(json_api) | 538 CreateTypeDefinitions(json_api) |
534 GenerateDomains(jinja_env, output_dirname, json_api) | 539 GenerateDomains(jinja_env, output_dirname, json_api) |
535 GenerateTypes(jinja_env, output_dirname, json_api) | 540 GenerateTypes(jinja_env, output_dirname, json_api) |
536 GenerateTypeConversions(jinja_env, output_dirname, json_api) | 541 GenerateTypeConversions(jinja_env, output_dirname, json_api) |
OLD | NEW |