__init__.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_ast", "c_lexer", "c_parser"]
  11. __version__ = "3.11"
  12. from subprocess import check_output
  13. from . import c_parser
  14. CParser = c_parser.CParser
  15. def preprocess_file(filename, cpp_path="cpp", cpp_args=""):
  16. """Preprocess a file using cpp.
  17. filename:
  18. Name of the file you want to preprocess.
  19. cpp_path:
  20. cpp_args:
  21. Refer to the documentation of parse_file for the meaning of these
  22. arguments.
  23. When successful, returns the preprocessed file's contents.
  24. Errors from cpp will be printed out.
  25. """
  26. path_list = [cpp_path]
  27. if isinstance(cpp_args, list):
  28. path_list += cpp_args
  29. elif cpp_args != "":
  30. path_list += [cpp_args]
  31. path_list += [filename]
  32. try:
  33. # Note the use of universal_newlines to treat all newlines
  34. # as \n for Python's purpose
  35. text = check_output(path_list, universal_newlines=True)
  36. except OSError as e:
  37. raise RuntimeError(
  38. "Unable to invoke 'cpp'. "
  39. + "Make sure its path was passed correctly\n"
  40. + f"Original error: {e}"
  41. )
  42. return text
  43. def parse_file(
  44. filename, use_cpp=False, cpp_path="cpp", cpp_args="", parser=None, encoding=None
  45. ):
  46. """Parse a C file using pycparser.
  47. filename:
  48. Name of the file you want to parse.
  49. use_cpp:
  50. Set to True if you want to execute the C pre-processor
  51. on the file prior to parsing it.
  52. cpp_path:
  53. If use_cpp is True, this is the path to 'cpp' on your
  54. system. If no path is provided, it attempts to just
  55. execute 'cpp', so it must be in your PATH.
  56. cpp_args:
  57. If use_cpp is True, set this to the command line arguments strings
  58. to cpp. Be careful with quotes - it's best to pass a raw string
  59. (r'') here. For example:
  60. r'-I../utils/fake_libc_include'
  61. If several arguments are required, pass a list of strings.
  62. encoding:
  63. Encoding to use for the file to parse
  64. parser:
  65. Optional parser object to be used instead of the default CParser
  66. When successful, an AST is returned. ParseError can be
  67. thrown if the file doesn't parse successfully.
  68. Errors from cpp will be printed out.
  69. """
  70. if use_cpp:
  71. text = preprocess_file(filename, cpp_path, cpp_args)
  72. else:
  73. with open(filename, encoding=encoding) as f:
  74. text = f.read()
  75. if parser is None:
  76. parser = CParser()
  77. return parser.parse(text, filename)