Skip to content

Commit

Permalink
Prefer explicitly specified array dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusAmshove committed Aug 15, 2023
1 parent c9c7858 commit 77fb66d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private VariableNode variable(GroupNode enclosingGroup) throws ParseError
if (peek().kind() == SyntaxKind.NUMBER_LITERAL || (peek().kind().isIdentifier() && isVariableDeclared(peek().symbolName())))
{
addArrayDimensions(variable);
var typedDdmArrayVariable = customTypedVariableFromDdm(variable);
var typedDdmArrayVariable = typedVariableFromDdm(variable, enclosingGroup);
consumeMandatory(typedDdmArrayVariable, SyntaxKind.RPAREN);
return typedDdmArrayVariable;
}
Expand Down Expand Up @@ -219,16 +219,6 @@ private GroupNode group(VariableNode variable) throws ParseError
return group;
}

// This is used when there is a type specified in the view.
// Type is checked against DDM.
private VariableNode customTypedVariableFromDdm(VariableNode variable)
{
var typedVariable = new TypedVariableNode(variable);

checkVariableTypeAgainstDdm(typedVariable);
return typedVariable;
}

// This is used when there is no type specified in the view.
// Type is loaded from DDM.
private VariableNode typedVariableFromDdm(VariableNode variable, GroupNode enclosingGroup)
Expand Down Expand Up @@ -275,6 +265,8 @@ private VariableNode typedVariableFromDdm(VariableNode variable, GroupNode enclo
typedVariable.setType(type);

if (ddmField.level() > 1
// if the variable already has a dimension explicitly specified we don't need to take it from the group
&& typedVariable.dimensions.isEmpty()
// if the user specified the periodic group explicitly, the dimensions will be passed down. No need to add the periodic dimension
&& (enclosingGroup == null || !enclosingGroup.isArray()))
{
Expand All @@ -293,7 +285,8 @@ private VariableNode typedVariableFromDdm(VariableNode variable, GroupNode enclo
}
}

if (ddmField.fieldType() == FieldType.MULTIPLE || ddmField.fieldType() == FieldType.PERIODIC)
if (typedVariable.dimensions.isEmpty() && // no dimensions explicitly given
(ddmField.fieldType() == FieldType.MULTIPLE || ddmField.fieldType() == FieldType.PERIODIC))
{
var dimension = new ArrayDimension();
dimension.setLowerBound(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ void parseViewsWithArraysReferencingVariables()

var view = findVariable(defineData, "MY-VIEW", IViewNode.class);
assertThat(view.variables().size()).isEqualTo(1);
var theArray = assertNodeType(view.variables().first(), ITypedVariableNode.class);
var theArray = assertNodeType(view.variables().first(), VariableNode.class);
assertThat(theArray.name()).isEqualTo("ARRAY-INSIDE");
assertThat(theArray.dimensions().first().lowerBound()).isEqualTo(1);
assertThat(theArray.dimensions().first().upperBound()).isEqualTo(8);
Expand All @@ -1301,7 +1301,7 @@ void parseAViewVariableWithoutTypeNotationButWithArrayNotation()

var view = assertNodeType(defineData.variables().first(), IViewNode.class);
assertThat(view.variables().size()).isEqualTo(1);
var theArray = assertNodeType(view.variables().first(), TypedVariableNode.class);
var theArray = assertNodeType(view.variables().first(), VariableNode.class);
assertThat(theArray.name()).isEqualTo("DDM-FIELD");
assertThat(theArray.dimensions().first().lowerBound()).isEqualTo(1);
assertThat(theArray.dimensions().first().upperBound()).isEqualTo(10);
Expand Down Expand Up @@ -2159,6 +2159,44 @@ void useArrayDimensionsOfGroupsWhenPeriodicGroupIsExplicitlySpecified()
assertThat(periodicMember.dimensions().first().upperBound()).isEqualTo(10);
}

@Test
void useArrayDimensionsOfPeriodicMembersWhenExplicitlySpecified()
{
useStubModuleProvider();
moduleProvider.addDdm("MY-DDM", myDdm());
var defineData = assertParsesWithoutDiagnostics("""
DEFINE DATA LOCAL
1 MY-VIEW VIEW OF MY-DDM
2 A-PERIODIC-MEMBER (1)
END-DEFINE
""");

var periodicMember = assertNodeType(defineData.findVariable("A-PERIODIC-MEMBER"), ITypedVariableNode.class);
assertThat(periodicMember.dimensions()).hasSize(1);
assertThat(periodicMember.dimensions().first().lowerBound()).isEqualTo(1);
assertThat(periodicMember.dimensions().first().upperBound()).isEqualTo(1);
}

@Test
void useArrayDimensionsOfMultipleValueFieldsWhenExplicitlySpecifiedButWithoutType()
{
useStubModuleProvider();
moduleProvider.addDdm("MY-DDM", myDdm());
var defineData = assertParsesWithoutDiagnostics("""
DEFINE DATA LOCAL
1 MY-VIEW VIEW OF MY-DDM
2 A-MULTIPLE-FIELD (1:10)
END-DEFINE
""");

var field = assertNodeType(defineData.findVariable("A-MULTIPLE-FIELD"), ITypedVariableNode.class);
assertThat(field.type().format()).isEqualTo(DataFormat.NUMERIC);
assertThat(field.type().length()).isEqualTo(7.2);
assertThat(field.dimensions()).hasSize(1);
assertThat(field.dimensions().first().lowerBound()).isEqualTo(1);
assertThat(field.dimensions().first().upperBound()).isEqualTo(10);
}

@Test
void reportADiagnosticForUnresolvedDdmFields()
{
Expand Down

0 comments on commit 77fb66d

Please sign in to comment.