messageFromError function

String messageFromError(
  1. PintoError error,
  2. String source
)

Implementation

String messageFromError(PintoError error, String source) {
  final offset = switch (error) {
    LexingError() => error.offset,
    ParseError(:final syntacticEntity) || ResolveError(syntacticEntity: final syntacticEntity) => syntacticEntity.offset,
  };

  final end = switch (error) {
    LexingError() => offset + 1,
    ParseError(:final syntacticEntity) || ResolveError(syntacticEntity: final syntacticEntity) => syntacticEntity.end,
  };

  final fragment = source.substring(offset, end);

  return switch (error) {
    // Parse errors
    ExpectError error => expectError('${error.expectation}', fragment),
    ExpectAfterError error => expectAfterError('${error.expectation}', '${error.after}', fragment),
    ExpectBeforeError error => expectBeforeError('${error.expectation}', '${error.before}', fragment),

    // Resolve errors
    ImportedPackageNotAvailableError() => importedPackageNotAvailableError(fragment),
    IdentifierAlreadyDefinedError() => identifierAlreadyDefinedError(fragment),
    NotAFunctionError() => notAFunctionError(fragment),
    SymbolNotInScopeError() => symbolNotInScopeError(fragment),
    TypeParameterAlreadyDefinedError() => typeParameterAlreadyDefinedError(fragment),
    WrongNumberOfArgumentsError error => wrongNumberOfArgumentsError(error.argumentsCount, error.expectedArgumentsCount, fragment),

    // Scan errors
    NumberEndingWithSeparatorError() => numberEndingWithSeparatorError(),
    UnexpectedCharacterError() => unexpectedCharacterError(fragment),
    UnterminatedStringError() => unterminatedStringError(),
  };
}