summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-08-07 04:29:07 +0000
committerptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-08-07 04:29:07 +0000
commit31ce7d348df910a64d18359b652a9b9fe298af23 (patch)
treea16201b29c0d097a99757bca0f363c5e02b7d342
parent15124e075525ae1a63b1babd71466f47a482c43c (diff)
downloadpyparsing-31ce7d348df910a64d18359b652a9b9fe298af23.tar.gz
Update indentedGrammarExample.py to use indentedBlock
Remove confusing example tagCapture.py git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/trunk@393 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b
-rw-r--r--src/examples/indentedGrammarExample.py44
-rw-r--r--src/examples/tagCapture.py35
2 files changed, 10 insertions, 69 deletions
diff --git a/src/examples/indentedGrammarExample.py b/src/examples/indentedGrammarExample.py
index f96524a..442e6a4 100644
--- a/src/examples/indentedGrammarExample.py
+++ b/src/examples/indentedGrammarExample.py
@@ -1,10 +1,12 @@
# indentedGrammarExample.py
#
-# Copyright (c) 2006, Paul McGuire
+# Copyright (c) 2006,2016 Paul McGuire
#
# A sample of a pyparsing grammar using indentation for
# grouping (like Python does).
#
+# Updated to use indentedBlock helper method.
+#
from pyparsing import *
@@ -29,41 +31,14 @@ def spam(x,y):
pass
"""
-indentStack = [1]
-
-def checkPeerIndent(s,l,t):
- curCol = col(l,s)
- if curCol != indentStack[-1]:
- if (not indentStack) or curCol > indentStack[-1]:
- raise ParseFatalException(s,l,"illegal nesting")
- raise ParseException(s,l,"not a peer entry")
-
-def checkSubIndent(s,l,t):
- curCol = col(l,s)
- if curCol > indentStack[-1]:
- indentStack.append( curCol )
- else:
- raise ParseException(s,l,"not a subentry")
-
-def checkUnindent(s,l,t):
- if l >= len(s): return
- curCol = col(l,s)
- if not(curCol < indentStack[-1] and curCol <= indentStack[-2]):
- raise ParseException(s,l,"not an unindent")
-
-def doUnindent():
- indentStack.pop()
-
-INDENT = lineEnd.suppress() + empty + empty.copy().setParseAction(checkSubIndent)
-UNDENT = FollowedBy(empty).setParseAction(checkUnindent)
-UNDENT.setParseAction(doUnindent)
+indentStack = [1]
stmt = Forward()
-suite = Group( OneOrMore( empty + stmt.setParseAction( checkPeerIndent ) ) )
+suite = indentedBlock(stmt, indentStack)
identifier = Word(alphas, alphanums)
funcDecl = ("def" + identifier + Group( "(" + Optional( delimitedList(identifier) ) + ")" ) + ":")
-funcDef = Group( funcDecl + INDENT + suite + UNDENT )
+funcDef = Group( funcDecl + suite )
rvalue = Forward()
funcCall = Group(identifier + "(" + Optional(delimitedList(rvalue)) + ")")
@@ -71,8 +46,9 @@ rvalue << (funcCall | identifier | Word(nums))
assignment = Group(identifier + "=" + rvalue)
stmt << ( funcDef | assignment | identifier )
+module_body = OneOrMore(stmt)
+
print(data)
-parseTree = suite.parseString(data)
+parseTree = module_body.parseString(data)
+parseTree.pprint()
-import pprint
-pprint.pprint( parseTree.asList() )
diff --git a/src/examples/tagCapture.py b/src/examples/tagCapture.py
deleted file mode 100644
index e07d518..0000000
--- a/src/examples/tagCapture.py
+++ /dev/null
@@ -1,35 +0,0 @@
-#
-# tagCapture.py
-#
-# Simple demo showing how to match HTML tags
-#
-
-from pyparsing import *
-
-src = "this is test <b> bold <i>text</i> </b> normal text "
-
-def matchingCloseTag(other):
- ret = Forward()
- ret << anyCloseTag.copy()
-
- def setupMatchingClose(tokens):
- opentag = tokens[0]
-
- def mustMatch(tokens):
- if tokens[0][0].strip('<>/') != opentag:
- raise ParseException("",0,"")
-
- ret.setParseAction(mustMatch)
-
- other.addParseAction(setupMatchingClose)
-
- return ret
-
-for m in originalTextFor(anyOpenTag + SkipTo(matchingCloseTag(anyOpenTag),
- include=True,
- failOn=anyOpenTag) ).searchString(src):
- print(m.dump())
-
-for m in originalTextFor(anyOpenTag + SkipTo(matchingCloseTag(anyOpenTag),
- include=True) ).searchString(src):
- print(m.dump())