parseExpression method

List<GreenNode> parseExpression({
  1. bool breakOnInfix = false,
  2. String? breakOnTokenText,
  3. bool infixArgumentMode = false,
})

Implementation

List<GreenNode> parseExpression({
  bool breakOnInfix = false,
  String? breakOnTokenText,
  bool infixArgumentMode = false,
}) {
  final body = <GreenNode>[];
  while (true) {
    if (this.mode == Mode.math) {
      this.consumeSpaces();
    }
    final lex = this.fetch();
    if (endOfExpression.contains(lex.text)) {
      break;
    }
    if (breakOnTokenText != null && lex.text == breakOnTokenText) {
      break;
    }
    // Detects a infix function
    final funcData = functions[lex.text];
    if (funcData != null && funcData.infix == true) {
      if (infixArgumentMode) {
        throw ParseException('only one infix operator per group', lex);
      }
      if (breakOnInfix) {
        break;
      }
      this.consume();
      _enterArgumentParsingMode(lex.text, funcData);
      try {
        // A new way to handle infix operations
        final atom = funcData.handler(
          this,
          FunctionContext(
            funcName: lex.text,
            breakOnTokenText: breakOnTokenText,
            token: lex,
            infixExistingArguments: List.of(body, growable: false),
          ),
        );
        body.clear();
        body.add(atom);
      } finally {
        _leaveArgumentParsingMode(lex.text);
      }
    } else {
      // Add a normal atom
      final atom = this.parseAtom(breakOnTokenText);
      if (atom == null) {
        break;
      }
      body.add(atom);
    }
  }

  return body;
  // We will NOT handle ligatures between '-' and "'", as neither did MathJax.
  // if (this.mode == Mode.text) {
  //   formLigatures(body);
  // }
  // We will not handle infix as well
  // return handleInfixNodes(body);
}