In SLY there is an example for writing a calculator (reproduced from calc.py
here):
from sly import Lexer
class CalcLexer(Lexer):
tokens = { NAME, NUMBER }
ignore = ' \t'
literals = { '=', '+', '-', '*', '/', '(', ')' }
# Tokens
NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
@_(r'\d+')
def NUMBER(self, t):
t.value = int(t.value)
return t
@_(r'\n+')
def newline(self, t):
self.lineno += t.value.count('\n')
def error(self, t):
print("Illegal character '%s'" % t.value[0])
self.index += 1
It looks like it's bugged because NAME
and NUMBER
are used before they've been defined. But actually, there is no NameError
, and this code executes fine. How does that work? When can you reference a name before it's been defined?