Skip to content

Commit

Permalink
implemented Type.glbWithModifySyntax and added subtype cases to Type …
Browse files Browse the repository at this point in the history
…and ParseTree
  • Loading branch information
jurgenvinju committed Nov 16, 2023
1 parent 49cb9c0 commit 17de9ab
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 43 deletions.
31 changes: 20 additions & 11 deletions src/org/rascalmpl/library/ParseTree.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -352,17 +352,26 @@ Symbol \layout(\keywords(n)) = layouts(n);
Symbol \layout(\layouts(n)) = layouts(n);
bool subtype(Symbol::\sort(_), Symbol::\adt("Tree", _)) = true;
bool subtype(Symbol::\parameterized-sort(_,_), Symbol::\adt("Tree", _)) = true;
bool subtype(Symbol::\lex(_), Symbol::\adt("Tree", _)) = true;
bool subtype(Symbol::\parameterized-lex(_,_), Symbol::\adt("Tree", _)) = true;
bool subtype(Symbol::\layouts(_), Symbol::\adt("Tree", _)) = true;
bool subtype(Symbol::\keywords(_), Symbol::\adt("Tree", _)) = true;
bool subtype(Symbol::\layouts(_), Symbol::\adt("Tree", _)) = true;
bool subtype(\syntax(_), Symbol::\adt("Tree", _)) = true;
bool subtype(\lexical(_), Symbol::\adt("Tree", _)) = true;
bool subtype(\layout(_), Symbol::\adt("Tree", _)) = true;
bool subtype(\keyword(_), Symbol::\adt("Tree", _)) = true;
bool subtype(Symbol::\sort(_), Symbol::\adt("Tree", [])) = true;
bool subtype(Symbol::\parameterized-sort(_,_), Symbol::\adt("Tree", [])) = true;
bool subtype(Symbol::\lex(_), Symbol::\adt("Tree", [])) = true;
bool subtype(Symbol::\parameterized-lex(_,_), Symbol::\adt("Tree", [])) = true;
bool subtype(Symbol::\layouts(_), Symbol::\adt("Tree", [])) = true;
bool subtype(Symbol::\keywords(_), Symbol::\adt("Tree", [])) = true;
bool subtype(Symbol::\layouts(_), Symbol::\adt("Tree", [])) = true;
bool subtype(\syntax(_), Symbol::\adt("Tree", [])) = true;
bool subtype(\lexical(_), Symbol::\adt("Tree", [])) = true;
bool subtype(\layout(_), Symbol::\adt("Tree", [])) = true;
bool subtype(\keyword(_), Symbol::\adt("Tree", [])) = true;
bool subtype(\data(parameter(_,_)), adt(_,_)) = true;
bool subtype(\syntax(parameter(_,_)), sort(_)) = true;
bool subtype(\syntax(parameter(_,_)), \parameterized-sort(_,_)) = true;
bool subtype(\lexical(parameter(_,_)), lex(_)) = true;
bool subtype(\lexical(parameter(_,_)), \parameterized-lex(_,_)) = true;
bool subtype(\keyword(parameter(_,_)), keywords(_)) = true;
bool subtype(\layout(parameter(_,_)), layouts(_)) = true;
@synopsis{Datatype for declaring preconditions and postconditions on symbols}
Expand Down
3 changes: 2 additions & 1 deletion src/org/rascalmpl/library/Type.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ data Symbol = \data(Symbol modified); // to-data modifier
Symbol \data(\data(Symbol s)) = \data(s);
Symbol \data(adt(n, ps)) = adt(n, ps);

bool subtype(\data(Symbol s), \node()) = true;
bool subtype(\data(Symbol s), \node()) = true;
bool subtype(\data(parameter(_,_)), adt(_,_)) = true;

