scanExamples.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #
  2. # scanExamples.py
  3. #
  4. # Illustration of using pyparsing's scanString,transformString, and searchString methods
  5. #
  6. # Copyright (c) 2004, 2006 Paul McGuire
  7. #
  8. from pyparsing import Word, alphas, alphanums, Literal, restOfLine, OneOrMore, \
  9. empty, Suppress, replaceWith
  10. # simulate some C++ code
  11. testData = """
  12. #define MAX_LOCS=100
  13. #define USERNAME = "floyd"
  14. #define PASSWORD = "swordfish"
  15. a = MAX_LOCS;
  16. CORBA::initORB("xyzzy", USERNAME, PASSWORD );
  17. """
  18. #################
  19. print("Example of an extractor")
  20. print("----------------------")
  21. # simple grammar to match #define's
  22. ident = Word(alphas, alphanums+"_")
  23. macroDef = Literal("#define") + ident.setResultsName("name") + "=" + restOfLine.setResultsName("value")
  24. for t,s,e in macroDef.scanString( testData ):
  25. print(t.name,":", t.value)
  26. # or a quick way to make a dictionary of the names and values
  27. # (return only key and value tokens, and construct dict from key-value pairs)
  28. # - empty ahead of restOfLine advances past leading whitespace, does implicit lstrip during parsing
  29. macroDef = Suppress("#define") + ident + Suppress("=") + empty + restOfLine
  30. macros = dict(list(macroDef.searchString(testData)))
  31. print("macros =", macros)
  32. print()
  33. #################
  34. print("Examples of a transformer")
  35. print("----------------------")
  36. # convert C++ namespaces to mangled C-compatible names
  37. scopedIdent = ident + OneOrMore( Literal("::").suppress() + ident )
  38. scopedIdent.setParseAction(lambda t: "_".join(t))
  39. print("(replace namespace-scoped names with C-compatible names)")
  40. print(scopedIdent.transformString( testData ))
  41. # or a crude pre-processor (use parse actions to replace matching text)
  42. def substituteMacro(s,l,t):
  43. if t[0] in macros:
  44. return macros[t[0]]
  45. ident.setParseAction( substituteMacro )
  46. ident.ignore(macroDef)
  47. print("(simulate #define pre-processor)")
  48. print(ident.transformString( testData ))
  49. #################
  50. print("Example of a stripper")
  51. print("----------------------")
  52. from pyparsing import dblQuotedString, LineStart
  53. # remove all string macro definitions (after extracting to a string resource table?)
  54. stringMacroDef = Literal("#define") + ident + "=" + dblQuotedString + LineStart()
  55. stringMacroDef.setParseAction( replaceWith("") )
  56. print(stringMacroDef.transformString( testData ))