Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions src/org/rascalmpl/compiler/CompileParserGenerator.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,28 @@ import IO;
import lang::rascalcore::compile::Compile;

@synopsis{Compile the parser generator including all needed library modules}
void main(loc REPO = |file:///Users/paulklint/git/|,
loc COMPILED = REPO + "compiled-rascal"){
void main(loc rascalRepo = |project://rascal|,
loc generatedDir = rascalRepo + "generated",
loc license = |memory:///LICENSE|){

pcfg = pathConfig(
srcs = [ REPO + "rascal/src/org/rascalmpl/library",
REPO + "rascal/test/org/rascalmpl/benchmark/",
REPO + "rascal/src/org/rascalmpl/compiler",
REPO + "rascal/src/org/rascalmpl/tutor"],
bin = COMPILED + "/target/classes",
generatedSources = COMPILED + "/src/main/java",
generatedTestSources = COMPILED + "/src/test/java/",
generatedResources = COMPILED + "/src/main/java",
generatedTestResources = COMPILED + "/src/test/java/",
srcs = [ rascalRepo + "src/org/rascalmpl/library",
rascalRepo + "test/org/rascalmpl/benchmark/",
rascalRepo + "src/org/rascalmpl/compiler",
rascalRepo + "src/org/rascalmpl/tutor"],
bin = generatedDir + "/target/classes",
generatedSources = generatedDir + "/src/main/java",
generatedTestSources = generatedDir + "/src/test/java/",
generatedResources = generatedDir + "/src/main/java",
generatedTestResources = generatedDir + "/src/test/java/",
libs = [ ]
);
);

ccfg = rascalCompilerConfig(pcfg)[verbose = true][logWrittenFiles=true];
ccfg = rascalCompilerConfig(pcfg)[verbose = true][logWrittenFiles=true][license=license];
msgs = compile(["lang::rascal::grammar::ParserGenerator"], ccfg);
if(isEmpty(msgs)){
println("ok");
} else {
iprintln(msgs);
}
}
}
2 changes: 1 addition & 1 deletion src/org/rascalmpl/compiler/CompileTestSources.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void compileTestSources(PathConfig pcfg) {
testCompilerConfig = getAllSrcCompilerConfig()[logPathConfig=false];
total = 0;

println(readFile(|lib://rascal/META-INF/MANIFEST.MF|));
println(readFile(|project://rascal/META-INF/MANIFEST.MF|));

libraryModules = ["Boolean",
"DateTime",
Expand Down
70 changes: 70 additions & 0 deletions src/org/rascalmpl/compiler/RascalGenerateSources.rsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
@license{
Copyright (c) 2018-2025, NWO-I CWI, Swat.engineering and Paul Klint
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
}
module RascalGenerateSources

import IO;

import CompileParserGenerator;

@synopsis{Entry point for code generation}
void main(loc rascalRepo = |project://rascal|, loc generatedDir = rascalRepo + "generated", loc license = |memory:///LICENSE|) {
if (!license?) {
saveDefaultLicense();
}

CompileParserGenerator::main(rascalRepo=rascalRepo, generatedDir=generatedDir, license=license);
}

private void saveDefaultLicense() {
writeFile(|memory:///LICENSE|,
"Copyright (c) 2018-2025, NWO-I CWI and Swat.engineering
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without
'modification, are permitted provided that the following conditions are met:
'
'1. Redistributions of source code must retain the above copyright notice,
'this list of conditions and the following disclaimer.
'
'2. Redistributions in binary form must reproduce the above copyright notice,
'this list of conditions and the following disclaimer in the documentation
'and/or other materials provided with the distribution.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"
'AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
'IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
'ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
'LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
'CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
'SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
'INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
'ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
'POSSIBILITY OF SUCH DAMAGE.
'"
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ tuple[JCode, JCode, JCode, list[value]] muRascal2Java(MuModule m, ModuleStatus m

main_method = "public static void main(String[] args) {
' long start_time = System.currentTimeMillis();
' RascalExecutionContext rex = new RascalExecutionContext(new InputStreamReader(System.in), new PrintWriter(System.out), new PrintWriter(System.err), null, null, <packageName>.<baseClassName>.class);
' RascalExecutionContext rex = new RascalExecutionContext(new InputStreamReader(System.in), new PrintWriter(System.out), new PrintWriter(System.err), null, null);
' <baseClassName> instance = new <baseClassName>(rex);
' long init_time = System.currentTimeMillis();
' <if (!hasListStrArgs && !hasDefaultArgs) {>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ str generateTestClass(str preamble, str packageName, str className, list[MuFunct
' final GenerateActuals generator = new GenerateActuals(5, 5, 10);
'
' public <className>Tests(){
' super(new RascalExecutionContext(new InputStreamReader(System.in), new PrintWriter(System.out), new PrintWriter(System.err), null, null, <packageName>.<className>.class));
' super(new RascalExecutionContext(new InputStreamReader(System.in), new PrintWriter(System.out), new PrintWriter(System.err), null, null));
' ModuleStore store = $rex.getModuleStore();
' store.importModule(<className>.class, this.$rex, <className>::new);
' $me = store.getModule(<className>.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ set[Production] expand(set[Production] prods) {

instantiated = {};
while (uses != {}) {
instances = {};
set[Production] instances = {};
for (u <- uses, def <- defs, def.def.name == u.name) {
name = u.name;
actuals = u.parameters;
Expand Down
31 changes: 31 additions & 0 deletions src/org/rascalmpl/runtime/$RascalModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.rascalmpl.interpreter.utils.IResourceLocationProvider;
import org.rascalmpl.interpreter.utils.RascalManifest;
import org.rascalmpl.library.util.PathConfig;
import org.rascalmpl.library.util.ParseErrorRecovery;
import org.rascalmpl.library.util.ToplevelType;
import org.rascalmpl.parser.gtd.result.out.INodeFlattener;
import org.rascalmpl.runtime.traverse.Traverse;
Expand Down Expand Up @@ -914,6 +915,10 @@ private IValueInputStream constructValueReader(ISourceLocation loc) throws IOExc

protected final IValue $aadt_get_field(final IConstructor cons, final String fieldName) {
Type consType = cons.getConstructorType();

if (consType == RascalValueFactory.Tree_Amb) {
throwOnAmbFieldAccess(cons, fieldName);
}

if(TreeAdapter.isTree(cons) && TreeAdapter.isAppl((ITree) cons)) {
FieldResult fldres = TreeAdapter.getLabeledField((ITree) cons, fieldName);
Expand Down Expand Up @@ -948,6 +953,28 @@ private IValueInputStream constructValueReader(ISourceLocation loc) throws IOExc
}
}

private void throwOnAmbFieldAccess(final IConstructor cons, final String fieldName) {
ITree tree = (ITree) cons;
ISet alts = (ISet) tree.get(0);
for (IValue alt : alts) {
if (TreeAdapter.getLabeledField((ITree) alt, fieldName) != null) {
// If the ambiguity is generated by error recovery we throw an ParseErrorRecovery exception,
// otherwise we throw an Ambiguity exception
if ((new ParseErrorRecovery($RVF)).hasParseErrors(tree).getValue()) {
throw RuntimeExceptionFactory.parseErrorRecoveryNoSuchField(fieldName, TreeAdapter.getLocation(tree));
}
else {
IString typeString = $RVF.string(SymbolAdapter.toString(tree, false));
IString treeString = $RVF.string(TreeAdapter.yield(tree));
ISourceLocation loc = TreeAdapter.getLocation(tree);
throw RuntimeExceptionFactory.ambiguity(loc, typeString, treeString);
}
}
}

throw RuntimeExceptionFactory.noSuchField(fieldName);
}

protected final IValue $aloc_get_field(final ISourceLocation sloc, final String field) {
IValue v;
switch (field) {
Expand Down Expand Up @@ -1783,6 +1810,10 @@ else if (iEndLine != -1 || iEndColumn != -1) {

Type consType = cons.getConstructorType();

if (consType == RascalValueFactory.Tree_Amb) {
throwOnAmbFieldAccess(cons, fieldName);
}

// Does fieldName exist as field in a parse tree?
if(TreeAdapter.isTree(cons)) {
if(TreeAdapter.isAppl((ITree) cons) && TreeAdapter.getLabeledField((ITree)cons, fieldName) != null) {
Expand Down
5 changes: 2 additions & 3 deletions src/org/rascalmpl/runtime/RascalExecutionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public RascalExecutionContext(
PrintWriter outwriter,
PrintWriter errwriter,
PathConfig pcfg,
IDEServices ideServices,
Class<?> clazz
IDEServices ideServices
){

currentModuleName = "UNDEFINED";
Expand All @@ -82,7 +81,7 @@ public RascalExecutionContext(
this.errwriter = errwriter;

this.pcfg = pcfg == null ? new PathConfig() : pcfg;
ISourceLocation projectRoot = inferProjectRoot(clazz);
ISourceLocation projectRoot = URIUtil.rootLocation("cwd");
this.ideServices = ideServices == null ? new BasicIDEServices(errwriter, this, null, projectRoot) : ideServices;
$RVF = new RascalRuntimeValueFactory(this);
$TF = TypeFactory.getInstance();
Expand Down
19 changes: 19 additions & 0 deletions src/org/rascalmpl/shell/RascalGenerateSources.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.rascalmpl.shell;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

import org.rascalmpl.debug.IRascalMonitor;
import org.rascalmpl.repl.streams.StreamUtil;

public class RascalGenerateSources extends AbstractCommandlineTool {
public static void main(String[] args) throws IOException {
var term = RascalShell.connectToTerminal();
var monitor = IRascalMonitor.buildConsoleMonitor(term);
var err = (monitor instanceof Writer) ? StreamUtil.generateErrorStream(term, (Writer)monitor) : new PrintWriter(System.err, true);
var out = (monitor instanceof PrintWriter) ? (PrintWriter) monitor : new PrintWriter(System.out, false);

System.exit(main("RascalGenerateSources", new String[] { "org/rascalmpl/compiler", "org/rascalmpl/typepal" }, args, term, monitor, err, out));
}
}
Loading