Skip to content

Commit

Permalink
Refactor parsing of values in evaluator
Browse files Browse the repository at this point in the history
Refactor parsing of values in evaluator to use the new structured value
converter support instead of relying on the full ST parser and
evaluator.
  • Loading branch information
mx990 authored and azoitl committed Sep 6, 2024
1 parent d0ffaf0 commit 6762663
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.eclipse.fordiac.ide.model.data.DataType;
import org.eclipse.fordiac.ide.model.datatype.helper.IecTypes;
import org.eclipse.fordiac.ide.model.eval.value.Value;
import org.eclipse.fordiac.ide.model.eval.value.ValueOperations;
import org.eclipse.fordiac.ide.model.libraryElement.INamedElement;

public abstract class AbstractVariable<T extends Value> implements Variable<T> {
Expand Down Expand Up @@ -52,6 +53,21 @@ public INamedElement getType() {
return type;
}

@Override
public void setValue(final String value) {
setValue(ValueOperations.parseValue(value, getType()));
}

@Override
public boolean validateValue(final String value) {
try {
ValueOperations.parseValue(value, getType());
} catch (final Exception e) {
return false;
}
return true;
}

protected RuntimeException createCastException(final Value value) {
throw new ClassCastException("Cannot assign value with incompatible type " + value.getType().getName() //$NON-NLS-1$
+ " as " + type.getName()); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,6 @@ public void setValue(final Value value) {
.forEach(index -> this.value.get(index).setValue(arrayValue.get(index).getValue()));
}

@Override
public void setValue(final String value) {
setValue(VariableOperations.evaluateValue(getType(), value));
}

@Override
public boolean validateValue(final String value) {
return VariableOperations.validateValue(getType(), value).isEmpty();
}

@Override
public ArrayType getType() {
return (ArrayType) super.getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,6 @@ public void setValue(final Value value) {
this.value = (T) ValueOperations.castValue(value, getType());
}

@Override
public void setValue(final String value) {
setValue(VariableOperations.evaluateValue(getType(), value));
}

@Override
public boolean validateValue(final String value) {
return VariableOperations.validateValue(getType(), value).isEmpty();
}

@Override
public AnyType getType() {
return (AnyType) super.getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,12 @@

import java.util.Collections;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.eclipse.fordiac.ide.model.eval.value.FBValue;
import org.eclipse.fordiac.ide.model.eval.value.Value;
import org.eclipse.fordiac.ide.model.libraryElement.FBType;

public class FBVariable extends AbstractVariable<FBValue> {
private static final Pattern MAP_PATTERN = Pattern.compile(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"); //$NON-NLS-1$
private static final Pattern MAP_KV_PATTERN = Pattern.compile(":=(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"); //$NON-NLS-1$

private final FBValue value;

public FBVariable(final String name, final FBType type) {
Expand Down Expand Up @@ -54,43 +49,6 @@ public void setValue(final Value value) {
fbValue.getMembers().forEach((name, variable) -> this.value.get(name).setValue(variable.getValue()));
}

@Override
public void setValue(final String value) {
final String trimmed = value.trim();
if (!trimmed.startsWith("(") || !trimmed.endsWith(")")) { //$NON-NLS-1$ //$NON-NLS-2$
throw createInvalidValueException();
}
final String inner = trimmed.substring(1, trimmed.length() - 1);
for (final String elem : FBVariable.MAP_PATTERN.split(inner)) {
final String[] split = FBVariable.MAP_KV_PATTERN.split(elem);
if (split.length != 2) {
throw createInvalidValueException();
}
final Variable<?> variable = this.value.get(split[0].trim());
if (variable == null) {
throw createInvalidValueException();
}
variable.setValue(split[1].trim());
}
}

@Override
public boolean validateValue(final String value) {
final String trimmed = value.trim();
if (!trimmed.startsWith("(") || !trimmed.endsWith(")")) { //$NON-NLS-1$ //$NON-NLS-2$
return false;
}
final String inner = trimmed.substring(1, trimmed.length() - 1);
return Stream.of(FBVariable.MAP_PATTERN.split(inner)).allMatch(elem -> {
final String[] split = FBVariable.MAP_KV_PATTERN.split(elem);
if (split.length != 2) {
return false;
}
final Variable<?> variable = this.value.get(split[0].trim());
return variable != null && variable.validateValue(split[1].trim());
});
}

public Map<String, Variable<?>> getMembers() {
return value.getMembers();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ public void setValue(final Value value) {
delegate.setValue(ValueOperations.partial(delegate.getValue(), getType(), index, value));
}

@Override
public void setValue(final String value) {
setValue(VariableOperations.evaluateValue(getType(), value));
}

@Override
public boolean validateValue(final String value) {
return VariableOperations.validateValue(getType(), value).isEmpty();
}

@Override
public AnyBitType getType() {
return (AnyBitType) super.getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ public void setValue(final Value value) {
delegate.setValue(delegate.getValue().withCharAt(index, charValue));
}

@Override
public void setValue(final String value) {
setValue(VariableOperations.evaluateValue(getType(), value));
}

@Override
public boolean validateValue(final String value) {
return VariableOperations.validateValue(getType(), value).isEmpty();
}

@Override
public CharType getType() {
return (CharType) super.getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ public void setValue(final Value value) {
structValue.getMembers().forEach((name, variable) -> this.value.get(name).setValue(variable.getValue()));
}

@Override
public void setValue(final String value) {
setValue(VariableOperations.evaluateValue(getType(), value));
}

@Override
public boolean validateValue(final String value) {
return VariableOperations.validateValue(getType(), value).isEmpty();
}

@Override
public StructuredType getType() {
return (StructuredType) super.getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ public void setValue(final Value value) {
delegate.setValue(delegate.getValue().withCharAt(index, charValue));
}

@Override
public void setValue(final String value) {
setValue(VariableOperations.evaluateValue(getType(), value));
}

@Override
public boolean validateValue(final String value) {
return VariableOperations.validateValue(getType(), value).isEmpty();
}

@Override
public WcharType getType() {
return (WcharType) super.getType();
Expand Down

0 comments on commit 6762663

Please sign in to comment.