[Pyparsing] setResultsName: request for clarification
Brought to you by:
ptmcg
From: stefaan.himpe <ste...@gm...> - 2008-05-01 15:55:27
|
Hello, I have difficulties in understanding how to use setResultsName correctly. The problem is illustrated in the code below: In the first version, r.BODY is empty. In the second version, it contains what I expected it to contain. But I am not sure what the difference is between these two versions? Thanks for any insights. Best regards, Stefaan. --- import pyparsing as p s = """ SECTION { // { if (1) { dosomething; } else { if (0) then { a; } else { b } } printf ( "{" ); /* { { { */ { nog; iets; } } """ # VERSION 1 section_body = p.Combine(p.nestedExpr("{","}", ignoreExpr=p.quotedString|p.cppStyleComment|p.cStyleComment)) section_body.setParseAction(p.keepOriginalText) section_body.setResultsName("BODY") grammar = p.CaselessKeyword("SECTION").suppress() + section_body r = grammar.parseString(s) print r.BODY # VERSION 2 section_body = p.Combine(p.nestedExpr("{","}", ignoreExpr=p.quotedString|p.cppStyleComment|p.cStyleComment)) section_body.setParseAction(p.keepOriginalText) grammar = p.CaselessKeyword("SECTION").suppress() + \ section_body.setResultsName("BODY") r = grammar.parseString(s) print r.BODY |