summaryrefslogtreecommitdiff
path: root/examples/scanExamples.py
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2019-10-31 21:10:28 -0700
committerPaul McGuire <ptmcg@users.noreply.github.com>2019-10-31 23:10:28 -0500
commit53d1b4a6f48a53c4c4ec4ac7031362b691c0366d (patch)
tree088ad3cf3561b78a00af4fb2fd474f4a2b8ca70c /examples/scanExamples.py
parent41752aa52cc97c710474bb2972cceab057b52ad4 (diff)
downloadpyparsing-git-53d1b4a6f48a53c4c4ec4ac7031362b691c0366d.tar.gz
Blacken the project (#141)
Diffstat (limited to 'examples/scanExamples.py')
-rw-r--r--examples/scanExamples.py43
1 files changed, 29 insertions, 14 deletions
diff --git a/examples/scanExamples.py b/examples/scanExamples.py
index 4ee62a1..91d0739 100644
--- a/examples/scanExamples.py
+++ b/examples/scanExamples.py
@@ -5,8 +5,17 @@
#
# Copyright (c) 2004, 2006 Paul McGuire
#
-from pyparsing import Word, alphas, alphanums, Literal, restOfLine, OneOrMore, \
- empty, Suppress, replaceWith
+from pyparsing import (
+ Word,
+ alphas,
+ alphanums,
+ Literal,
+ restOfLine,
+ OneOrMore,
+ empty,
+ Suppress,
+ replaceWith,
+)
# simulate some C++ code
testData = """
@@ -24,10 +33,15 @@ 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)
+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)
@@ -43,23 +57,24 @@ print("Examples of a transformer")
print("----------------------")
# convert C++ namespaces to mangled C-compatible names
-scopedIdent = ident + OneOrMore( Literal("::").suppress() + ident )
+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 ))
+print(scopedIdent.transformString(testData))
# or a crude pre-processor (use parse actions to replace matching text)
-def substituteMacro(s,l,t):
+def substituteMacro(s, l, t):
if t[0] in macros:
return macros[t[0]]
-ident.setParseAction( substituteMacro )
+
+
+ident.setParseAction(substituteMacro)
ident.ignore(macroDef)
print("(simulate #define pre-processor)")
-print(ident.transformString( testData ))
-
+print(ident.transformString(testData))
#################
@@ -70,6 +85,6 @@ 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("") )
+stringMacroDef.setParseAction(replaceWith(""))
-print(stringMacroDef.transformString( testData ))
+print(stringMacroDef.transformString(testData))