Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 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. Execution
  33. ---------
  34. (py3.6.1) [backend@gc-backend-2 backend]$ python pp2.py
  35. example: indentStack:  [1] 0x7fd7e281c388        <----- the first instance of indentStack
  36. example: funcDef    :  function_definition
  37. checkSubIndent: curCol     :  5
  38. checkSubIndent: indentStack:  0x7fd7e281c388     <----- the first instance of indentStack
  39. checkSubIndent: indentStack:  [1]
  40. checkSubIndent: indentStack:  [1, 5]  (append)
  41. example: indentStack:  [1] 0x7fd7e281c388        <----- the first instance of indentStack
  42. example: funcDef    :  function_definition
  43. checkSubIndent: curCol     :  5
  44. checkSubIndent: indentStack:  0x7fd7e281c388     <----- the first instance of indentStack
  45. checkSubIndent: indentStack:  [1]
  46. checkSubIndent: indentStack:  [1, 5]  (append)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement