OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 # |
| 4 # Redistribution and use in source and binary forms, with or without |
| 5 # modification, are permitted provided that the following conditions are |
| 6 # met: |
| 7 # |
| 8 # * Redistributions of source code must retain the above copyright |
| 9 # notice, this list of conditions and the following disclaimer. |
| 10 # * Redistributions in binary form must reproduce the above |
| 11 # copyright notice, this list of conditions and the following disclaimer |
| 12 # in the documentation and/or other materials provided with the |
| 13 # distribution. |
| 14 # * Neither the name of Google Inc. nor the names of its |
| 15 # contributors may be used to endorse or promote products derived from |
| 16 # this software without specific prior written permission. |
| 17 # |
| 18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 30 import glob |
| 31 import fnmatch |
| 32 import os |
| 33 import sys |
| 34 import unittest |
| 35 |
| 36 from blink_idl_lexer import BlinkIDLLexer |
| 37 from blink_idl_parser import BlinkIDLParser |
| 38 |
| 39 # Base parser is in Chromium src/tools/idl_parser |
| 40 module_path, module_name = os.path.split(__file__) |
| 41 tools_dir = os.path.join(module_path, os.pardir, os.pardir, os.pardir, os.pardir
, os.pardir, 'tools') |
| 42 sys.path.append(tools_dir) |
| 43 |
| 44 from idl_parser.idl_parser import ParseFile |
| 45 |
| 46 |
| 47 def has_preprocessor_directives(filename): |
| 48 with open(filename) as idl_file: |
| 49 for line in idl_file: |
| 50 if line.startswith('#'): |
| 51 return True |
| 52 return False |
| 53 |
| 54 |
| 55 def parse_files(parser, file_list): |
| 56 nodes = [] |
| 57 errors = 0 |
| 58 failed_to_tokenize = 0 |
| 59 for filename in file_list: |
| 60 if filename == './../../../Source/core/inspector/InspectorInstrumentatio
n.idl': |
| 61 # intentionally invalid IDL (w/ pointers etc.) |
| 62 continue |
| 63 |
| 64 # Can't process preprocessor directives |
| 65 if has_preprocessor_directives(filename): |
| 66 continue |
| 67 |
| 68 filenode = ParseFile(parser, filename) |
| 69 if filenode is not None: |
| 70 errors += filenode.GetProperty('ERRORS') |
| 71 nodes.append(filenode) |
| 72 else: |
| 73 failed_to_tokenize += 1 |
| 74 |
| 75 if errors or failed_to_tokenize: |
| 76 print '\nFound %d errors.' % errors |
| 77 print 'Failed to tokenize %d files.\n' % failed_to_tokenize |
| 78 |
| 79 |
| 80 class BlinkIDLParserTest(unittest.TestCase): |
| 81 def setUp(self): |
| 82 self.parser = BlinkIDLParser(BlinkIDLLexer()) |
| 83 # self.parser = BlinkIDLParser(BlinkIDLLexer(), debug=True) |
| 84 |
| 85 def testParseTestIDLFiles(self): |
| 86 test_idl_dir = os.path.join(module_path, os.pardir, 'tests', 'idls') |
| 87 test_idl_glob = os.path.join(test_idl_dir, '*.idl') |
| 88 idl_list = glob.glob(test_idl_glob) |
| 89 parse_files(self.parser, idl_list) |
| 90 |
| 91 def testParseRealIDLFiles(self): |
| 92 idl_list = [] |
| 93 source_dir = os.path.join(module_path, os.pardir, os.pardir, os.pardir,
'Source') |
| 94 for root, _, filenames in os.walk(source_dir): |
| 95 for filename in fnmatch.filter(filenames, '*.idl'): |
| 96 idl_list.append(os.path.join(root, filename)) |
| 97 parse_files(self.parser, idl_list) |
| 98 |
| 99 if __name__ == '__main__': |
| 100 unittest.main() |
OLD | NEW |