parseExpression method Null safety

Expression? parseExpression(
  1. int precedence
)

Implementation

Expression? parseExpression(int precedence) {
  // Only consume a token if it could potentially be a prefix parselet

  for (var type in prefixParselets.keys) {
    if (next(type)) {
      var left = prefixParselets[type]?.parse(this, _current);

      while (precedence < _nextPrecedence()) {
        _current = scanner.tokens[++_index];

        if (_current.type == TokenType.slash &&
            peek()?.type == TokenType.gt) {
          // Handle `/>`
          //
          // Don't register this as an infix expression.
          // Instead, backtrack, and return the current expression.
          _index--;
          return left;
        }

        var infix = infixParselets[_current.type]!;
        Expression? newLeft;
        if (left != null) {
          newLeft = infix.parse(this, left, _current);
        }

        if (newLeft == null) {
          if (_current.type == TokenType.gt) _index--;
          return left;
        }
        left = newLeft;
      }

      return left;
    }
  }

  // Nothing was parsed; return null.
  return null;
}