Guest User

https://stackoverflow.com/questions/48874455/pyparsing-2nd-e

a guest
Feb 21st, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.90 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import pyparsing as pp
  3. from pprint import pprint
  4.  
  5. indentStack = [1]
  6. stmt = pp.Forward()
  7. suite = pp.Group(pp.indentedBlock(stmt, indentStack)).setName("suite") #.setDebug()
  8. command = (pp.Keyword("open") | pp.Keyword("close")).setName("command") #.setDebug()
  9. funcDecl = (pp.Keyword("def") + pp.Word(pp.printables)).setName("function_declaration") #.setDebug()
  10. funcDef = (funcDecl + suite).setName("function_definition") #.setDebug()
  11. stmt << ( funcDef | command)
  12. module_body = pp.OneOrMore(stmt)
  13.  
  14. code="""\
  15. def process
  16.    open
  17.    close"""
  18.  
  19. print("example: indentStack: ", indentStack, hex(id(indentStack)))
  20. print("example: funcDef    : ", funcDef)
  21. f1 = funcDef.parseString(code)  ### works fine
  22.  
  23. indentStack = [1]
  24. # indentStack.pop()
  25.  
  26. print("example: indentStack: ", indentStack, hex(id(indentStack)))
  27. print("example: funcDef    : ", funcDef)
  28. f2 = funcDef.parseString(code)  
  29.  
  30.  
  31.  
  32. Executoion
  33. ----------
  34. (py3.6.1) [backend@gc-backend-2 backend]$
  35. (py3.6.1) [backend@gc-backend-2 backend]$ python pp2.py
  36. example: indentStack:  [1] 0x7f77b56ae3c8           <------------ the first instance of the indentStack list
  37. example: funcDef    :  function_definition
  38. checkSubIndent: curCol     :  5
  39. checkSubIndent: indentStack:  0x7f77b56ae3c8
  40. checkSubIndent: indentStack:  [1]
  41. checkSubIndent: indentStack:  [1, 5]  (append)
  42. example: indentStack:  [1] 0x7f77b56b0148           <------------ the NEW instance of the indentStack list!!!
  43. example: funcDef    :  function_definition
  44. checkSubIndent: curCol     :  5
  45. checkSubIndent: indentStack:  0x7f77b56ae3c8        <------------ the first instance of the indentStack list
  46. checkSubIndent: indentStack:  [1, 5]
  47. Traceback (most recent call last):
  48.   File "pp2.py", line 28, in <module>
  49.     f2 = funcDef.parseString(code)
  50.   File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 1632, in parseString
  51.     raise exc
  52.   File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 1622, in parseString
  53.     loc, tokens = self._parse( instring, 0 )
  54.   File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 1379, in _parseNoCache
  55.     loc,tokens = self.parseImpl( instring, preloc, doActions )
  56.   File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 3395, in parseImpl
  57.     loc, exprtokens = e._parse( instring, loc, doActions )
  58.   File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 1379, in _parseNoCache
  59.     loc,tokens = self.parseImpl( instring, preloc, doActions )
  60.   File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 3717, in parseImpl
  61.     return self.expr._parse( instring, loc, doActions, callPreParse=False )
  62.   File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 1379, in _parseNoCache
  63.     loc,tokens = self.parseImpl( instring, preloc, doActions )
  64.   File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 3717, in parseImpl
  65.     return self.expr._parse( instring, loc, doActions, callPreParse=False )
  66.   File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 1379, in _parseNoCache
  67.     loc,tokens = self.parseImpl( instring, preloc, doActions )
  68.   File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 3395, in parseImpl
  69.     loc, exprtokens = e._parse( instring, loc, doActions )
  70.   File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 1405, in _parseNoCache
  71.     tokens = fn( instring, tokensStart, retTokens )
  72.   File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 1049, in wrapper
  73.     ret = func(*args[limit[0]:])
  74.   File "/home/backend/venvs/py3.6.1/lib/python3.6/site-packages/pyparsing.py", line 5321, in checkSubIndent
  75.     raise ParseException(s,l,"not a subentry")
  76. pyparsing.ParseException: not a subentry (at char 16), (line:2, col:5)
Add Comment
Please, Sign In to add comment