cLibHeader.py 803 B

12345678910111213141516171819202122232425
  1. #
  2. # cLibHeader.py
  3. #
  4. # A simple parser to extract API doc info from a C header file
  5. #
  6. # Copyright, 2012 - Paul McGuire
  7. #
  8. from pyparsing import Word, alphas, alphanums, Combine, oneOf, Optional, delimitedList, Group, Keyword
  9. testdata = """
  10. int func1(float *vec, int len, double arg1);
  11. int func2(float **arr, float *vec, int len, double arg1, double arg2);
  12. """
  13. ident = Word(alphas, alphanums + "_")
  14. vartype = Combine( oneOf("float double int char") + Optional(Word("*")), adjacent = False)
  15. arglist = delimitedList(Group(vartype("type") + ident("name")))
  16. functionCall = Keyword("int") + ident("name") + "(" + arglist("args") + ")" + ";"
  17. for fn,s,e in functionCall.scanString(testdata):
  18. print(fn.name)
  19. for a in fn.args:
  20. print(" - %(name)s (%(type)s)" % a)