| Index: Source/bindings/scripts/blink_idl_parser_perl.py
|
| diff --git a/Source/bindings/scripts/blink_idl_parser_perl.py b/Source/bindings/scripts/blink_idl_parser_perl.py
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..a7840e850aaea6ba876261606448333b416e9682
|
| --- /dev/null
|
| +++ b/Source/bindings/scripts/blink_idl_parser_perl.py
|
| @@ -0,0 +1,92 @@
|
| +# 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 json
|
| +import subprocess
|
| +
|
| +
|
| +def strip_types(subtree):
|
| + """Clean up IR converted through JSON.
|
| +
|
| + The Perl IDL IR consists of objects, lists, and leaves.
|
| + Perl's JSON.pm converts objects to dictionaries with keys type::key.
|
| + This strips the 'type::' and stores it in an element called TYPE.
|
| + """
|
| + if isinstance(subtree, dict):
|
| + new_dict = {}
|
| + prefix = None
|
| + for type_key, value in subtree.iteritems():
|
| + new_prefix, _, key = type_key.partition('::')
|
| + if prefix is None:
|
| + prefix = new_prefix
|
| + new_dict['TYPE'] = prefix
|
| + else:
|
| + if prefix != new_prefix:
|
| + raise ValueError('Got multiple prefixes: %s %s' % prefix, new_prefix)
|
| + new_dict[key] = strip_types(value)
|
| + return new_dict
|
| + elif isinstance(subtree, list):
|
| + new_list = []
|
| + for item in subtree:
|
| + new_list.append(strip_types(item))
|
| + return new_list
|
| + else:
|
| + return subtree
|
| +
|
| +
|
| +def none_to_empty_list(document):
|
| + """Replace various None values with [], so loops work properly."""
|
| + for interface in document['interfaces']:
|
| + if interface['attributes'] is None:
|
| + interface['attributes'] = []
|
| + if interface['functions'] is None:
|
| + interface['functions'] = []
|
| + else:
|
| + for function in interface['functions']:
|
| + if function['parameters'] is None:
|
| + function['parameters'] = []
|
| + return document
|
| +
|
| +
|
| +def parse_file(idl_file, defines, preprocessor):
|
| + """Wrapper function to parse IDL file using Perl."""
|
| + defines = defines or ''
|
| + preprocessor = preprocessor or ''
|
| + cmd = ['perl', '-w',
|
| + '-I../../core/scripts',
|
| + 'idl-to-json.pl',
|
| + '--defines', defines,
|
| + '--preprocessor', preprocessor,
|
| + idl_file]
|
| + try:
|
| + perl_ir_as_json = subprocess.check_output(cmd)
|
| + except subprocess.CalledProcessError:
|
| + return None
|
| + raw_dict = json.loads(perl_ir_as_json)
|
| + clean_dict = strip_types(raw_dict)
|
| + return none_to_empty_list(clean_dict)
|
|
|