0

I am using the sly lexer to build a simple lexer, and I am a little confused by the Python syntax in this one. The below code works, but I get a syntax warning in my editor, that I am using OPCODE before assignment.

from sly import Lexer

class AsmLexer(Lexer):    
    tokens = { OPCODE }
    ignore = '\t\n'

    OPCODE          = r'[A-Z]{3}'

When I move the OPCODE line before tokens though, I get this error:

sly.lex.LexerBuildError: OPCODE does not match a name in tokens

Now, I can live with a pylint warning, but I always want my code to be clean (warnings are bugs in disguise) and I don't really understand why this happens.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • Does this answer your question? [When can you use a name before it's defined?](https://stackoverflow.com/questions/66016983/when-can-you-use-a-name-before-its-defined) – MisterMiyagi Feb 07 '21 at 12:46
  • 1
    The TLDR is: ``sly`` use metaclasses to (legally) change the name resolution in the class body. A static code analyzer cannot infer that. – MisterMiyagi Feb 07 '21 at 12:50

0 Answers0