OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2006-2008 the V8 project authors. All rights reserved. | 3 # Copyright 2006-2008 the V8 project authors. All rights reserved. |
4 # Redistribution and use in source and binary forms, with or without | 4 # Redistribution and use in source and binary forms, with or without |
5 # modification, are permitted provided that the following conditions are | 5 # modification, are permitted provided that the following conditions are |
6 # met: | 6 # met: |
7 # | 7 # |
8 # * Redistributions of source code must retain the above copyright | 8 # * Redistributions of source code must retain the above copyright |
9 # notice, this list of conditions and the following disclaimer. | 9 # notice, this list of conditions and the following disclaimer. |
10 # * Redistributions in binary form must reproduce the above | 10 # * Redistributions in binary form must reproduce the above |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 | 97 |
98 | 98 |
99 def ExpandConstants(lines, constants): | 99 def ExpandConstants(lines, constants): |
100 for key, value in constants.items(): | 100 for key, value in constants.items(): |
101 lines = lines.replace(key, str(value)) | 101 lines = lines.replace(key, str(value)) |
102 return lines | 102 return lines |
103 | 103 |
104 | 104 |
105 def ExpandMacros(lines, macros): | 105 def ExpandMacros(lines, macros): |
106 for name, macro in macros.items(): | 106 for name, macro in macros.items(): |
107 start = lines.find(name, 0) | 107 start = lines.find(name + '(', 0) |
108 while start != -1: | 108 while start != -1: |
109 # Scan over the arguments | 109 # Scan over the arguments |
110 assert lines[start + len(name)] == '(' | 110 assert lines[start + len(name)] == '(' |
111 height = 1 | 111 height = 1 |
112 end = start + len(name) + 1 | 112 end = start + len(name) + 1 |
113 last_match = end | 113 last_match = end |
114 arg_index = 0 | 114 arg_index = 0 |
115 mapping = { } | 115 mapping = { } |
116 def add_arg(str): | 116 def add_arg(str): |
117 # Remember to expand recursively in the arguments | 117 # Remember to expand recursively in the arguments |
118 replacement = ExpandMacros(str.strip(), macros) | 118 replacement = ExpandMacros(str.strip(), macros) |
119 mapping[macro.args[arg_index]] = replacement | 119 mapping[macro.args[arg_index]] = replacement |
120 while end < len(lines) and height > 0: | 120 while end < len(lines) and height > 0: |
121 # We don't count commas at higher nesting levels. | 121 # We don't count commas at higher nesting levels. |
122 if lines[end] == ',' and height == 1: | 122 if lines[end] == ',' and height == 1: |
123 add_arg(lines[last_match:end]) | 123 add_arg(lines[last_match:end]) |
124 last_match = end + 1 | 124 last_match = end + 1 |
125 elif lines[end] in ['(', '{', '[']: | 125 elif lines[end] in ['(', '{', '[']: |
126 height = height + 1 | 126 height = height + 1 |
127 elif lines[end] in [')', '}', ']']: | 127 elif lines[end] in [')', '}', ']']: |
128 height = height - 1 | 128 height = height - 1 |
129 end = end + 1 | 129 end = end + 1 |
130 # Remember to add the last match. | 130 # Remember to add the last match. |
131 add_arg(lines[last_match:end-1]) | 131 add_arg(lines[last_match:end-1]) |
132 result = macro.expand(mapping) | 132 result = macro.expand(mapping) |
133 # Replace the occurrence of the macro with the expansion | 133 # Replace the occurrence of the macro with the expansion |
134 lines = lines[:start] + result + lines[end:] | 134 lines = lines[:start] + result + lines[end:] |
135 start = lines.find(name, end) | 135 start = lines.find(name + '(', end) |
136 return lines | 136 return lines |
137 | 137 |
138 class TextMacro: | 138 class TextMacro: |
139 def __init__(self, args, body): | 139 def __init__(self, args, body): |
140 self.args = args | 140 self.args = args |
141 self.body = body | 141 self.body = body |
142 def expand(self, mapping): | 142 def expand(self, mapping): |
143 result = self.body | 143 result = self.body |
144 for key, value in mapping.items(): | 144 for key, value in mapping.items(): |
145 result = result.replace(key, value) | 145 result = result.replace(key, value) |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 | 356 |
357 def main(): | 357 def main(): |
358 natives = sys.argv[1] | 358 natives = sys.argv[1] |
359 natives_empty = sys.argv[2] | 359 natives_empty = sys.argv[2] |
360 type = sys.argv[3] | 360 type = sys.argv[3] |
361 source_files = sys.argv[4:] | 361 source_files = sys.argv[4:] |
362 JS2C(source_files, [natives, natives_empty], { 'TYPE': type }) | 362 JS2C(source_files, [natives, natives_empty], { 'TYPE': type }) |
363 | 363 |
364 if __name__ == "__main__": | 364 if __name__ == "__main__": |
365 main() | 365 main() |
OLD | NEW |