[Python] Tatsu parsing – how to skip until a specific word using a rule?

Discussão em 'Python' iniciado por Stack, Agosto 4, 2025.

  1. Stack

    Stack Membro Participativo

    Is there an elegant way to skip input until a specific word using TatSu, without relying on raw regex?

    For example, I can do:

    start=/(.|\n)*?John/


    But I'd like to write it using proper grammar rules, something like:

    start = skip_until('John').


    I tried defining rules, but couldn't make it work. I want to match multiple names (e.g., "John", "Anna", etc.) that may appear after arbitrary text (including newlines).

    Here is the code I want to improve.
    Is there a more modular way to do this in TatSu?

    from tatsu import compile

    grammar = '''
    start = /(.|\n)*?John/ ;
    '''

    model = compile(grammar)
    text = """Some random text
    on multiple lines
    before John."""

    ast = model.parse(text)
    print(ast)

    Continue reading...

Compartilhe esta Página