| Index: Source/bindings/scripts/semantic_analyzer.py
|
| diff --git a/Source/bindings/scripts/semantic_analyzer.py b/Source/bindings/scripts/semantic_analyzer.py
|
| new file mode 100755
|
| index 0000000000000000000000000000000000000000..e05b7f6b5e452da5c55494ae446d8be7579e1887
|
| --- /dev/null
|
| +++ b/Source/bindings/scripts/semantic_analyzer.py
|
| @@ -0,0 +1,97 @@
|
| +# 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.
|
| +
|
| +import os.path
|
| +
|
| +
|
| +class AttributeFileFormatError(Exception):
|
| + pass
|
| +
|
| +
|
| +class InvalidIDLAttributeError(Exception):
|
| + pass
|
| +
|
| +
|
| +def read_idl_file(idl_attributes_filename):
|
| + idl_attributes = {}
|
| + with open(idl_attributes_filename) as idl_attributes_file:
|
| + for line in idl_attributes_file:
|
| + line = line.strip()
|
| + if not line or line.startswith('#'):
|
| + continue
|
| + name, _, values_string = map(str.strip, line.partition('='))
|
| + if not name:
|
| + raise AttributeFileFormatError('The format of %s is wrong, in line "%s"' % os.path.basename(idl_attributes_filename), line)
|
| + idl_attributes[name] = set()
|
| + value_list = values_string.split('|')
|
| + if not value_list:
|
| + idl_attributes[name].add(None)
|
| + continue
|
| + for value in value_list:
|
| + value = value.strip()
|
| + if not value:
|
| + value = None
|
| + idl_attributes[name].add(value)
|
| + return idl_attributes
|
| +
|
| +
|
| +class IDLAttributeChecker():
|
| + def __init__(self, idl_attributes_filename):
|
| + self.idl_attributes = read_idl_file(idl_attributes_filename)
|
| +
|
| + def validate_name_value_string(self, name, values_string):
|
| + if name not in self.idl_attributes:
|
| + raise InvalidIDLAttributeError('Unknown IDL attribute [%s]' % name)
|
| + valid_values = self.idl_attributes[name]
|
| + if '*' in valid_values: # wildcard, any value ok
|
| + return
|
| + value_list = values_string.strip.split('|')
|
| + for value in value_list:
|
| + if value not in valid_values:
|
| + raise InvalidIDLAttributeError('Invalid IDL value {value} found in attribute [{name}={value_string}]'.format(**locals))
|
| +
|
| + def validate_extended_attributes(self, node):
|
| + for name, value_string in node['extendedAttributes'].iteritems():
|
| + self.validate_name_value_string(name, value_string)
|
| +
|
| + def check_idl_attributes(self, document, idl_filename):
|
| + # FIXME: this should be done when parsing the tree, rather than after
|
| + try:
|
| + for interface in document['interfaces']:
|
| + self.validate_extended_attributes(interface)
|
| + for attribute in interface['attributes']:
|
| + self.validate_extended_attributes(attribute['signature'])
|
| + for function in interface['functions']:
|
| + self.validate_extended_attributes(function['signature'])
|
| + for parameter in function['parameters']:
|
| + self.validate_extended_attributes(parameter)
|
| + except InvalidIDLAttributeError, error:
|
| + raise InvalidIDLAttributeError("""IDL ATTRIBUTE ERROR in file %s:
|
| +%s
|
| +If you want to add a new IDL 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, error.value)
|
|
|