Skip to main content

module util::Reflective

rascal-0.34.0

Usage

import util::Reflective;

Dependencies

import ParseTree;
import IO;
import String;
import lang::rascal::\syntax::Rascal;
import lang::manifest::IO;

function getLineSeparator

Returns the system-dependent line separator string

str getLineSeparator()

function evalCommands

lrel[str result, str out, str err] evalCommands(list[str] command, loc org)

function parseModule

Just parse a module at a given location without any furter processing (i.e., fragment parsing) or side-effects (e.g. module loading)

lang::rascal::\syntax::Rascal::Module parseModule(loc location)

function parseModuleWithSpaces

Parse a module (including surounding spaces) at a given location without any furter processing (i.e., fragment parsing) or side-effects (e.g. module loading)

start[Module] parseModuleWithSpaces(loc location)

data RascalConfigMode

data RascalConfigMode  
= compiler()
| interpreter()
;

data PathConfig

data PathConfig  
= pathConfig(list[loc] srcs = [|std:///|], // List of directories to search for source files
list[loc] ignores = [], // List of locations to ignore from the source files
loc bin = |home:///bin/|, // Global directory for derived files outside projects
list[loc] libs = [|lib://rascal/|], // List of directories to search source for derived files
list[loc] javaCompilerPath = [], // TODO: must generate the same defaults as in PathConfig
list[loc] classloaders = [|system:///|] // TODO: must generate the same defaults as in PathConfig
)
;

data RascalManifest

data RascalManifest  
= rascalManifest(
str \Project-Name = "Project",
str \Main-Module = "Plugin",
str \Main-Function = "main",
list[str] Source = ["src"],
str Bin = "bin",
list[str] \Required-Libraries = [],
list[str] \Required-Dependencies = []
)
;

data JavaBundleManifest

data JavaBundleManifest  
= javaManifest(
str \Manifest-Version = "",
str \Bundle-SymbolicName = "",
str \Bundle-RequiredExecutionEnvironment = "JavaSE-1.8",
list[str] \Require-Bundle = [],
str \Bundle-Version = "0.0.0.qualifier",
list[str] \Export-Package = [],
str \Bundle-Vendor = "",
str \Bundle-Name = "",
list[str] \Bundle-ClassPath = [],
list[str] \Import-Package = []
)
;

function metafile

loc metafile(loc l)

function applyManifests

Converts a PathConfig and replaces all references to roots of projects or bundles by the folders which are nested under these roots as configured in their respective META-INF/RASCAL.MF files.

PathConfig applyManifests(PathConfig cfg)

function makeFileName

str makeFileName(str qualifiedModuleName, str extension = "rsc")

function getSearchPathLoc

loc getSearchPathLoc(str filePath, PathConfig pcfg)

function getModuleLocation

loc getModuleLocation(str qualifiedModuleName,  PathConfig pcfg, str extension = "rsc")

function splitFileExtension

tuple[str,str] splitFileExtension(str path)

function getModuleName

str getModuleName(loc moduleLoc,  PathConfig pcfg, set[str] extensions = {"tc", "tpl"})

function getDerivedReadLoc

Derive a location from a given module name for reading

tuple[bool, loc] getDerivedReadLoc(str qualifiedModuleName, str extension, PathConfig pcfg, set[str] srcExtensions = {"rsc", "mu"}, str rootDir = "")

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>import util::Reflective;
ok
rascal>getDerivedReadLoc("List", "rsc", pathConfig());
tuple[bool,loc]: <true,|std:///List.rsc|>
rascal>getDerivedReadLoc("experiments::Compiler::Compile", "rvm", pathConfig());
tuple[bool,loc]: <false,|error:///|>
rascal>getDerivedReadLoc("experiments::Compiler::muRascal2RVM::Library", "mu", pathConfig());
tuple[bool,loc]: <false,|error:///|>

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.

function getDerivedWriteLoc

Derive a location from a given module name for writing

loc getDerivedWriteLoc(str qualifiedModuleName, str extension, PathConfig pcfg, set[str] srcExtensions = {"rsc", "mu"}, str rootDir = "")

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>import util::Reflective;
ok
rascal>getDerivedWriteLoc("List", "rvm", pathConfig());
loc: |home:///bin/List.rvm|
rascal>getDerivedWriteLoc("experiments::Compiler::Compile", "rvm", pathConfig());
loc: |home:///bin/experiments/Compiler/Compile.rvm|
rascal>getDerivedWriteLoc("experiments::Compiler::muRascal2RVM::Library", "rsc", pathConfig());
|prompt:///|(0,18,<1,0>,<1,18>): Undeclared variable: getDerivedWriteLoc
Advice: |https://www.rascal-mpl.org/docs/Rascal/Errors/CompileTimeErrors/UndeclaredVariable|
ok

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.

function getProjectPathConfig

PathConfig getProjectPathConfig(loc projectRoot, RascalConfigMode mode = compiler())

function inCompiledMode

Is the current Rascal code executed by the compiler or the interpreter?

bool inCompiledMode()

function diff

Give a textual diff between two values.

str diff(value old, value new)

function watch

Watch value val:

  • running in interpreted mode: write val to a file,
  • running in compiled mode: compare val with previously written value
&T watch(type[&T] tp, &T val, str name)

&T watch(type[&T] tp, &T val, str name, value suffix)

function getFingerprint

Compute a fingerprint of a value for the benefit of the compiler and the compiler runtime

int getFingerprint(value val, bool concretePatterns)

function getFingerprint

Compute a fingerprint of a value and arity modifier for the benefit of the compiler and the compiler runtime

int getFingerprint(value val, int arity, bool concretePatterns)

function getFingerprintNode

Compute a fingerprint of a complete node for the benefit of the compiler and the compiler runtime

int getFingerprintNode(node nd)

function throwNullPointerException

Throw a raw Java NullPointerException, to help simulate an unexpected exception in test scenarios

void throwNullPointerException()

function getRascalReservedIdentifiers

Return a list of all Rascal reserved identifiers (a.k.a. keywords)

set[str] getRascalReservedIdentifiers()

function getRascalVersion

str getRascalVersion()

function newRascalProject

Create a folder structure for an empty Rascal project with Maven support

void newRascalProject(loc folder, str group="org.rascalmpl", str version="0.1.0-SNAPSHOT")

function pomFile

loc pomFile(loc folder)

function emptyModule

str emptyModule()

function rascalMF

str rascalMF(str name)

function pomXml

str pomXml(str name, str group, str version)