• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#-----------------------------------------------------------------
2# pycparser: __init__.py
3#
4# This package file exports some convenience functions for
5# interacting with pycparser
6#
7# Eli Bendersky [https://eli.thegreenplace.net/]
8# License: BSD
9#-----------------------------------------------------------------
10__all__ = ['c_lexer', 'c_parser', 'c_ast']
11__version__ = '2.20'
12
13import io
14from subprocess import check_output
15from .c_parser import CParser
16
17
18def preprocess_file(filename, cpp_path='cpp', cpp_args=''):
19    """ Preprocess a file using cpp.
20
21        filename:
22            Name of the file you want to preprocess.
23
24        cpp_path:
25        cpp_args:
26            Refer to the documentation of parse_file for the meaning of these
27            arguments.
28
29        When successful, returns the preprocessed file's contents.
30        Errors from cpp will be printed out.
31    """
32    path_list = [cpp_path]
33    if isinstance(cpp_args, list):
34        path_list += cpp_args
35    elif cpp_args != '':
36        path_list += [cpp_args]
37    path_list += [filename]
38
39    try:
40        # Note the use of universal_newlines to treat all newlines
41        # as \n for Python's purpose
42        text = check_output(path_list, universal_newlines=True)
43    except OSError as e:
44        raise RuntimeError("Unable to invoke 'cpp'.  " +
45            'Make sure its path was passed correctly\n' +
46            ('Original error: %s' % e))
47
48    return text
49
50
51def parse_file(filename, use_cpp=False, cpp_path='cpp', cpp_args='',
52               parser=None):
53    """ Parse a C file using pycparser.
54
55        filename:
56            Name of the file you want to parse.
57
58        use_cpp:
59            Set to True if you want to execute the C pre-processor
60            on the file prior to parsing it.
61
62        cpp_path:
63            If use_cpp is True, this is the path to 'cpp' on your
64            system. If no path is provided, it attempts to just
65            execute 'cpp', so it must be in your PATH.
66
67        cpp_args:
68            If use_cpp is True, set this to the command line arguments strings
69            to cpp. Be careful with quotes - it's best to pass a raw string
70            (r'') here. For example:
71            r'-I../utils/fake_libc_include'
72            If several arguments are required, pass a list of strings.
73
74        parser:
75            Optional parser object to be used instead of the default CParser
76
77        When successful, an AST is returned. ParseError can be
78        thrown if the file doesn't parse successfully.
79
80        Errors from cpp will be printed out.
81    """
82    if use_cpp:
83        text = preprocess_file(filename, cpp_path, cpp_args)
84    else:
85        with io.open(filename) as f:
86            text = f.read()
87
88    if parser is None:
89        parser = CParser()
90    return parser.parse(text, filename)
91