Pyparsing: when ignoring comments parseAll = True does not throw ParseException

I noticed a strange side effect in pyparsing:

When using .ignore () in a superset of parsing parseString (..., parseAll = True) stops checking the entire line on the comment character. It is better to explain the code below.

How can I fix this without using stringEnd?

Example:

def test():        
    import pyparsing as p
    unquoted_exclude = "\\\"" + "':/|<>,;#"   
    unquoted_chars = ''.join(set(p.printables) - set(unquoted_exclude))
    unquotedkey = p.Word(unquoted_chars)

    more = p.OneOrMore(unquotedkey)
    more.ignore("#" + p.restOfLine) 
    # ^^ "more" should ignore comments, but not "unquotedkey" !!

    def parse(parser, input_, parseAll=True):
        try: 
            print input_
            print parser.parseString(input_, parseAll).asList()
        except Exception as err:
            print err


    parse(unquotedkey, "abc#d")
    parse(unquotedkey, "abc|d")

    withstringend = unquotedkey + p.stringEnd 
    parse(withstringend, "abc#d", False)
    parse(withstringend, "abc|d", False)

Conclusion:

abc#d     
['abc'] <--- should throw an exception but does not  
abc|d
Expected end of text (at char 3), (line:1, col:4)
abc#d
Expected stringEnd (at char 3), (line:1, col:4)
abc|d
Expected stringEnd (at char 3), (line:1, col:4)
+4
source share
1 answer

To compare apples with apples, you should also add this line after the definition withstringend:

withstringend.ignore('#' + p.restOfLine)

I think you will see that it has the same behavior as your parsing test with unquotedKey.

ignore - , . , C :

/* add one to x */
x ++;

, :

x /* this is a post-increment 
so it really won't add 1 to x until after the
statement executes */ ++
/* and this is the trailing semicolon 
for the previous statement -> */;

, , :

for (x = ptr; /* start at ptr */
     *x; /* keep going as long as we point to non-zero */
     x++ /* add one to x */ )

, , ignore() , . ignore , .

, , :

more = p.OneOrMore(unquotedKey)
more.ignore('#' + p.restOfline)

ignorables unquotedKey. unquotedKey, , more, :

more = p.OneOrMore(unquotedKey.copy())

- , " , ". 1.5.6, excludeChars Word. , Word . :

unquotedKey = p.Word(p.printables,
                     excludeChars = r'\"' + "':/|<>,;#")
+3

All Articles