Skip to content

Commit

Permalink
Merge branch 'regex-fix' into 'dev'
Browse files Browse the repository at this point in the history
Regex fix

See merge request monticore/monticore!1045
  • Loading branch information
SE-FDr committed Oct 15, 2024
2 parents cfb7441 + a0923f2 commit 04020ac
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,29 +174,17 @@ protected static List<SIUnitBasic> string2SIUnitBasics(String inputStr) {
// but it would be useful for this method only.
// Runtime can be improved if required.

// we have prefixes and units in a list,
// they need to be split
// "^" to match only start of String
// "(?!ol|in)" to avoid issues with "lmin/lmol" having "lm" matched
Pattern prefixPat =
Pattern.compile("^" + PREFIX_PATTERN + "(?!ol|in)");
Pattern unitWithPrefixPat =
Pattern.compile("^" + PREFIX_UNIT_PATTERN + "(?!ol|in)");
Pattern unitWithoutPrefixPat =
Pattern.compile("^" + NO_PREFIX_UNIT_PATTERN + "(?!ol|in)");
Pattern unitWithoutPrefixPrefPat =
Pattern.compile("^" + NO_PREFIX_PREFERED_UNIT_PATTERN + "(?!ol|in)");
// hint: longest potential finding of a unit (with prefix) is 5 chars long,
// e.g., "dakat"
String toBeParsed = inputStr;
while (!toBeParsed.isEmpty()) {
Optional<String> prefix = Optional.empty();
String unit;
// start by searching for prefix + unit
Matcher unitWithPrefixMat = unitWithPrefixPat.matcher(toBeParsed);
Matcher unitWithPrefixMat = getUnitWithPrefixPattern().matcher(toBeParsed);
if (unitWithPrefixMat.find()) {
String prefixedUnit = unitWithPrefixMat.group();
Matcher prefixMat = prefixPat.matcher(prefixedUnit);
Matcher prefixMat = getPrefixPattern().matcher(prefixedUnit);
if (prefixMat.find()) {
// remove the prefix
prefix = Optional.of(prefixMat.group());
Expand All @@ -209,9 +197,9 @@ protected static List<SIUnitBasic> string2SIUnitBasics(String inputStr) {
}
// search for unit without prefix,
// but prefer 'min' over 'm', etc.
Matcher unitMat = unitWithoutPrefixPrefPat.matcher(toBeParsed);
Matcher unitMat = getUnitWithoutPrefixPrefPattern().matcher(toBeParsed);
if (!unitMat.find()) {
unitMat = unitWithoutPrefixPat.matcher(toBeParsed);
unitMat = getUnitWithoutPrefixPattern().matcher(toBeParsed);
if (!unitMat.find()) {
Log.error(INTERNAL_LOGIC_ERROR + inputStr);
return Collections.emptyList();
Expand All @@ -235,7 +223,7 @@ protected static List<SIUnitBasic> string2SIUnitBasics(String inputStr) {
* logs an error if the input is not a valid SIUnitGroup
*/
protected static boolean assertValidSIUnitGroup(String inputStr) {
if (inputStr.matches(GROUP_PATTERN)) {
if (getGroupPattern().matcher(inputStr).matches()) {
return true;
}
else {
Expand All @@ -251,4 +239,57 @@ protected static int getValue(ASTSignedLiteral lit) {
return res;
}

// Helper/Cache - Compiled Patterns:
// these will be initialized during get() and then cached,
// as in most cases, they are not used at all.
// we have prefixes and units in a list,
// they need to be split
// "^" to match only start of String
// "(?!ol|in)" to avoid issues with "lmin/lmol" having "lm" matched

protected static Pattern prefixPat;

protected static Pattern unitWithPrefixPat;

protected static Pattern unitWithoutPrefixPat;

protected static Pattern unitWithoutPrefixPrefPat;

protected static Pattern groupPat;

protected static Pattern getPrefixPattern() {
if (prefixPat == null) {
prefixPat = Pattern.compile("^" + PREFIX_PATTERN + "(?!ol|in)");
}
return prefixPat;
}

protected static Pattern getUnitWithPrefixPattern() {
if (unitWithPrefixPat == null) {
unitWithPrefixPat = Pattern.compile("^" + PREFIX_UNIT_PATTERN + "(?!ol|in)");
}
return unitWithPrefixPat;
}

protected static Pattern getUnitWithoutPrefixPattern() {
if (unitWithoutPrefixPat == null) {
unitWithoutPrefixPat = Pattern.compile("^" + NO_PREFIX_UNIT_PATTERN + "(?!ol|in)");
}
return unitWithoutPrefixPat;
}

protected static Pattern getUnitWithoutPrefixPrefPattern() {
if (unitWithoutPrefixPrefPat == null) {
unitWithoutPrefixPrefPat = Pattern.compile("^" + NO_PREFIX_PREFERED_UNIT_PATTERN + "(?!ol|in)");
}
return unitWithoutPrefixPrefPat;
}

protected static Pattern getGroupPattern() {
if (groupPat == null) {
groupPat = Pattern.compile(GROUP_PATTERN);
}
return groupPat;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -143,7 +144,13 @@ Optional<URL> do_find(FindCacheKey k) {
String fileNameRegEx = Names.getSimpleName(k.qualifiedName) + "\\." + k.fileExtRegEx;

// initialize a file filter filtering for the regular expression
FileFilter filter = new RegexFileFilter(fileNameRegEx);
FileFilter filter;
try {
filter = new RegexFileFilter(fileNameRegEx);
} catch (PatternSyntaxException e) {
Log.error("0xFDAB0 MCPath: received invalid RegEx.", e);
return Optional.empty();
}

List<URL> resolvedURLs = new ArrayList<>();
// iterate MCPath entries and check whether folder path exists within these
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class ResolvingTest {
public static void setup() {
LogStub.init();
Log.enableFailQuick(false);
SimpleCDMill.init();
}

protected Optional<ASTCDCompilationUnit> parseAndTransform(String model){
Expand Down Expand Up @@ -118,5 +119,12 @@ public void testInterModelInvalid(){
}
}

@Test
public void testNameInvalid() {
SimpleCDMill.globalScope().resolveType("veryInvalid(b(#)(%$(@");
assert (!Log.getFindings().isEmpty());
assert (Log.getFindings().get(0).getMsg().startsWith("0xFDAB0"));
}


}

0 comments on commit 04020ac

Please sign in to comment.