diff options
Diffstat (limited to 'examples/macroExpander.py')
-rw-r--r-- | examples/macroExpander.py | 9 |
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
|