Twisted
Home FAQ Docs Download Timeline Roadmap View Tickets Search
class documentation

Processes tokens from source code lines.

Example usage: >>> from token import NAME, OP, NUMBER, STRING >>> src = ["foo = 123 # comment\n"] >>> tp = TokenProcessor(src) >>> isinstance(tp.buffers, list) True

# get_line returns the line at given lineno (1-based) >>> tp.get_line(1) 'foo = 123 # comment\n'

# fetch_token yields tokens one by one >>> t1 = tp.fetch_token() >>> t1.kind == NAME True >>> t2 = tp.fetch_token() >>> t2.kind == OP True >>> t3 = tp.fetch_token() >>> t3.kind == NUMBER True

# previous and current attributes >>> tp.previous == t2 True >>> tp.current == t3 True

# fetch_until collects tokens until matching condition >>> tp2 = TokenProcessor(["foo = (1 + 2)\n"]) >>> tokens = tp2.fetch_until([OP, ')']) >>> any(t.value == ')' for t in tokens) True

Method __init__ Undocumented
Method fetch_token Fetch a next token from source code.
Method fetch_until Fetch tokens until specified token appeared.
Method get_line Returns specified line.
Instance Variable buffers Undocumented
Instance Variable current Undocumented
Instance Variable previous Undocumented
Instance Variable tokens Undocumented
def __init__(self, buffers: Sequence[str]): (source)

Undocumented

def fetch_token(self) -> Token | None: (source)

Fetch a next token from source code.

Returns ``None`` if sequence finished.

def fetch_until(self, condition: Any) -> list[Token]: (source)

Fetch tokens until specified token appeared.

.. note:: This also handles parenthesis well.

def get_line(self, lineno: int) -> str: (source)

Returns specified line.

Undocumented

Undocumented

Undocumented

Undocumented