Search code examples
c#antlrruleskip

ANTLR rule to skip method body


My task is to create ANTLR grammar, to analyse C# source code files and generate class hierarchy. Then, I will use it to generate class diagram.

I wrote rules to parse namespaces, class declarations and method declarations. Now I have problem with skipping methods bodies. I don't need to parse them, because bodies are useless in my task.

I wrote simple rule:

body:
'{' .* '}'
;

but it does not work properly, when method looks like:

void foo()
{
  ...
  {
    ...
  }
  ...
}

rule matches first brace what is ok, then it matches

... 
{
  ...

as 'any'(.*) and then third brace as final brace, what is not ok, and rule ends.

Anybody could help me to write proper rule for method bodies? As I said before, I don't want to parse them - only to skip.

UPDATE:

here is solution of my problem strongly based on Adam12 answer

body:
'{' ( ~('{' | '}') | body)* '}'
;

Solution

  • You have to use recursive rules that match parentheses pairs.

    rule1 : '(' 
      (
        nestedParan
      | (~')')*
      )
      ')';
    
    nestedParan : '('
      (
        nestedParan
      | (~')')*
      )
      ')';
    

    This code assumes you are using the parser here so strings and comments are already excluded. ANTLR doesn't allow negation of multiple alternatives in parser rules so the code above relies on the fact that alternatives are tried in order. It should give a warning that alternatives 1 and 2 both match '(' and thus choose the first alternative, which is what we want.