Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make USING not import variables from module types other than data areas #307

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.amshove.natparse.lexing.SyntaxToken;
import org.amshove.natparse.lexing.TokenList;
import org.amshove.natparse.natural.*;
import org.amshove.natparse.natural.project.NaturalFileType;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -71,12 +72,27 @@ protected INaturalModule sideloadModule(String referableName, SyntaxToken module
return module;
}

private static final Set<NaturalFileType> TYPES_FOR_USINGS = Set.of(NaturalFileType.GDA, NaturalFileType.LDA, NaturalFileType.PDA);

protected IHasDefineData sideloadDefineData(TokenNode importNode)
{
if (sideloadModule(importNode.token().symbolName(), importNode.token())instanceof IHasDefineData hasDefineData)
var module = sideloadModule(importNode.token().symbolName(), importNode.token());
if (module instanceof IHasDefineData hasDefineData
&& TYPES_FOR_USINGS.contains(module.file().getFiletype()))
{
return hasDefineData;
}
else
if (module != null)
{
report(
ParserErrors.invalidModuleType(
"Only data areas can be imported via USING. This is a %s."
.formatted(module.file().getFiletype()),
importNode.token()
)
);
}

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ protected NaturalModule newEmptyLda()
return module;
}

protected NaturalModule newEmptySubprogram()
{
var file = new NaturalFile("SUBPROG", Path.of(""), NaturalFileType.SUBPROGRAM);
var module = new NaturalModule(file);
module.setDefineData(new DefineDataNode());
return module;
}

protected IVariableReferenceNode assertIsVariableReference(IOperandNode operand, String name)
{
assertThat(operand).as("Expected a variable reference, but operand is null").isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,25 @@ void referenceAUsingTarget()
assertThat(using.reference()).isEqualTo(importedLda);
}

@Test
void notImportVariablesFromModulesThatAreNotDataAreas()
{
useStubModuleProvider();
var subprogram = newEmptySubprogram();
moduleProvider.addModule("SUBPROG", subprogram);

var source = """
DEFINE DATA
LOCAL USING SUBPROG
END-DEFINE
""";

var defineData = assertDiagnostic(source, ParserError.INVALID_MODULE_TYPE);
var using = defineData.localUsings().first();
assertThat(using.referencingToken().symbolName()).isEqualTo("SUBPROG");
assertThat(using.reference()).isNull();
}

@Test
void setTheCorrectParentForNodes()
{
Expand Down