Skip to content

Commit 5867002

Browse files
authored
Merge pull request #2729 from usethesource/fix/add-parser-load-test
fix/add parser load test
2 parents 586b096 + 67cb5e9 commit 5867002

4 files changed

Lines changed: 75 additions & 8 deletions

File tree

src/org/rascalmpl/library/lang/rascal/grammar/definition/Modules.rsc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import lang::rascal::grammar::definition::Literals;
1515
import lang::rascal::grammar::definition::Names;
1616
import Grammar;
1717
import Set;
18+
import IO;
19+
import util::Monitor;
1820

1921
@memo
2022
@synopsis{Converts internal module representation of Rascal interpreter to single grammar definition}
@@ -57,6 +59,9 @@ public Grammar fuse(GrammarDefinition def) {
5759
result = (compose(result, \mod.grammar) | compose(it, def.modules[i].grammar) | i <- deps[nm], def.modules[i]?);
5860
todo += (\mod.extends - done);
5961
}
62+
else {
63+
warning("Fuse algorithm misses module definition for dependency <nm>", |unknown:///|);
64+
}
6065
}
6166

6267
return result;
@@ -74,11 +79,11 @@ public tuple[str, set[str], set[str]] getModuleMetaInf(Module \mod) {
7479
// Tags tags "module" QualifiedName name ModuleParameters params Import* imports
7580
switch (\mod) {
7681
case \default(parameters(_, QualifiedName name, _, Import* is),_) :
77-
return <deslash("<name>"), { "<i>" | \default(\default(QualifiedName i)) <- is }
78-
, { "<i>" | \extend(\default(QualifiedName i)) <- is }>;
82+
return <deslash("<name>"), { deslash("<i>") | \default(\default(QualifiedName i)) <- is }
83+
, { deslash("<i>") | \extend(\default(QualifiedName i)) <- is }>;
7984
case \default(\default(_, QualifiedName name, Import* is), _) :
80-
return <deslash("<name>"), { "<i>" | \default(\default(QualifiedName i)) <- is }
81-
, { "<i>" | \extend(\default(QualifiedName i)) <- is }>;
85+
return <deslash("<name>"), { deslash("<i>") | \default(\default(QualifiedName i)) <- is }
86+
, { deslash("<i>") | \extend(\default(QualifiedName i)) <- is }>;
8287
}
8388

8489
throw "unexpected module syntax <\mod>";

src/org/rascalmpl/library/lang/rascal/grammar/storage/ModuleParserStorage.rsc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ the `.rsc` respective file.
2020
* loading modules without having to first load and use a parser generator can be up 1000 times faster.
2121
}
2222
@pitfalls{
23-
:::warning
24-
This caching feature is _static_. There is no automated cache clearance.
23+
* This caching feature is _static_. There is no automated cache clearance.
2524
If your grammars change, any saved `.parsers` files do not change with it.
2625
It is advised that you programmatically execute this compiler at deployment time
2726
to store the `.parsers` file _only_ in deployed `jar` files. That way, you can not
2827
be bitten by a concrete syntax parser that is out of date at development time.
29-
:::
3028
}
3129
@license{
3230
Copyright (c) 2009-2023 NWO-I CWI
@@ -50,6 +48,7 @@ import ParseTree;
5048
import Grammar;
5149
import IO;
5250
import Exception;
51+
import String;
5352

5453
@synopsis{For all modules in pcfg.srcs this will produce a `.parsers` stored parser capable of parsing concrete syntax fragment in said module.}
5554
@description{
@@ -114,7 +113,7 @@ void storeParsersForModules(set[loc] moduleFiles, PathConfig pcfg) {
114113
115114
void storeParsersForModules(set[Module] modules, PathConfig pcfg) {
116115
for (m <- modules) {
117-
storeParserForModule("<m.header.name>", m@\loc, modules, pcfg);
116+
storeParserForModule(deslash("<m.header.name>"), m@\loc, modules, pcfg);
118117
}
119118
}
120119
@@ -123,6 +122,9 @@ void storeParserForModule(str main, loc file, set[Module] modules, PathConfig pc
123122
// with import and extend. Each main module has a different grammar because of this.
124123
def = modules2definition(main, modules);
125124
125+
println("storeParserForModule def:");
126+
iprintln(def);
127+
126128
// here the layout semantics comes really into action
127129
gr = resolve(fuse(layouts(def)));
128130
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module lang::rascal::tests::functionality::storedParsers::ExampleModule
2+
3+
import lang::pico::\syntax::Main;
4+
import util::Math;
5+
import ValueIO;
6+
7+
int compute((Expression) `<Expression a> + <Expression b>`) = compute(a) + compute(b);
8+
int compute((Expression) `<Natural c>`) = readTextValueString(#int, "<c>");
9+
10+
test bool testCompute() {
11+
example = (Expression) `1 + 1`;
12+
return compute(example) == 2;
13+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module lang::rascal::tests::functionality::storedParsers::StoredParserTests
2+
3+
import lang::rascal::grammar::storage::ModuleParserStorage;
4+
import util::Reflective;
5+
import util::Eval;
6+
import IO;
7+
import Location;
8+
import util::PathConfig;
9+
10+
void prepareStorage(PathConfig pcfg) {
11+
storeParsersForModules(
12+
{
13+
resolveLocation(|project://rascal/src/org/rascalmpl/library/lang/rascal/tests/functionality/storedParsers/ExampleModule.rsc|),
14+
resolveLocation(|project://rascal/src/org/rascalmpl/library/lang/pico/syntax/Main.rsc|)
15+
},
16+
pcfg);
17+
}
18+
19+
test bool testStorageAndUse() {
20+
pcfg = getProjectPathConfig(|project://rascal|);
21+
prepareStorage(pcfg);
22+
23+
// first we run in the source environment. There is no cached parser
24+
// there necause it is written to the target folder
25+
rt = createRascalRuntime(pcfg=pcfg);
26+
rt.eval(#void, "import lang::rascal::tests::functionality::storedParsers::ExampleModule;");
27+
28+
// a bit of fiddling to allow the Rascal project itself to be a testbed for this configuration
29+
modLoc = srcsFile("lang::rascal::tests::functionality::storedParsers::ExampleModule", pcfg, fileConfig());
30+
generatedPath = pcfg.bin + relativize(pcfg.srcs, modLoc)[extension="parsers"].path;
31+
targetLibrary = pcfg.bin + "org/rascalmpl/library";
32+
requiredPath = ((pcfg.bin + "org/rascalmpl/library") + relativize(pcfg.srcs, modLoc).path)[extension="parsers"];
33+
println(requiredPath);
34+
copy(generatedPath, requiredPath);
35+
36+
// we create a runtime which includes the generate .parser file, and we run again
37+
pcfg.srcs = [pcfg.bin + "org/rascalmpl/library"];
38+
rt = createRascalRuntime(pcfg=pcfg);
39+
rt.eval(#void, "import lang::rascal::tests::functionality::storedParsers::ExampleModule;");
40+
41+
return result(true) := rt.eval(#bool, "testCompute()");
42+
}
43+
44+
45+
46+
47+

0 commit comments

Comments
 (0)