Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(387)

Side by Side Diff: tools/json_schema_compiler/cpp_util.py

Issue 23549025: Clean up JSON Schema Compiler. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Removed enum test and added blank lines. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 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 """Utilies and constants specific to Chromium C++ code. 5 """Utilies and constants specific to Chromium C++ code.
6 """ 6 """
7 7
8 from code import Code 8 from code import Code
9 from datetime import datetime 9 from datetime import datetime
10 from model import Property, PropertyType, Type 10 from model import PropertyType
11 import os 11 import os
12 import re 12 import re
13 13
14 CHROMIUM_LICENSE = ( 14 CHROMIUM_LICENSE = (
15 """// Copyright (c) %d The Chromium Authors. All rights reserved. 15 """// Copyright (c) %d The Chromium Authors. All rights reserved.
16 // Use of this source code is governed by a BSD-style license that can be 16 // Use of this source code is governed by a BSD-style license that can be
17 // found in the LICENSE file.""" % datetime.now().year 17 // found in the LICENSE file.""" % datetime.now().year
18 ) 18 )
19 GENERATED_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITION IN 19 GENERATED_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITION IN
20 // %s 20 // %s
21 // DO NOT EDIT. 21 // DO NOT EDIT.
22 """ 22 """
23 GENERATED_BUNDLE_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITIONS IN 23 GENERATED_BUNDLE_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITIONS IN
24 // %s 24 // %s
25 // DO NOT EDIT. 25 // DO NOT EDIT.
26 """ 26 """
27 27
28 def Classname(s): 28 def Classname(s):
29 """Translates a namespace name or function name into something more 29 """Translates a namespace name or function name into something more
30 suited to C++. 30 suited to C++.
31 31
32 eg experimental.downloads -> Experimental_Downloads 32 eg experimental.downloads -> Experimental_Downloads
33 updateAll -> UpdateAll. 33 updateAll -> UpdateAll.
34 """ 34 """
35 return '_'.join([x[0].upper() + x[1:] for x in re.split('\W', s)]) 35 return '_'.join([x[0].upper() + x[1:] for x in re.split('\W', s)])
36 36
37
37 def GetAsFundamentalValue(type_, src, dst): 38 def GetAsFundamentalValue(type_, src, dst):
38 """Returns the C++ code for retrieving a fundamental type from a 39 """Returns the C++ code for retrieving a fundamental type from a
39 Value into a variable. 40 Value into a variable.
40 41
41 src: Value* 42 src: Value*
42 dst: Property* 43 dst: Property*
43 """ 44 """
44 return { 45 return {
45 PropertyType.BOOLEAN: '%s->GetAsBoolean(%s)', 46 PropertyType.BOOLEAN: '%s->GetAsBoolean(%s)',
46 PropertyType.DOUBLE: '%s->GetAsDouble(%s)', 47 PropertyType.DOUBLE: '%s->GetAsDouble(%s)',
47 PropertyType.INTEGER: '%s->GetAsInteger(%s)', 48 PropertyType.INTEGER: '%s->GetAsInteger(%s)',
48 PropertyType.STRING: '%s->GetAsString(%s)', 49 PropertyType.STRING: '%s->GetAsString(%s)',
49 }[type_.property_type] % (src, dst) 50 }[type_.property_type] % (src, dst)
50 51
52
51 def GetValueType(type_): 53 def GetValueType(type_):
52 """Returns the Value::Type corresponding to the model.Type. 54 """Returns the Value::Type corresponding to the model.Type.
53 """ 55 """
54 return { 56 return {
55 PropertyType.ARRAY: 'base::Value::TYPE_LIST', 57 PropertyType.ARRAY: 'base::Value::TYPE_LIST',
56 PropertyType.BINARY: 'base::Value::TYPE_BINARY', 58 PropertyType.BINARY: 'base::Value::TYPE_BINARY',
57 PropertyType.BOOLEAN: 'base::Value::TYPE_BOOLEAN', 59 PropertyType.BOOLEAN: 'base::Value::TYPE_BOOLEAN',
58 # PropertyType.CHOICES can be any combination of types. 60 # PropertyType.CHOICES can be any combination of types.
59 PropertyType.DOUBLE: 'base::Value::TYPE_DOUBLE', 61 PropertyType.DOUBLE: 'base::Value::TYPE_DOUBLE',
60 PropertyType.ENUM: 'base::Value::TYPE_STRING', 62 PropertyType.ENUM: 'base::Value::TYPE_STRING',
61 PropertyType.FUNCTION: 'base::Value::TYPE_DICTIONARY', 63 PropertyType.FUNCTION: 'base::Value::TYPE_DICTIONARY',
62 PropertyType.INTEGER: 'base::Value::TYPE_INTEGER', 64 PropertyType.INTEGER: 'base::Value::TYPE_INTEGER',
63 PropertyType.OBJECT: 'base::Value::TYPE_DICTIONARY', 65 PropertyType.OBJECT: 'base::Value::TYPE_DICTIONARY',
64 PropertyType.STRING: 'base::Value::TYPE_STRING', 66 PropertyType.STRING: 'base::Value::TYPE_STRING',
65 }[type_.property_type] 67 }[type_.property_type]
66 68
69
67 def GetParameterDeclaration(param, type_): 70 def GetParameterDeclaration(param, type_):
68 """Gets a parameter declaration of a given model.Property and its C++ 71 """Gets a parameter declaration of a given model.Property and its C++
69 type. 72 type.
70 """ 73 """
71 if param.type_.property_type in (PropertyType.ANY, 74 if param.type_.property_type in (PropertyType.ANY,
72 PropertyType.ARRAY, 75 PropertyType.ARRAY,
73 PropertyType.CHOICES, 76 PropertyType.CHOICES,
74 PropertyType.OBJECT, 77 PropertyType.OBJECT,
75 PropertyType.REF, 78 PropertyType.REF,
76 PropertyType.STRING): 79 PropertyType.STRING):
77 arg = 'const %(type)s& %(name)s' 80 arg = 'const %(type)s& %(name)s'
78 else: 81 else:
79 arg = '%(type)s %(name)s' 82 arg = '%(type)s %(name)s'
80 return arg % { 83 return arg % {
81 'type': type_, 84 'type': type_,
82 'name': param.unix_name, 85 'name': param.unix_name,
83 } 86 }
84 87
88
85 def GenerateIfndefName(path, filename): 89 def GenerateIfndefName(path, filename):
86 """Formats a path and filename as a #define name. 90 """Formats a path and filename as a #define name.
87 91
88 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__. 92 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__.
89 """ 93 """
90 return (('%s_%s_H__' % (path, filename)) 94 return (('%s_%s_H__' % (path, filename))
91 .upper().replace(os.sep, '_').replace('/', '_')) 95 .upper().replace(os.sep, '_').replace('/', '_'))
92 96
97
93 def PadForGenerics(var): 98 def PadForGenerics(var):
94 """Appends a space to |var| if it ends with a >, so that it can be compiled 99 """Appends a space to |var| if it ends with a >, so that it can be compiled
95 within generic types. 100 within generic types.
96 """ 101 """
97 return ('%s ' % var) if var.endswith('>') else var 102 return ('%s ' % var) if var.endswith('>') else var
98 103
104
99 def OpenNamespace(namespace): 105 def OpenNamespace(namespace):
100 """Get opening root namespace declarations. 106 """Get opening root namespace declarations.
101 """ 107 """
102 c = Code() 108 c = Code()
103 for component in namespace.split('::'): 109 for component in namespace.split('::'):
104 c.Append('namespace %s {' % component) 110 c.Append('namespace %s {' % component)
105 return c 111 return c
106 112
113
107 def CloseNamespace(namespace): 114 def CloseNamespace(namespace):
108 """Get closing root namespace declarations. 115 """Get closing root namespace declarations.
109 """ 116 """
110 c = Code() 117 c = Code()
111 for component in reversed(namespace.split('::')): 118 for component in reversed(namespace.split('::')):
112 c.Append('} // namespace %s' % component) 119 c.Append('} // namespace %s' % component)
113 return c 120 return c
OLDNEW
« no previous file with comments | « tools/json_schema_compiler/cpp_type_generator.py ('k') | tools/json_schema_compiler/dart_generator.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698