I'm parsing a language that has a statement 'code' followed by '{', followed by a bunch of code that I have no interest in parsing, followed by '}'. I'd ideally like to have a rule like:
skip_code: 'code' '{' ~['}']* '}'
..which would simply skip ahead to the closing curly brace. The problem is that the code being skipped could itself have pairs of curly braces. So, what I essentially need to do is run a counter and increment on each '{' and decrement on each '}', and end the parse rule when the counter is back to 0.
What's the best way of doing this in ANTLR4? Should I skip off to a custom function when 'code' is detected and swallow up the tokens and run my counter, or is there some elegant way to express this in the grammar itself?
EDIT: Some sample code, as requested:
class foo;
int m_bar;
function foo_bar;
print("hello world");
endfunction
code {
// This is some C code
void my_c_func() {
printf("I have curly braces {} in a string!");
}
}
function back_to_parsed_code;
endfunction
endclass