parse method

ParseResult parse()

Parses the whole input.

Implementation

ParseResult parse() {
  _clock.start();
  if (!options.globalGroup) _macros.beginGroup();
  final doubled = _doubled;
  if (doubled != null) {
    // Leniency: source escaped twice (a JSON string encoded again) has
    // `\\frac` for `\frac`; it is read as written once.
    _report(
      Diagnostic(
        code: 'doubledBackslashes',
        message:
            r'Every backslash is doubled (\\frac); read as single '
            r'backslashes',
        span: doubled,
        severity: Severity.warning,
      ),
    );
  }
  RowNode root;
  try {
    final RowNode row;
    if (_implicitAlignment) {
      row = _alignedRoot();
    } else {
      (row, _) = _parseRow(const _Stops());
    }
    final limit = _unshownLimit;
    // A limit hit outside any group still shows where the formula
    // stopped, as one hit inside a group does.
    root = limit == null
        ? row
        : RowNode(row.span, [
            ...row.nodes,
            ErrorNode(limit.span, limit.code, limit.message, ''),
          ]);
  } finally {
    if (!options.globalGroup) _macros.endGroup();
    _clock.stop();
  }
  if (_wantsAlignment) {
    // Leniency: `a &= b \\ &= c` with no environment is laid out as
    // the rows of `aligned` (LaTeX: "Misplaced alignment tab").
    return (LatexParser(
      source.original,
      options: options,
      macros: _sharedMacros,
    ).._implicitAlignment = true).parse();
  }
  final result = ParseResult(
    source: source,
    root: root,
    diagnostics: List.unmodifiable(_diagnostics),
    tag: _tag,
    label: _label,
    noNumber: _noNumber,
    incomplete: _sawIncomplete || _x.hitEndOfInput,
    tagForm: _tagForm,
  );
  if (options.throwOnError) {
    for (final d in _diagnostics) {
      if (d.severity == Severity.error) throw LatexParseException(d);
    }
  }
  return result;
}