summaryrefslogtreecommitdiff
path: root/pycparser/__init__.py
diff options
context:
space:
mode:
authorEli Bendersky <eliben@gmail.com>2010-05-21 09:05:39 +0300
committerEli Bendersky <eliben@gmail.com>2010-05-21 09:05:39 +0300
commit3921e8e23ef7952e2e118e9d534cfc4a221e2450 (patch)
tree60efcf9f007a5657422a75ed0aad28cb97cb3fa2 /pycparser/__init__.py
downloadpycparser-3921e8e23ef7952e2e118e9d534cfc4a221e2450.tar.gz
initial import from SVN
Diffstat (limited to 'pycparser/__init__.py')
-rw-r--r--pycparser/__init__.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/pycparser/__init__.py b/pycparser/__init__.py
new file mode 100644
index 0000000..1489b68
--- /dev/null
+++ b/pycparser/__init__.py
@@ -0,0 +1,74 @@
+#-----------------------------------------------------------------
+# pycparser: __init__.py
+#
+# This package file exports some convenience functions for
+# interacting with pycparser
+#
+# Copyright (C) 2008-2009, Eli Bendersky
+# License: LGPL
+#-----------------------------------------------------------------
+
+__all__ = ['c_lexer', 'c_parser', 'c_ast']
+__version__ = '1.07'
+
+from subprocess import Popen, PIPE
+
+from .c_parser import CParser
+
+
+def parse_file( filename, use_cpp=False,
+ cpp_path='cpp', cpp_args=''):
+ """ Parse a C file using pycparser.
+
+ filename:
+ Name of the file you want to parse.
+
+ use_cpp:
+ Set to True if you want to execute the C pre-processor
+ on the file prior to parsing it.
+
+ cpp_path:
+ If use_cpp is True, this is the path to 'cpp' on your
+ system. If no path is provided, it attempts to just
+ execute 'cpp', so it must be in your PATH.
+
+ cpp_args:
+ If use_cpp is True, set this to the command line
+ arguments strings to cpp. Be careful with quotes -
+ it's best to pass a raw string (r'') here.
+ For example:
+ r'-I../utils/fake_libc_include'
+ If several arguments are required, pass a list of
+ strings.
+
+ When successful, an AST is returned. ParseError can be
+ thrown if the file doesn't parse successfully.
+
+ Errors from cpp will be printed out.
+ """
+ if use_cpp:
+ path_list = [cpp_path]
+ if isinstance(cpp_args, list):
+ path_list += cpp_args
+ elif cpp_args != '':
+ path_list += [cpp_args]
+ path_list += [filename]
+
+ # Note the use of universal_newlines to treat all newlines
+ # as \n for Python's purpose
+ #
+ pipe = Popen( path_list,
+ stdout=PIPE,
+ universal_newlines=True)
+ text = pipe.communicate()[0]
+ else:
+ text = open(filename, 'rU').read()
+
+ parser = CParser()
+ return parser.parse(text, filename)
+
+
+if __name__ == "__main__":
+ pass
+
+