NoneType 对象不可调用(制作词法分析器)

NoneType object is not callable (Making lexer)

我的代码:

import re
from collections import namedtuple

class TokenDef(namedtuple("TokenDef", ("name", "pattern", "value_filter"))):
    def __repr_(self):
        return "TokenType." + self.name
    
class TokenType(object):
    _defs = [
        TokenDef("plus", "+", None),
        TokenDef("minus", "-", None),
        TokenDef("aterisk", "*", None),
        TokenDef("slash", "/", None),

        TokenDef("left_paren", "(", None),
        TokenDef("right_paren", ")", None),
        
        TokenDef("integer", re.compile("[0-9]+"), int),
        TokenDef("whitespace", re.compile("[ \t]+"), None),
    ]

for def_ in TokenType._defs:
    setattr(TokenType, def_.name, def_)

token = namedtuple("Token", ("type", "value", "slice"))

def first_token(text, start=0):
    match_text = text[start:]
    token = None
    token_text = None
    
    for type_ in TokenType._defs:
        name, pattern, value_filter = type_
        
        if pattern is None:
            continue
            
        elif isinstance(pattern, str):
            
            if not match_text.startswith:
                continue 
            match_value = pattern
            
        else:
            match = pattern.match(match_text)
            
            if not match:
                continue
                
            match_value = match.group(0)
            
        if token_text is not None and len(token_text) >= len(match_value):
            continue 
            
        token_text = match_value
        
        if value_filter is not None:
            match_value = value_filter(match_value)
            
        token = token(type_, match_value, slice(start, start + len(token_text)))
        
    return token

first_token("6")

我的错误:

Traceback (most recent call last):
  File ".\lexer.py", line 64, in <module>
    first_token("6")
  File ".\lexer.py", line 60, in first_token
    token = token(type_, match_value, slice(start, start + len(token_text)))
TypeError: 'NoneType' object is not callable

为什么会这样?

我正在尝试制作一个词法分析器,我是从教程开始的,但我找不到我的错误在哪里。我在 jupyter 笔记本上,使用 windows 10 pro,我的 python 版本是 3.9.0(anaconda 环境)。什么是 NoneType 对象? idk 这很奇怪但是 idk.

我看到你的问题,你有两个不同的东西叫做 token

token = namedtuple("Token", ("type", "value", "slice"))

是您想要用作调用的内容,将另一个标记重命名为 my_token

更改以下行:

token = None
token = token(type_, match_value, slice(start, start + len(token_text)))
return token

my_token = None
my_token = token(type_, match_value, slice(start, start + len(token_text)))
return my_token