Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import pyparsing as pp
- from pprint import pprint
- indentStack = [1]
- stmt = pp.Forward()
- suite = pp.Group(pp.indentedBlock(stmt, indentStack)).setName("suite") #.setDebug()
- command = (pp.Keyword("open") | pp.Keyword("close")).setName("command") #.setDebug()
- funcDecl = (pp.Keyword("def") + pp.Word(pp.printables)).setName("function_declaration") #.setDebug()
- funcDef = (funcDecl + suite).setName("function_definition") #.setDebug()
- stmt << ( funcDef | command)
- module_body = pp.OneOrMore(stmt)
- code="""\
- def process
- open
- close"""
- print("example: indentStack: ", indentStack, hex(id(indentStack)))
- print("example: funcDef : ", funcDef)
- f1 = funcDef.parseString(code) ### works fine
- indentStack = [1]
- # indentStack.pop()
- print("example: indentStack: ", indentStack, hex(id(indentStack)))
- print("example: funcDef : ", funcDef)
- f2 = funcDef.parseString(code)
- Executoion
- ----------
- (py3.6.1) [backend@gc-backend-2 backend]$
- (py3.6.1) [backend@gc-backend-2 backend]$ python pp2.py
- example: indentStack: [1] 0x7f77b56ae3c8 <------------ the first instance of the indentStack list
- example: funcDef : function_definition
- checkSubIndent: curCol : 5
- checkSubIndent: indentStack: 0x7f77b56ae3c8
- checkSubIndent: indentStack: [1]
- checkSubIndent: indentStack: [1, 5] (append)
- example: indentStack: [1] 0x7f77b56b0148 <------------ the NEW instance of the indentStack list!!!
- example: funcDef : function_definition
- checkSubIndent: curCol : 5
- checkSubIndent: indentStack: 0x7f77b56ae3c8 <------------ the first instance of the indentStack list
- checkSubIndent: indentStack: [1, 5]
- Traceback (most recent call last):
- File "pp2.py", line 28, in <module>
- f2 = funcDef.parseString(code)
- File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 1632, in parseString
- raise exc
- File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 1622, in parseString
- loc, tokens = self._parse( instring, 0 )
- File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 1379, in _parseNoCache
- loc,tokens = self.parseImpl( instring, preloc, doActions )
- File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 3395, in parseImpl
- loc, exprtokens = e._parse( instring, loc, doActions )
- File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 1379, in _parseNoCache
- loc,tokens = self.parseImpl( instring, preloc, doActions )
- File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 3717, in parseImpl
- return self.expr._parse( instring, loc, doActions, callPreParse=False )
- File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 1379, in _parseNoCache
- loc,tokens = self.parseImpl( instring, preloc, doActions )
- File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 3717, in parseImpl
- return self.expr._parse( instring, loc, doActions, callPreParse=False )
- File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 1379, in _parseNoCache
- loc,tokens = self.parseImpl( instring, preloc, doActions )
- File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 3395, in parseImpl
- loc, exprtokens = e._parse( instring, loc, doActions )
- File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 1405, in _parseNoCache
- tokens = fn( instring, tokensStart, retTokens )
- File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 1049, in wrapper
- ret = func(*args[limit[0]:])
- File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 5321, in checkSubIndent
- raise ParseException(s,l,"not a subentry")
- pyparsing.ParseException: not a subentry (at char 16), (line:2, col:5)
Add Comment
Please, Sign In to add comment