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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 # We use regular expressions for parsing; this is incorrect (Web IDL is not a | 51 # We use regular expressions for parsing; this is incorrect (Web IDL is not a |
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_implemented_interfaces_from_idl(file_contents, interface_name): | 61 def get_right_or_left_interfaces_of_implements_from_idl(file_contents, interface
_name, get_right): |
62 # Rule is: identifier-A implements identifier-B; | 62 # Rule is: identifier-A implements identifier-B; |
63 # http://www.w3.org/TR/WebIDL/#idl-implements-statements | 63 # http://www.w3.org/TR/WebIDL/#idl-implements-statements |
64 def get_implemented(left_identifier, right_identifier): | 64 # Get the right interfaces (identifier-Bs) when the statements are in the le
ft interface (identifier-A) IDL. |
65 # identifier-A must be the current interface | 65 # Get the left interfaces (identifier-As) when the statements are in the rig
ht interface (identifier-B) IDL. |
66 if left_identifier != interface_name: | 66 # Support both cases to avoid layering violation. |
67 raise IdlBadFilenameError("Identifier on the left of the 'implements
' statement should be %s in %s.idl, but found %s" % (interface_name, interface_n
ame, left_identifier)) | |
68 return right_identifier | |
69 | |
70 implements_re = (r'^\s*' | 67 implements_re = (r'^\s*' |
71 r'(\w+)\s+' | 68 r'(\w+)\s+' |
72 r'implements\s+' | 69 r'implements\s+' |
73 r'(\w+)\s*' | 70 r'(\w+)\s*' |
74 r';') | 71 r';') |
75 implements_matches = re.finditer(implements_re, file_contents, re.MULTILINE) | 72 implements_matches = re.finditer(implements_re, file_contents, re.MULTILINE) |
76 implements_pairs = [(match.group(1), match.group(2)) | 73 implements_pairs = [(match.group(1), match.group(2)) |
77 for match in implements_matches] | 74 for match in implements_matches] |
78 return [get_implemented(left, right) for left, right in implements_pairs] | 75 left_or_right_interfaces = [] |
| 76 for left, right in implements_pairs: |
| 77 if get_right: |
| 78 if left == interface_name: |
| 79 left_or_right_interfaces.append(right) |
| 80 else: |
| 81 if right == interface_name: |
| 82 left_or_right_interfaces.append(left) |
| 83 return left_or_right_interfaces |
79 | 84 |
80 | 85 |
81 def is_callback_interface_from_idl(file_contents): | 86 def is_callback_interface_from_idl(file_contents): |
82 match = re.search(r'callback\s+interface\s+\w+\s*{', file_contents) | 87 match = re.search(r'callback\s+interface\s+\w+\s*{', file_contents) |
83 return bool(match) | 88 return bool(match) |
84 | 89 |
85 | 90 |
86 def get_parent_interface(file_contents): | 91 def get_parent_interface(file_contents): |
87 match = re.search(r'interface\s+' | 92 match = re.search(r'interface\s+' |
88 r'\w+\s*' | 93 r'\w+\s*' |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 | 128 |
124 def get_put_forward_interfaces_from_idl(file_contents): | 129 def get_put_forward_interfaces_from_idl(file_contents): |
125 put_forwards_pattern = (r'\[[^\]]*PutForwards=[^\]]*\]\s+' | 130 put_forwards_pattern = (r'\[[^\]]*PutForwards=[^\]]*\]\s+' |
126 r'readonly\s+' | 131 r'readonly\s+' |
127 r'attribute\s+' | 132 r'attribute\s+' |
128 r'(\w+)') | 133 r'(\w+)') |
129 return sorted(set(match.group(1) | 134 return sorted(set(match.group(1) |
130 for match in re.finditer(put_forwards_pattern, | 135 for match in re.finditer(put_forwards_pattern, |
131 file_contents, | 136 file_contents, |
132 flags=re.DOTALL))) | 137 flags=re.DOTALL))) |
OLD | NEW |