From 44e48389b6fde7867b766abe7a72c9eb1f49bacb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Wolski?= Date: Tue, 18 Dec 2018 09:02:16 +0100 Subject: [PATCH] ModularityPlugin: capture quotes directly in pattern - rename usePattern etc to includePattern, compile only once - add unit test --- .../plugins/modularity/ModularityPlugin.java | 20 +++++---- .../modularity/ModularityRegexTest.java | 42 +++++++++++++++++++ 2 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 org.coreasm.engine/test/org/coreasm/engine/test/plugins/modularity/ModularityRegexTest.java diff --git a/org.coreasm.engine/src/org/coreasm/engine/plugins/modularity/ModularityPlugin.java b/org.coreasm.engine/src/org/coreasm/engine/plugins/modularity/ModularityPlugin.java index 9f57910c..451d0936 100644 --- a/org.coreasm.engine/src/org/coreasm/engine/plugins/modularity/ModularityPlugin.java +++ b/org.coreasm.engine/src/org/coreasm/engine/plugins/modularity/ModularityPlugin.java @@ -65,6 +65,10 @@ public class ModularityPlugin extends Plugin implements ParserPlugin, private static final String[] keywords = {"CoreModule", "include"}; private static final String[] operators = {}; + + // compile pattern to find "include " directives using regular expression + private static final String includeRegex = "^[\\s]*include[\\s]+(\"([^\"]+)\"|([^\"]+))"; + public static final Pattern includePattern = Pattern.compile(includeRegex); private Map parsers = null; @@ -230,23 +234,21 @@ public Node apply(Object[] from) { private ArrayList injectModules(List lines, String relativePath) { // CHANGE: Includes inside included specifications will be looked up relative to the including specification now ArrayList newSpec = new ArrayList(); - // compile pattern to find "include " directives using regular expression - String useRegex = "^[\\s]*[i][n][c][l][u][d][e][\\s]+"; - Pattern usePattern = Pattern.compile(useRegex); for (SpecLine line : lines) { // get an "include" directive matcher object for the line - Matcher useMatcher = usePattern.matcher(line.text); + Matcher includeMatcher = includePattern.matcher(line.text); // if match found - if (useMatcher.find()) { + if (includeMatcher.find()) { // are there inner include files? // get the include file name and load the file - String fileName = useMatcher.replaceFirst("").trim(); - // remove potential quotation marks - while (fileName.startsWith("\"") && fileName.endsWith("\"")) - fileName = fileName.substring(1, fileName.length()-1); + String fileName = includeMatcher.group(2); + + if (fileName.contains("\"")) + capi.error("ModularityPlugin: unexpected quotation mark in module name '" + fileName + "'"); + // CHANGE: make sure that filenames have the same format to ensure that equal paths match, not only equal include statements try { fileName = (new File(relativePath, fileName)).getCanonicalPath(); diff --git a/org.coreasm.engine/test/org/coreasm/engine/test/plugins/modularity/ModularityRegexTest.java b/org.coreasm.engine/test/org/coreasm/engine/test/plugins/modularity/ModularityRegexTest.java new file mode 100644 index 00000000..649ce7ab --- /dev/null +++ b/org.coreasm.engine/test/org/coreasm/engine/test/plugins/modularity/ModularityRegexTest.java @@ -0,0 +1,42 @@ +package org.coreasm.engine.test.plugins.modularity; + +import org.coreasm.engine.plugins.modularity.ModularityPlugin; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import java.util.regex.Matcher; + +public class ModularityRegexTest { + + @Test + public void testRegex() { + String[] testLines = new String[]{ + "include a", + "include a.b", + "include ..\\a.b", + "include ../a.b", + + // quoted + "include \"a.b\"", + "include \"..\\a.b\"", + "include \"../a.b\"", + + // with white spaces + "\tinclude a", + "include\ta", + "include a\t", + + // with comment at the end + "include a // foo", + "include a /* foo */" + }; + + for (String line : testLines) { + Matcher includeMatcher = ModularityPlugin.includePattern.matcher(line); + + assertTrue(line, includeMatcher.find()); + } + } +}