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

Unified Diff: mojo/public/tools/bindings/pylib/mojom/generate/generator.py

Issue 2855263002: Mojo bindings: support generating identifers using different style rules for different target langu…
Patch Set: . Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: mojo/public/tools/bindings/pylib/mojom/generate/generator.py
diff --git a/mojo/public/tools/bindings/pylib/mojom/generate/generator.py b/mojo/public/tools/bindings/pylib/mojom/generate/generator.py
index 0e64af78a12b6db2fb6adfc863e081099d87081d..52fc3590852d25f0f20e2df4a7a1d9785787b7e0 100644
--- a/mojo/public/tools/bindings/pylib/mojom/generate/generator.py
+++ b/mojo/public/tools/bindings/pylib/mojom/generate/generator.py
@@ -17,12 +17,14 @@ def ExpectedArraySize(kind):
return kind.length
return None
-def StudlyCapsToCamel(studly):
- return studly[0].lower() + studly[1:]
-
-def UnderToCamel(under):
- """Converts underscore_separated strings to CamelCase strings."""
- return ''.join(word.capitalize() for word in under.split('_'))
+def ToCamel(identifier, lower_initial=False, dilimiter='_'):
+ """Splits |identifier| using |dilimiter|, capitalizes each word (but makes the
+ first character of the first word lowercased if |lower_initial| is set to
+ True), and joins the words."""
+ result = ''.join(word.capitalize() for word in identifier.split(dilimiter))
+ if lower_initial:
+ result = result[0].lower() + result[1:]
+ return result
def WriteFile(contents, full_path):
# Make sure the containing directory exists.
@@ -33,6 +35,39 @@ def WriteFile(contents, full_path):
with open(full_path, "w+") as f:
f.write(contents)
+
+class Stylizer(object):
+ def StylizeConstant(self, name):
+ return name
+
+ def StylizeField(self, name):
+ return name
+
+ def StylizeStruct(self, name):
+ return name
+
+ def StylizeUnion(self, name):
+ return name
+
+ def StylizeParameter(self, name):
+ return name
+
+ def StylizeMethod(self, name):
+ return name
+
+ def StylizeInterface(self, name):
+ return name
+
+ def StylizeEnumField(self, name):
+ return name
+
+ def StylizeEnum(self, name):
+ return name
+
+ def StylizeModule(self, name):
+ return name
+
+
class Generator(object):
# Pass |output_dir| to emit files to disk. Omit |output_dir| to echo all
# files to stdout.
@@ -52,24 +87,26 @@ class Generator(object):
self.export_header = export_header
self.generate_non_variant_code = generate_non_variant_code
+ def FinalizeModule(self, stylizer=None):
+ for struct in self.module.structs:
+ self._AddStructComputedData(True, struct)
+ for union in self.module.unions:
+ self._AddUnionComputedData(union)
+ for interface in self.module.interfaces:
+ self._AddInterfaceComputedData(interface)
+
+ if stylizer:
+ self.module.Stylize(stylizer)
+
def GetStructsFromMethods(self):
result = []
for interface in self.module.interfaces:
for method in interface.methods:
- result.append(self._GetStructFromMethod(method))
- if method.response_parameters != None:
- result.append(self._GetResponseStructFromMethod(method))
+ result.append(method.param_struct)
+ if method.response_param_struct is not None:
+ result.append(method.response_param_struct)
return result
- def GetStructs(self):
- return map(partial(self._AddStructComputedData, True), self.module.structs)
-
- def GetUnions(self):
- return map(self._AddUnionComputedData, self.module.unions)
-
- def GetInterfaces(self):
- return map(self._AddInterfaceComputedData, self.module.interfaces)
-
# Prepend the filename with a directory that matches the directory of the
# original .mojom file, relative to the import root.
def MatchMojomFilePath(self, filename):
@@ -100,7 +137,6 @@ class Generator(object):
struct.bytes = pack.GetByteLayout(struct.packed)
struct.versions = pack.GetVersionInfo(struct.packed)
struct.exported = exported
- return struct
def _AddUnionComputedData(self, union):
"""Adds computed data to the given union. The data is computed once and
@@ -111,7 +147,6 @@ class Generator(object):
ordinal = field.ordinal
field.ordinal = ordinal
ordinal += 1
- return union
def _AddInterfaceComputedData(self, interface):
"""Adds computed data to the given interface. The data is computed once and
@@ -132,7 +167,6 @@ class Generator(object):
method.response_param_struct.versions[-1].version)
else:
method.response_param_struct = None
- return interface
def _GetStructFromMethod(self, method):
"""Converts a method's parameters into the fields of a struct."""
@@ -141,7 +175,8 @@ class Generator(object):
for param in method.parameters:
struct.AddField(param.name, param.kind, param.ordinal,
attributes=param.attributes)
- return self._AddStructComputedData(False, struct)
+ self._AddStructComputedData(False, struct)
+ return struct
def _GetResponseStructFromMethod(self, method):
"""Converts a method's response_parameters into the fields of a struct."""
@@ -150,4 +185,5 @@ class Generator(object):
for param in method.response_parameters:
struct.AddField(param.name, param.kind, param.ordinal,
attributes=param.attributes)
- return self._AddStructComputedData(False, struct)
+ self._AddStructComputedData(False, struct)
+ return struct
« no previous file with comments | « mojo/public/tools/bindings/generators/mojom_js_generator.py ('k') | mojo/public/tools/bindings/pylib/mojom/generate/module.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698