Chirayu Desai | 2248c17 | 2013-11-06 20:43:21 +0530 | [diff] [blame] | 1 | #!/usr/bin/env python |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 2 | # |
Elliott Hughes | d320a9a | 2013-10-06 21:48:46 -0700 | [diff] [blame] | 3 | # Copyright (C) 2012 The Android Open Source Project |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 4 | # |
Elliott Hughes | d320a9a | 2013-10-06 21:48:46 -0700 | [diff] [blame] | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 8 | # |
Elliott Hughes | d320a9a | 2013-10-06 21:48:46 -0700 | [diff] [blame] | 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 10 | # |
Elliott Hughes | d320a9a | 2013-10-06 21:48:46 -0700 | [diff] [blame] | 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 16 | |
| 17 | """Generates default implementations of operator<< for enum types.""" |
| 18 | |
| 19 | import codecs |
| 20 | import os |
| 21 | import re |
| 22 | import string |
| 23 | import sys |
| 24 | |
| 25 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 26 | _ENUM_START_RE = re.compile(r'\benum\b\s+(class\s+)?(\S+)\s+:?.*\{(\s+// private)?') |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 27 | _ENUM_VALUE_RE = re.compile(r'([A-Za-z0-9_]+)(.*)') |
| 28 | _ENUM_END_RE = re.compile(r'^\s*\};$') |
| 29 | _ENUMS = {} |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 30 | _NAMESPACES = {} |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 31 | _ENUM_CLASSES = {} |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 32 | |
| 33 | def Confused(filename, line_number, line): |
| 34 | 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] | 35 | raise Exception("giving up!") |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 36 | sys.exit(1) |
| 37 | |
| 38 | |
| 39 | def ProcessFile(filename): |
| 40 | lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n') |
| 41 | in_enum = False |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 42 | is_enum_class = False |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 43 | line_number = 0 |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 44 | |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 45 | |
| 46 | namespaces = [] |
| 47 | enclosing_classes = [] |
| 48 | |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 49 | for raw_line in lines: |
| 50 | line_number += 1 |
| 51 | |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 52 | if not in_enum: |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 53 | # Is this the start of a new enum? |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 54 | m = _ENUM_START_RE.search(raw_line) |
| 55 | if m: |
| 56 | # Yes, so add an empty entry to _ENUMS for this enum. |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 57 | |
| 58 | # Except when it's private |
| 59 | if m.group(3) is not None: |
| 60 | continue |
| 61 | |
| 62 | is_enum_class = m.group(1) is not None |
| 63 | enum_name = m.group(2) |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 64 | if len(enclosing_classes) > 0: |
| 65 | enum_name = '::'.join(enclosing_classes) + '::' + enum_name |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 66 | _ENUMS[enum_name] = [] |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 67 | _NAMESPACES[enum_name] = '::'.join(namespaces) |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 68 | _ENUM_CLASSES[enum_name] = is_enum_class |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 69 | in_enum = True |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 70 | continue |
| 71 | |
| 72 | # Is this the start or end of a namespace? |
| 73 | m = re.compile(r'^namespace (\S+) \{').search(raw_line) |
| 74 | if m: |
| 75 | namespaces.append(m.group(1)) |
| 76 | continue |
| 77 | m = re.compile(r'^\}\s+// namespace').search(raw_line) |
| 78 | if m: |
| 79 | namespaces = namespaces[0:len(namespaces) - 1] |
| 80 | continue |
| 81 | |
| 82 | # Is this the start or end of an enclosing class or struct? |
Elliott Hughes | 4825756 | 2012-06-06 17:42:44 -0700 | [diff] [blame] | 83 | m = re.compile(r'^(?:class|struct)(?: MANAGED)? (\S+).* \{').search(raw_line) |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 84 | if m: |
| 85 | enclosing_classes.append(m.group(1)) |
| 86 | continue |
| 87 | m = re.compile(r'^\};').search(raw_line) |
| 88 | if m: |
| 89 | enclosing_classes = enclosing_classes[0:len(enclosing_classes) - 1] |
| 90 | continue |
| 91 | |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 92 | continue |
| 93 | |
| 94 | # Is this the end of the current enum? |
| 95 | m = _ENUM_END_RE.search(raw_line) |
| 96 | if m: |
| 97 | if not in_enum: |
| 98 | Confused(filename, line_number, raw_line) |
| 99 | in_enum = False |
| 100 | continue |
| 101 | |
Elliott Hughes | a9719eb | 2012-06-07 11:09:48 -0700 | [diff] [blame] | 102 | # The only useful thing in comments is the <<alternate text>> syntax for |
| 103 | # overriding the default enum value names. Pull that out... |
| 104 | enum_text = None |
| 105 | m_comment = re.compile(r'// <<(.*?)>>').search(raw_line) |
| 106 | if m_comment: |
| 107 | enum_text = m_comment.group(1) |
| 108 | # ...and then strip // comments. |
| 109 | line = re.sub(r'//.*', '', raw_line) |
Elliott Hughes | 4825756 | 2012-06-06 17:42:44 -0700 | [diff] [blame] | 110 | |
| 111 | # Strip whitespace. |
| 112 | line = line.strip() |
| 113 | |
| 114 | # Skip blank lines. |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 115 | if len(line) == 0: |
| 116 | continue |
| 117 | |
Elliott Hughes | a9719eb | 2012-06-07 11:09:48 -0700 | [diff] [blame] | 118 | # Since we know we're in an enum type, and we're not looking at a comment |
| 119 | # or a blank line, this line should be the next enum value... |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 120 | m = _ENUM_VALUE_RE.search(line) |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 121 | if not m: |
| 122 | Confused(filename, line_number, raw_line) |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 123 | enum_value = m.group(1) |
| 124 | |
| 125 | # By default, we turn "kSomeValue" into "SomeValue". |
Elliott Hughes | a9719eb | 2012-06-07 11:09:48 -0700 | [diff] [blame] | 126 | if enum_text == None: |
| 127 | enum_text = enum_value |
| 128 | if enum_text.startswith('k'): |
| 129 | enum_text = enum_text[1:] |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 130 | |
| 131 | # Lose literal values because we don't care; turn "= 123, // blah" into ", // blah". |
| 132 | rest = m.group(2).strip() |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 133 | 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] | 134 | if m_literal: |
| 135 | rest = rest[(len(m_literal.group(0))):] |
| 136 | |
| 137 | # With "kSomeValue = kOtherValue," we take the original and skip later synonyms. |
| 138 | # TODO: check that the rhs is actually an existing value. |
| 139 | if rest.startswith('= k'): |
| 140 | continue |
| 141 | |
| 142 | # Remove any trailing comma and whitespace |
| 143 | if rest.startswith(','): |
| 144 | rest = rest[1:] |
| 145 | rest = rest.strip() |
| 146 | |
Elliott Hughes | a9719eb | 2012-06-07 11:09:48 -0700 | [diff] [blame] | 147 | # There shouldn't be anything left. |
| 148 | if len(rest): |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 149 | Confused(filename, line_number, raw_line) |
| 150 | |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 151 | if len(enclosing_classes) > 0: |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 152 | if is_enum_class: |
| 153 | enum_value = enum_name + '::' + enum_value |
| 154 | else: |
| 155 | enum_value = '::'.join(enclosing_classes) + '::' + enum_value |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 156 | |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 157 | _ENUMS[enum_name].append((enum_value, enum_text)) |
| 158 | |
| 159 | def main(): |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 160 | local_path = sys.argv[1] |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 161 | header_files = [] |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 162 | for header_file in sys.argv[2:]: |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 163 | header_files.append(header_file) |
| 164 | ProcessFile(header_file) |
| 165 | |
Bernhard Rosenkränzer | c2e0260 | 2014-07-14 13:30:58 +0200 | [diff] [blame^] | 166 | print('#include <iostream>') |
| 167 | print('') |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 168 | |
| 169 | for header_file in header_files: |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 170 | header_file = header_file.replace(local_path + '/', '') |
Bernhard Rosenkränzer | c2e0260 | 2014-07-14 13:30:58 +0200 | [diff] [blame^] | 171 | print('#include "%s"' % header_file) |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 172 | |
Bernhard Rosenkränzer | c2e0260 | 2014-07-14 13:30:58 +0200 | [diff] [blame^] | 173 | print('') |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 174 | |
| 175 | for enum_name in _ENUMS: |
Bernhard Rosenkränzer | c2e0260 | 2014-07-14 13:30:58 +0200 | [diff] [blame^] | 176 | 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] | 177 | |
| 178 | namespaces = _NAMESPACES[enum_name].split('::') |
| 179 | for namespace in namespaces: |
Bernhard Rosenkränzer | c2e0260 | 2014-07-14 13:30:58 +0200 | [diff] [blame^] | 180 | print('namespace %s {' % namespace) |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 181 | |
Bernhard Rosenkränzer | c2e0260 | 2014-07-14 13:30:58 +0200 | [diff] [blame^] | 182 | print('std::ostream& operator<<(std::ostream& os, const %s& rhs) {' % enum_name) |
| 183 | print(' switch (rhs) {') |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 184 | for (enum_value, enum_text) in _ENUMS[enum_name]: |
Bernhard Rosenkränzer | c2e0260 | 2014-07-14 13:30:58 +0200 | [diff] [blame^] | 185 | print(' case %s: os << "%s"; break;' % (enum_value, enum_text)) |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 186 | if not _ENUM_CLASSES[enum_name]: |
Bernhard Rosenkränzer | c2e0260 | 2014-07-14 13:30:58 +0200 | [diff] [blame^] | 187 | print(' default: os << "%s[" << static_cast<int>(rhs) << "]"; break;' % enum_name) |
| 188 | print(' }') |
| 189 | print(' return os;') |
| 190 | print('}') |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 191 | |
Elliott Hughes | 460384f | 2012-04-04 16:53:10 -0700 | [diff] [blame] | 192 | for namespace in reversed(namespaces): |
Bernhard Rosenkränzer | c2e0260 | 2014-07-14 13:30:58 +0200 | [diff] [blame^] | 193 | print('} // namespace %s' % namespace) |
| 194 | print('') |
Elliott Hughes | 0e57ccb | 2012-04-03 16:04:52 -0700 | [diff] [blame] | 195 | |
| 196 | sys.exit(0) |
| 197 | |
| 198 | |
| 199 | if __name__ == '__main__': |
| 200 | main() |