Staging
v0.5.1
https://github.com/python/cpython
Raw File
Tip revision: 08bd63da6e64c49130d244ecf8f246089b9c5d9f authored by Ɓukasz Langa on 07 September 2020, 15:52:19 UTC
Python 3.8.6rc1
Tip revision: 08bd63d
token.py
import itertools


def generate_tokens(tokens):
    numbers = itertools.count(0)
    for line in tokens:
        line = line.strip()

        if not line:
            continue
        if line.strip().startswith('#'):
            continue

        name = line.split()[0]
        yield (name, next(numbers))

    yield ('N_TOKENS', next(numbers))
    yield ('NT_OFFSET', 256)


def generate_opmap(tokens):
    for line in tokens:
        line = line.strip()

        if not line:
            continue
        if line.strip().startswith('#'):
            continue

        pieces = line.split()

        if len(pieces) != 2:
            continue

        name, op = pieces
        yield (op.strip("'"), name)

    # Yield independently <>. This is needed so it does not collide
    # with the token generation in "generate_tokens" because if this
    # symbol is included in Grammar/Tokens, it will collide with !=
    # as it has the same name (NOTEQUAL).
    yield ('<>', 'NOTEQUAL')
back to top