Skip to content

Commit

Permalink
ModularityPlugin: capture quotes directly in pattern
Browse files Browse the repository at this point in the history
- rename usePattern etc to includePattern, compile only once
- add unit test
  • Loading branch information
Locke committed Feb 6, 2019
1 parent 57cf531 commit 44e4838
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, GrammarRule> parsers = null;

Expand Down Expand Up @@ -230,23 +234,21 @@ public Node apply(Object[] from) {
private ArrayList<SpecLine> injectModules(List<SpecLine> lines, String relativePath) {
// CHANGE: Includes inside included specifications will be looked up relative to the including specification now
ArrayList<SpecLine> newSpec = new ArrayList<SpecLine>();
// 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
}

0 comments on commit 44e4838

Please sign in to comment.