|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:_fe_analyzer_shared/src/messages/codes.dart'; |
| 6 | +import 'package:_fe_analyzer_shared/src/scanner/error_token.dart'; |
| 7 | +import 'package:_fe_analyzer_shared/src/scanner/token.dart' |
| 8 | + show Token, TokenType; |
| 9 | +import 'package:_fe_analyzer_shared/src/scanner/token_constants.dart'; |
| 10 | +import 'package:analyzer/src/dart/scanner/scanner.dart'; |
| 11 | + |
| 12 | +/// Translates the given error [token] into an analyzer error and reports it |
| 13 | +/// using [reportError]. |
| 14 | +void translateErrorToken(ErrorToken token, ReportError reportError) { |
| 15 | + int charOffset = token.charOffset; |
| 16 | + // TODO(paulberry): why is endOffset sometimes null? |
| 17 | + int endOffset = token.endOffset ?? charOffset; |
| 18 | + void makeError(ScannerErrorCode errorCode, List<Object>? arguments) { |
| 19 | + if (_isAtEnd(token, charOffset)) { |
| 20 | + // Analyzer never generates an error message past the end of the input, |
| 21 | + // since such an error would not be visible in an editor. |
| 22 | + // TODO(paulberry): would it make sense to replicate this behavior |
| 23 | + // in cfe, or move it elsewhere in analyzer? |
| 24 | + charOffset--; |
| 25 | + } |
| 26 | + reportError(errorCode, charOffset, arguments); |
| 27 | + } |
| 28 | + |
| 29 | + Code errorCode = token.errorCode; |
| 30 | + switch (errorCode.pseudoSharedCode) { |
| 31 | + case PseudoSharedCode.encoding: |
| 32 | + reportError(ScannerErrorCode.encoding, charOffset, null); |
| 33 | + return; |
| 34 | + |
| 35 | + case PseudoSharedCode.unterminatedStringLiteral: |
| 36 | + // TODO(paulberry): Fasta reports the error location as the entire |
| 37 | + // string; analyzer expects the end of the string. |
| 38 | + reportError( |
| 39 | + ScannerErrorCode.unterminatedStringLiteral, |
| 40 | + endOffset - 1, |
| 41 | + null, |
| 42 | + ); |
| 43 | + return; |
| 44 | + |
| 45 | + case PseudoSharedCode.unterminatedMultiLineComment: |
| 46 | + // TODO(paulberry): Fasta reports the error location as the entire |
| 47 | + // comment; analyzer expects the end of the comment. |
| 48 | + reportError( |
| 49 | + ScannerErrorCode.unterminatedMultiLineComment, |
| 50 | + endOffset - 1, |
| 51 | + null, |
| 52 | + ); |
| 53 | + return; |
| 54 | + |
| 55 | + case PseudoSharedCode.missingDigit: |
| 56 | + // TODO(paulberry): Fasta reports the error location as the entire |
| 57 | + // number; analyzer expects the end of the number. |
| 58 | + charOffset = endOffset - 1; |
| 59 | + return makeError(ScannerErrorCode.missingDigit, null); |
| 60 | + |
| 61 | + case PseudoSharedCode.missingHexDigit: |
| 62 | + // TODO(paulberry): Fasta reports the error location as the entire |
| 63 | + // number; analyzer expects the end of the number. |
| 64 | + charOffset = endOffset - 1; |
| 65 | + return makeError(ScannerErrorCode.missingHexDigit, null); |
| 66 | + |
| 67 | + case PseudoSharedCode.illegalCharacter: |
| 68 | + // We can safely assume `token.character` is non-`null` because this error |
| 69 | + // is only reported when there is a character associated with the token. |
| 70 | + return makeError(ScannerErrorCode.illegalCharacter, [token.character!]); |
| 71 | + |
| 72 | + case PseudoSharedCode.unexpectedSeparatorInNumber: |
| 73 | + return makeError(ScannerErrorCode.unexpectedSeparatorInNumber, null); |
| 74 | + |
| 75 | + case PseudoSharedCode.unsupportedOperator: |
| 76 | + return makeError(ScannerErrorCode.unsupportedOperator, [ |
| 77 | + (token as UnsupportedOperator).token.lexeme, |
| 78 | + ]); |
| 79 | + |
| 80 | + default: |
| 81 | + if (errorCode == codeUnmatchedToken) { |
| 82 | + charOffset = token.begin!.endToken!.charOffset; |
| 83 | + TokenType type = token.begin!.type; |
| 84 | + if (type == TokenType.OPEN_CURLY_BRACKET || |
| 85 | + type == TokenType.STRING_INTERPOLATION_EXPRESSION) { |
| 86 | + return makeError(ScannerErrorCode.expectedToken, ['}']); |
| 87 | + } |
| 88 | + if (type == TokenType.OPEN_SQUARE_BRACKET) { |
| 89 | + return makeError(ScannerErrorCode.expectedToken, [']']); |
| 90 | + } |
| 91 | + if (type == TokenType.OPEN_PAREN) { |
| 92 | + return makeError(ScannerErrorCode.expectedToken, [')']); |
| 93 | + } |
| 94 | + if (type == TokenType.LT) { |
| 95 | + return makeError(ScannerErrorCode.expectedToken, ['>']); |
| 96 | + } |
| 97 | + } else if (errorCode == codeUnexpectedDollarInString) { |
| 98 | + return makeError(ScannerErrorCode.missingIdentifier, null); |
| 99 | + } |
| 100 | + throw UnimplementedError('$errorCode "${errorCode.pseudoSharedCode}"'); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/// Determines whether the given [charOffset], which came from the non-EOF token |
| 105 | +/// [token], represents the end of the input. |
| 106 | +bool _isAtEnd(Token token, int charOffset) { |
| 107 | + while (true) { |
| 108 | + // Skip to the next token. |
| 109 | + token = token.next!; |
| 110 | + // If we've found an EOF token, its charOffset indicates where the end of |
| 111 | + // the input is. |
| 112 | + if (token.isEof) return token.charOffset == charOffset; |
| 113 | + // If we've found a non-error token, then we know there is additional input |
| 114 | + // text after [charOffset]. |
| 115 | + if (token.type.kind != BAD_INPUT_TOKEN) return false; |
| 116 | + // Otherwise keep looking. |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/// Used to report a scan error at the given offset. |
| 121 | +/// The [errorCode] is the error code indicating the nature of the error. |
| 122 | +/// The [arguments] are any arguments needed to complete the error message. |
| 123 | +typedef ReportError = |
| 124 | + void Function( |
| 125 | + ScannerErrorCode errorCode, |
| 126 | + int offset, |
| 127 | + List<Object>? arguments, |
| 128 | + ); |
0 commit comments