getResult method

ParseResult getResult({
  1. bool complete = false,
})

Implementation

ParseResult getResult({bool complete = false}) {
  final next = preprocess(_buf);
  if (!next.startsWith(_cleaned.substring(0, _completedEnd))) {
    _completedEnd = 0;
    _completed.clear();
    _completedCount = 0;
  }
  _cleaned = next;
  final pendingStart = _scanCompleted();
  final pendingText = _cleaned.substring(pendingStart).trim();
  // A trimmed text never ends right after a completed statement.
  if (pendingText.isEmpty) return ParseResult.empty();
  final closed = autoClose(pendingText);
  final stmts = split(tokenize(closed.text));
  if (stmts.isEmpty) {
    if (_completedCount == 0) {
      return ParseResult.empty(incomplete: closed.wasIncomplete);
    }
    return _buildResult(
      {..._completed},
      closed.wasIncomplete,
      _completedCount,
      schema,
      rootName,
      boundNames: boundNames,
    );
  }
  final all = {..._completed};
  final pending = Set<Statement>.identity();
  for (final s in stmts) {
    if (_completed.containsKey(s.id) && closed.wasIncomplete) continue;
    final stmt = classify(s, parseExpression(s.tokens));
    all[s.id] = stmt;
    pending.add(stmt);
  }
  String? tailName;
  if (!complete) {
    // Only the last `$` can start a match, so match there, not from the start.
    final at = _buf.lastIndexOf(r'$');
    final tail = at < 0
        ? null
        : RegExp(r'\$\w+$').matchAsPrefix(_buf, at)?.group(0);
    if (tail != null &&
        pendingText.endsWith(tail) &&
        !RegExp('${RegExp.escape(tail)}(?!\\w)').hasMatch(
          pendingText.substring(0, pendingText.length - tail.length),
        )) {
      tailName = tail;
    }
  }
  return _buildResult(
    all,
    closed.wasIncomplete,
    _completedCount + stmts.length,
    schema,
    rootName,
    partialRef: tailName == null ? null : (name: tailName, stmts: pending),
    boundNames: boundNames,
  );
}