Skip to content

Commit

Permalink
Revert move of getModuleName etc. from util::Reflective
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulKlint committed Dec 15, 2024
1 parent 6033bb6 commit b3b4e6a
Showing 1 changed file with 0 additions and 237 deletions.
237 changes: 0 additions & 237 deletions src/org/rascalmpl/core/library/lang/rascalcore/check/CheckerCommon.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -27,247 +27,10 @@ import Set;
import String;
import ValueIO;
import util::Reflective;
import util::FileSystem;

import lang::rascalcore::compile::util::Names; // TODO: refactor, this is an undesired dependency on compile
import lang::rascalcore::compile::CompileTimeError;


str makeFileName(str qualifiedModuleName, str extension = "rsc") {
str qnameSlashes = replaceAll(qualifiedModuleName, "::", "/");
int n = findLast(qnameSlashes, "/");
str prefix = extension == "rsc" ? "" : "$";
str package = extension == "rsc" ? "" : "rascal/";
qnameSlashes = n < 0 ? "<prefix>" + qnameSlashes : qnameSlashes[0..n] + "/<prefix>" + qnameSlashes[n+1..];
return "<package><qnameSlashes><isEmpty(extension) ? "" : ".<extension>">";
}

loc getSearchPathLoc(str filePath, PathConfig pcfg){
for(loc dir <- pcfg.srcs + pcfg.libs){
fileLoc = dir + filePath;
if(exists(fileLoc)){
//println("getModuleLocation <qualifiedModuleName> =\> <fileLoc>");
return fileLoc;
}
}
throw "Module with path <filePath> not found";
}

@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");
for(loc dir <- pcfg.srcs){
fileLoc = dir + fileName;
if(exists(fileLoc)){
return fileLoc;
}
}
fileName = makeFileName(qualifiedModuleName, extension="tpl");
for(loc dir <- pcfg.libs){
fileLoc = dir + fileName;

if(exists(fileLoc)){
return fileLoc;
}
}
throw "Module `<qualifiedModuleName>` not found;\n<pcfg>";
}

tuple[str,str] splitFileExtension(str path){
int n = findLast(path, ".");
if(n < 0) return <path, "">;
return <path[0 .. n], path[n+1 .. ]>;
}

@synopsis{Determine length of common suffix of list of strings}
int commonSuffix(list[str] dir, list[str] m)
= commonPrefix(reverse(dir), reverse(m));

@synopsis{Determine length of common prefix of list of strings}
int commonPrefix(list[str] rdir, list[str] rm){
for(int i <- index(rm)){
if(i >= size(rdir)){
return i;
} else if(rdir[i] != rm[i]){
return i;
} else {
continue;
}
}
return size(rm);
}

@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(!( rscFile || tplFile )){
throw "Not a Rascal .src or .tpl file: <moduleLoc>";
}

// Find matching .rsc file in source directories
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;
}
}
}

// Find longest matching .tpl file in library directories

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

modulePathAsList = split("/", modulePathNoExt);
if(tplFile){
lastName = modulePathAsList[-1];
if(lastName[0] == "$"){
modulePathAsList = [*modulePathAsList[..-1],lastName[1..]];
}
}
if(modulePathAsList[0] == "rascal"){
modulePathAsList = modulePathAsList[1..];
}
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, dpath, "");
<candidate, ext> = splitFileExtension(candidate);
while(candidate[0] == "/"){
candidate = candidate[1..];
}

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

if(n > longestSuffix){
longestSuffix = n;
}
}
}

if(longestSuffix > 0){
lastName = modulePathAsList[-1];
if(lastName[0] == "$"){
modulePathAsList = [*modulePathAsList[..-1],lastName[1..]];
}
return intercalate("::", modulePathAsList[size(modulePathAsList) - longestSuffix .. ]);
}
throw "No module name found for <moduleLoc>;\nsrcs=<pcfg.srcs>;\nlibs=<pcfg.libs>";
}

@synopsis{Derive a location from a given module name for reading}
@description{
Given a module name, a file name extension, and a PathConfig,
a path name is constructed from the module name + extension.
If a file F with this path exists in one of the directories in the PathConfig,
then the pair <true, F> is returned. Otherwise <false, some error location> is returned.
For a source extension (typically "rsc" or "mu" but this can be configured) srcs is searched, otherwise binPath + libs.
}
@examples{
```rascal-shell
import util::Reflective;
getDerivedReadLoc("List", "rsc", pathConfig());
getDerivedReadLoc("experiments::Compiler::Compile", "rvm", pathConfig());
getDerivedReadLoc("experiments::Compiler::muRascal2RVM::Library", "mu", pathConfig());
```
}
@benefits{
This function is useful for type checking and compilation tasks, when derived information related to source modules has to be read
from locations in different, configurable, directories.
}

tuple[bool, loc] getDerivedReadLoc(str qualifiedModuleName, str extension, PathConfig pcfg, set[str] srcExtensions = {"rsc", "mu"}, str rootDir = ""){
fileName = makeFileName(qualifiedModuleName, extension=extension);
//println("getDerivedReadLoc: <fileName>");

if(extension in srcExtensions){
for(loc dir <- pcfg.srcs){ // In a source directory?
fileLoc = dir + rootDir + fileName;
if(exists(fileLoc)){
//println("getDerivedReadLoc: <qualifiedModuleName>, <extension> =\> <fileLoc");
return <true, fileLoc>;
}
}
} else {
for(loc dir <- pcfg.bin + pcfg.libs){ // In a bin or lib directory?

fileLoc = dir + rootDir + fileName;
if(exists(fileLoc)){
//println("getDerivedReadLoc: <qualifiedModuleName>, <extension> =\> <fileLoc>");
return <true, fileLoc>;
}
}
}
//println("getDerivedReadLoc: <qualifiedModuleName>, <extension> =\> |error:///|");
return <false, |error:///|>;
}


@synopsis{Derive a location from a given module name for writing}
@description{
Given a module name, a file name extension, and a PathConfig,
a path name is constructed from the module name + extension.
For source modules, a writable location cannot be derived.
For other modules, a location for this path in bin will be returned.
}
@examples{
```rascal-shell
import util::Reflective;
getDerivedWriteLoc("List", "rvm", pathConfig());
getDerivedWriteLoc("experiments::Compiler::Compile", "rvm", pathConfig());
```
```rascal-shell,error
getDerivedWriteLoc("experiments::Compiler::muRascal2RVM::Library", "rsc", pathConfig());
```
}
@benefits{
This function is useful for type checking and compilation tasks, when derived information related to source modules has to be written
to locations in separate, configurable, directories.
}
loc getDerivedWriteLoc(str qualifiedModuleName, str extension, PathConfig pcfg, set[str] srcExtensions = {"rsc", "mu"}, str rootDir = ""){
if(extension in srcExtensions){
throw "Cannot derive writable location for module <qualifiedModuleName> with extension <extension>";
}
fileNameSrc = makeFileName(qualifiedModuleName);
fileNameBin = makeFileName(qualifiedModuleName, extension=extension);

bin = pcfg.bin;
fileLocBin = bin + rootDir + fileNameBin;
//println("getDerivedWriteLoc: <qualifiedModuleName>, <extension> =\> <fileLocBin>");
return fileLocBin;
}

void checkSupportedByParserGenerator(Tree t, Collector c){
c.require("implemented by parsergenerator", t, [t], void(Solver s){
tp = s.getType(t);
Expand Down

0 comments on commit b3b4e6a

Please sign in to comment.