Skip to content

Commit

Permalink
Issue #1399: relax restrictions for neuron names
Browse files Browse the repository at this point in the history
  • Loading branch information
rbudde committed Oct 29, 2024
1 parent 2aea994 commit 283031b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private boolean neuronNamesConsistent(List<String> l1, List<String> l2) {
Set<String> set = new HashSet<>(l1);
set.addAll(l2);
for ( String s : set ) {
if ( s.isEmpty() || !Util.isValidJavaIdentifier(s) ) {
if ( s.isEmpty() || !Util.isSafeIdentifier(s) ) {
return false;
}
}
Expand Down
20 changes: 17 additions & 3 deletions OpenRobertaRobot/src/main/java/de/fhg/iais/roberta/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class Util {
private static final Pattern IFDEF_PATTERN = Pattern.compile("^\\s*#ifdef (.*)$");
private static final Pattern END_PATTERN = Pattern.compile("^\\s*#end$");
private static final Pattern HEX_VALUE_PATTERN = Pattern.compile("^#[0-9a-fA-F]+$");
private static final Pattern UNSAFE_CHAR_PATTERN = Pattern.compile("^.*[;,\n\t\r].*$", Pattern.MULTILINE);
private static final String INVALID = "invalid";
/**
* YAML parser. NOT thread-safe!
Expand Down Expand Up @@ -281,7 +282,22 @@ public final static boolean isValidEmailAddress(String s) {
}

/**
* Check whether a String is a valid Java identifier. It is checked, that no reserved word is used
* Check that a String does not contain problematic characters as ";" or "\n"
*
* @param s String to check
* @return <code>true</code> if the given String is safe; <code>false</code> otherwise.
*/
public final static boolean isSafeIdentifier(String s) {
if ( s == null || s.length() == 0 ) {
return false;
} else if ( UNSAFE_CHAR_PATTERN.matcher(s).matches() ) {
return false;
}
return true;
}

/**
* Check whether a String is a valid Java identifier. It is checked also, that no reserved word is used
*
* @param s String to check
* @return <code>true</code> if the given String is a valid Java identifier; <code>false</code> otherwise.
Expand Down Expand Up @@ -323,8 +339,6 @@ public static String getErrorTicketId() {
return "E-" + Util.errorTicketNumber.incrementAndGet();
}

private static List<String> EMPTY_STRING_LIST = new ArrayList<>();

/**
* read all lines from a resource, concatenate them to a string separated by a newline
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import de.fhg.iais.roberta.syntax.lang.stmt.WaitTimeStmt;
import de.fhg.iais.roberta.typecheck.BlocklyType;
import de.fhg.iais.roberta.typecheck.NepoInfoProcessor;
import de.fhg.iais.roberta.util.Util;
import de.fhg.iais.roberta.util.ast.BlocklyProperties;
import de.fhg.iais.roberta.util.syntax.FunctionNames;

Expand Down Expand Up @@ -604,11 +605,17 @@ public Void visitVar(Var var) {
if ( !(this.getBuilder(UsedHardwareBean.Builder.class).containsInScopeVariable(variableName)) ) {
addErrorToPhrase(var, "SCOPE_ERROR");
}
if (!Util.isSafeIdentifier(variableName)) {
addErrorToPhrase(var, "SCOPE_ERROR");
}
return null;
}

@Override
public Void visitVarDeclaration(VarDeclaration var) {
if (!Util.isSafeIdentifier(var.name)) {
addErrorToPhrase(var, "SCOPE_ERROR");
}
if ( var.global ) {
this.getBuilder(UsedHardwareBean.Builder.class).addVisitedVariable(var);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public void testJavaIdentifier() {
assertFalse(Util.isValidJavaIdentifier(" Pid"));
assertFalse(Util.isValidJavaIdentifier("class"));
assertFalse(Util.isValidJavaIdentifier("for"));

assertTrue(Util.isSafeIdentifier("__üäö$€"));
assertTrue(Util.isSafeIdentifier("if"));
assertFalse(Util.isSafeIdentifier("x;y"));
assertFalse(Util.isSafeIdentifier("x\ny"));
}

@Test
Expand Down

0 comments on commit 283031b

Please sign in to comment.