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().
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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().
??? 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
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
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