I have a source code which frequently includes a piece of code like
foo
(
    bar
    (
        foo0(<An arbitrary number of parenthesis may appear here>)
    ),
    foo1bar(<An arbitrary number of parenthesis may appear here>)
)
I want to capture this piece; the way that I am going for is
grep -A15 -E "foo[[:space:]]*$" <file_name>
to make sure that enough lines after foo are captured.
However, a more accurate way is looking for a pattern which counts opened/closed parenthesis after foo in order to stop searching right after the matching closed parenthesis of foo is found.
Is it possible to avoid scripting this algorithm by using grep options?
Example
My file is
...
foo
(
    bar
    (
        a(b)
    ),
    c(d)
)
...
dummy
(
    nextDummy()
)
...
where ... represents lines of code which does not contain any ( or ) character.The expected output of grep is
foo
(
    bar
    (
        a(b)
    ),
    c(d)
)
dummy
(
    nextDummy()
)