AKA "Add sub-nodes built from Parser.parseAction results to the parent parsing tree"
I am trying to parse PHP files using PyParsing (which IMHO rules), resulting in function descriptions annotated with JavaDoc style annotations. The reason is because I want to store type information in a way that can be used to create client stub code.
For example:
public function blah($user){ ......
Now I was able to write a parser, it is very simple using PyParser. But PyParser comes with a built-in explicit javaStyleComment icon that I would like to reuse. So I parsed the code, and then tried to attach a parseAction that would cut out the gunk and run a sub-parser (sorry, not a specific terminology) and attach the result to the parsing tree.
I canβt figure out how to do this. The code is attached below. By the way, I could easily write my own javaStyleComment, but I wonder if it is possible to link the parsing results?
Again, sorry if my question is not brief, I'm just new to this.
#@PydevCodeAnalysisIgnore from pyparsing import delimitedList,Literal,Keyword,Regex,ZeroOrMore,Suppress,Optional,QuotedString,Word,hexnums,alphas,\ dblQuotedString,FollowedBy, sglQuotedString,oneOf,Group import pyparsing digits = "0123456789" colon = Literal(':') semi = Literal(';') period = Literal('.') comma = Literal(',') lparen = Literal('{') rparen = Literal('}') lbracket = Literal('(') rbracket = Literal(')') number = Word(digits) hexint = Word(hexnums,exact=2) text = Word(alphas) php = Literal("<?php") + Literal("echo") + Literal("?>") print php.parseString("""<?php echo ?>""") funcPerm = oneOf("public private protected") print funcPerm.parseString("""public""") print funcPerm.parseString("""private""") print funcPerm.parseString("""protected""") stdParam = Regex(r"\$[az][a-zA-Z0-9]*") print stdParam.parseString("""$dog""") dblQuotedString.setParseAction(lambda t:t[0][1:-1]) sglQuotedString.setParseAction(lambda t:t[0][1:-1]) defaultParam = Group(stdParam + Literal("=") + ( dblQuotedString | sglQuotedString | number)) print defaultParam.parseString(""" $dave = 'dog' """) param = ( defaultParam | stdParam ) print param.parseString("""$dave""") #print param.parseString("""dave""") print param.parseString(""" $dave = 'dog' """) print param.parseString(""" $dave = "dog" """) csl = Optional(param + ZeroOrMore( Suppress( "," ) + param)) print csl.parseString("""$dog,$cat,$moose """) print csl.parseString("""$dog,$cat,$moose = "denny" """) print csl.parseString("""""") # funcIdent = Regex(r"[az][_a-zA-Z0-9]*") funcIdent.parseString("farb_asdfdfsDDDDDDD") # funcStart = Group(funcPerm + Literal("function") + funcIdent) print funcStart.parseString("private function dave") # # litWordlit = Literal("(") + csl + Literal(")") print litWordlit.parseString("""( )""") funcDef = funcStart + Literal("(") + Group(csl) + Literal(")") #funcDef.Name = "FUNCTION" #funcDef.ParseAction = lambda t: (("found %s") % t) print funcDef.parseString("""private function doggy($bow,$sddfs)""") funcDefPopulated = funcStart + Literal("(") + Group(csl) + Literal(")") + Group(Literal("{") + ZeroOrMore(pyparsing.CharsNotIn("}")) +Literal("}")) #funcDef.Name = "FUNCTION" #funcDef.ParseAction = lambda t: (("found %s") % t) print funcDefPopulated.parseString("""private function doggy($bow,$sddfs){ $dog="dave" }""") #" @vo{$bow=BowVo}" docAnnotations = ZeroOrMore( Group( Literal("@") + text + Suppress(lparen) + param + Literal("=") + text + Suppress(rparen ) )) print docAnnotations.parseString(""" @vo{$bow=BowVo}""") def extractDoco(s,l,t): """ Helper parse action for parsing the content of a comment block """ ret = t[0] ret = ret.replace('/**','') ret = ret.replace('*\n','') ret = ret.replace('*\n','\n') ret = ret.replace('*/','') t = docAnnotations.parseString(ret) return t phpCustomComment = pyparsing.javaStyleComment #Can't figure out what to do here. Help !!!!! phpCustomComment.addParseAction(extractDoco) commentedFuncDef = phpCustomComment + funcDefPopulated print commentedFuncDef.parseString( """ /** * @vo{$bow=BowVo} * @vo{$sddfs=UserAccount} */ private function doggy($bow,$sddfs){ $dog="dave" }""" ) *emphasized text* #example = open("./example.php","r") #funcDef.parseFile(example) #f4.parseString("""private function dave ( $bow )""") #funcDef = funcPerm + Keyword("function") + funcName + Literal("(") + csl + Literal(")") #print funcDef.parseString(""" private function doggy($bow)""")
=== Update
I found that ParseResults, for example, has an insert method that allows you to grow the parse tree, but still cannot figure out how to do this dynamically.
For example:
title = oneOf("Mr Miss Sir Dr Madame") aname = title + Group(Word(alphas) + Word(alphas)) res=aname.parseString("Mr Dave Young") res (['Mr', (['Dave', 'Young'], {})], {}) res.insert(3,3) res (['Mr', (['Dave', 'Young'], {}), 3], {})