blob: 23d6d3ec574f6b1898bd515f3e874bbe71a0c3b9 [file] [log] [blame]
Elliott Hughes08b82a92012-04-05 12:13:56 -07001#!/usr/bin/python
Elliott Hughes0e57ccb2012-04-03 16:04:52 -07002#
3# Copyright 2012 Google Inc. All Rights Reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9# * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15# * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31"""Generates default implementations of operator<< for enum types."""
32
33import codecs
34import os
35import re
36import string
37import sys
38
39
40_ENUM_START_RE = re.compile(r'\benum\b\s+(\S+)\s+\{')
41_ENUM_VALUE_RE = re.compile(r'([A-Za-z0-9_]+)(.*)')
42_ENUM_END_RE = re.compile(r'^\s*\};$')
43_ENUMS = {}
Elliott Hughes460384f2012-04-04 16:53:10 -070044_NAMESPACES = {}
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070045
46def Confused(filename, line_number, line):
47 sys.stderr.write('%s:%d: confused by:\n%s\n' % (filename, line_number, line))
Elliott Hughes48257562012-06-06 17:42:44 -070048 raise Exception("giving up!")
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070049 sys.exit(1)
50
51
52def ProcessFile(filename):
53 lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
54 in_enum = False
55 line_number = 0
Elliott Hughes460384f2012-04-04 16:53:10 -070056
57 namespaces = []
58 enclosing_classes = []
59
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070060 for raw_line in lines:
61 line_number += 1
62
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070063 if not in_enum:
Elliott Hughes460384f2012-04-04 16:53:10 -070064 # Is this the start of a new enum?
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070065 m = _ENUM_START_RE.search(raw_line)
66 if m:
67 # Yes, so add an empty entry to _ENUMS for this enum.
68 enum_name = m.group(1)
Elliott Hughes460384f2012-04-04 16:53:10 -070069 if len(enclosing_classes) > 0:
70 enum_name = '::'.join(enclosing_classes) + '::' + enum_name
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070071 _ENUMS[enum_name] = []
Elliott Hughes460384f2012-04-04 16:53:10 -070072 _NAMESPACES[enum_name] = '::'.join(namespaces)
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070073 in_enum = True
Elliott Hughes460384f2012-04-04 16:53:10 -070074 continue
75
76 # Is this the start or end of a namespace?
77 m = re.compile(r'^namespace (\S+) \{').search(raw_line)
78 if m:
79 namespaces.append(m.group(1))
80 continue
81 m = re.compile(r'^\}\s+// namespace').search(raw_line)
82 if m:
83 namespaces = namespaces[0:len(namespaces) - 1]
84 continue
85
86 # Is this the start or end of an enclosing class or struct?
Elliott Hughes48257562012-06-06 17:42:44 -070087 m = re.compile(r'^(?:class|struct)(?: MANAGED)? (\S+).* \{').search(raw_line)
Elliott Hughes460384f2012-04-04 16:53:10 -070088 if m:
89 enclosing_classes.append(m.group(1))
90 continue
91 m = re.compile(r'^\};').search(raw_line)
92 if m:
93 enclosing_classes = enclosing_classes[0:len(enclosing_classes) - 1]
94 continue
95
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070096 continue
97
98 # Is this the end of the current enum?
99 m = _ENUM_END_RE.search(raw_line)
100 if m:
101 if not in_enum:
102 Confused(filename, line_number, raw_line)
103 in_enum = False
104 continue
105
Elliott Hughes48257562012-06-06 17:42:44 -0700106 # Strip // comments.
107 line = re.sub(r'//.*', '', raw_line)
108
109 # Strip whitespace.
110 line = line.strip()
111
112 # Skip blank lines.
Elliott Hughes460384f2012-04-04 16:53:10 -0700113 if len(line) == 0:
114 continue
115
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700116 # Is this another enum value?
Elliott Hughes460384f2012-04-04 16:53:10 -0700117 m = _ENUM_VALUE_RE.search(line)
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700118 if not m:
119 Confused(filename, line_number, raw_line)
120
121 enum_value = m.group(1)
122
123 # By default, we turn "kSomeValue" into "SomeValue".
124 enum_text = enum_value
125 if enum_text.startswith('k'):
126 enum_text = enum_text[1:]
127
128 # Lose literal values because we don't care; turn "= 123, // blah" into ", // blah".
129 rest = m.group(2).strip()
Elliott Hughes460384f2012-04-04 16:53:10 -0700130 m_literal = re.compile(r'= (0x[0-9a-f]+|-?[0-9]+|\'.\')').search(rest)
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700131 if m_literal:
132 rest = rest[(len(m_literal.group(0))):]
133
134 # With "kSomeValue = kOtherValue," we take the original and skip later synonyms.
135 # TODO: check that the rhs is actually an existing value.
136 if rest.startswith('= k'):
137 continue
138
139 # Remove any trailing comma and whitespace
140 if rest.startswith(','):
141 rest = rest[1:]
142 rest = rest.strip()
143
144 # Anything left should be a comment.
145 if len(rest) and not rest.startswith('// '):
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700146 Confused(filename, line_number, raw_line)
147
148 m_comment = re.compile(r'<<(.*?)>>').search(rest)
149 if m_comment:
150 enum_text = m_comment.group(1)
151
Elliott Hughes460384f2012-04-04 16:53:10 -0700152 if len(enclosing_classes) > 0:
153 enum_value = '::'.join(enclosing_classes) + '::' + enum_value
154
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700155 _ENUMS[enum_name].append((enum_value, enum_text))
156
157def main():
158 header_files = []
159 for header_file in sys.argv[1:]:
160 header_files.append(header_file)
161 ProcessFile(header_file)
162
163 print '#include <iostream>'
164 print
165
166 for header_file in header_files:
Elliott Hughesef67aec2012-04-04 12:01:27 -0700167 # Make gives us paths relative to the top of the tree, but our -I is art/.
168 # We also have -I art/src/, but icu4c is higher on the include path and has a "mutex.h" too.
169 header_file = header_file.replace('art/', '')
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700170 print '#include "%s"' % header_file
171
172 print
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700173
174 for enum_name in _ENUMS:
175 print '// This was automatically generated by %s --- do not edit!' % sys.argv[0]
Elliott Hughes460384f2012-04-04 16:53:10 -0700176
177 namespaces = _NAMESPACES[enum_name].split('::')
178 for namespace in namespaces:
179 print 'namespace %s {' % namespace
180
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700181 print 'std::ostream& operator<<(std::ostream& os, const %s& rhs) {' % enum_name
182 print ' switch (rhs) {'
183 for (enum_value, enum_text) in _ENUMS[enum_name]:
184 print ' case %s: os << "%s"; break;' % (enum_value, enum_text)
185 print ' default: os << "%s[" << static_cast<int>(rhs) << "]"; break;' % enum_name
186 print ' }'
187 print ' return os;'
188 print '}'
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700189
Elliott Hughes460384f2012-04-04 16:53:10 -0700190 for namespace in reversed(namespaces):
191 print '} // namespace %s' % namespace
192 print
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700193
194 sys.exit(0)
195
196
197if __name__ == '__main__':
198 main()