class CmakeOutputParser implements BuildOutputParser {

  @Override
  public boolean parse(String line, BuildOutputInstantReader reader, Consumer<? super BuildEvent> messageConsumer) {
    if (cmakeErrorOrWarning.matcher(line).matches()) {
      List<String> messages = new ArrayList<>();
      messages.add(line.trim());
      String nextLine;
      // stop when nextLine is blank or matches the CMake prefix
      while ((nextLine = reader.readLine()) != null) {
        if (nextLine.isEmpty() || cmakeErrorOrWarning.matcher(nextLine).matches()) {
          reader.pushBack();
          break;
        }
        else {
          nextLine = nextLine.trim();
          if (!nextLine.isEmpty()) {
            messages.add(nextLine);
          }
        }
      }

      if (matchesErrorFileAndLineNumberError(messages, reader.getParentEventId(), messageConsumer) ||
          matchesFileAndLineNumberError(messages, reader.getParentEventId(), messageConsumer)) {
        return true;
      }

      if (messages.get(0).startsWith(CMAKE_ERROR)) {
        messageConsumer.accept(
          new MessageEventImpl(reader.getParentEventId(), MessageEvent.Kind.ERROR, CMAKE + MESSAGE_GROUP_ERROR_SUFFIX, String.join(" ", messages),
                               String.join("\n", messages)));
      }
      else {
        messageConsumer.accept(
          new MessageEventImpl(reader.getParentEventId(), MessageEvent.Kind.WARNING, CMAKE + MESSAGE_GROUP_WARNING_SUFFIX,
                               String.join(" ", messages),
                               String.join("\n", messages)));
      }
      return true;
    }

    return false;
  }

}