@synopsis{A production in a grammar or constructor in a data type.}
@description{
Expand Down
93 changes: 68 additions & 25 deletions src/org/rascalmpl/types/ModifySyntaxRole.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
import java.util.Set;
import java.util.stream.StreamSupport;

import org.rascalmpl.ast.Nonterminal;
import org.rascalmpl.ast.SyntaxRoleModifier;
import org.rascalmpl.values.IRascalValueFactory;
import org.rascalmpl.values.RascalValueFactory;
import org.rascalmpl.values.parsetrees.SymbolAdapter;

import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.ISetWriter;
import io.usethesource.vallang.IValue;
Expand Down Expand Up @@ -79,18 +75,18 @@ else if (arg.isAbstractData()) {
return applyToData(arg);
}
else if (arg instanceof NonTerminalType) {
Type kind = ((NonTerminalType) arg).getSymbol().getConstructorType();
IConstructor symbol = ((NonTerminalType) arg).getSymbol();

if (kind == RascalValueFactory.Symbol_Sort || kind == RascalValueFactory.Symbol_ParameterizedSort) {
if (isSort(symbol) || isParameterizedSort(symbol)) {
return applyToSyntax((NonTerminalType) arg);
}
else if (kind == RascalValueFactory.Symbol_Lex || kind == RascalValueFactory.Symbol_ParameterizedLex) {
else if (isLex(symbol) || isParameterizedLex(symbol)) {
return applyToLexical((NonTerminalType) arg);
}
else if (kind == RascalValueFactory.Symbol_Layouts) {
else if (isLayouts(symbol)) {
return applyToLayout((NonTerminalType) arg);
}
else if (kind == RascalValueFactory.Symbol_Keywords) {
else if (isKeyword(symbol)) {
return applyToKeyword((NonTerminalType) arg);
}
}
Expand Down Expand Up @@ -135,10 +131,19 @@ else if (type instanceof Keyword) {
return RascalValueFactory.Tree;
}
else if (type instanceof Data) {
return TypeFactory.getInstance().nodeType();
return tf.nodeType();
}

return TypeFactory.getInstance().nodeType();
return tf.nodeType();
}

@Override
protected Type glbWithModifySyntax(RascalType type) {
if (type instanceof Syntax) {
return TF.modifyToSyntax(((ModifySyntaxRole) type).arg.glb(arg));
}

return tf.voidType();
}

@Override
Expand Down Expand Up @@ -247,10 +252,19 @@ else if (type instanceof Keyword) {
return RascalValueFactory.Tree;
}
else if (type instanceof Data) {
return TypeFactory.getInstance().nodeType();
return tf.nodeType();
}

return TypeFactory.getInstance().nodeType();
return tf.nodeType();
}

@Override
protected Type glbWithModifySyntax(RascalType type) {
if (type instanceof Lexical) {
return TF.modifyToLexical(((ModifySyntaxRole) type).arg.glb(arg));
}

return tf.voidType();
}

@Override
Expand Down Expand Up @@ -347,10 +361,19 @@ else if (type instanceof Keyword) {
return RascalValueFactory.Tree;
}
else if (type instanceof Data) {
return TypeFactory.getInstance().nodeType();
return tf.nodeType();
}

return TypeFactory.getInstance().nodeType();
return tf.nodeType();
}

@Override
protected Type glbWithModifySyntax(RascalType type) {
if (type instanceof Layout) {
return TF.modifyToLayout(((ModifySyntaxRole) type).arg.glb(arg));
}

return tf.voidType();
}


Expand Down Expand Up @@ -434,8 +457,6 @@ public IConstructor asSymbol(IValueFactory vf, TypeStore store, ISetWriter gramm
return vf.constructor(RascalValueFactory.Symbol_KeywordModifier, arg.asSymbol(vf, store, grammar, done));
}



@Override
protected Type lubWithModifySyntax(RascalType type) {
if (type instanceof Syntax) {
Expand All @@ -451,10 +472,20 @@ else if (type instanceof Keyword) {
return TF.modifyToKeyword(((ModifySyntaxRole) type).arg.lub(arg));
}
else if (type instanceof Data) {
return TypeFactory.getInstance().nodeType();
return tf.nodeType();
}

return tf.nodeType();
}

@Override
protected Type glbWithModifySyntax(RascalType type) {
if (type instanceof Keyword) {
return TF.modifyToKeyword(((ModifySyntaxRole) type).arg.glb(arg));
}


return TypeFactory.getInstance().nodeType();
return tf.voidType();
}


Expand Down Expand Up @@ -552,7 +583,16 @@ else if (type instanceof Data) {
return this;
}

return TypeFactory.getInstance().nodeType();
return tf.nodeType();
}

@Override
protected Type glbWithModifySyntax(RascalType type) {
if (type instanceof Data) {
return this;
}

return tf.voidType();
}

@Override
Expand Down Expand Up @@ -582,10 +622,10 @@ public Type applyToSyntax(NonTerminalType arg) {

if (arg.isParameterized()) {
Type[] params = SymbolAdapter.getParameters(arg.getSymbol()).stream().map(c -> TF.nonTerminalType((IConstructor) c)).toArray(Type[]::new);
return TypeFactory.getInstance().abstractDataType(new TypeStore(), name, params);
return tf.abstractDataType(new TypeStore(), name, params);
}
else {
return TypeFactory.getInstance().abstractDataType(new TypeStore(), name);
return tf.abstractDataType(new TypeStore(), name);
}
}

Expand All @@ -603,7 +643,7 @@ public Type applyToKeyword(NonTerminalType arg) {
@Override
public Type applyToLayout(NonTerminalType arg) {
String name = SymbolAdapter.getName(arg.getSymbol());
return TypeFactory.getInstance().abstractDataType(new TypeStore(), name);
return tf.abstractDataType(new TypeStore(), name);
}

@Override
Expand All @@ -629,13 +669,13 @@ protected Type lubWithAbstractData(Type type) {
return type;
}

return TypeFactory.getInstance().valueType();
return tf.valueType();
}

@Override
protected Type glbWithAbstractData(Type type) {
// this is for all the syntax roles that are not data. Data overrides this
return type == RascalValueFactory.Tree ? this : TypeFactory.getInstance().voidType();
return type == RascalValueFactory.Tree ? this : tf.voidType();
}

@Override
Expand All @@ -659,6 +699,9 @@ protected Type glb(RascalType type) {
return type.glbWithModifySyntax(this);
}

@Override
abstract protected Type glbWithModifySyntax(RascalType type);

@Override
protected boolean intersects(RascalType type) {
return type.intersectsWithModifySyntax(this);
Expand Down
7 changes: 1 addition & 6 deletions src/org/rascalmpl/values/parsetrees/SymbolAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,7 @@ public static boolean isStartSort(IConstructor tree) {
tree = delabel(tree);
return tree.getConstructorType() == Symbol_Start;
}

// public static boolean isStart(IConstructor tree) {
// tree = delabel(tree);
// return tree.getConstructorType() == Factory.Symbol_START;
// }


public static IConstructor getStart(IConstructor tree) {
if (isStartSort(tree)) {
tree = delabel(tree);
Expand Down

0 comments on commit 17de9ab

Please sign in to comment.