summaryrefslogtreecommitdiff
path: root/src/examples/matchPreviousDemo.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/examples/matchPreviousDemo.py')
-rw-r--r--src/examples/matchPreviousDemo.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/examples/matchPreviousDemo.py b/src/examples/matchPreviousDemo.py
new file mode 100644
index 0000000..91d7eb1
--- /dev/null
+++ b/src/examples/matchPreviousDemo.py
@@ -0,0 +1,33 @@
+#
+# matchPreviousDemo.py
+#
+
+from pyparsing import *
+
+src = """
+class a
+...
+end a;
+
+class b
+...
+end b;
+
+class c
+...
+end d;"""
+
+
+identifier = Word(alphas)
+
+classIdent = identifier("classname") # note that this also makes a copy of identifier
+classHead = "class" + classIdent
+classBody = "..."
+classEnd = "end" + matchPreviousLiteral(classIdent) + ';'
+classDefn = classHead + classBody + classEnd
+
+# use this form to catch syntax error
+# classDefn = classHead + classBody - classEnd
+
+for tokens in classDefn.searchString(src):
+ print tokens.classname \ No newline at end of file