lexer.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:ox 作者: fabiommendes 项目源码 文件源码
def simple_lexer(rules):
    """
    A very simple lexer factory based on a recipe on Python's regex module.
    """

    # This is a simplified version of the techique described at
    # https://docs.python.org/3/library/re.html#writing-a-lexer

    regex = '|'.join(r'(?P<%s>%s)' % item for item in rules)
    regex += r'|(?P<whitespace>\s+)|(?P<error>.+)'
    regex = re.compile(regex)

    def lexer(expr):
        for match in re.finditer(regex, expr):
            typ = match.lastgroup
            value = match.group(typ)

            if typ == 'whitespace':
                continue
            elif typ == 'error':
                raise SyntaxError('invalid value: %r' % value)

            yield Token(typ, value)

    lexer.which = 'simple'
    return lexer
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号