Skip to content

Commit

Permalink
Improved getModuleName for use with library modules
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulKlint committed Sep 25, 2024
1 parent 039a443 commit 5e213b6
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions src/org/rascalmpl/library/util/Reflective.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ loc getModuleLocation(str qualifiedModuleName, PathConfig pcfg){
fileName = makeFileName(qualifiedModuleName, extension="tpl");
for(loc dir <- pcfg.libs){
fileLoc = dir + fileName;

if(exists(fileLoc)){
return fileLoc;
}
Expand Down Expand Up @@ -170,15 +171,11 @@ int commonPrefix(list[str] rdir, list[str] rm){
str getModuleName(loc moduleLoc, PathConfig pcfg){
modulePath = moduleLoc.path;

if(!endsWith(modulePath, "rsc")){
throw "Not a Rascal source file: <moduleLoc>";
if(!(endsWith(modulePath, "rsc") || endsWith(modulePath, "tpl"))){
throw "Not a Rascal .src or .tpl file: <moduleLoc>";
}
<modulePathNoExt, ext> = splitFileExtension(modulePath);
if(modulePathNoExt[0] == "/"){
modulePathNoExt = modulePathNoExt[1..];
}
modulePathAsList = split("/", modulePathNoExt);
modulePathAsListReversed = reverse(modulePathAsList);

// Find matching .rsc file in source directories

for(loc dir <- pcfg.srcs){
if(moduleLoc.authority == dir.authority && startsWith(modulePath, dir.path)) {
Expand All @@ -191,25 +188,43 @@ str getModuleName(loc moduleLoc, PathConfig pcfg){
return moduleName;
}
}


// Find longest matching .tpl file in library directories

<modulePathNoExt, ext> = splitFileExtension(modulePath);
while(modulePathNoExt[0] == "/"){
modulePathNoExt = modulePathNoExt[1..];
}
modulePathAsList = split("/", modulePathNoExt);
modulePathReversed = reverse(modulePathAsList);

int longestSuffix = 0;
for(loc dir <- pcfg.libs){
dir = dir + "rascal";
dpath = dir.path;

while(dpath[0] == "/"){
dpath = dpath[1..];
}

for(loc file <- find(dir, "tpl")){
candidate = replaceFirst(file.path, dir.path, "");
candidate = replaceLast(candidate, "$", "");
if(candidate[0] == "/"){
candidate = replaceFirst(file.path, dpath, "");
<candidate, ext> = splitFileExtension(candidate);
while(candidate[0] == "/"){
candidate = candidate[1..];
}
if(candidate[0] == "$"){
candidate = candidate[1..];
}
<candidate, ext> = splitFileExtension(candidate);
candidateAsList = split("/", candidate);
n = commonPrefix(reverse(candidateAsList), modulePathAsListReversed);
//println("<candidateAsList>, <modulePathAsList> =\> <n>");
n = commonPrefix(reverse(candidateAsList), modulePathReversed);
if(n > longestSuffix){
longestSuffix = n;
}
}
}

if(longestSuffix > 0){
return intercalate("::", modulePathAsList[size(modulePathAsList) - longestSuffix .. ]);
}
Expand Down

0 comments on commit 5e213b6

Please sign in to comment.