Advertisement
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)
- Execution
- ---------
- (py3.6.1) [backend@gc-backend-2 backend]$ python pp2.py
- example: indentStack: [1] 0x7fd7e281c388 <----- the first instance of indentStack
- example: funcDef : function_definition
- checkSubIndent: curCol : 5
- checkSubIndent: indentStack: 0x7fd7e281c388 <----- the first instance of indentStack
- checkSubIndent: indentStack: [1]
- checkSubIndent: indentStack: [1, 5] (append)
- example: indentStack: [1] 0x7fd7e281c388 <----- the first instance of indentStack
- example: funcDef : function_definition
- checkSubIndent: curCol : 5
- checkSubIndent: indentStack: 0x7fd7e281c388 <----- the first instance of indentStack
- checkSubIndent: indentStack: [1]
- checkSubIndent: indentStack: [1, 5] (append)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement