Jakob Sachs
Jakob Sachs

Reputation: 715

How do i get all occurrences of my parser rule in ANTLRs java runtime?

I'm working at a code-formatter in Java for my own ANTLR Grammar and now want to work on formatting one special rule.

What I want, is to somehow iterate over all occurrences of one of my parser rules (if_stmt to be specific). I'm working with ANTLR4 and the Java runtime.


This is the smallest version of my grammar for this issue:

stmt_block
:
    EOF
    | stmt_list EOF
;

stmt_list
:
    stmt
    | stmt_list stmt
;

stmt
:

    if_stmt
    |  RETURN SEMICOLON NEWLINE?
;

if_stmt
:
    IF BRACKET_OPEN expr BRACKET_CLOSED CBRACKET_OPEN stmt_list CBRACKET_CLOSED
;

expr
:
    TRUE
;

This is how my format function looks so far

private String formatCode(String input) 
{
    // Parse the input
    MyLexer lex = new MyLexer(new CaseChangingCharStream(
        CharStreams.fromString(input), true));

    CommonTokenStream tokens = new CommonTokenStream(lex);
    tokens.fill();
    MyParser pars = new MyParser(tokens);
    Stmt_blockContext stmt_blockContext = pars.stmt_block();

    stmt_blockContext.children.forEach(parseTree->{
        //Iterate over the parseTree to get all if_stmtContext's and modify them
    });


    // Then return the modified contexts text
    return stmt_blockContext.getText();
}

This is how I've gotten so far, and I know I'm asking for someone to just solve my problem, but I am stuck at this and have no Idea how to proceed.

Any help is appreciated!

Upvotes: 0

Views: 342

Answers (1)

Mike Lischke
Mike Lischke

Reputation: 53307

You cannot use only a parser for any formatting task, unless you want it to be able to work only with valid input. Formatting is mostly rewriting whitespaces, for which a lexer is much better suited. See my ANTLR4 plugin for Visual Studio Code for an example how to implement a code formatter. This one is for ANTLR4 grammars, but the same principles apply for any other programming language.

Upvotes: 1

Related Questions