blob: 13b8c4be32f57f9b9d8845c1ec4296e926937f74 [file]
// Copyright (c) 2017, 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.
import 'package:_fe_analyzer_shared/src/messages/diagnostic.dart' as diag;
import '../scanner/token.dart';
import 'parser_impl.dart';
class DirectiveContext {
/// `true` if the 'enhanced-parts' feature is enabled.
final bool isEnhancedPartsFeatureEnabled;
DirectiveState state = DirectiveState.Unknown;
/// Whether the file starts with `part of`.
///
/// Unlike [state], which is [DirectiveState.PartOf] only until the next
/// directive or declaration, this stays `true` to the end of the file.
bool _isPart = false;
/// The script tag of the file, until it is reported.
Token? _scriptTag;
new({required this.isEnhancedPartsFeatureEnabled});
void checkScriptTag(Parser parser, Token token) {
if (state == DirectiveState.Unknown && _scriptTag == null) {
_scriptTag = token;
return;
}
// The scanner only produces the SCRIPT_TAG
// when it is the first token in the file.
throw "Internal error: Unexpected script tag.";
}
void checkDeclaration() {
// Without enhanced parts, any directive after `part of` is an error,
// whether or not declarations are between them.
if (state == DirectiveState.PartOf && !isEnhancedPartsFeatureEnabled) {
return;
}
state = DirectiveState.Declarations;
}
void checkExport(Parser parser, Token token) {
switch (state) {
case DirectiveState.Unknown:
case DirectiveState.Library:
case DirectiveState.ImportAndExport:
state = DirectiveState.ImportAndExport;
case DirectiveState.Part:
parser.reportRecoverableError(token, diag.exportAfterPart);
case DirectiveState.PartOf:
if (isEnhancedPartsFeatureEnabled) {
state = DirectiveState.ImportAndExport;
} else {
parser.reportRecoverableError(token, diag.nonPartOfDirectiveInPart);
}
case DirectiveState.Declarations:
parser.reportRecoverableError(token, diag.directiveAfterDeclaration);
}
}
void checkImport(Parser parser, Token token) {
switch (state) {
case DirectiveState.Unknown:
case DirectiveState.Library:
case DirectiveState.ImportAndExport:
state = DirectiveState.ImportAndExport;
case DirectiveState.Part:
parser.reportRecoverableError(token, diag.importAfterPart);
case DirectiveState.PartOf:
if (isEnhancedPartsFeatureEnabled) {
state = DirectiveState.ImportAndExport;
} else {
parser.reportRecoverableError(token, diag.nonPartOfDirectiveInPart);
}
case DirectiveState.Declarations:
parser.reportRecoverableError(token, diag.directiveAfterDeclaration);
}
}
void checkLibrary(Parser parser, Token token) {
if (state.index < DirectiveState.Library.index) {
state = DirectiveState.Library;
return;
}
// Recovery
if (_isPart) {
parser.reportRecoverableError(token, diag.partWithLibraryDirective);
} else if (state == DirectiveState.Library) {
parser.reportRecoverableError(token, diag.multipleLibraryDirectives);
} else {
parser.reportRecoverableError(token, diag.libraryDirectiveNotFirst);
}
}
void checkPart(Parser parser, Token token) {
switch (state) {
case DirectiveState.Unknown:
case DirectiveState.Library:
case DirectiveState.ImportAndExport:
case DirectiveState.Part:
state = DirectiveState.Part;
case DirectiveState.PartOf:
if (isEnhancedPartsFeatureEnabled) {
state = DirectiveState.Part;
} else {
parser.reportRecoverableError(token, diag.nonPartOfDirectiveInPart);
}
case DirectiveState.Declarations:
parser.reportRecoverableError(token, diag.directiveAfterDeclaration);
}
}
void checkPartOf(Parser parser, Token token) {
if (_scriptTag case Token scriptTag?) {
_scriptTag = null;
parser.reportRecoverableError(scriptTag, diag.scriptTagInPartFile);
}
if (state == DirectiveState.Unknown) {
state = DirectiveState.PartOf;
_isPart = true;
return;
}
// Recovery
if (_isPart) {
parser.reportRecoverableError(token, diag.partOfTwice);
} else if (state == DirectiveState.Declarations) {
parser.reportRecoverableError(token, diag.directiveAfterDeclaration);
} else if (isEnhancedPartsFeatureEnabled) {
parser.reportRecoverableError(token, diag.partOfDirectiveNotFirst);
} else {
// Without enhanced parts, moving the part-of directive first is not
// enough, because a part can't have any other directives.
parser.reportRecoverableError(token, diag.nonPartOfDirectiveInPart);
}
}
@override
String toString() => 'DirectiveContext(${state})';
}
enum DirectiveState {
Unknown,
Library,
ImportAndExport,
Part,
PartOf,
Declarations,
}