blob: b1d102eab62387b9ff1ae8556c969459a3ee17be [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 = {}
44
45def Confused(filename, line_number, line):
46 sys.stderr.write('%s:%d: confused by:\n%s\n' % (filename, line_number, line))
47 sys.exit(1)
48
49
50def ProcessFile(filename):
51 lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
52 in_enum = False
53 line_number = 0
54 for raw_line in lines:
55 line_number += 1
56
57 # TODO: take namespaces and enclosing classes/structs into account.
58
59 # Is this the start of a new enum?
60 if not in_enum:
61 m = _ENUM_START_RE.search(raw_line)
62 if m:
63 # Yes, so add an empty entry to _ENUMS for this enum.
64 enum_name = m.group(1)
65 _ENUMS[enum_name] = []
66 in_enum = True
67 continue
68
69 # Is this the end of the current enum?
70 m = _ENUM_END_RE.search(raw_line)
71 if m:
72 if not in_enum:
73 Confused(filename, line_number, raw_line)
74 in_enum = False
75 continue
76
77 # Is this another enum value?
78 m = _ENUM_VALUE_RE.search(raw_line.strip())
79 if not m:
80 Confused(filename, line_number, raw_line)
81
82 enum_value = m.group(1)
83
84 # By default, we turn "kSomeValue" into "SomeValue".
85 enum_text = enum_value
86 if enum_text.startswith('k'):
87 enum_text = enum_text[1:]
88
89 # Lose literal values because we don't care; turn "= 123, // blah" into ", // blah".
90 rest = m.group(2).strip()
91 m_literal = re.compile(r'= (0x[0-9a-f]+|-?[0-9]+)').search(rest)
92 if m_literal:
93 rest = rest[(len(m_literal.group(0))):]
94
95 # With "kSomeValue = kOtherValue," we take the original and skip later synonyms.
96 # TODO: check that the rhs is actually an existing value.
97 if rest.startswith('= k'):
98 continue
99
100 # Remove any trailing comma and whitespace
101 if rest.startswith(','):
102 rest = rest[1:]
103 rest = rest.strip()
104
105 # Anything left should be a comment.
106 if len(rest) and not rest.startswith('// '):
107 print rest
108 Confused(filename, line_number, raw_line)
109
110 m_comment = re.compile(r'<<(.*?)>>').search(rest)
111 if m_comment:
112 enum_text = m_comment.group(1)
113
114 _ENUMS[enum_name].append((enum_value, enum_text))
115
116def main():
117 header_files = []
118 for header_file in sys.argv[1:]:
119 header_files.append(header_file)
120 ProcessFile(header_file)
121
122 print '#include <iostream>'
123 print
124
125 for header_file in header_files:
126 print '#include "%s"' % header_file
127
128 print
129 print 'namespace art {'
130 print
131
132 for enum_name in _ENUMS:
133 print '// This was automatically generated by %s --- do not edit!' % sys.argv[0]
134 print 'std::ostream& operator<<(std::ostream& os, const %s& rhs) {' % enum_name
135 print ' switch (rhs) {'
136 for (enum_value, enum_text) in _ENUMS[enum_name]:
137 print ' case %s: os << "%s"; break;' % (enum_value, enum_text)
138 print ' default: os << "%s[" << static_cast<int>(rhs) << "]"; break;' % enum_name
139 print ' }'
140 print ' return os;'
141 print '}'
142 print
143
144 print '} // namespace art'
145
146 sys.exit(0)
147
148
149if __name__ == '__main__':
150 main()