Elliott Hughes | 08b82a9 | 2012-04-05 12:13:56 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 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 | |
| 33 | import codecs |
| 34 | import os |
| 35 | import re |
| 36 | import string |
| 37 | import 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 Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 44 | _NAMESPACES = {} |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 45 | |
| 46 | def 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 | |
| 51 | def ProcessFile(filename): |
| 52 | lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n') |
| 53 | in_enum = False |
| 54 | line_number = 0 |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 55 | |
| 56 | namespaces = [] |
| 57 | enclosing_classes = [] |
| 58 | |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 59 | for raw_line in lines: |
| 60 | line_number += 1 |
| 61 | |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 62 | if not in_enum: |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 63 | # Is this the start of a new enum? |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 64 | 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 Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 68 | if len(enclosing_classes) > 0: |
| 69 | enum_name = '::'.join(enclosing_classes) + '::' + enum_name |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 70 | _ENUMS[enum_name] = [] |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 71 | _NAMESPACES[enum_name] = '::'.join(namespaces) |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 72 | in_enum = True |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 73 | 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 Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 95 | 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 Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 105 | # Whitespace? |
| 106 | line = raw_line.strip() |
| 107 | if len(line) == 0: |
| 108 | continue |
| 109 | |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 110 | # Is this another enum value? |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 111 | m = _ENUM_VALUE_RE.search(line) |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 112 | 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 Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 124 | m_literal = re.compile(r'= (0x[0-9a-f]+|-?[0-9]+|\'.\')').search(rest) |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 125 | 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 Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 140 | 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 Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 146 | if len(enclosing_classes) > 0: |
| 147 | enum_value = '::'.join(enclosing_classes) + '::' + enum_value |
| 148 | |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 149 | _ENUMS[enum_name].append((enum_value, enum_text)) |
| 150 | |
| 151 | def 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 Hughes | ef67aec | 2012-04-04 12:01:27 -0700 | [diff] [blame] | 161 | # 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 Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 164 | print '#include "%s"' % header_file |
| 165 | |
| 166 | print |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 167 | |
| 168 | for enum_name in _ENUMS: |
| 169 | print '// This was automatically generated by %s --- do not edit!' % sys.argv[0] |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 170 | |
| 171 | namespaces = _NAMESPACES[enum_name].split('::') |
| 172 | for namespace in namespaces: |
| 173 | print 'namespace %s {' % namespace |
| 174 | |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 175 | 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 Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 183 | |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 184 | for namespace in reversed(namespaces): |
| 185 | print '} // namespace %s' % namespace |
| 186 | print |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 187 | |
| 188 | sys.exit(0) |
| 189 | |
| 190 | |
| 191 | if __name__ == '__main__': |
| 192 | main() |