Menu

MatchFirst shuld return ParseResults similar as what the element been match should return

2017-07-01
2017-07-01
  • Alvaro Leiva

    Alvaro Leiva - 2017-07-01

    I think this is a bug, but it may be intended behaivure, just dont seems right o me (by the way love pyparsing!!!).

    concider opt1 and opt2, defined as follow:

    from pyparsing import Literal, pyparsing_common
    
    ipv6 = (Literal('[') + pyparsing_common.ipv6_address + Literal(']'))
    ipv4 = pyparsing_common.ipv4_address
    
    opt1 = ipv6.setResultsName('ip')
    opt2 = (ipv4 | ipv6).setResultsName('ip')
    
    print(opt1.parseString('[::1]').get('ip'))
    print(opt2.parseString('[::1]').get('ip'))
    

    Both cases should print the same statement but they print similar statements but the outpur of this is:

    ['[', '::1', ']']
    [
    

    The answer to this is simple, opt1.saveAsList == True but opt2.saveAsList == False, so when building the ParseResults after the element has been match, only the first element of ipv6 is selected given a name.

    I know that if i use Combine(ipv6) when declaring opt2 and it should be enought, i also know that i could alays change MatchFirst.saveAsList, but seems like a extra step for something that seems intuitive from the syntax, match ipv4 or ipv6 and then name that result as IP.

    I cna think of 2 solutions for this:

    1) is to overwrrite the setResultsName in MatchFirst to a simpler version of map(lambda e: e.setResultsName(name), self.exprs).
    2) in parseImpl after ret = e._parse( instring, loc, doActions ) do self.saveAsList = e.saveAsList

    what do you guys think?

     

    Last edit: Alvaro Leiva 2017-07-01

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.