Skip to content

Commit

Permalink
Separate parsing and error message output for annotation values
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdell committed Feb 13, 2023
1 parent c013951 commit 5839427
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,23 @@

package com.wudsn.ide.lng;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;

import com.wudsn.ide.base.common.MarkerUtility;
import com.wudsn.ide.base.common.StringUtility;
import com.wudsn.ide.base.common.TextUtility;
import com.wudsn.ide.lng.compiler.CompilerFiles.SourceFile;

public final class LanguageAnnotationValues {

Expand All @@ -41,11 +48,13 @@ public static final class LanguageAnnotationValue {
public final String key;
public final String value;
public final int lineNumber;
public final List<IStatus> statusList;

LanguageAnnotationValue(String key, String value, int lineNumber) {
this.key = key;
this.value = value;
this.lineNumber = lineNumber;
this.statusList = new ArrayList<IStatus>(1);
}

public boolean equals(Object other) {
Expand Down Expand Up @@ -185,6 +194,8 @@ public static LanguageAnnotationValues parseDocument(IDocument document) {
index = getMinIndex(content.indexOf(LanguageAnnotation.PREFIX, indexNewLine),
content.indexOf(LanguageAnnotation.OLD_PREFIX, indexNewLine));
}

checkAnnotations(result);
return result;
}

Expand All @@ -205,4 +216,32 @@ private static int getMinIndex(int index1, int index2) {
}
return Math.min(index1, index2);
}

private static void checkAnnotations(LanguageAnnotationValues annotationValues) {
if (annotationValues == null) {
throw new IllegalArgumentException("Parameter 'annotationValues' must not be null.");
}

var annotations = LanguageAnnotation.getAnnotations();
for (String key : annotationValues.keySet()) {
var value = annotationValues.get(key);
if (key.startsWith(LanguageAnnotation.OLD_PREFIX)) {
var newKey = LanguageAnnotation.PREFIX + key.substring(LanguageAnnotation.OLD_PREFIX.length());
// WARNING: Use annotation '{0}' instead of the deprecated annotation '{1}'.
value.statusList.add(new Status(IStatus.WARNING, LanguagePlugin.ID,
TextUtility.format(Texts.MESSAGE_W144, new String[] { newKey, key })));
// New annotations take precedence if they are already present.
if (!annotationValues.keySet().contains(newKey)) {
annotationValues.put(newKey, value.value, value.lineNumber);
}
key = newKey;
}
if (!annotations.contains(key)) {
// ERROR: Annotation '{0}' is unknown.
value.statusList.add(new Status(IStatus.WARNING, LanguagePlugin.ID,
TextUtility.format(Texts.MESSAGE_E145, new String[] { key })));
}
}

}
}
32 changes: 16 additions & 16 deletions com.wudsn.ide.lng/src/com/wudsn/ide/lng/compiler/CompilerFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,21 @@ public final class SourceFile {
*/
public final SourceFile mainSourceFile;

public final LanguageAnnotationValue outputFolderModeProperty;
public final LanguageAnnotationValue outputFolderModeAnnotationValue;
public final String outputFolderMode;

public final LanguageAnnotationValue outputFolderProperty;
public final LanguageAnnotationValue outputFolderAnnotationValue;
public final File outputFolder;
public final String outputFolderPath;

public final File outputFile;
public final String outputFilePath;
public final String outputFilePathWithoutExtension;

public final LanguageAnnotationValue outputFileProperty;
public final LanguageAnnotationValue outputFileAnnotationValue;
public final String outputFileName;
public final String outputFileNameWithoutExtension;
public final LanguageAnnotationValue outputFileExtensionProperty;
public final LanguageAnnotationValue outputFileExtensionAnnotationValue;
public final String outputFileExtension;
public final String outputFileNameShortWithoutExtension;

Expand Down Expand Up @@ -138,20 +138,20 @@ public CompilerFiles(IFile mainSourceIFile, LanguageAnnotationValues mainSourceF
String localOutputFileExtension = languageHardwareCompilerDefinitionPreferences.getOutputFileExtension();

// Properties which override the preferences
outputFolderModeProperty = mainSourceFileLanguageProperties.get(LanguageAnnotation.OUTPUT_FOLDER_MODE);
outputFolderProperty = mainSourceFileLanguageProperties.get(LanguageAnnotation.OUTPUT_FOLDER);
outputFileExtensionProperty = mainSourceFileLanguageProperties.get(LanguageAnnotation.OUTPUT_FILE_EXTENSION);
outputFileProperty = mainSourceFileLanguageProperties.get(LanguageAnnotation.OUTPUT_FILE);
outputFolderModeAnnotationValue = mainSourceFileLanguageProperties.get(LanguageAnnotation.OUTPUT_FOLDER_MODE);
outputFolderAnnotationValue = mainSourceFileLanguageProperties.get(LanguageAnnotation.OUTPUT_FOLDER);
outputFileExtensionAnnotationValue = mainSourceFileLanguageProperties.get(LanguageAnnotation.OUTPUT_FILE_EXTENSION);
outputFileAnnotationValue = mainSourceFileLanguageProperties.get(LanguageAnnotation.OUTPUT_FILE);

// The following sequence sets the instance fields "outputFolder" and
// "outputFileNameWithoutExtension" as well as the
// "outputFileNameWithoutExtension".
// If the output file is specified explicitly, it overrides all output
// properties.
if (outputFileProperty != null) {
if (outputFileAnnotationValue != null) {

// Make the file an absolute file.
File file = new File(outputFileProperty.value);
File file = new File(outputFileAnnotationValue.value);
if (!file.isAbsolute()) {
file = new File(mainSourceFile.file.getParentFile(), file.getPath());
}
Expand All @@ -172,21 +172,21 @@ public CompilerFiles(IFile mainSourceIFile, LanguageAnnotationValues mainSourceF
}
} else {
// The output file extension is independent of the rest.
if (outputFileExtensionProperty != null) {
localOutputFileExtension = outputFileExtensionProperty.value;
if (outputFileExtensionAnnotationValue != null) {
localOutputFileExtension = outputFileExtensionAnnotationValue.value;
}
// If the output folder mode is specified explicitly, it overrides
// the output
// folder mode preferences.
if (outputFolderModeProperty != null) {
localOutputFolderMode = outputFolderModeProperty.value;
if (outputFolderModeAnnotationValue != null) {
localOutputFolderMode = outputFolderModeAnnotationValue.value;
}

// If the output folder is specified explicitly, it overrides the
// output folder mode and folder preferences.
if (outputFolderProperty != null) {
if (outputFolderAnnotationValue != null) {
localOutputFolderMode = CompilerOutputFolderMode.FIXED_FOLDER;
localOutputFolderPath = outputFolderProperty.value;
localOutputFolderPath = outputFolderAnnotationValue.value;
}

if (localOutputFolderMode.equals(CompilerOutputFolderMode.SOURCE_FOLDER)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public static LanguageAnnotationValues getAnnotationValues(IDocument document) {
throw new IllegalArgumentException("Parameter 'document' must not be null.");
}
var result = LanguageAnnotationValues.parseDocument(document);
System.out.println("TEST:" + result);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IBreakpointManager;
import org.eclipse.debug.core.model.IBreakpoint;
Expand Down Expand Up @@ -418,27 +419,34 @@ private boolean executeInternal(LanguageEditor languageEditor, CompilerFiles fil
return true;
}

/**
* Check the status message attached to annotation values and create markers for
* them.
*
* @param sourceFile The source file, not <code>null</code>.
*/
private void checkAnnotations(SourceFile sourceFile) {
if (sourceFile == null) {
throw new IllegalArgumentException("Parameter 'sourceFile' must not be null.");
}

var annotationValues = sourceFile.languageAnnotationValues;
var annotations = LanguageAnnotation.getAnnotations();
for (String key : annotationValues.keySet()) {
var value = annotationValues.get(key);
if (key.startsWith(LanguageAnnotation.OLD_PREFIX)) {
var newKey = LanguageAnnotation.PREFIX + key.substring(LanguageAnnotation.OLD_PREFIX.length());
// WARNING: Use annotation '{0}' instead of the deprecated annotation '{1}'.
MarkerUtility.createMarker(sourceFile.iFile, value.lineNumber, IMarker.SEVERITY_WARNING,
Texts.MESSAGE_W144, new String[] { newKey, key });
annotationValues.put(newKey, value.value, value.lineNumber);
key = newKey;
}
if (!annotations.contains(key)) {
// ERROR: Annotation '{0}' is unknown.
MarkerUtility.createMarker(sourceFile.iFile, value.lineNumber, IMarker.SEVERITY_WARNING,
Texts.MESSAGE_E145, new String[] { key });
int markerSeverity;
for (var status : value.statusList) {
switch (status.getSeverity()) {
case IStatus.WARNING:
markerSeverity = IMarker.SEVERITY_WARNING;
break;
case IStatus.ERROR:
markerSeverity = IMarker.SEVERITY_ERROR;
break;
default:
throw new IllegalStateException("Unsupported severity " + status.getSeverity());
}
MarkerUtility.createMarker(sourceFile.iFile, value.lineNumber, markerSeverity, "{0}",
status.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,31 +303,31 @@ public boolean validateOutputFile(CompilerFiles files) {
if (StringUtility.isEmpty(files.outputFileExtension)) {
// ERROR: Output file extension must be set in the preferences of {0} '{1}' or
// via the annotation '{2}'.
createMainSourceFileMessage(files, files.outputFileExtensionProperty, IMarker.SEVERITY_ERROR,
createMainSourceFileMessage(files, files.outputFileExtensionAnnotationValue, IMarker.SEVERITY_ERROR,
Texts.MESSAGE_E104, compilerDefinition.getText(), compilerDefinition.getName(),
LanguageAnnotation.OUTPUT_FILE_EXTENSION);

return false;
}
if (!files.outputFileExtension.startsWith(".")) {
// ERROR: Output file extension {0} must start with ".".
createMainSourceFileMessage(files, files.outputFileExtensionProperty, IMarker.SEVERITY_ERROR,
createMainSourceFileMessage(files, files.outputFileExtensionAnnotationValue, IMarker.SEVERITY_ERROR,
Texts.MESSAGE_E139, files.outputFileExtension);
return false;
}

if (StringUtility.isEmpty(files.outputFolderMode)) {
// ERROR: Output folder mode be set in the preferences of
// compiler '{0}' or via the annotation '{1}'.
createMainSourceFileMessage(files, files.outputFolderModeProperty, IMarker.SEVERITY_ERROR,
createMainSourceFileMessage(files, files.outputFolderModeAnnotationValue, IMarker.SEVERITY_ERROR,
Texts.MESSAGE_E140, compilerDefinition.getName(), LanguageAnnotation.OUTPUT_FOLDER_MODE);

return false;
}
if (!CompilerOutputFolderMode.isDefined(files.outputFolderMode)) {
// ERROR: Unknown output folder mode {0}. Specify one of the
// following valid values '{1}'.
createMainSourceFileMessage(files, files.outputFolderModeProperty, IMarker.SEVERITY_ERROR,
createMainSourceFileMessage(files, files.outputFolderModeAnnotationValue, IMarker.SEVERITY_ERROR,
Texts.MESSAGE_E141, files.outputFolderMode, CompilerOutputFolderMode.getAllowedValues());
return false;

Expand Down

0 comments on commit 5839427

Please sign in to comment.