| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 # regular language), but simple and sufficient in practice. | 52 # regular language), but simple and sufficient in practice. |
| 53 # Leading and trailing context (e.g. following '{') used to avoid false matches. | 53 # Leading and trailing context (e.g. following '{') used to avoid false matches. |
| 54 ################################################################################ | 54 ################################################################################ |
| 55 | 55 |
| 56 def get_partial_interface_name_from_idl(file_contents): | 56 def get_partial_interface_name_from_idl(file_contents): |
| 57 match = re.search(r'partial\s+interface\s+(\w+)\s*{', file_contents) | 57 match = re.search(r'partial\s+interface\s+(\w+)\s*{', file_contents) |
| 58 return match and match.group(1) | 58 return match and match.group(1) |
| 59 | 59 |
| 60 | 60 |
| 61 def get_implements_from_idl(file_contents, interface_name): | 61 def get_implements_from_idl(file_contents, interface_name): |
| 62 # Rule is: identifier-A implements identifier-B; | 62 """Returns lists of implementing and implemented interfaces. |
| 63 # http://www.w3.org/TR/WebIDL/#idl-implements-statements | 63 |
| 64 # Returns two lists of interfaces that contain identifier-As and identifier-
Bs. | 64 Rule is: identifier-A implements identifier-B; |
| 65 # The 'implements' statements are supported in both identifier-A IDL and ide
ntifier-B IDL | 65 i.e., implement*ing* implements implement*ed*; |
| 66 # to avoid potential layering violation. | 66 http://www.w3.org/TR/WebIDL/#idl-implements-statements |
| 67 |
| 68 Returns two lists of interfaces: identifier-As and identifier-Bs. |
| 69 An 'implements' statements can be present in the IDL file for either the |
| 70 implementing or the implemented interface, but not other files. |
| 71 """ |
| 67 implements_re = (r'^\s*' | 72 implements_re = (r'^\s*' |
| 68 r'(\w+)\s+' | 73 r'(\w+)\s+' |
| 69 r'implements\s+' | 74 r'implements\s+' |
| 70 r'(\w+)\s*' | 75 r'(\w+)\s*' |
| 71 r';') | 76 r';') |
| 72 implements_matches = re.finditer(implements_re, file_contents, re.MULTILINE) | 77 implements_matches = re.finditer(implements_re, file_contents, re.MULTILINE) |
| 73 implements_pairs = [(match.group(1), match.group(2)) | 78 implements_pairs = [match.groups() for match in implements_matches] |
| 74 for match in implements_matches] | 79 |
| 75 A_interfaces = [] | 80 foreign_implements = [pair for pair in implements_pairs |
| 76 B_interfaces = [] | 81 if interface_name not in pair] |
| 77 for left, right in implements_pairs: | 82 if foreign_implements: |
| 78 if left == interface_name: | 83 left, right = foreign_implements.pop() |
| 79 B_interfaces.append(right) | 84 raise IdlBadFilenameError( |
| 80 elif right == interface_name: | 85 'implements statement found in unrelated IDL file.\n' |
| 81 A_interfaces.append(left) | 86 'Statement is:\n' |
| 82 return (A_interfaces, B_interfaces) | 87 ' %s implements %s;\n' |
| 88 'but filename is unrelated "%s.idl"' % |
| 89 (left, right, interface_name)) |
| 90 |
| 91 return ( |
| 92 [left for left, right in implements_pairs if right == interface_name], |
| 93 [right for left, right in implements_pairs if left == interface_name]) |
| 83 | 94 |
| 84 | 95 |
| 85 def is_callback_interface_from_idl(file_contents): | 96 def is_callback_interface_from_idl(file_contents): |
| 86 match = re.search(r'callback\s+interface\s+\w+\s*{', file_contents) | 97 match = re.search(r'callback\s+interface\s+\w+\s*{', file_contents) |
| 87 return bool(match) | 98 return bool(match) |
| 88 | 99 |
| 89 | 100 |
| 90 def get_parent_interface(file_contents): | 101 def get_parent_interface(file_contents): |
| 91 match = re.search(r'interface\s+' | 102 match = re.search(r'interface\s+' |
| 92 r'\w+\s*' | 103 r'\w+\s*' |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 | 141 |
| 131 def get_put_forward_interfaces_from_idl(file_contents): | 142 def get_put_forward_interfaces_from_idl(file_contents): |
| 132 put_forwards_pattern = (r'\[[^\]]*PutForwards=[^\]]*\]\s+' | 143 put_forwards_pattern = (r'\[[^\]]*PutForwards=[^\]]*\]\s+' |
| 133 r'readonly\s+' | 144 r'readonly\s+' |
| 134 r'attribute\s+' | 145 r'attribute\s+' |
| 135 r'(\w+)') | 146 r'(\w+)') |
| 136 return sorted(set(match.group(1) | 147 return sorted(set(match.group(1) |
| 137 for match in re.finditer(put_forwards_pattern, | 148 for match in re.finditer(put_forwards_pattern, |
| 138 file_contents, | 149 file_contents, |
| 139 flags=re.DOTALL))) | 150 flags=re.DOTALL))) |
| OLD | NEW |