1# Copyright (C) 2013 Google Inc. All rights reserved. 2# 3# Redistribution and use in source and binary forms, with or without 4# modification, are permitted provided that the following conditions are 5# met: 6# 7# * Redistributions of source code must retain the above copyright 8# notice, this list of conditions and the following disclaimer. 9# * Redistributions in binary form must reproduce the above 10# copyright notice, this list of conditions and the following disclaimer 11# in the documentation and/or other materials provided with the 12# distribution. 13# * Neither the name of Google Inc. nor the names of its 14# contributors may be used to endorse or promote products derived from 15# this software without specific prior written permission. 16# 17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29"""Read an IDL file or complete IDL interface, producing an IdlDefinitions object. 30 31Design doc: 32http://www.chromium.org/developers/design-documents/idl-compiler#TOC-Front-end 33""" 34 35import os 36 37import blink_idl_parser 38from blink_idl_parser import BlinkIDLParser 39from idl_definitions import IdlDefinitions 40from idl_validator import EXTENDED_ATTRIBUTES_RELATIVE_PATH, IDLInvalidExtendedAttributeError, IDLExtendedAttributeValidator 41from interface_dependency_resolver import InterfaceDependencyResolver 42 43 44class IdlReader(object): 45 def __init__(self, interfaces_info=None, outputdir=''): 46 self.extended_attribute_validator = IDLExtendedAttributeValidator() 47 48 if interfaces_info: 49 self.interface_dependency_resolver = InterfaceDependencyResolver(interfaces_info, self) 50 else: 51 self.interface_dependency_resolver = None 52 53 self.parser = BlinkIDLParser(outputdir=outputdir) 54 55 def read_idl_definitions(self, idl_filename): 56 """Returns an IdlDefinitions object for an IDL file, including all dependencies.""" 57 definitions = self.read_idl_file(idl_filename) 58 if not self.interface_dependency_resolver: 59 return definitions 60 self.interface_dependency_resolver.resolve_dependencies(definitions) 61 return definitions 62 63 def read_idl_file(self, idl_filename): 64 """Returns an IdlDefinitions object for an IDL file, without any dependencies. 65 66 The IdlDefinitions object is guaranteed to contain a single 67 IdlInterface; it may also contain other definitions, such as 68 callback functions and enumerations.""" 69 ast = blink_idl_parser.parse_file(self.parser, idl_filename) 70 if not ast: 71 raise Exception('Failed to parse %s' % idl_filename) 72 definitions = IdlDefinitions(ast) 73 74 # Validate file contents with filename convention 75 # The Blink IDL filenaming convention is that the file 76 # <interface_name>.idl MUST contain exactly 1 interface (or exception), 77 # and the interface name must agree with the file's basename, 78 # unless it is a partial interface. 79 # (e.g., 'partial interface Foo' can be in FooBar.idl). 80 number_of_interfaces = len(definitions.interfaces) 81 if number_of_interfaces != 1: 82 raise Exception( 83 'Expected exactly 1 interface in file {0}, but found {1}' 84 .format(idl_filename, number_of_interfaces)) 85 interface = next(definitions.interfaces.itervalues()) 86 idl_file_basename, _ = os.path.splitext(os.path.basename(idl_filename)) 87 if not interface.is_partial and interface.name != idl_file_basename: 88 raise Exception( 89 'Interface name "{0}" disagrees with IDL file basename "{1}".' 90 .format(interface.name, idl_file_basename)) 91 92 # Validate extended attributes 93 if not self.extended_attribute_validator: 94 return definitions 95 96 try: 97 self.extended_attribute_validator.validate_extended_attributes(definitions) 98 except IDLInvalidExtendedAttributeError as error: 99 raise IDLInvalidExtendedAttributeError(""" 100IDL ATTRIBUTE ERROR in file: 101%s: 102 %s 103If you want to add a new IDL extended attribute, please add it to: 104 %s 105and add an explanation to the Blink IDL documentation at: 106 http://www.chromium.org/blink/webidl/blink-idl-extended-attributes 107 """ % (idl_filename, str(error), EXTENDED_ATTRIBUTES_RELATIVE_PATH)) 108 109 return definitions 110