OLD | NEW |
---|---|
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Utility functions (file reading, simple IDL parsing by regexes) for IDL build . | 5 """Utility functions (file reading, simple IDL parsing by regexes) for IDL build . |
6 | 6 |
7 Design doc: http://www.chromium.org/developers/design-documents/idl-build | 7 Design doc: http://www.chromium.org/developers/design-documents/idl-build |
8 """ | 8 """ |
9 | 9 |
10 import os | 10 import os |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 # We use regular expressions for parsing; this is incorrect (Web IDL is not a | 129 # We use regular expressions for parsing; this is incorrect (Web IDL is not a |
130 # regular language), but simple and sufficient in practice. | 130 # regular language), but simple and sufficient in practice. |
131 # Leading and trailing context (e.g. following '{') used to avoid false matches. | 131 # Leading and trailing context (e.g. following '{') used to avoid false matches. |
132 ################################################################################ | 132 ################################################################################ |
133 | 133 |
134 def is_callback_interface_from_idl(file_contents): | 134 def is_callback_interface_from_idl(file_contents): |
135 match = re.search(r'callback\s+interface\s+\w+\s*{', file_contents) | 135 match = re.search(r'callback\s+interface\s+\w+\s*{', file_contents) |
136 return bool(match) | 136 return bool(match) |
137 | 137 |
138 | 138 |
139 def get_interface_extended_attributes_from_idl(file_contents): | 139 def match_interface_extended_attributes_from_idl(file_contents): |
140 # Strip comments | 140 # Strip comments |
141 # re.compile needed b/c Python 2.6 doesn't support flags in re.sub | 141 # re.compile needed b/c Python 2.6 doesn't support flags in re.sub |
142 single_line_comment_re = re.compile(r'//.*$', flags=re.MULTILINE) | 142 single_line_comment_re = re.compile(r'//.*$', flags=re.MULTILINE) |
143 block_comment_re = re.compile(r'/\*.*?\*/', flags=re.MULTILINE | re.DOTALL) | 143 block_comment_re = re.compile(r'/\*.*?\*/', flags=re.MULTILINE | re.DOTALL) |
144 file_contents = re.sub(single_line_comment_re, '', file_contents) | 144 file_contents = re.sub(single_line_comment_re, '', file_contents) |
145 file_contents = re.sub(block_comment_re, '', file_contents) | 145 file_contents = re.sub(block_comment_re, '', file_contents) |
146 | 146 |
147 match = re.search(r'\[(.*)\]\s*' | 147 match = re.search(r'\[(.*)\]\s*' |
148 r'((callback|partial)\s+)?' | 148 r'((callback|partial)\s+)?' |
149 r'(interface|exception)\s+' | 149 r'(interface|exception)\s+' |
150 r'\w+\s*' | 150 r'\w+\s*' |
151 r'(:\s*\w+\s*)?' | 151 r'(:\s*\w+\s*)?' |
152 r'{', | 152 r'{', |
153 file_contents, flags=re.DOTALL) | 153 file_contents, flags=re.DOTALL) |
154 return match | |
155 | |
156 | |
157 def get_interface_extended_attributes_from_idl(file_contents): | |
158 match = match_interface_extended_attributes_from_idl(file_contents) | |
154 if not match: | 159 if not match: |
155 return {} | 160 return {} |
156 | 161 |
157 extended_attributes_string = match.group(1) | 162 extended_attributes_string = match.group(1) |
158 extended_attributes = {} | 163 extended_attributes = {} |
159 # FIXME: this splitting is WRONG: it fails on extended attributes where list s of | 164 # FIXME: this splitting is WRONG: it fails on extended attributes where list s of |
160 # multiple values are used, which are seperated by a comma and a space. | 165 # multiple values are used, which are seperated by a comma and a space. |
161 parts = [extended_attribute.strip() | 166 parts = [extended_attribute.strip() |
162 for extended_attribute in re.split(',\s+', extended_attributes_stri ng) | 167 for extended_attribute in re.split(',\s+', extended_attributes_stri ng) |
163 # Discard empty parts, which may exist due to trailing comma | 168 # Discard empty parts, which may exist due to trailing comma |
164 if extended_attribute.strip()] | 169 if extended_attribute.strip()] |
165 for part in parts: | 170 for part in parts: |
166 name, _, value = map(string.strip, part.partition('=')) | 171 name, _, value = map(string.strip, part.partition('=')) |
167 extended_attributes[name] = value | 172 extended_attributes[name] = value |
168 return extended_attributes | 173 return extended_attributes |
174 | |
175 | |
176 def get_interface_exposed_arguments(file_contents): | |
177 match = match_interface_extended_attributes_from_idl(file_contents) | |
178 if not match: | |
179 return None | |
180 | |
181 extended_attributes_string = match.group(1) | |
182 match = re.search(r'Exposed\(([^)]*)\)', file_contents, flags=re.DOTALL) | |
Jens Widell
2014/12/09 09:20:54
Put a "\b" before "Exposed", perhaps? In the unlik
| |
183 if not match: | |
184 return None | |
185 arguments = [] | |
186 for argument in map(string.strip, match.group(1).split(',')): | |
187 exposed, runtime_enabled = argument.split(' ') | |
188 arguments.append({'exposed': exposed, 'runtime_enabled': runtime_enabled }) | |
189 | |
190 return arguments | |
OLD | NEW |