diff --git a/src/org/rascalmpl/compiler/CompileParserGenerator.rsc b/src/org/rascalmpl/compiler/CompileParserGenerator.rsc index d653f40af78..f424fd2db02 100644 --- a/src/org/rascalmpl/compiler/CompileParserGenerator.rsc +++ b/src/org/rascalmpl/compiler/CompileParserGenerator.rsc @@ -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); } -} \ No newline at end of file +} diff --git a/src/org/rascalmpl/compiler/CompileTestSources.rsc b/src/org/rascalmpl/compiler/CompileTestSources.rsc index f9ce0e4b87a..5bd92aef95f 100644 --- a/src/org/rascalmpl/compiler/CompileTestSources.rsc +++ b/src/org/rascalmpl/compiler/CompileTestSources.rsc @@ -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", diff --git a/src/org/rascalmpl/compiler/RascalGenerateSources.rsc b/src/org/rascalmpl/compiler/RascalGenerateSources.rsc new file mode 100644 index 00000000000..0ff982cadd0 --- /dev/null +++ b/src/org/rascalmpl/compiler/RascalGenerateSources.rsc @@ -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. + '" + ); +} diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/CodeGen.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/CodeGen.rsc index 7c0cbdbd024..9333dbd9b2e 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/CodeGen.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/CodeGen.rsc @@ -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, ..class); + ' RascalExecutionContext rex = new RascalExecutionContext(new InputStreamReader(System.in), new PrintWriter(System.out), new PrintWriter(System.err), null, null); ' instance = new (rex); ' long init_time = System.currentTimeMillis(); ' diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/Tests.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/Tests.rsc index 93a6e7e6784..6e8b91d7e42 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/Tests.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/compile/muRascal2Java/Tests.rsc @@ -82,7 +82,7 @@ str generateTestClass(str preamble, str packageName, str className, list[MuFunct ' final GenerateActuals generator = new GenerateActuals(5, 5, 10); ' ' public Tests(){ - ' super(new RascalExecutionContext(new InputStreamReader(System.in), new PrintWriter(System.out), new PrintWriter(System.err), null, null, ..class)); + ' super(new RascalExecutionContext(new InputStreamReader(System.in), new PrintWriter(System.out), new PrintWriter(System.err), null, null)); ' ModuleStore store = $rex.getModuleStore(); ' store.importModule(.class, this.$rex, ::new); ' $me = store.getModule(.class); diff --git a/src/org/rascalmpl/library/lang/rascal/grammar/definition/Parameters.rsc b/src/org/rascalmpl/library/lang/rascal/grammar/definition/Parameters.rsc index 2caf299c18f..5db891c02d9 100644 --- a/src/org/rascalmpl/library/lang/rascal/grammar/definition/Parameters.rsc +++ b/src/org/rascalmpl/library/lang/rascal/grammar/definition/Parameters.rsc @@ -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; diff --git a/src/org/rascalmpl/runtime/$RascalModule.java b/src/org/rascalmpl/runtime/$RascalModule.java index cc718e0eea3..f52ed56109b 100644 --- a/src/org/rascalmpl/runtime/$RascalModule.java +++ b/src/org/rascalmpl/runtime/$RascalModule.java @@ -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; @@ -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); @@ -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) { @@ -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) { diff --git a/src/org/rascalmpl/runtime/RascalExecutionContext.java b/src/org/rascalmpl/runtime/RascalExecutionContext.java index f0d24603a15..3a2963afcd2 100644 --- a/src/org/rascalmpl/runtime/RascalExecutionContext.java +++ b/src/org/rascalmpl/runtime/RascalExecutionContext.java @@ -71,8 +71,7 @@ public RascalExecutionContext( PrintWriter outwriter, PrintWriter errwriter, PathConfig pcfg, - IDEServices ideServices, - Class clazz + IDEServices ideServices ){ currentModuleName = "UNDEFINED"; @@ -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(); diff --git a/src/org/rascalmpl/shell/RascalGenerateSources.java b/src/org/rascalmpl/shell/RascalGenerateSources.java new file mode 100644 index 00000000000..cab4025a886 --- /dev/null +++ b/src/org/rascalmpl/shell/RascalGenerateSources.java @@ -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)); + } +}