callArgumentsFrom function

String callArgumentsFrom(
  1. String source,
  2. int open
)

The text between the parenthesis that ends at open and its match.

Counts parentheses and steps over string literals, so a ) inside a path does not end the call early. Returns what is left of the source when the call never closes — a syntax error is the analyzer's to report.

Implementation

String callArgumentsFrom(String source, int open) {
  var depth = 1;
  var i = open;
  while (i < source.length && depth > 0) {
    final c = source[i];
    if (c == "'" || c == '"') {
      i++;
      while (i < source.length && source[i] != c) {
        if (source[i] == r'\') i++;
        i++;
      }
    } else if (c == '(') {
      depth++;
    } else if (c == ')') {
      depth--;
    }
    i++;
  }
  return source.substring(open, i > source.length ? source.length : i);
}