diff options
author | Paul McGuire <ptmcg@austin.rr.com> | 2016-08-07 04:29:07 +0000 |
---|---|---|
committer | Paul McGuire <ptmcg@austin.rr.com> | 2016-08-07 04:29:07 +0000 |
commit | 544aadd91c32bb46ac6e329d9983b29ed5932747 (patch) | |
tree | a16201b29c0d097a99757bca0f363c5e02b7d342 /src/examples/indentedGrammarExample.py | |
parent | dbc7a512e0ac63b5cd5534b16f477648080cf30a (diff) | |
download | pyparsing-git-544aadd91c32bb46ac6e329d9983b29ed5932747.tar.gz |
Update indentedGrammarExample.py to use indentedBlock
Remove confusing example tagCapture.py
Diffstat (limited to 'src/examples/indentedGrammarExample.py')
-rw-r--r-- | src/examples/indentedGrammarExample.py | 44 |
1 files changed, 10 insertions, 34 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() )
|