reportErrorCommon method

dynamic reportErrorCommon(
  1. dynamic lexerMessage,
  2. dynamic tokenDisplay
)

Implementation

reportErrorCommon(lexerMessage, tokenDisplay) {
  var lines = [];
  lines.add(lexerMessage);
  var lastColumnIndex = this.table.length - 1;
  var lastColumn = this.table[lastColumnIndex];
  var expectantStates = lastColumn!.states.where((state) {
    var nextSymbol = state!.rule.symbols[state.dot] ?? null;
    return nextSymbol != null && !(nextSymbol is String);
  }).toList();

  if (expectantStates.length == 0) {
    lines.add('Unexpected ' +
        tokenDisplay +
        '. I did not expect any more input. Here is the state of my parse table:\n');
    this.displayStateStack(lastColumn.states, lines);
  } else {
    lines.add('Unexpected ' +
        tokenDisplay +
        '. Instead, I was expecting to see one of the following:\n');

    var stateStacks = expectantStates.map((state) {
      return this.buildFirstStateStack(state, []) ?? [state];
    });

    stateStacks.forEach((stateStack) {
      var state = stateStack[0];
      var nextSymbol = state.rule.symbols[state.dot];
      var symbolDisplay = this.getSymbolDisplay(nextSymbol);
      lines.add('A ' + symbolDisplay + ' based on:');
      this.displayStateStack(stateStack, lines);
    });
  }
  lines.add("");
  return lines.join("\n");
}