function parseStatements()

in packages/shell-parser/src/parser.ts [350:402]


function parseStatements(
  str: string,
  index: number,
  terminalChar: string,
  mustTerminate = false,
): {
  statements: BaseNode[];
  terminatorIndex: number;
} {
  const statements = [];

  let i = index;
  while (i < str.length) {
    // Will only exit on EOF, terminalChar or terminator symbol (;, &, &;)
    let statement = parseStatement(str, i, mustTerminate ? "" : terminalChar);

    const opIndex = nextWordIndex(str, statement.endIndex);
    const reachedEnd = opIndex === -1;
    if (!mustTerminate && !reachedEnd && terminalChar === str.charAt(opIndex)) {
      statements.push(statement);
      return { statements, terminatorIndex: opIndex };
    }

    if (reachedEnd) {
      statements.push(statement);
      break;
    }

    const op = !reachedEnd && parseOperator(str, opIndex);
    if (op) {
      // Terminator symbol, ; | & | &;
      i = opIndex + op.length;
      const nextIndex = nextWordIndex(str, i);
      statements.push(statement);
      if (nextIndex !== -1 && str.charAt(nextIndex) === terminalChar) {
        return { statements, terminatorIndex: nextIndex };
      }
    } else {
      // Missing terminator but still have tokens left.
      // assignments do not require terminators
      statement = createNode(str, {
        ...statement,
        complete:
          statement.type === NodeType.AssignmentList
            ? statement.complete
            : false,
      });
      statements.push(statement);
      i = opIndex;
    }
  }
  return { statements, terminatorIndex: -1 };
}