Only accept ASCII characters in type assumption name

This commit is contained in:
Arne Keller 2021-03-06 13:43:42 +01:00
parent c7c313650c
commit a176d299ce
3 changed files with 15 additions and 7 deletions

View File

@ -17,6 +17,7 @@ import java.util.regex.Pattern;
*/ */
public class TypeAssumptionParser { public class TypeAssumptionParser {
private static final Pattern TYPE_NAME_PATTERN = Pattern.compile("[a-zA-Z][a-zA-Z0-9]*");
private static final Pattern TYPE_VARIABLE_PATTERN = Pattern.compile("t(\\d+)"); private static final Pattern TYPE_VARIABLE_PATTERN = Pattern.compile("t(\\d+)");
/** /**
@ -28,7 +29,11 @@ public class TypeAssumptionParser {
public Result<Map<VarTerm, TypeAbstraction>, ParseError> parse(Map<String, String> assumptions) { public Result<Map<VarTerm, TypeAbstraction>, ParseError> parse(Map<String, String> assumptions) {
Map<VarTerm, TypeAbstraction> typeAssumptions = new LinkedHashMap<>(); Map<VarTerm, TypeAbstraction> typeAssumptions = new LinkedHashMap<>();
for (Map.Entry<String, String> entry : assumptions.entrySet()) { for (Map.Entry<String, String> entry : assumptions.entrySet()) {
VarTerm var = new VarTerm(entry.getKey()); String typeName = entry.getKey();
if (!TYPE_NAME_PATTERN.matcher(typeName).matches()) {
return new Result<>(null, ParseError.UNEXPECTED_CHARACTER);
}
VarTerm var = new VarTerm(typeName);
Result<TypeAbstraction, ParseError> typeAbs = parseType(entry.getValue()); Result<TypeAbstraction, ParseError> typeAbs = parseType(entry.getValue());
if (typeAbs.isError()) { if (typeAbs.isError()) {
return new Result<>(typeAbs); return new Result<>(typeAbs);

View File

@ -1,9 +1,4 @@
/** /**
* The util package contains classes used in all components of the application. * The util package contains classes used in all components of the application.
*/ */
@NonNullFields
@NonNullApi
package edu.kit.typicalc.util; package edu.kit.typicalc.util;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;

View File

@ -209,6 +209,14 @@ class TypeAssumptionParserTest {
assertTrue(type.isError()); assertTrue(type.isError());
assertEquals(entry.getValue(), type.unwrapError()); assertEquals(entry.getValue(), type.unwrapError());
} }
TypeAssumptionParser parser = new TypeAssumptionParser();
Result<Map<VarTerm, TypeAbstraction>, ParseError> type = parser.parse(Map.of("föhn", "int"));
assertTrue(type.isError());
assertEquals(ParseError.UNEXPECTED_CHARACTER, type.unwrapError());
parser = new TypeAssumptionParser();
type = parser.parse(Map.of("1typ", "int"));
assertTrue(type.isError());
assertEquals(ParseError.UNEXPECTED_CHARACTER, type.unwrapError());
} }
@Test @Test