Index: Source/bindings/scripts/idl_validator.py |
diff --git a/Source/bindings/scripts/idl_validator.py b/Source/bindings/scripts/idl_validator.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..314cb50255d2fafc18a4958be8b5b1668b44a6aa |
--- /dev/null |
+++ b/Source/bindings/scripts/idl_validator.py |
@@ -0,0 +1,112 @@ |
+# Copyright (C) 2013 Google Inc. All rights reserved. |
+# |
+# Redistribution and use in source and binary forms, with or without |
+# modification, are permitted provided that the following conditions are |
+# met: |
+# |
+# * Redistributions of source code must retain the above copyright |
+# notice, this list of conditions and the following disclaimer. |
+# * Redistributions in binary form must reproduce the above |
+# copyright notice, this list of conditions and the following disclaimer |
+# in the documentation and/or other materials provided with the |
+# distribution. |
+# * Neither the name of Google Inc. nor the names of its |
+# contributors may be used to endorse or promote products derived from |
+# this software without specific prior written permission. |
+# |
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ |
+"""Validate extended attributes.""" |
+ |
+ |
+import re |
+ |
+ |
+class IDLExtendedAttributeFileFormatError(Exception): |
+ pass |
+ |
+ |
+class IDLInvalidExtendedAttributeError(Exception): |
+ pass |
+ |
+ |
+def validate_extended_attributes(definitions, basename, idl_attributes_filename): |
+ if idl_attributes_filename is None: |
+ return |
haraken
2013/07/17 02:44:49
You might want to output an error for this case.
Nils Barth (inactive)
2013/07/17 12:05:09
Fixed by refactoring.
This “return” is because the
|
+ extended_attribute_validator = IDLExtendedAttributeValidator(idl_attributes_filename) |
+ extended_attribute_validator.validate_extended_attributes(definitions, basename) |
+ |
+ |
+def read_extended_attributes_file(extended_attributes_filename): |
+ valid_extended_attributes = {} |
+ with open(extended_attributes_filename) as extended_attributes_file: |
+ for line in extended_attributes_file: |
+ line = line.strip() |
+ if not line or line.startswith('#'): |
+ continue |
+ name, _, values_string = map(str.strip, line.partition('=')) |
+ if not name: |
+ raise IDLExtendedAttributeFileFormatError('The format of %s is wrong, in line "%s"' % (extended_attributes_filename, line)) |
+ valid_extended_attributes[name] = set() |
+ value_list = values_string.split('|') |
+ if not value_list: |
+ valid_extended_attributes[name].add(None) |
+ continue |
+ for value in value_list: |
+ value = value.strip() |
+ if not value: |
+ value = None |
+ valid_extended_attributes[name].add(value) |
+ return valid_extended_attributes |
+ |
+ |
+class IDLExtendedAttributeValidator: |
+ def __init__(self, extended_attributes_filename): |
+ self.valid_extended_attributes = read_extended_attributes_file(extended_attributes_filename) |
+ |
+ def validate_extended_attributes(self, definitions, idl_filename): |
+ # FIXME: this should be done when parsing the file, rather than after. |
haraken
2013/07/17 02:44:49
I think postmortem processing sounds reasonable.
Nils Barth (inactive)
2013/07/17 12:05:09
(See longer reply.)
Nils Barth (inactive)
2013/07/17 12:11:02
It’s not a big deal either way, but validating whi
haraken
2013/07/21 14:31:50
Sounds like a plan. I don't have a strong opinion
Nils Barth (inactive)
2013/07/22 06:32:01
Got it; we’ll see what makes sense when actually t
|
+ try: |
+ for interface in definitions.interfaces.itervalues(): |
+ self.validate_extended_attributes_node(interface) |
+ for attribute in interface.attributes: |
+ self.validate_extended_attributes_node(attribute) |
+ for function in interface.functions: |
+ self.validate_extended_attributes_node(function) |
+ for argument in function.arguments: |
+ self.validate_extended_attributes_node(argument) |
+ except IDLInvalidExtendedAttributeError, error: |
+ raise IDLInvalidExtendedAttributeError("""IDL ATTRIBUTE ERROR in file %s: |
+%s |
+If you want to add a new IDL extended attribute, please add it to bindings/scripts/IDLAttributes.txt and add an explanation to the Blink IDL document at http://chromium.org/blink/webidl |
+""" % (idl_filename, str(error))) |
+ |
+ def validate_extended_attributes_node(self, node): |
+ for name, value_string in node.extended_attributes.iteritems(): |
+ self.validate_name_value_string(name, value_string) |
+ |
+ def validate_name_value_string(self, name, values_string): |
+ if name == 'ImplementedBy': # attribute added when merging interfaces |
+ return |
+ if name not in self.valid_extended_attributes: |
+ raise IDLInvalidExtendedAttributeError('Unknown extended attribute [%s]' % name) |
+ valid_values = self.valid_extended_attributes[name] |
+ if '*' in valid_values: # wildcard, any value ok |
+ return |
+ if values_string is None: |
+ value_list = [None] |
+ else: |
+ value_list = re.split('[|&]', values_string.strip()) |
+ for value in value_list: |
+ if value not in valid_values: |
haraken
2013/07/17 02:44:49
Don't you need to do value.strip() ?
Nils Barth (inactive)
2013/07/17 12:05:09
Nope, since the parser has already handled whitesp
|
+ raise IDLInvalidExtendedAttributeError('Invalid value "{value}" found in extended attribute [{name}={value_string}]'.format(**locals())) |