blob: 91d4669cc7b15d7abfa76af13966130bec9c2640 [file] [log] [blame]
Elliott Hughes0e57ccb2012-04-03 16:04:52 -07001#!/usr/bin/python2.4
2#
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))
48 sys.exit(1)
49
50
51def ProcessFile(filename):
52 lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
53 in_enum = False
54 line_number = 0
Elliott Hughes460384f2012-04-04 16:53:10 -070055
56 namespaces = []
57 enclosing_classes = []
58
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070059 for raw_line in lines:
60 line_number += 1
61
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070062 if not in_enum:
Elliott Hughes460384f2012-04-04 16:53:10 -070063 # Is this the start of a new enum?
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070064 m = _ENUM_START_RE.search(raw_line)
65 if m:
66 # Yes, so add an empty entry to _ENUMS for this enum.
67 enum_name = m.group(1)
Elliott Hughes460384f2012-04-04 16:53:10 -070068 if len(enclosing_classes) > 0:
69 enum_name = '::'.join(enclosing_classes) + '::' + enum_name
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070070 _ENUMS[enum_name] = []
Elliott Hughes460384f2012-04-04 16:53:10 -070071 _NAMESPACES[enum_name] = '::'.join(namespaces)
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070072 in_enum = True
Elliott Hughes460384f2012-04-04 16:53:10 -070073 continue
74
75 # Is this the start or end of a namespace?
76 m = re.compile(r'^namespace (\S+) \{').search(raw_line)
77 if m:
78 namespaces.append(m.group(1))
79 continue
80 m = re.compile(r'^\}\s+// namespace').search(raw_line)
81 if m:
82 namespaces = namespaces[0:len(namespaces) - 1]
83 continue
84
85 # Is this the start or end of an enclosing class or struct?
86 m = re.compile(r'^(?:class|struct) (\S+) \{').search(raw_line)
87 if m:
88 enclosing_classes.append(m.group(1))
89 continue
90 m = re.compile(r'^\};').search(raw_line)
91 if m:
92 enclosing_classes = enclosing_classes[0:len(enclosing_classes) - 1]
93 continue
94
Elliott Hughes0e57ccb2012-04-03 16:04:52 -070095 continue
96
97 # Is this the end of the current enum?
98 m = _ENUM_END_RE.search(raw_line)
99 if m:
100 if not in_enum:
101 Confused(filename, line_number, raw_line)
102 in_enum = False
103 continue
104
Elliott Hughes460384f2012-04-04 16:53:10 -0700105 # Whitespace?
106 line = raw_line.strip()
107 if len(line) == 0:
108 continue
109
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700110 # Is this another enum value?
Elliott Hughes460384f2012-04-04 16:53:10 -0700111 m = _ENUM_VALUE_RE.search(line)
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700112 if not m:
113 Confused(filename, line_number, raw_line)
114
115 enum_value = m.group(1)
116
117 # By default, we turn "kSomeValue" into "SomeValue".
118 enum_text = enum_value
119 if enum_text.startswith('k'):
120 enum_text = enum_text[1:]
121
122 # Lose literal values because we don't care; turn "= 123, // blah" into ", // blah".
123 rest = m.group(2).strip()
Elliott Hughes460384f2012-04-04 16:53:10 -0700124 m_literal = re.compile(r'= (0x[0-9a-f]+|-?[0-9]+|\'.\')').search(rest)
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700125 if m_literal:
126 rest = rest[(len(m_literal.group(0))):]
127
128 # With "kSomeValue = kOtherValue," we take the original and skip later synonyms.
129 # TODO: check that the rhs is actually an existing value.
130 if rest.startswith('= k'):
131 continue
132
133 # Remove any trailing comma and whitespace
134 if rest.startswith(','):
135 rest = rest[1:]
136 rest = rest.strip()
137
138 # Anything left should be a comment.
139 if len(rest) and not rest.startswith('// '):
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700140 Confused(filename, line_number, raw_line)
141
142 m_comment = re.compile(r'<<(.*?)>>').search(rest)
143 if m_comment:
144 enum_text = m_comment.group(1)
145
Elliott Hughes460384f2012-04-04 16:53:10 -0700146 if len(enclosing_classes) > 0:
147 enum_value = '::'.join(enclosing_classes) + '::' + enum_value
148
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700149 _ENUMS[enum_name].append((enum_value, enum_text))
150
151def main():
152 header_files = []
153 for header_file in sys.argv[1:]:
154 header_files.append(header_file)
155 ProcessFile(header_file)
156
157 print '#include <iostream>'
158 print
159
160 for header_file in header_files:
Elliott Hughesef67aec2012-04-04 12:01:27 -0700161 # Make gives us paths relative to the top of the tree, but our -I is art/.
162 # We also have -I art/src/, but icu4c is higher on the include path and has a "mutex.h" too.
163 header_file = header_file.replace('art/', '')
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700164 print '#include "%s"' % header_file
165
166 print
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700167
168 for enum_name in _ENUMS:
169 print '// This was automatically generated by %s --- do not edit!' % sys.argv[0]
Elliott Hughes460384f2012-04-04 16:53:10 -0700170
171 namespaces = _NAMESPACES[enum_name].split('::')
172 for namespace in namespaces:
173 print 'namespace %s {' % namespace
174
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700175 print 'std::ostream& operator<<(std::ostream& os, const %s& rhs) {' % enum_name
176 print ' switch (rhs) {'
177 for (enum_value, enum_text) in _ENUMS[enum_name]:
178 print ' case %s: os << "%s"; break;' % (enum_value, enum_text)
179 print ' default: os << "%s[" << static_cast<int>(rhs) << "]"; break;' % enum_name
180 print ' }'
181 print ' return os;'
182 print '}'
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700183
Elliott Hughes460384f2012-04-04 16:53:10 -0700184 for namespace in reversed(namespaces):
185 print '} // namespace %s' % namespace
186 print
Elliott Hughes0e57ccb2012-04-03 16:04:52 -0700187
188 sys.exit(0)
189
190
191if __name__ == '__main__':
192 main()