parseExprList<T extends ASTNode> method

List<T> parseExprList<T extends ASTNode>({
  1. required String endToken,
  2. bool handleComma = true,
  3. required T? parseFunction(),
})

A functional programming way to parse expression seperated by comma, such as parameter list, argumetn list, list, group... etc. the comma after the last expression is optional. Note that this method will not consume either the start or the end mark.

astListResult is a predefined list, and this function will modify the list in the process.

Implementation

List<T> parseExprList<T extends ASTNode>({
  required String endToken,
  bool handleComma = true,
  required T? Function() parseFunction,
}) {
  final List<T> listResult = [];
  final savedPrecedings = savePrecedings();
  while (curTok.type != endToken && curTok.type != Semantic.endOfFile) {
    // deal with comments or empty liens before spread syntax
    handlePrecedings();
    if (curTok.type == endToken) break;
    final expr = parseFunction();
    if (expr != null) {
      listResult.add(expr);
      handleTrailing(expr,
          handleComma: handleComma, endMarkForCommaExpressions: endToken);
    }
  }
  if (currentPrecedings.isNotEmpty && listResult.isNotEmpty) {
    listResult.last.succeedings = currentPrecedings;
    currentPrecedings = [];
  }
  currentPrecedings = savedPrecedings;
  return listResult;
}