Skip to content

Commit

Permalink
properly report unknown platforms
Browse files Browse the repository at this point in the history
Fixes #1919
  • Loading branch information
cmnrd committed Oct 2, 2023
1 parent 9362bb8 commit 66933e4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 42 deletions.
75 changes: 33 additions & 42 deletions core/src/main/java/org/lflang/target/property/PlatformProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.lflang.AbstractTargetProperty;
import org.lflang.MessageReporter;
import org.lflang.Target;
Expand All @@ -22,6 +23,10 @@

public class PlatformProperty extends AbstractTargetProperty<PlatformOptions> {

public static final String UNKNOW_PLATFORM =
"Unidentified Platform Type, LF supports the following platform types: "
+ Arrays.asList(Platform.values());

public PlatformProperty() {
super(UnionType.PLATFORM_STRING_OR_DICTIONARY);
}
Expand All @@ -34,41 +39,25 @@ public PlatformOptions initialValue() {
@Override
public PlatformOptions fromAst(Element node, MessageReporter reporter) {
var config = new PlatformOptions();
if (node.getLiteral() != null) {
if (node.getLiteral() != null || node.getId() != null) {
config.platform =
(Platform) UnionType.PLATFORM_UNION.forName(ASTUtils.elementToSingleString(node));
if (config.platform == null) {
String s =
"Unidentified Platform Type, LF supports the following platform types: "
+ Arrays.asList(Platform.values());
// err.at(value).error(s);
throw new AssertionError(s);
}
} else {
for (KeyValuePair entry : node.getKeyvalue().getPairs()) {
PlatformOption option =
(PlatformOption) DictionaryType.PLATFORM_DICT.forName(entry.getName());
switch (option) {
case NAME -> {
Platform p =
config.platform =
(Platform)
UnionType.PLATFORM_UNION.forName(
ASTUtils.elementToSingleString(entry.getValue()));
if (p == null) {
String s =
"Unidentified Platform Type, LF supports the following platform types: "
+ Arrays.asList(Platform.values());
reporter.at(entry).error(s);
throw new AssertionError(s);
}
config.platform = p;
}
case BAUDRATE -> config.baudRate = ASTUtils.toInteger(entry.getValue());
case BOARD -> config.board = ASTUtils.elementToSingleString(entry.getValue());
case FLASH -> config.flash = ASTUtils.toBoolean(entry.getValue());
case PORT -> config.port = ASTUtils.elementToSingleString(entry.getValue());
case USER_THREADS -> config.userThreads = ASTUtils.toInteger(entry.getValue());
default -> {}
}
}
}
Expand All @@ -88,33 +77,35 @@ public List<Target> supportedTargets() {

@Override
public void validate(KeyValuePair pair, Model ast, MessageReporter reporter) {
var threading = TargetProperty.getKeyValuePair(ast, TargetProperty.THREADING);
if (threading != null) {
if (pair != null && ASTUtils.toBoolean(threading.getValue())) {
var lit = ASTUtils.elementToSingleString(pair.getValue());
var dic = pair.getValue().getKeyvalue();
if (lit != null && lit.equalsIgnoreCase(Platform.RP2040.toString())) {
reporter
.at(pair, Literals.KEY_VALUE_PAIR__VALUE)
.error("Platform " + Platform.RP2040 + " does not support threading");
}
if (dic != null) {
var rp =
dic.getPairs().stream()
.filter(
kv ->
kv.getName().equalsIgnoreCase("name")
&& ASTUtils.elementToSingleString(kv.getValue())
.equalsIgnoreCase(Platform.RP2040.toString()))
.findFirst();
rp.ifPresent(
keyValuePair ->
reporter
.at(keyValuePair, Literals.KEY_VALUE_PAIR__VALUE)
.error("Platform " + Platform.RP2040 + " does not support threading"));
final var node = pair.getValue();
Platform platform = null;
if (node.getLiteral() != null || node.getId() != null) {
platform = (Platform) UnionType.PLATFORM_UNION.forName(ASTUtils.elementToSingleString(node));
if (platform == null) {
reporter.at(pair, Literals.KEY_VALUE_PAIR__VALUE).error(UNKNOW_PLATFORM);
}
} else {
for (KeyValuePair entry : node.getKeyvalue().getPairs()) {
PlatformOption option =
(PlatformOption) DictionaryType.PLATFORM_DICT.forName(entry.getName());
if (Objects.requireNonNull(option) == PlatformOption.NAME) {
platform =
(Platform)
UnionType.PLATFORM_UNION.forName(
ASTUtils.elementToSingleString(entry.getValue()));
if (platform == null) {
reporter.at(entry, Literals.KEY_VALUE_PAIR__VALUE).error(UNKNOW_PLATFORM);
}
}
}
}

var threading = TargetProperty.getKeyValuePair(ast, TargetProperty.THREADING);
if (threading != null && platform == Platform.RP2040) {
reporter
.at(pair, Literals.KEY_VALUE_PAIR__VALUE)
.error("Platform " + Platform.RP2040 + " does not support threading");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import org.lflang.lf.Model;
import org.lflang.lf.Visibility;
import org.lflang.target.TargetProperty;
import org.lflang.target.property.PlatformProperty;
import org.lflang.target.property.PlatformProperty.Platform;
import org.lflang.target.property.type.ArrayType;
import org.lflang.target.property.type.DictionaryType;
import org.lflang.target.property.type.DictionaryType.DictionaryElement;
Expand Down Expand Up @@ -1610,6 +1612,23 @@ public void checkCargoDependencyProperty() throws Exception {
"Expected an array of strings for key 'features'");
}

@Test
public void checkPlatformProperty() throws Exception {
validator.assertNoErrors(createModel(TargetProperty.PLATFORM, Platform.ARDUINO.toString()));
validator.assertNoErrors(
createModel(TargetProperty.PLATFORM, String.format("{name: %s}", Platform.ZEPHYR)));
validator.assertError(
createModel(TargetProperty.PLATFORM, "foobar"),
LfPackage.eINSTANCE.getKeyValuePair(),
null,
PlatformProperty.UNKNOW_PLATFORM);
validator.assertError(
createModel(TargetProperty.PLATFORM, "{ name: foobar }"),
LfPackage.eINSTANCE.getKeyValuePair(),
null,
PlatformProperty.UNKNOW_PLATFORM);
}

@Test
public void testImportedCyclicReactor() throws Exception {
// File tempFile = File.createTempFile("lf-validation", ".lf");
Expand Down

0 comments on commit 66933e4

Please sign in to comment.