blob: aa0c00e743948bbc20b11494061e71d9dca9cce0 [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 Hughesa9719eb2012-06-07 11:09:48 -0700106 # The only useful thing in comments is the <<alternate text>> syntax for
107 # overriding the default enum value names. Pull that out...
108 enum_text = None
109 m_comment = re.compile(r'// <<(.*?)>>').search(raw_line)
110 if m_comment:
111 enum_text = m_comment.group(1)
112 # ...and then strip // comments.
113 line = re.sub(r'//.*', '', raw_line)
Elliott Hughes48257562012-06-06 17:42:44 -0700114
115 # Strip whitespace.
116 line = line.strip()
117
118 # Skip blank lines.
Elliott Hughes460384f2012-04-04 16:53:10 -0700119 if len(line) == 0:
120 continue
121
Elliott Hughesa9719eb2012-06-07 11:09:48 -0700122 # Since we know we're in an enum type, and we're not looking at a comment
123 # or a blank line, this line should be the next enum value...
Elliott Hughes460384f2012-04-04 16:53:10 -0700124 m = _ENUM_VALUE_RE.search(line)
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700125 if not m:
126 Confused(filename, line_number, raw_line)
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700127 enum_value = m.group(1)
128
129 # By default, we turn "kSomeValue" into "SomeValue".
Elliott Hughesa9719eb2012-06-07 11:09:48 -0700130 if enum_text == None:
131 enum_text = enum_value
132 if enum_text.startswith('k'):
133 enum_text = enum_text[1:]
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700134
135 # Lose literal values because we don't care; turn "= 123, // blah" into ", // blah".
136 rest = m.group(2).strip()
Elliott Hughes460384f2012-04-04 16:53:10 -0700137 m_literal = re.compile(r'= (0x[0-9a-f]+|-?[0-9]+|\'.\')').search(rest)
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700138 if m_literal:
139 rest = rest[(len(m_literal.group(0))):]
140
141 # With "kSomeValue = kOtherValue," we take the original and skip later synonyms.
142 # TODO: check that the rhs is actually an existing value.
143 if rest.startswith('= k'):
144 continue
145
146 # Remove any trailing comma and whitespace
147 if rest.startswith(','):
148 rest = rest[1:]
149 rest = rest.strip()
150
Elliott Hughesa9719eb2012-06-07 11:09:48 -0700151 # There shouldn't be anything left.
152 if len(rest):
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700153 Confused(filename, line_number, raw_line)
154
Elliott Hughes460384f2012-04-04 16:53:10 -0700155 if len(enclosing_classes) > 0:
156 enum_value = '::'.join(enclosing_classes) + '::' + enum_value
157
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700158 _ENUMS[enum_name].append((enum_value, enum_text))
159
160def main():
161 header_files = []
162 for header_file in sys.argv[1:]:
163 header_files.append(header_file)
164 ProcessFile(header_file)
165
166 print '#include <iostream>'
167 print
168
169 for header_file in header_files:
Elliott Hughesef67aec2012-04-04 12:01:27 -0700170 # Make gives us paths relative to the top of the tree, but our -I is art/.
171 # We also have -I art/src/, but icu4c is higher on the include path and has a "mutex.h" too.
172 header_file = header_file.replace('art/', '')
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700173 print '#include "%s"' % header_file
174
175 print
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700176
177 for enum_name in _ENUMS:
178 print '// This was automatically generated by %s --- do not edit!' % sys.argv[0]
Elliott Hughes460384f2012-04-04 16:53:10 -0700179
180 namespaces = _NAMESPACES[enum_name].split('::')
181 for namespace in namespaces:
182 print 'namespace %s {' % namespace
183
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700184 print 'std::ostream& operator<<(std::ostream& os, const %s& rhs) {' % enum_name
185 print ' switch (rhs) {'
186 for (enum_value, enum_text) in _ENUMS[enum_name]:
187 print ' case %s: os << "%s"; break;' % (enum_value, enum_text)
188 print ' default: os << "%s[" << static_cast<int>(rhs) << "]"; break;' % enum_name
189 print ' }'
190 print ' return os;'
191 print '}'
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700192
Elliott Hughes460384f2012-04-04 16:53:10 -0700193 for namespace in reversed(namespaces):
194 print '} // namespace %s' % namespace
195 print
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700196
197 sys.exit(0)
198
199
200if __name__ == '__main__':
201 main()