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

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

Issue 39113003: Docserver: Display enum value descriptions in API docs. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 7 years, 1 month 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 from code import Code 5 from code import Code
6 from model import PropertyType 6 from model import PropertyType
7 import cpp_util 7 import cpp_util
8 import schema_util 8 import schema_util
9 import util_cc_helper 9 import util_cc_helper
10 10
(...skipping 766 matching lines...) Expand 10 before | Expand all | Expand 10 after
777 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))) 777 self._util_cc_helper.GetValueTypeString('%%(src_var)s', True)))
778 .Append('return %s;' % failure_value) 778 .Append('return %s;' % failure_value)
779 .Eblock('}') 779 .Eblock('}')
780 .Append('%s = Parse%s(%s);' % (dst_var, 780 .Append('%s = Parse%s(%s);' % (dst_var,
781 self._type_helper.GetCppType(type_), 781 self._type_helper.GetCppType(type_),
782 enum_as_string)) 782 enum_as_string))
783 .Sblock('if (%s == %s) {' % (dst_var, 783 .Sblock('if (%s == %s) {' % (dst_var,
784 self._type_helper.GetEnumNoneValue(type_))) 784 self._type_helper.GetEnumNoneValue(type_)))
785 .Concat(self._GenerateError( 785 .Concat(self._GenerateError(
786 '\"\'%%(key)s\': expected \\"' + 786 '\"\'%%(key)s\': expected \\"' +
787 '\\" or \\"'.join(self._type_helper.FollowRef(type_).enum_values) + 787 '\\" or \\"'.join(
788 enum_value['name']
789 for enum_value in self._type_helper.FollowRef(type_).enum_values) +
788 '\\", got \\"" + %s + "\\""' % enum_as_string)) 790 '\\", got \\"" + %s + "\\""' % enum_as_string))
789 .Append('return %s;' % failure_value) 791 .Append('return %s;' % failure_value)
790 .Eblock('}') 792 .Eblock('}')
791 .Substitute({'src_var': src_var, 'key': type_.name}) 793 .Substitute({'src_var': src_var, 'key': type_.name})
792 ) 794 )
793 return c 795 return c
794 796
795 def _GeneratePropertyFunctions(self, namespace, params): 797 def _GeneratePropertyFunctions(self, namespace, params):
796 """Generates the member functions for a list of parameters. 798 """Generates the member functions for a list of parameters.
797 """ 799 """
(...skipping 14 matching lines...) Expand all
812 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) 814 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name))
813 815
814 if cpp_namespace is not None: 816 if cpp_namespace is not None:
815 c.Append('// static') 817 c.Append('// static')
816 maybe_namespace = '' if cpp_namespace is None else '%s::' % cpp_namespace 818 maybe_namespace = '' if cpp_namespace is None else '%s::' % cpp_namespace
817 819
818 c.Sblock('std::string %sToString(%s enum_param) {' % 820 c.Sblock('std::string %sToString(%s enum_param) {' %
819 (maybe_namespace, classname)) 821 (maybe_namespace, classname))
820 c.Sblock('switch (enum_param) {') 822 c.Sblock('switch (enum_param) {')
821 for enum_value in self._type_helper.FollowRef(type_).enum_values: 823 for enum_value in self._type_helper.FollowRef(type_).enum_values:
822 (c.Append('case %s: ' % self._type_helper.GetEnumValue(type_, enum_value)) 824 (c.Append('case %s: ' % self._type_helper.GetEnumValue(
823 .Append(' return "%s";' % enum_value)) 825 type_, enum_value['name']))
826 .Append(' return "%s";' % enum_value['name']))
824 (c.Append('case %s:' % self._type_helper.GetEnumNoneValue(type_)) 827 (c.Append('case %s:' % self._type_helper.GetEnumNoneValue(type_))
825 .Append(' return "";') 828 .Append(' return "";')
826 .Eblock('}') 829 .Eblock('}')
827 .Append('NOTREACHED();') 830 .Append('NOTREACHED();')
828 .Append('return "";') 831 .Append('return "";')
829 .Eblock('}') 832 .Eblock('}')
830 ) 833 )
831 return c 834 return c
832 835
833 def _GenerateEnumFromString(self, cpp_namespace, type_): 836 def _GenerateEnumFromString(self, cpp_namespace, type_):
834 """Generates FromClassNameString() which gets an enum from its string 837 """Generates FromClassNameString() which gets an enum from its string
835 representation. 838 representation.
836 """ 839 """
837 c = Code() 840 c = Code()
838 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) 841 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name))
839 842
840 if cpp_namespace is not None: 843 if cpp_namespace is not None:
841 c.Append('// static') 844 c.Append('// static')
842 maybe_namespace = '' if cpp_namespace is None else '%s::' % cpp_namespace 845 maybe_namespace = '' if cpp_namespace is None else '%s::' % cpp_namespace
843 846
844 c.Sblock('%s%s %sParse%s(const std::string& enum_string) {' % 847 c.Sblock('%s%s %sParse%s(const std::string& enum_string) {' %
845 (maybe_namespace, classname, maybe_namespace, classname)) 848 (maybe_namespace, classname, maybe_namespace, classname))
846 for i, enum_value in enumerate( 849 for i, enum_value in enumerate(
847 self._type_helper.FollowRef(type_).enum_values): 850 self._type_helper.FollowRef(type_).enum_values):
848 # This is broken up into all ifs with no else ifs because we get 851 # This is broken up into all ifs with no else ifs because we get
849 # "fatal error C1061: compiler limit : blocks nested too deeply" 852 # "fatal error C1061: compiler limit : blocks nested too deeply"
850 # on Windows. 853 # on Windows.
851 (c.Append('if (enum_string == "%s")' % enum_value) 854 (c.Append('if (enum_string == "%s")' % enum_value['name'])
852 .Append(' return %s;' % 855 .Append(' return %s;' %
853 self._type_helper.GetEnumValue(type_, enum_value))) 856 self._type_helper.GetEnumValue(type_, enum_value['name'])))
854 (c.Append('return %s;' % self._type_helper.GetEnumNoneValue(type_)) 857 (c.Append('return %s;' % self._type_helper.GetEnumNoneValue(type_))
855 .Eblock('}') 858 .Eblock('}')
856 ) 859 )
857 return c 860 return c
858 861
859 def _GenerateCreateCallbackArguments(self, function_scope, callback): 862 def _GenerateCreateCallbackArguments(self, function_scope, callback):
860 """Generate all functions to create Value parameters for a callback. 863 """Generate all functions to create Value parameters for a callback.
861 864
862 E.g for function "Bar", generate Bar::Results::Create 865 E.g for function "Bar", generate Bar::Results::Create
863 E.g for event "Baz", generate Baz::Create 866 E.g for event "Baz", generate Baz::Create
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
933 if self._generate_error_messages: 936 if self._generate_error_messages:
934 params = list(params) + ['base::string16* error'] 937 params = list(params) + ['base::string16* error']
935 return ', '.join(str(p) for p in params) 938 return ', '.join(str(p) for p in params)
936 939
937 def _GenerateArgs(self, args): 940 def _GenerateArgs(self, args):
938 """Builds the argument list for a function, given an array of arguments. 941 """Builds the argument list for a function, given an array of arguments.
939 """ 942 """
940 if self._generate_error_messages: 943 if self._generate_error_messages:
941 args = list(args) + ['error'] 944 args = list(args) + ['error']
942 return ', '.join(str(a) for a in args) 945 return ', '.join(str(a) for a in args)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698