Support Python 3.5's matrix multiplication operator (@)

Issue #1194 open
Antony Lee
created an issue

All's in the title.

Comments (7)

  1. Aaron Meurer

    It seems to lex a@b incorrectly. It still thinks @b is a decorator. It only works if you put the space, a @ b. Since @ is a multiplication-like operator, the space is not always used (and anyway, it's impossible for a@b to be a decorator).

  2. Georg Brandl repo owner

    Well, this is one of the few (maybe the only) instance in Python where a lot more context is required than a lexer can provide.

    Sure, we can add some more heuristics, such as "decorator must start at the beginning of a line", but still:

    x = a \
    @ b
    

    I'll see if I can mitigate the common cases though.

  3. Aaron Meurer

    Is checking if it's on a line before another decorator or function or class definition too context sensitive?

    At any rate, just start of the line would go a long way. I didn't realize PEP 8 recommends breaking before operators. But on the other hand, I think that plus the space rule should be enough. No one puts a space before the @ for a decorator, and conversely,

    x = a\
    @b
    

    seems unbalanced (to me at least).

  4. Georg Brandl repo owner

    Is checking if it's on a line before another decorator or function or class definition too context sensitive?

    Definitely, because that requires potentially infinite lookahead.

    At any rate, just start of the line would go a long way.

    Yep, that's what we should go for.

  5. Miro HronĨok

    This still seem not to work on 2.2.0:

    In [1]: import pygments
    
    In [2]: import pygments.lexers
    
    In [3]: import pygments.formatters.html
    
    In [4]: pygments_formatter = pygments.formatters.html.HtmlFormatter()
    
    In [5]: def highlight(code, lang):
       ...:     lexer = pygments.lexers.get_lexer_by_name(lang)
       ...:     return pygments.highlight(code, lexer, pygments_formatter).strip()
       ...: 
    
    In [6]: highlight('a @ a', 'python')
    Out[6]: '<div class="highlight"><pre><span></span><span class="n">a</span> <span class="err">@</span> <span class="n">a</span>\n</pre></div>'
    
    In [7]: highlight('>>> a @ a', 'pycon')
    Out[7]: '<div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="err">@</span> <span class="n">a</span>\n</pre></div>'
    

    Notice the <span class="err">@</span>?

  6. Log in to comment