Menu

Problem with lineStart

2006-03-31
2013-05-14
  • Larry Maccherone

    If I run this code:
    ---------
    from pyparsing import *
    directive = (lineStart +
                 Combine(Literal('#') + restOfLine))
    directive_statement = (ZeroOrMore(cppStyleComment) + directive)
    s = """
    // preceding comment
    # directive - OK

        // preceding comment
        # directive with preceding whitespace and comment - OK

        # directive with preceding whitespace - DOES NOT WORK
    """
    for t in directive.scanString(s):
        print t

    for t in directive_statement.scanString(s):
        print t
    ---------
    It doesn't seem to find the directive_statement with the preceding whitespace but no preceding comment.  I even tried it with an explicit White().

     
    • Paul McGuire

      Paul McGuire - 2006-03-31

      ???  Your program looks like it's okay for me.  Here's the output:

      ((['#', ' directive - OK'], {}), 21, 38)
      ((['#', ' directive with preceding whitespace and comment - OK'], {}), 64, 123)
      ((['#', ' directive with preceding whitespace - DOES NOT WORK'], {}), 123, 182)
      ((['// preceding comment', '#', ' directive - OK'], {}), 1, 38)
      ((['// preceding comment', '#', ' directive with preceding whitespace and comment - OK'], {}), 44, 123)

      What version of pyparsing are you running?  I tested this successfully with 1.4.1, and also with the upcoming 1.4.2, and both gave this output.

      -- Paul

       
      • Larry Maccherone

        Thanks for the quick response both today and yesterday. 

        I get the same output as you.  I'm using 1.4.1.  However, that's not the desired output.

        My test code runs the two different scanners - the "directive" one first and then the "directive_statement" one next.  The "directive_statement" scanner is missing the third directive.  Only the "directive" scanner picks it up.  Notice the missing "DOES NOT WORK" line in the second run. 

        Sorry about the confusion.  I should have labled the output better.

        Once again, great tool and thanks.

        Larry

         
    • Paul McGuire

      Paul McGuire - 2006-03-31

      Try this:

      from pyparsing import *
      directive = lineStart + Optional(White()) + Combine(Literal('#') + restOfLine) + Suppress(lineEnd)
      directive_statement = ZeroOrMore(cppStyleComment) + directive
      directive_statement.leaveWhitespace()

      The problem is that lineStart is pretty touchy about being tested exactly at col 1, and it's possible that col 1 was already skipped over during default whitespace skipping.

      I might take a look at making lineStart a little more tolerant of being tested at the logical line start, not just "at col 1".

      -- 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.