diff options
Diffstat (limited to 'subprojects/language/src/main/java/tools/refinery/language/validation/ProblemValidator.java')
-rw-r--r-- | subprojects/language/src/main/java/tools/refinery/language/validation/ProblemValidator.java | 69 |
1 files changed, 64 insertions, 5 deletions
diff --git a/subprojects/language/src/main/java/tools/refinery/language/validation/ProblemValidator.java b/subprojects/language/src/main/java/tools/refinery/language/validation/ProblemValidator.java index 4cbb02c2..d9eb5fd3 100644 --- a/subprojects/language/src/main/java/tools/refinery/language/validation/ProblemValidator.java +++ b/subprojects/language/src/main/java/tools/refinery/language/validation/ProblemValidator.java | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | 2 | * SPDX-FileCopyrightText: 2021-2024 The Refinery Authors <https://refinery.tools/> |
3 | * | 3 | * |
4 | * SPDX-License-Identifier: EPL-2.0 | 4 | * SPDX-License-Identifier: EPL-2.0 |
5 | */ | 5 | */ |
@@ -13,16 +13,16 @@ import com.google.inject.Inject; | |||
13 | import org.eclipse.emf.ecore.EObject; | 13 | import org.eclipse.emf.ecore.EObject; |
14 | import org.eclipse.emf.ecore.EReference; | 14 | import org.eclipse.emf.ecore.EReference; |
15 | import org.eclipse.xtext.EcoreUtil2; | 15 | import org.eclipse.xtext.EcoreUtil2; |
16 | import org.eclipse.xtext.naming.IQualifiedNameConverter; | ||
16 | import org.eclipse.xtext.validation.Check; | 17 | import org.eclipse.xtext.validation.Check; |
17 | import org.jetbrains.annotations.Nullable; | 18 | import org.jetbrains.annotations.Nullable; |
18 | import tools.refinery.language.model.problem.*; | 19 | import tools.refinery.language.model.problem.*; |
20 | import tools.refinery.language.naming.NamingUtil; | ||
21 | import tools.refinery.language.scoping.imports.ImportAdapter; | ||
19 | import tools.refinery.language.utils.ProblemDesugarer; | 22 | import tools.refinery.language.utils.ProblemDesugarer; |
20 | import tools.refinery.language.utils.ProblemUtil; | 23 | import tools.refinery.language.utils.ProblemUtil; |
21 | 24 | ||
22 | import java.util.ArrayList; | 25 | import java.util.*; |
23 | import java.util.LinkedHashMap; | ||
24 | import java.util.LinkedHashSet; | ||
25 | import java.util.Set; | ||
26 | 26 | ||
27 | /** | 27 | /** |
28 | * This class contains custom validation rules. | 28 | * This class contains custom validation rules. |
@@ -33,6 +33,10 @@ import java.util.Set; | |||
33 | public class ProblemValidator extends AbstractProblemValidator { | 33 | public class ProblemValidator extends AbstractProblemValidator { |
34 | private static final String ISSUE_PREFIX = "tools.refinery.language.validation.ProblemValidator."; | 34 | private static final String ISSUE_PREFIX = "tools.refinery.language.validation.ProblemValidator."; |
35 | 35 | ||
36 | public static final String UNEXPECTED_MODULE_NAME_ISSUE = ISSUE_PREFIX + "UNEXPECTED_MODULE_NAME"; | ||
37 | |||
38 | public static final String INVALID_IMPORT_ISSUE = ISSUE_PREFIX + "INVALID_IMPORT"; | ||
39 | |||
36 | public static final String SINGLETON_VARIABLE_ISSUE = ISSUE_PREFIX + "SINGLETON_VARIABLE"; | 40 | public static final String SINGLETON_VARIABLE_ISSUE = ISSUE_PREFIX + "SINGLETON_VARIABLE"; |
37 | 41 | ||
38 | public static final String NODE_CONSTANT_ISSUE = ISSUE_PREFIX + "NODE_CONSTANT_ISSUE"; | 42 | public static final String NODE_CONSTANT_ISSUE = ISSUE_PREFIX + "NODE_CONSTANT_ISSUE"; |
@@ -65,6 +69,61 @@ public class ProblemValidator extends AbstractProblemValidator { | |||
65 | @Inject | 69 | @Inject |
66 | private ProblemDesugarer desugarer; | 70 | private ProblemDesugarer desugarer; |
67 | 71 | ||
72 | @Inject | ||
73 | private IQualifiedNameConverter qualifiedNameConverter; | ||
74 | |||
75 | @Check | ||
76 | public void checkModuleName(Problem problem) { | ||
77 | var nameString = problem.getName(); | ||
78 | if (nameString == null) { | ||
79 | return; | ||
80 | } | ||
81 | var resource = problem.eResource(); | ||
82 | if (resource == null) { | ||
83 | return; | ||
84 | } | ||
85 | var resourceSet = resource.getResourceSet(); | ||
86 | if (resourceSet == null) { | ||
87 | return; | ||
88 | } | ||
89 | var adapter = ImportAdapter.getOrInstall(resourceSet); | ||
90 | var expectedName = adapter.getQualifiedName(resource.getURI()); | ||
91 | if (expectedName == null) { | ||
92 | return; | ||
93 | } | ||
94 | var name = NamingUtil.stripRootPrefix(qualifiedNameConverter.toQualifiedName(nameString)); | ||
95 | if (!expectedName.equals(name)) { | ||
96 | var moduleKindName = switch (problem.getKind()) { | ||
97 | case PROBLEM -> "problem"; | ||
98 | case MODULE -> "module"; | ||
99 | }; | ||
100 | var message = "Expected %s to have name '%s', got '%s' instead.".formatted( | ||
101 | moduleKindName, qualifiedNameConverter.toString(expectedName), | ||
102 | qualifiedNameConverter.toString(name)); | ||
103 | error(message, problem, ProblemPackage.Literals.NAMED_ELEMENT__NAME, INSIGNIFICANT_INDEX, | ||
104 | UNEXPECTED_MODULE_NAME_ISSUE); | ||
105 | } | ||
106 | } | ||
107 | |||
108 | @Check | ||
109 | public void checkImportStatement(ImportStatement importStatement) { | ||
110 | var importedModule = importStatement.getImportedModule(); | ||
111 | if (importedModule == null || importedModule.eIsProxy()) { | ||
112 | return; | ||
113 | } | ||
114 | String message = null; | ||
115 | var problem = EcoreUtil2.getContainerOfType(importStatement, Problem.class); | ||
116 | if (importedModule == problem) { | ||
117 | message = "A module cannot import itself."; | ||
118 | } else if (importedModule.getKind() != ModuleKind.MODULE) { | ||
119 | message = "Only modules can be imported."; | ||
120 | } | ||
121 | if (message != null) { | ||
122 | error(message, importStatement, ProblemPackage.Literals.IMPORT_STATEMENT__IMPORTED_MODULE, | ||
123 | INSIGNIFICANT_INDEX, INVALID_IMPORT_ISSUE); | ||
124 | } | ||
125 | } | ||
126 | |||
68 | @Check | 127 | @Check |
69 | public void checkSingletonVariable(VariableOrNodeExpr expr) { | 128 | public void checkSingletonVariable(VariableOrNodeExpr expr) { |
70 | var variableOrNode = expr.getVariableOrNode(); | 129 | var variableOrNode = expr.getVariableOrNode(); |