summaryrefslogtreecommitdiff
path: root/examples/macroExpander.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/macroExpander.py
parent41752aa52cc97c710474bb2972cceab057b52ad4 (diff)
downloadpyparsing-git-53d1b4a6f48a53c4c4ec4ac7031362b691c0366d.tar.gz
Blacken the project (#141)
Diffstat (limited to 'examples/macroExpander.py')
-rw-r--r--examples/macroExpander.py9
1 files changed, 5 insertions, 4 deletions
diff --git a/examples/macroExpander.py b/examples/macroExpander.py
index c6b7034..3b06250 100644
--- a/examples/macroExpander.py
+++ b/examples/macroExpander.py
@@ -16,7 +16,7 @@ from pyparsing import *
# define the structure of a macro definition (the empty term is used
# to advance to the next non-whitespace character)
-identifier = Word(alphas+"_",alphanums+"_")
+identifier = Word(alphas + "_", alphanums + "_")
macroDef = "#def" + identifier("macro") + empty + restOfLine("value")
# define a placeholder for defined macros - initially nothing
@@ -27,16 +27,18 @@ macroExpr << NoMatch()
macros = {}
# parse action for macro definitions
-def processMacroDefn(s,l,t):
+def processMacroDefn(s, l, t):
macroVal = macroExpander.transformString(t.value)
macros[t.macro] = macroVal
macroExpr << MatchFirst(map(Keyword, macros.keys()))
return "#def " + t.macro + " " + macroVal
+
# parse action to replace macro references with their respective definition
-def processMacroRef(s,l,t):
+def processMacroRef(s, l, t):
return macros[t[0]]
+
# attach parse actions to expressions
macroExpr.setParseAction(processMacroRef)
macroDef.setParseAction(processMacroDefn)
@@ -45,7 +47,6 @@ macroDef.setParseAction(processMacroDefn)
macroExpander = macroExpr | macroDef
-
# test macro substitution using transformString
testString = """
#def A 100