title: return count of top level parameters in method invocation string
body: <p>Lets say I have a string that holds a method invocation for some c-ish language:</p>
  
  <p><code>var codeLine = 'method1(var1, var2, method2(var3, method3(var4, var5)),var6);';</code></p>
  
  <p>What is the easiest way to count the number of parameters being passed into the top level function (<code>method1</code>)? </p>
  
  <p>In the example above, I would be looking for the result of 4.  </p>
  
  <p>I also need this solution to function properly if the method is not complete (imagine it's being typed):</p>
  
  <p><code>method1(var1, var2, method2(var3, meth</code></p>
  
  <p>Would return 3.</p>
  
  <p>I understand there the 'proper' way to do this would be to parse the language using a tool like <a href="https://github.com/antlr/antlr4" rel="nofollow noreferrer">antlr</a>, but I'm hoping there is a quick and easy solution for this.</p>
  
  <p><em>If your curious, this is for a vscode extension that's providing signature help support</em></p>
  
  <p><strong>Update:</strong></p>
  
  <p>Took a stab at it... </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function findTopLevelParamCount(s){
      //trim top level function away
      s = s.substr(s.indexOf('(')+1,s.lastIndexOf(')')-s.indexOf('(')-1);
      console.log(s);
      while(s.indexOf('(') &gt;= 0){
         s = s.substr(0,s.indexOf('(')) + (s.indexOf(')')==-1?'':s.substr(s.lastIndexOf(')')+1));
         console.log(s);
      }
      return s.split(',').length;
  }
  
  var code = 'method1(var1, var2, method2(var3, method3(var4, var5)), var6);';
  console.log(findTopLevelParamCount(code));</code></pre>
  </div>
  </div>
  </p>
  
  <p>Doesn't work if the signature isn't complete.  Also breaks with parethsis groupings like <code>method1(var1, (var1-va2))</code>. </p>