The C lexer may parse part of a ?: condition as a label

Issue #436 resolved
Pierre Bourdon
created an issue

Example code reproducing the bug (using the C lexer with the Pygments Mercurial tip) :
int x = (a?b:c);

"b:" is parsed as a label where it should not.

Comments (3)

  1. Artem Shinkarov

    The problem seems to be still present. I am using Pygments version 2.2.0, (c) 2006-2017 by Georg Brandl. and for the following C program:

    int main() {
        return x?y:z;

    y is still incorrectly identified as label. I have:

    <span class="n">x</span><span class="o">?</span><span class="nl">y</span><span class="p">:</span><span class="n">z</span>

    Also, if I look at the attached fix.diff, I cannot wrap my head around why this should help...
    The existing rule for the Label is this:

    (r'([a-zA-Z_]\w*)(\s*)(:)(?!:)', bygroups(Name.Label, Text, Punctuation))

    So all it says is that ID :<whatever> is good, but ID :: is not. However, this doesn't solve the original problem --- in x?y:z y is still parsed as label. I guess the right solution would be to enter a different state after we have seen ? and within that state ignore the label rule. As in C/C++ the e1 ? e2 : e3 is an expression, therefore it cannot contain labels.

  2. Log in to comment