Skip to content

Commit

Permalink
Fixed omission in grammar generation; removed ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulKlint committed Oct 20, 2024
1 parent e42bcfa commit 3dad72c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import lang::rascal::\syntax::Rascal;

void collect(current: (SyntaxDefinition) `<Visibility vis> layout <Sym defined> = <Prod production>;`, Collector c){
declareSyntax(current, layoutSyntax(), layoutId(), c, vis=getVis(vis, publicVis()));
}
}

void collect (current: (SyntaxDefinition) `lexical <Sym defined> = <Prod production>;`, Collector c){
declareSyntax(current, lexicalSyntax(), lexicalId(), c);
}

void collect (current: (SyntaxDefinition) `keyword <Sym defined> = <Prod production>;`, Collector c){
declareSyntax(current, keywordSyntax(), keywordId(), c);
}
}

void collect (current: (SyntaxDefinition) `<Start strt> syntax <Sym defined> = <Prod production>;`, Collector c){
declareSyntax(current, contextFreeSyntax(), nonterminalId(), c);
Expand All @@ -42,20 +42,20 @@ void declareSyntax(SyntaxDefinition current, SyntaxRole syntaxRole, IdRole idRol
Sym defined = current.defined;
Prod production = current.production;
nonterminalType = defsym2AType(defined, syntaxRole);

if(isADTAType(nonterminalType)){
adtName = nonterminalType.adtName;

typeParameters = getTypeParameters(defined);
if(!isEmpty(typeParameters)){
nonterminalType = nonterminalType[parameters=[ aparameter("<tp.nonterminal>", treeType,closed=true)| tp <- typeParameters ]];
}
dt = defType(nonterminalType);
dt.vis = vis;
dt.md5 = md5Hash("<current is language ? current.\start : ""><adtName><syndefCounter><unparseNoLayout(defined)>");
syndefCounter += 1;

dt = defType(current is language && current.\start is present ? \start(nonterminalType) : nonterminalType);
dt.vis = vis;
dt.md5 = md5Hash("<current is language ? "<current.\start>" : ""><adtName><syndefCounter><unparseNoLayout(defined)>");
syndefCounter += 1;

// Define the syntax symbol itself and all labelled alternatives as constructors
c.define(adtName, idRole, current, dt);

Expand All @@ -64,7 +64,7 @@ void declareSyntax(SyntaxDefinition current, SyntaxRole syntaxRole, IdRole idRol
beginDefineOrReuseTypeParameters(c,closed=false);
collect(defined, c);
endDefineOrReuseTypeParameters(c);

// visit all the productions in the parent scope of the syntax declaration
c.push(currentAdt, <current, [], 0, adtParentScope>);
beginUseTypeParameters(c,closed=true);
Expand All @@ -78,10 +78,10 @@ void declareSyntax(SyntaxDefinition current, SyntaxRole syntaxRole, IdRole idRol
}

// ---- Prod ------------------------------------------------------------------

AProduction getProd(AType adtType, Tree tree, Solver s){
symType = s.getType(tree);
if(aprod(AProduction p) := symType) return p;
if(aprod(AProduction p) := symType) return p;
return prod(adtType, [symType]/*, src=getLoc(tree)*/);
}

Expand All @@ -95,14 +95,14 @@ void requireNonLayout(Tree current, AType u, str msg, Solver s){
}

AProduction computeProd(Tree current, str name, AType adtType, ProdModifier* modifiers, list[Sym] symbols, Solver s) {
args = [s.getType(sym) | sym <- symbols];
args = [s.getType(sym) | sym <- symbols];
m2a = mods2attrs(modifiers);
src = getLoc(current);
p = isEmpty(m2a) ? prod(adtType, args/*, src=src*/) : prod(adtType, args, attributes=m2a/*, src=src*/);
if(name != ""){
p.alabel = name;
}

forbidConsecutiveLayout(current, args, s);
if(!isEmpty(args)){
requireNonLayout(current, args[0], "at begin of production", s);
Expand All @@ -123,7 +123,7 @@ private default AType removeChainRule(AType t) = t;

void collect(current: (Prod) `<ProdModifier* modifiers> <Name name> : <Sym* syms>`, Collector c){
symbols = [sym | sym <- syms];

if(<Tree adt, _, _, loc adtParentScope> := c.top(currentAdt)){
// Compute the production type
c.calculate("named production", current, adt + symbols,
Expand All @@ -142,9 +142,9 @@ void collect(current: (Prod) `<ProdModifier* modifiers> <Name name> : <Sym* syms
println("*** Collect: <adt> is not a SyntaxDefinition ***");
}
qualName = "<SyntaxDefinition sd := adt ? sd.defined.nonterminal : "???">_<unescape("<name>")>";

// Define the constructor
c.defineInScope(adtParentScope, unescape("<name>"), constructorId(), name, defType([current],
c.defineInScope(adtParentScope, unescape("<name>"), constructorId(), name, defType([current],
AType(Solver s){
ptype = s.getType(current);
if(aprod(AProduction cprod) := ptype){
Expand All @@ -153,14 +153,14 @@ void collect(current: (Prod) `<ProdModifier* modifiers> <Name name> : <Sym* syms
}
def = cprod.def;
fields = [ inLexicalAdt && isLexicalAType(stp) ? astr() : stp
|
sym <- symbols,
!isTerminalSym(sym),
tsym := s.getType(sym),
|
sym <- symbols,
!isTerminalSym(sym),
tsym := s.getType(sym),
isNonTerminalAType(tsym),
stp := getSyntaxType(removeChainRule(tsym), s)
];
];

def = \start(sdef) := def ? sdef : def;
//def = \start(sdef) := def ? sdef : unset(def, "alabel");
return acons(def, fields, [], alabel=unescape("<name>"));
Expand All @@ -178,7 +178,7 @@ void collect(current: (Prod) `<ProdModifier* modifiers> <Name name> : <Sym* syms

void collect(current: (Prod) `<ProdModifier* modifiers> <Sym* syms>`, Collector c){
symbols = [sym | sym <- syms];

if(<Tree adt, _, _, _> := c.top(currentAdt)){
c.calculate("unnamed production", current, adt + symbols,
AType(Solver s){
Expand Down Expand Up @@ -206,7 +206,7 @@ void collect(current: (Prod) `<Assoc ass> ( <Prod group> )`, Collector c){
case "non-assoc": asc = AAssociativity::\a-non-assoc();
case "right": asc = AAssociativity::aright();
}

if(<Tree adt, _, _, _> := c.top(currentAdt)){
c.calculate("assoc", current, [adt, group],
AType(Solver s){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import Set;
import lang::rascalcore::check::ATypeBase;

TModel check(str moduleName, RascalCompilerConfig compilerConfig){
ModuleStatus result = rascalTModelForNames([moduleName],

ModuleStatus result = rascalTModelForNames([moduleName],
compilerConfig,
dummy_compile1);
<found, tm, result> = getTModelForModule(moduleName, result);
Expand Down Expand Up @@ -89,31 +89,31 @@ set[loc] reduce(set[loc] locs, set[str] restrict){
tuple[TModel old, TModel new] checkBoth(str oldM, str newM){
remove(|memory://stableHashProject/|, recursive=true);
pcfg = pathConfig(
srcs=[|memory://stableHashProject/src|],
bin=|memory://stableHashProject/bin|,
generatedSources=|memory://stableHashProject/generated|,
resources=|memory://stableHashProject/resources|,
libs=[]
);
// First create old version of M
writeFile(|memory://stableHashProject/src/M.rsc|, "module M\n<oldM>\n");
ccfg = rascalCompilerConfig(pcfg)[verbose=false][logPathConfig=false];
tmOld = check("M", ccfg);
if(verbose && !isEmpty(tmOld.messages)) println("old: <tmOld.messages>");
// Remove source and generated tpls
remove(|memory://stableHashProject/|, recursive=true);
// Next create new version of M
writeFile(|memory://stableHashProject/src/M.rsc|, "module M\n<newM>");
tmNew = check("M", ccfg);
if(verbose && !isEmpty(tmNew.messages)) println("new: <tmNew.messages>");
return <tmOld, tmNew>;
}
Expand All @@ -129,7 +129,7 @@ tuple[set[loc] old, set[loc] new] checkAndReduce(str oldM, str newM, set[str] re
test bool moduleContentChanged() =
expectSubset("", "int f(int n) = n + 1;");
// Variable declarations
test bool varDeclLayoutChanged()
Expand All @@ -148,7 +148,7 @@ test bool varDeclSameStr()
test bool annoLayoutChanged()
= expectEqual("anno int node@i;", "anno int node@i;");
test bool annoChanged()
= expectNotEqual("anno int node@i;", "anno real node@i;");
Expand All @@ -170,7 +170,7 @@ test bool funDefaultChanged()
test bool funLayoutChanged()
= expectEqual("int f(int n) = n + 1;", "int f (int n) = n + 1;");
test bool funResultChanged()
= expectNotEqual("int f(int n) = n + 1;", "value f(int n) = n + 1;");
Expand Down Expand Up @@ -202,19 +202,19 @@ test bool funFailRemoved()
= expectNotEqual("int f(int n) { x = n + 1; fail; }", "int f(int n) { x = n + 2; return x; }");
test bool funsSwitched()
= expectEqual("int f(int n) = n + 1; int g(int n) = n + 1;",
= expectEqual("int f(int n) = n + 1; int g(int n) = n + 1;",
"int g(int n) = n + 1; int f(int n) = n + 1;");
test bool funsCommentAdded()
= expectEqual("int f(int n) { return n + 1; } int g(int n) { return n + 1; }",
= expectEqual("int f(int n) { return n + 1; } int g(int n) { return n + 1; }",
"int f(int n) { /*C1*/ return n + 1; } /*C2*/ int g(int n) { /*C3*/ return n + 1; }");
test bool funAdded()
= expectSubset("int f(int n) = n + 1;",
= expectSubset("int f(int n) = n + 1;",
"int f(int n) = n + 1; int g(int n) = n + 2;");
test bool funDeleted()
= expectSuperset("int f(int n) = n + 1; int g(int n) = n + 2;",
= expectSuperset("int f(int n) = n + 1; int g(int n) = n + 2;",
"int f(int n) = n + 1;");
// Data declarations
Expand All @@ -230,30 +230,30 @@ test bool consAdded()
test bool consDeleted()
= expectSuperset("data D = d(int n);", "data D;");
test bool consSwitched()
= expectEqual("data D = d(int n) | e(str s);", "data D = e(str s) | d(int n);");
test bool altLayoutChanged()
= expectEqual("data D = d(int n) | e(str s);", "data D = d(int n) | e(str s);");
= expectEqual("data D = d(int n) | e(str s);", "data D = d(int n) | e(str s);");
test bool consFieldChanged()
= expectNotEqual("data D = d(int n);", "data D = d(int m);");
test bool consFieldsSwitched()
= expectNotEqual("data D = d(int n, int m);", "data D = d(int m, int n);");
test bool consFieldLayoutChanged1()
= expectEqual("data D = d(int n);", "data D = d( int n );");
test bool consFieldLayoutChanged2()
= expectEqual("data D = d(int n);", "data D = d (int n);");
// Keyword fields n and m generate separate locs, therefore we filter on constructors
test bool consKwFieldChanged()
= expectEqual("data D = d(int n = 0);", "data D = d(int m = 0);", restrict = {"constructor"});
test bool consKwFieldLayoutChanged()
= expectEqual("data D = d(int n = 0);", "data D = d(int n = 0);");
Expand Down Expand Up @@ -286,7 +286,6 @@ test bool synParameterChanged()
test bool synParameterLayoutChanged()
= expectEqualGrammar("syntax A[&T] = &T;", "syntax A[ &T ] = &T ;");
@ignore{It seems start is not part of the grammar}
test bool synStartChanged()
= expectNotEqualGrammar("syntax A = \"a\";", "start syntax A = \"a\";");
Expand All @@ -307,15 +306,15 @@ test bool synIterStarChanged2()
test bool synIterStarLayoutChanged1()
= expectEqualGrammar("syntax A = \"a\"*;", "syntax A = \"a\" *;");
test bool synIterStarLayoutChanged2()
= expectEqualGrammar("syntax A = \"a\"+;", "syntax A = \"a\" +;");
test bool synIterStarSepChanged1()
= expectNotEqualGrammar("syntax A = {\"a\" \"x\"}*;", "syntax A = {\"a\" \"x\"}+;");
= expectNotEqualGrammar("syntax A = {\"a\" \"x\"}*;", "syntax A = {\"a\" \"x\"}+;");
test bool synIterStarSepChanged2()
= expectNotEqualGrammar("syntax A = {\"a\" \"x\"}*;", "syntax A = {\"b\" \"x\"}*;");
= expectNotEqualGrammar("syntax A = {\"a\" \"x\"}*;", "syntax A = {\"b\" \"x\"}*;");
test bool synIterStarSepChanged3()
= expectNotEqualGrammar("syntax A = {\"a\" \"x\"}*;", "syntax A = {\"a\" \"y\"}*;");
Expand All @@ -336,10 +335,10 @@ test bool synIterPlusLayoutChanged()
= expectEqualGrammar("syntax A = {\"a\" \"x\"}+;", "syntax A = { \"a\" \"x\" } + ;");
test bool synIterPlusSepChanged1()
= expectNotEqualGrammar("syntax A = {\"a\" \"x\"}+;", "syntax A = {\"a\" \"x\"}*;");
= expectNotEqualGrammar("syntax A = {\"a\" \"x\"}+;", "syntax A = {\"a\" \"x\"}*;");
test bool synIterPlusSepChanged2()
= expectNotEqualGrammar("syntax A = {\"a\" \"x\"}+;", "syntax A = {\"b\" \"x\"}+;");
= expectNotEqualGrammar("syntax A = {\"a\" \"x\"}+;", "syntax A = {\"b\" \"x\"}+;");
test bool synIterPlusSepChanged3()
= expectNotEqualGrammar("syntax A = {\"a\" \"x\"}+;", "syntax A = {\"a\" \"y\"}+;");
Expand All @@ -356,10 +355,10 @@ test bool synOptionalLayoutChanged()
test bool synAltAdded()
= expectNotEqualGrammar("syntax A = \"a\";",
"syntax A = \"a\" | \"aa\";");
test bool synAltsSwitched()
= expectEqualGrammar("syntax A = \"a\" | \"b\";", "syntax A = \"b\" | \"a\";");
test bool synAltsChanged()
= expectNotEqualGrammar("syntax A = \"a\" | \"b\";", "syntax A = \"a\" | \"c\";");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module lang::rascalcore::compile::Examples::Tst4

void main(){
x = y = "b";
}
start syntax A = "a";

// test bool Stat3() = checkOK("value zz = { n = 1; n = true; }; ");

Expand Down

0 comments on commit 3dad72c

Please sign in to comment.