Skip to content

Commit

Permalink
Merge branch 'main' into json-lazy-parser-unparser
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenvinju committed Dec 17, 2024
2 parents 477708a + 043b663 commit 927373f
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions src/org/rascalmpl/library/util/Reflective.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ PathConfig applyManifests(PathConfig cfg) {
return cfg;
}

@deprecated{Function will be moved to Rascal compiler}
str makeFileName(str qualifiedModuleName, str extension = "rsc") {
str qnameSlashes = replaceAll(qualifiedModuleName, "::", "/");
int n = findLast(qnameSlashes, "/");
Expand All @@ -112,6 +113,7 @@ str makeFileName(str qualifiedModuleName, str extension = "rsc") {
return "<package><qnameSlashes><isEmpty(extension) ? "" : ".<extension>">";
}

@deprecated{Function will be moved to Rascal compiler}
loc getSearchPathLoc(str filePath, PathConfig pcfg){
for(loc dir <- pcfg.srcs + pcfg.libs){
fileLoc = dir + filePath;
Expand All @@ -123,6 +125,7 @@ loc getSearchPathLoc(str filePath, PathConfig pcfg){
throw "Module with path <filePath> not found";
}

@deprecated{Function will be moved to Rascal compiler}
@synopsis{Get the location of a named module, search for `src` in srcs and `tpl` in libs}
loc getModuleLocation(str qualifiedModuleName, PathConfig pcfg){
fileName = makeFileName(qualifiedModuleName, extension="rsc");
Expand All @@ -143,16 +146,19 @@ loc getModuleLocation(str qualifiedModuleName, PathConfig pcfg){
throw "Module `<qualifiedModuleName>` not found;\n<pcfg>";
}

@deprecated{Function will be moved to Rascal compiler}
tuple[str,str] splitFileExtension(str path){
int n = findLast(path, ".");
if(n < 0) return <path, "">;
return <path[0 .. n], path[n+1 .. ]>;
}

@deprecated{Function will be moved to Rascal compiler}
@synopsis{Determine length of common suffix of list of strings}
int commonSuffix(list[str] dir, list[str] m)
= commonPrefix(reverse(dir), reverse(m));

@deprecated{Function will be moved to Rascal compiler}
@synopsis{Determine length of common prefix of list of strings}
int commonPrefix(list[str] rdir, list[str] rm){
for(int i <- index(rm)){
Expand All @@ -167,25 +173,30 @@ int commonPrefix(list[str] rdir, list[str] rm){
return size(rm);
}

@deprecated{Function will be moved to Rascal compiler}
@synopsis{Find the module name corresponding to a given module location via its (src or tpl) location}
str getModuleName(loc moduleLoc, PathConfig pcfg){
modulePath = moduleLoc.path;

rscFile = endsWith(modulePath, "rsc");
tplFile = endsWith(modulePath, "tpl");

if(!(endsWith(modulePath, "rsc") || endsWith(modulePath, "tpl"))){
if(!( rscFile || tplFile )){
throw "Not a Rascal .src or .tpl file: <moduleLoc>";
}

// Find matching .rsc file in source directories

for(loc dir <- pcfg.srcs){
if(moduleLoc.authority == dir.authority && startsWith(modulePath, dir.path)) {
moduleName = replaceFirst(modulePath, dir.path, "");
<moduleName, ext> = splitFileExtension(moduleName);
if(moduleName[0] == "/"){
moduleName = moduleName[1..];
}
moduleName = replaceAll(moduleName, "/", "::");
return moduleName;
if(rscFile){
for(loc dir <- pcfg.srcs){
if(moduleLoc.authority == dir.authority && startsWith(modulePath, dir.path)) {
moduleName = replaceFirst(modulePath, dir.path, "");
<moduleName, ext> = splitFileExtension(moduleName);
if(moduleName[0] == "/"){
moduleName = moduleName[1..];
}
moduleName = replaceAll(moduleName, "/", "::");
return moduleName;
}
}
}

Expand All @@ -197,6 +208,12 @@ str getModuleName(loc moduleLoc, PathConfig pcfg){
}

modulePathAsList = split("/", modulePathNoExt);
if(tplFile){
lastName = modulePathAsList[-1];
if(lastName[0] == "$"){
modulePathAsList = [*modulePathAsList[..-1],lastName[1..]];
}
}
if(modulePathAsList[0] == "rascal"){
modulePathAsList = modulePathAsList[1..];
}
Expand All @@ -219,6 +236,10 @@ str getModuleName(loc moduleLoc, PathConfig pcfg){
}

candidateAsList = split("/", candidate);
lastName = candidateAsList[-1];
if(lastName[0] == "$"){
candidateAsList = [*candidateAsList[..-1],lastName[1..]];
}
// println("cand: <candidateAsList>, modpath: <modulePathAsList>");
n = commonPrefix(reverse(candidateAsList), modulePathReversed);

Expand All @@ -238,6 +259,7 @@ str getModuleName(loc moduleLoc, PathConfig pcfg){
throw "No module name found for <moduleLoc>;\nsrcs=<pcfg.srcs>;\nlibs=<pcfg.libs>";
}

@deprecated{Function will be moved to Rascal compiler}
@synopsis{Derive a location from a given module name for reading}
@description{
Given a module name, a file name extension, and a PathConfig,
Expand Down Expand Up @@ -287,7 +309,7 @@ tuple[bool, loc] getDerivedReadLoc(str qualifiedModuleName, str extension, PathC
return <false, |error:///|>;
}


@deprecated{Function will be moved to Rascal compiler}
@synopsis{Derive a location from a given module name for writing}
@description{
Given a module name, a file name extension, and a PathConfig,
Expand Down

0 comments on commit 927373f

Please sign in to comment.