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)) |
Elliott Hughes | 4825756 | 2012-06-06 17:42:44 -0700 | [diff] [blame] | 48 | raise Exception("giving up!") |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 49 | sys.exit(1) |
| 50 | |
| 51 | |
| 52 | def ProcessFile(filename): |
| 53 | lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n') |
| 54 | in_enum = False |
| 55 | line_number = 0 |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 56 | |
| 57 | namespaces = [] |
| 58 | enclosing_classes = [] |
| 59 | |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 60 | for raw_line in lines: |
| 61 | line_number += 1 |
| 62 | |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 63 | if not in_enum: |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 64 | # Is this the start of a new enum? |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 65 | 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 Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 69 | if len(enclosing_classes) > 0: |
| 70 | enum_name = '::'.join(enclosing_classes) + '::' + enum_name |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 71 | _ENUMS[enum_name] = [] |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 72 | _NAMESPACES[enum_name] = '::'.join(namespaces) |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 73 | in_enum = True |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 74 | 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 Hughes | 4825756 | 2012-06-06 17:42:44 -0700 | [diff] [blame] | 87 | m = re.compile(r'^(?:class|struct)(?: MANAGED)? (\S+).* \{').search(raw_line) |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 88 | 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 Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 96 | 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 Hughes | a9719eb | 2012-06-07 11:09:48 -0700 | [diff] [blame] | 106 | # 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 Hughes | 4825756 | 2012-06-06 17:42:44 -0700 | [diff] [blame] | 114 | |
| 115 | # Strip whitespace. |
| 116 | line = line.strip() |
| 117 | |
| 118 | # Skip blank lines. |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 119 | if len(line) == 0: |
| 120 | continue |
| 121 | |
Elliott Hughes | a9719eb | 2012-06-07 11:09:48 -0700 | [diff] [blame] | 122 | # 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 Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 124 | m = _ENUM_VALUE_RE.search(line) |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 125 | if not m: |
| 126 | Confused(filename, line_number, raw_line) |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 127 | enum_value = m.group(1) |
| 128 | |
| 129 | # By default, we turn "kSomeValue" into "SomeValue". |
Elliott Hughes | a9719eb | 2012-06-07 11:09:48 -0700 | [diff] [blame] | 130 | if enum_text == None: |
| 131 | enum_text = enum_value |
| 132 | if enum_text.startswith('k'): |
| 133 | enum_text = enum_text[1:] |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 134 | |
| 135 | # Lose literal values because we don't care; turn "= 123, // blah" into ", // blah". |
| 136 | rest = m.group(2).strip() |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 137 | 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] | 138 | 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 Hughes | a9719eb | 2012-06-07 11:09:48 -0700 | [diff] [blame] | 151 | # There shouldn't be anything left. |
| 152 | if len(rest): |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 153 | Confused(filename, line_number, raw_line) |
| 154 | |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 155 | if len(enclosing_classes) > 0: |
| 156 | enum_value = '::'.join(enclosing_classes) + '::' + enum_value |
| 157 | |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 158 | _ENUMS[enum_name].append((enum_value, enum_text)) |
| 159 | |
| 160 | def 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 Hughes | ef67aec | 2012-04-04 12:01:27 -0700 | [diff] [blame] | 170 | # 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 Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 173 | print '#include "%s"' % header_file |
| 174 | |
| 175 | print |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 176 | |
| 177 | for enum_name in _ENUMS: |
| 178 | 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] | 179 | |
| 180 | namespaces = _NAMESPACES[enum_name].split('::') |
| 181 | for namespace in namespaces: |
| 182 | print 'namespace %s {' % namespace |
| 183 | |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 184 | 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 Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 192 | |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 193 | for namespace in reversed(namespaces): |
| 194 | print '} // namespace %s' % namespace |
| 195 | print |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 196 | |
| 197 | sys.exit(0) |
| 198 | |
| 199 | |
| 200 | if __name__ == '__main__': |
| 201 | main() |