parseScripts method

ScriptsParsingResults parseScripts({
  1. bool allowLimits = false,
})

The following functions are separated from parseAtoms in KaTeX This function will only be invoked in math mode

Implementation

ScriptsParsingResults parseScripts({bool allowLimits = false}) {
  EquationRowNode? subscript;
  EquationRowNode? superscript;
  bool? limits;
  loop:
  while (true) {
    this.consumeSpaces();
    final lex = this.fetch();
    switch (lex.text) {
      case '\\limits':
      case '\\nolimits':
        if (!allowLimits) {
          throw ParseException(
              'Limit controls must follow a math operator', lex);
        }
        limits = lex.text == '\\limits';
        this.consume();
        break;
      case '^':
        if (superscript != null) {
          throw ParseException('Double superscript', lex);
        }
        superscript = this._handleScript().wrapWithEquationRow();
        break;
      case '_':
        if (subscript != null) {
          throw ParseException('Double subscript', lex);
        }
        subscript = this._handleScript().wrapWithEquationRow();
        break;
      case "'":
        if (superscript != null) {
          throw ParseException('Double superscript', lex);
        }
        final primeCommand = texSymbolCommandConfigs[Mode.math]!['\\prime']!;
        final superscriptList = <GreenNode>[
          SymbolNode(
            mode: mode,
            symbol: primeCommand.symbol,
            variantForm: primeCommand.variantForm,
            overrideAtomType: primeCommand.type,
            overrideFont: primeCommand.font,
          ),
        ];
        this.consume();
        while (this.fetch().text == "'") {
          superscriptList.add(
            SymbolNode(
              mode: mode,
              symbol: primeCommand.symbol,
              variantForm: primeCommand.variantForm,
              overrideAtomType: primeCommand.type,
              overrideFont: primeCommand.font,
            ),
          );
          this.consume();
        }
        if (this.fetch().text == '^') {
          superscriptList.addAll(this._handleScript().expandEquationRow());
        }
        superscript = superscriptList.wrapWithEquationRow();
        break;
      default:
        break loop;
    }
  }
  return ScriptsParsingResults(
    subscript: subscript,
    superscript: superscript,
    limits: limits,
  );
}