Finish lazy parentheses The parentheses on my keyboard are all worn out, and I want to avoid using them as much as possible. Your challenge is to balance a line containing parentheses by adding them in before and after each line.

This is similar to TI-Basic's automatic parentheses and string closure (i.e. Output(1, 1, "Hello, World!). It also saves precious bytes from a program!

Example input:

This line has no parentheses
alert(Math.max(1, 2
1+1)*2).toString()
function() { alert('Hello, World!'); })(


Example (possible) output:

This line has no parentheses
alert(Math.max(1, 2))
((1+1)*2).toString()
(function() { alert('Hello, World!'); })()


Specification:


For each line of input,


Add as many open parentheses to the beginning and close parentheses to the end of the line as needed to balance the parentheses in the line


The definition of "balance" is:


Same amount of ( and ) in the line
For every substring starting from the beginning of the string, this substring must not have more closing parentheses than opening parentheses


For example, (foo))(bar is not balanced because (foo)) has more closing parentheses than opening parentheses



You may add extra unnecessary parentheses if you want to, if it makes your code shorter
You do not need to worry about string literals or anything like that, assume that all parentheses need balancing

Output each line with parentheses balanced


This is code-golf, so the shortest code in bytes will win!
GolfScript, 23 bytes

n/{"()"1/{.2$\-,*}%*n}/


The loophole I'm exploiting is the ruling that:


  You may add extra unnecessary parentheses if you want to, if it makes your code shorter


Basically, for each line, this code counts the number of characters on the line that are not opening parentheses, and prepends that many extra opening parentheses to the line, and then does the same for closing parentheses.  This is incredibly inefficient, but does ensure that all the parentheses on the output line are balanced.

For example, given the input:

This line has no parentheses
alert(Math.max(1, 2
1+1)*2).toString()
function() { alert('Hello, World!'); })(


this program will output:

((((((((((((((((((((((((((((This line has no parentheses))))))))))))))))))))))))))))
(((((((((((((((((alert(Math.max(1, 2)))))))))))))))))))
(((((((((((((((((1+1)*2).toString())))))))))))))))
(((((((((((((((((((((((((((((((((((((function() { alert('Hello, World!'); })()))))))))))))))))))))))))))))))))))))


Ps. You can also test this code online.