summaryrefslogtreecommitdiff
path: root/src/examples/scanExamples.py
diff options
context:
space:
mode:
authorPaul McGuire <ptmcg@austin.rr.com>2012-10-02 04:55:56 +0000
committerPaul McGuire <ptmcg@austin.rr.com>2012-10-02 04:55:56 +0000
commita7f9dda0668bfce4fba51df1bf2976b4a93a8bd5 (patch)
tree57ea8bcf2e66532a36c833a7bc57cff9d5d0e4dd /src/examples/scanExamples.py
parentf5d2b716ffb57b65660a7ee0bbf04332dfb29620 (diff)
downloadpyparsing-git-a7f9dda0668bfce4fba51df1bf2976b4a93a8bd5.tar.gz
Add example files to SVN
Diffstat (limited to 'src/examples/scanExamples.py')
-rw-r--r--src/examples/scanExamples.py75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/examples/scanExamples.py b/src/examples/scanExamples.py
new file mode 100644
index 0000000..995ace9
--- /dev/null
+++ b/src/examples/scanExamples.py
@@ -0,0 +1,75 @@
+#
+# scanExamples.py
+#
+# Illustration of using pyparsing's scanString,transformString, and searchString methods
+#
+# Copyright (c) 2004, 2006 Paul McGuire
+#
+from pyparsing import Word, alphas, alphanums, Literal, restOfLine, OneOrMore, \
+ empty, Suppress, replaceWith
+
+# simulate some C++ code
+testData = """
+#define MAX_LOCS=100
+#define USERNAME = "floyd"
+#define PASSWORD = "swordfish"
+
+a = MAX_LOCS;
+CORBA::initORB("xyzzy", USERNAME, PASSWORD );
+
+"""
+
+#################
+print "Example of an extractor"
+print "----------------------"
+
+# simple grammar to match #define's
+ident = Word(alphas, alphanums+"_")
+macroDef = Literal("#define") + ident.setResultsName("name") + "=" + restOfLine.setResultsName("value")
+for t,s,e in macroDef.scanString( testData ):
+ print t.name,":", t.value
+
+# or a quick way to make a dictionary of the names and values
+# (return only key and value tokens, and construct dict from key-value pairs)
+# - empty ahead of restOfLine advances past leading whitespace, does implicit lstrip during parsing
+macroDef = Suppress("#define") + ident + Suppress("=") + empty + restOfLine
+macros = dict(list(macroDef.searchString(testData)))
+print "macros =", macros
+print
+
+
+#################
+print "Examples of a transformer"
+print "----------------------"
+
+# convert C++ namespaces to mangled C-compatible names
+scopedIdent = ident + OneOrMore( Literal("::").suppress() + ident )
+scopedIdent.setParseAction(lambda t: "_".join(t))
+
+print "(replace namespace-scoped names with C-compatible names)"
+print scopedIdent.transformString( testData )
+
+
+# or a crude pre-processor (use parse actions to replace matching text)
+def substituteMacro(s,l,t):
+ if t[0] in macros:
+ return macros[t[0]]
+ident.setParseAction( substituteMacro )
+ident.ignore(macroDef)
+
+print "(simulate #define pre-processor)"
+print ident.transformString( testData )
+
+
+
+#################
+print "Example of a stripper"
+print "----------------------"
+
+from pyparsing import dblQuotedString, LineStart
+
+# remove all string macro definitions (after extracting to a string resource table?)
+stringMacroDef = Literal("#define") + ident + "=" + dblQuotedString + LineStart()
+stringMacroDef.setParseAction( replaceWith("") )
+
+print stringMacroDef.transformString( testData )