| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 3 # for details. All rights reserved. Use of this source code is governed by a | 3 # for details. All rights reserved. Use of this source code is governed by a |
| 4 # BSD-style license that can be found in the LICENSE file. | 4 # BSD-style license that can be found in the LICENSE file. |
| 5 | 5 |
| 6 import re | 6 import re |
| 7 import subprocess | 7 import subprocess |
| 8 import tempfile | 8 import tempfile |
| 9 | 9 |
| 10 from pegparser import * | 10 from pegparser import * |
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 | 378 |
| 379 # Return the grammar's root rule: | 379 # Return the grammar's root rule: |
| 380 return MANY(_Definition) | 380 return MANY(_Definition) |
| 381 | 381 |
| 382 def _whitespace_grammar(self): | 382 def _whitespace_grammar(self): |
| 383 return OR(re.compile(r'\s+'), | 383 return OR(re.compile(r'\s+'), |
| 384 re.compile(r'//.*'), | 384 re.compile(r'//.*'), |
| 385 re.compile(r'#.*'), | 385 re.compile(r'#.*'), |
| 386 re.compile(r'/\*.*?\*/', re.S)) | 386 re.compile(r'/\*.*?\*/', re.S)) |
| 387 | 387 |
| 388 def _pre_process(self, content, defines, includePaths): | 388 def parse(self, content): |
| 389 """Pre-processes the content using gcc. | |
| 390 | |
| 391 WebKit IDLs require pre-processing by gcc. This is done by invoking | |
| 392 gcc in a sub-process and capturing the results. | |
| 393 | |
| 394 Returns: | |
| 395 The result of running gcc on the content. | |
| 396 | |
| 397 Args: | |
| 398 content -- text to process. | |
| 399 defines -- an array of pre-processor defines. | |
| 400 includePaths -- an array of path strings. | |
| 401 """ | |
| 402 # FIXME: Handle gcc not found, or any other processing errors | |
| 403 gcc = 'gcc' | |
| 404 cmd = [gcc, '-E', '-P', '-C', '-x', 'c++'] | |
| 405 for define in defines: | |
| 406 cmd.append('-D%s' % define) | |
| 407 cmd.append('-') | |
| 408 pipe = subprocess.Popen(cmd, stdin=subprocess.PIPE, | |
| 409 stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
| 410 (content, stderr) = pipe.communicate(content) | |
| 411 return content | |
| 412 | |
| 413 def parse(self, content, defines=[], includePaths=[]): | |
| 414 """Parse the give content string. | 389 """Parse the give content string. |
| 415 | 390 |
| 416 The WebKit IDL syntax also allows gcc pre-processing instructions. | |
| 417 Lists of defined variables and include paths can be provided. | |
| 418 | |
| 419 Returns: | 391 Returns: |
| 420 An abstract syntax tree (AST). | 392 An abstract syntax tree (AST). |
| 421 | 393 |
| 422 Args: | 394 Args: |
| 423 content -- text to parse. | 395 content -- text to parse. |
| 424 defines -- an array of pre-processor defines. | |
| 425 includePaths -- an array of path strings used by the | |
| 426 gcc pre-processor. | |
| 427 """ | 396 """ |
| 428 if self._syntax == WEBKIT_SYNTAX: | |
| 429 content = self._pre_process(content, defines, includePaths) | |
| 430 | 397 |
| 431 return self._pegparser.parse(content) | 398 return self._pegparser.parse(content) |
| OLD | NEW |