Skip to content

Commit

Permalink
handle string arguments correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
cmnrd committed Nov 17, 2022
1 parent bc0aefd commit 0042b99
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
3 changes: 2 additions & 1 deletion org.lflang/src/org/lflang/AttributeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.lflang.lf.Reactor;
import org.lflang.lf.StateVar;
import org.lflang.lf.Timer;
import org.lflang.util.StringUtil;

/**
* A helper class for processing attributes in the AST.
Expand Down Expand Up @@ -105,7 +106,7 @@ public static String getFirstArgumentValue(Attribute attr) {
if (attr == null || attr.getAttrParms().isEmpty()) {
return null;
}
return attr.getAttrParms().get(0).getValue();
return StringUtil.removeQuotes(attr.getAttrParms().get(0).getValue());
}

/**
Expand Down
14 changes: 12 additions & 2 deletions org.lflang/src/org/lflang/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,23 @@ public static String removeQuotes(String str) {
if (str.length() < 2) {
return str;
}
if (str.startsWith("\"") && str.endsWith("\"")
|| str.startsWith("'") && str.endsWith("'")) {
if (hasQuotes(str)) {
return str.substring(1, str.length() - 1);
}
return str;
}

/**
* Return true if the given string is surrounded by single or double
* quotes,
*/
public static boolean hasQuotes(String str) {
if (str == null) {
return false;
}
return str.startsWith("\"") && str.endsWith("\"") || str.startsWith("'") && str.endsWith("'");
}

/**
* Intelligently trim the white space in a code block.
*
Expand Down
52 changes: 30 additions & 22 deletions org.lflang/src/org/lflang/validation/AttributeSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.lflang.lf.AttrParm;
import org.lflang.lf.Attribute;
import org.lflang.lf.LfPackage.Literals;
import org.lflang.util.StringUtil;

/**
* Specification of the structure of an attribute annotation.
Expand Down Expand Up @@ -155,28 +156,35 @@ private boolean isOptional() {
// Check if a parameter has the right type.
// Currently only String, Int, Boolean, and Float are supported.
public void check(LFValidator validator, AttrParm parm) {
switch(type) {
case STRING:
// nothing to check, all literals are reported as strings
break;
case INT:
if (!ASTUtils.isInteger(parm.getValue())) {
validator.error("Incorrect type: \"" + parm.getName() + "\"" + " should have type Int.",
Literals.ATTRIBUTE__ATTR_NAME);
}
break;
case BOOLEAN:
if (!ASTUtils.isBoolean(parm.getValue())) {
validator.error("Incorrect type: \"" + parm.getName() + "\"" + " should have type Boolean.",
Literals.ATTRIBUTE__ATTR_NAME);
}
break;
case FLOAT:
if (!ASTUtils.isFloat(parm.getValue())) {
validator.error("Incorrect type: \"" + parm.getName() + "\"" + " should have type Float.",
Literals.ATTRIBUTE__ATTR_NAME);
}
break;
switch (type) {
case STRING:
if (!StringUtil.hasQuotes(parm.getValue())) {
validator.error("Incorrect type: \"" + parm.getName() + "\""
+ " should have type String.",
Literals.ATTRIBUTE__ATTR_NAME);
}
break;
case INT:
if (!ASTUtils.isInteger(parm.getValue())) {
validator.error(
"Incorrect type: \"" + parm.getName() + "\"" + " should have type Int.",
Literals.ATTRIBUTE__ATTR_NAME);
}
break;
case BOOLEAN:
if (!ASTUtils.isBoolean(parm.getValue())) {
validator.error(
"Incorrect type: \"" + parm.getName() + "\"" + " should have type Boolean.",
Literals.ATTRIBUTE__ATTR_NAME);
}
break;
case FLOAT:
if (!ASTUtils.isFloat(parm.getValue())) {
validator.error(
"Incorrect type: \"" + parm.getName() + "\"" + " should have type Float.",
Literals.ATTRIBUTE__ATTR_NAME);
}
break;
}
}
}
Expand Down

0 comments on commit 0042b99

Please sign in to comment.