blob: 7c44d8db82322f2c48a7934ad3831450ce57959e [file] [log] [blame]
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
/// This file contains code to generate scanner and parser message
/// based on the information in pkg/front_end/messages.yaml.
///
/// For each message in messages.yaml that contains an 'index:' field,
/// this tool generates an error with the name specified by the 'analyzerCode:'
/// field and an entry in the fastaAnalyzerErrorList for that generated error.
/// The text in the 'analyzerCode:' field must contain the name of the class
/// containing the error and the name of the error separated by a `.`
/// (e.g. ParserErrorCode.EQUALITY_CANNOT_BE_EQUALITY_OPERAND).
///
/// It is expected that 'pkg/front_end/tool/fasta generate-messages'
/// has already been successfully run.
import 'dart:io';
import 'package:_fe_analyzer_shared/src/scanner/scanner.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/src/dart/error/syntactic_errors.dart';
import 'package:analyzer_utilities/package_root.dart' as pkg_root;
import 'package:analyzer_utilities/tools.dart';
import 'package:path/path.dart';
import 'package:yaml/yaml.dart' show loadYaml;
import 'error_code_info.dart';
main() async {
await GeneratedContent.generateAll(analyzerPkgPath, allTargets);
_SyntacticErrorGenerator()
..generateFormatCode()
..checkForManualChanges()
..printSummary();
}
/// A list of all targets generated by this code generator.
final List<GeneratedContent> allTargets = <GeneratedContent>[
GeneratedFile('lib/src/dart/error/syntactic_errors.g.dart',
(String pkgPath) async {
final codeGenerator = _SyntacticErrorGenerator();
codeGenerator.generateFormatCode();
return codeGenerator.out.toString();
}),
];
/// The path to the `analyzer` package.
final String analyzerPkgPath =
normalize(join(pkg_root.packageRoot, 'analyzer'));
class _SyntacticErrorGenerator {
final Map<String, ErrorCodeInfo> messages;
final CfeToAnalyzerErrorCodeTables tables;
final String errorConverterSource;
final String syntacticErrorsSource;
final String parserSource;
final out = StringBuffer('''
//
// THIS FILE IS GENERATED. DO NOT EDIT.
//
// Instead modify 'pkg/front_end/messages.yaml' and run
// 'dart pkg/analyzer/tool/messages/generate.dart' to update.
part of 'syntactic_errors.dart';
''');
factory _SyntacticErrorGenerator() {
String frontEndPkgPath = normalize(join(pkg_root.packageRoot, 'front_end'));
String frontEndSharedPkgPath =
normalize(join(pkg_root.packageRoot, '_fe_analyzer_shared'));
Map<dynamic, dynamic> messagesYaml = loadYaml(
File(join(frontEndPkgPath, 'messages.yaml')).readAsStringSync());
String errorConverterSource = File(join(analyzerPkgPath,
joinAll(posix.split('lib/src/fasta/error_converter.dart'))))
.readAsStringSync();
String syntacticErrorsSource = File(join(analyzerPkgPath,
joinAll(posix.split('lib/src/dart/error/syntactic_errors.dart'))))
.readAsStringSync();
String parserSource = File(join(frontEndSharedPkgPath,
joinAll(posix.split('lib/src/parser/parser.dart'))))
.readAsStringSync();
return _SyntacticErrorGenerator._(decodeCfeMessagesYaml(messagesYaml),
errorConverterSource, syntacticErrorsSource, parserSource);
}
_SyntacticErrorGenerator._(this.messages, this.errorConverterSource,
this.syntacticErrorsSource, this.parserSource)
: tables = CfeToAnalyzerErrorCodeTables(messages);
void checkForManualChanges() {
// Check for ParserErrorCodes that could be removed from
// error_converter.dart now that those ParserErrorCodes are auto generated.
int converterCount = 0;
for (var errorCode in tables.infoToAnalyzerCode.values) {
if (errorConverterSource.contains('"$errorCode"')) {
if (converterCount == 0) {
print('');
print('The following ParserErrorCodes could be removed'
' from error_converter.dart:');
}
print(' $errorCode');
++converterCount;
}
}
if (converterCount > 3) {
print(' $converterCount total');
}
// Check that the public ParserErrorCodes have been updated
// to reference the generated codes.
int publicCount = 0;
for (var errorCode in tables.infoToAnalyzerCode.values) {
if (!syntacticErrorsSource.contains(' _$errorCode')) {
if (publicCount == 0) {
print('');
print('The following ParserErrorCodes should be updated'
' in syntactic_errors.dart');
print('to reference the associated generated error code:');
}
print(' static const ParserErrorCode $errorCode = _$errorCode;');
++publicCount;
}
}
// Fail if there are manual changes to be made, but do so
// in a fire and forget manner so that the files are still generated.
if (converterCount > 0 || publicCount > 0) {
print('');
throw 'Error: missing manual code changes';
}
}
void generateErrorCodes() {
final entryMap = tables.analyzerCodeToInfo;
for (var errorCode in entryMap.keys.toList()..sort()) {
final entry = entryMap[errorCode]!;
final className = 'ParserErrorCode';
out.writeln();
out.writeln('const $className _$errorCode =');
out.writeln(entry.toAnalyzerCode(className, errorCode));
}
}
void generateFastaAnalyzerErrorCodeList() {
out.writeln('final fastaAnalyzerErrorCodes = <ErrorCode?>[');
for (var entry in tables.indexToInfo) {
var name = tables.infoToAnalyzerCode[entry];
out.writeln('${name == null ? 'null' : '_$name'},');
}
out.writeln('];');
}
void generateFormatCode() {
generateFastaAnalyzerErrorCodeList();
generateErrorCodes();
}
void printSummary() {
// Build a map of error message to ParserErrorCode
final messageToName = <String, String>{};
for (ErrorCode errorCode in errorCodeValues) {
if (errorCode is ParserErrorCode) {
String message = errorCode.message.replaceAll(RegExp(r'\{\d+\}'), '');
messageToName[message] = errorCode.name;
}
}
String messageFromEntryTemplate(ErrorCodeInfo entry) {
String template = entry.template!;
String message = template.replaceAll(RegExp(r'#\w+'), '');
return message;
}
// Remove entries that have already been translated
for (ErrorCodeInfo entry in tables.infoToAnalyzerCode.keys) {
messageToName.remove(messageFromEntryTemplate(entry));
}
// Print the # of autogenerated ParserErrorCodes.
print('${tables.infoToAnalyzerCode.length} of '
'${messageToName.length} ParserErrorCodes generated.');
// List the ParserErrorCodes that could easily be auto generated
// but have not been already.
final analyzerToFasta = <String, List<String>>{};
messages.forEach((fastaName, entry) {
final analyzerName = messageToName[messageFromEntryTemplate(entry)];
if (analyzerName != null) {
analyzerToFasta
.putIfAbsent(analyzerName, () => <String>[])
.add(fastaName);
}
});
if (analyzerToFasta.isNotEmpty) {
print('');
print('The following ParserErrorCodes could be auto generated:');
for (String analyzerName in analyzerToFasta.keys.toList()..sort()) {
List<String> fastaNames = analyzerToFasta[analyzerName]!;
if (fastaNames.length == 1) {
print(' $analyzerName = ${fastaNames.first}');
} else {
print(' $analyzerName = $fastaNames');
}
}
if (analyzerToFasta.length > 3) {
print(' ${analyzerToFasta.length} total');
}
}
// List error codes in the parser that have not been translated.
final untranslatedFastaErrorCodes = <String>{};
Token token = scanString(parserSource).tokens;
while (!token.isEof) {
if (token.isIdentifier) {
String? fastaErrorCode;
String lexeme = token.lexeme;
if (lexeme.length > 7) {
if (lexeme.startsWith('message')) {
fastaErrorCode = lexeme.substring(7);
} else if (lexeme.length > 8) {
if (lexeme.startsWith('template')) {
fastaErrorCode = lexeme.substring(8);
}
}
}
if (fastaErrorCode != null &&
tables.frontEndCodeToInfo[fastaErrorCode] == null) {
untranslatedFastaErrorCodes.add(fastaErrorCode);
}
}
token = token.next!;
}
if (untranslatedFastaErrorCodes.isNotEmpty) {
print('');
print('The following error codes in the parser are not auto generated:');
final sorted = untranslatedFastaErrorCodes.toList()..sort();
for (String fastaErrorCode in sorted) {
String analyzerCode = '';
String template = '';
var entry = messages[fastaErrorCode];
if (entry != null) {
// TODO(paulberry): handle multiple analyzer codes
if (entry.index == null && entry.analyzerCode.length == 1) {
analyzerCode = entry.analyzerCode.single;
template = entry.template!;
}
}
print(' ${fastaErrorCode.padRight(30)} --> $analyzerCode'
'\n $template');
}
}
}
}