Menu

Problem accessing parse results by name

2008-07-04
2013-05-14
  • Ihor Kaharlichenko

    Greetings. First of all I'd like to thank for this project. It saves a lot of my time.
    But as I am new to pyparsing I'd like to clarify some aspects of the API.
    Here's the code:

        # Test 1
        subscript = Word(alphas)
       
        item = Group(subscript.setResultsName("first") + subscript.setResultsName("second") + Literal(";").suppress())
        grammar = OneOrMore(item)
        s = r'qwe asd;qwe asd;'

        p = grammar.parseString(s)[0]
       
        assert p[0] == p.first, "Accessing result by index and by name should return the same object"

        # Test 2
        subscript = Literal("[").suppress() + Word(alphas) + Literal("]").suppress()
       
        item = Group(subscript.setResultsName("first") + subscript.setResultsName("second") + Literal(";").suppress())
        grammar = OneOrMore(item)
        s = r'[qwe][asd];[qwe][asd];'

        p = grammar.parseString(s)[0]
       
        assert p[0] == p.first, "Accessing result by index and by name should return the same object"

    The first assertion is ok, but the second fails, because p[0] is a string "qwe" (as expected), but p.first is an instance of ParseResults object. The question is: do I misunderstand something or is it a bug?

    Regards, Ihor

     
    • Paul McGuire

      Paul McGuire - 2008-07-06

      Ihor -

      Even though your second example parses only a single word 'qwe' as the first subscript, that word is defined as part of an And().  And() expressions always return an array of tokens, even if only one of the elements of the And() returns any text.  If you print out p.dump(), you can see that p.first returns ['qwe']:
         
      ['qwe', 'asd']
      - first: ['qwe']
      - second: ['asd']

      so to get the 'qwe' string, you'll have to refer to p.first[0].

      If you are going to suppress the leading and trailing brackets, you can collapse this back to a single string value by wrapping the subscript expression in a Combine():

      subscript = Combine(Literal("[").suppress() + Word(alphas) + Literal("]").suppress())

      Using this expression for subscript, p.dump() now returns:
         
      ['qwe', 'asd']
      - first: qwe
      - second: asd

      which is the same value returned from your first grammar.

      Not really a bug, but a subtle difference in behavior for And()'ed results.
         
      -- Paul

       

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.