Skip to content

Commit

Permalink
added match and instantiate for SyntaxRole modifier types, and remove…
Browse files Browse the repository at this point in the history
…d dead commented code
  • Loading branch information
jurgenvinju committed Nov 17, 2023
1 parent acea788 commit 366d3cc
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 69 deletions.
108 changes: 107 additions & 1 deletion src/org/rascalmpl/types/ModifySyntaxRole.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.usethesource.vallang.ISetWriter;
import io.usethesource.vallang.IValue;
import io.usethesource.vallang.IValueFactory;
import io.usethesource.vallang.exceptions.FactTypeUseException;
import io.usethesource.vallang.type.Type;
import io.usethesource.vallang.type.TypeFactory;
import io.usethesource.vallang.type.TypeStore;
Expand Down Expand Up @@ -162,6 +163,27 @@ public boolean isSubtypeOfNonTerminal(RascalType type) {
return SymbolAdapter.isSort(sym) || SymbolAdapter.isParameterizedSort(sym);
}

@Override
public boolean match(Type matched, Map<Type, Type> bindings) throws FactTypeUseException {
if (matched instanceof NonTerminalType) {
IConstructor sym = ((NonTerminalType) matched).getSymbol();

if (SymbolAdapter.isSort(sym) || SymbolAdapter.isParameterizedSort(sym)) {
return arg.match(matched, bindings);
}
}
else if (matched.isBottom()) {
return arg.match(matched, bindings);
}

return false;
}

@Override
public Type instantiate(Map<Type, Type> bindings) {
return TF.modifyToSyntax(arg.instantiate(bindings));
}

@Override
protected boolean isSubtypeOfAbstractData(Type type) {
// syntax[T] <: Tree
Expand Down Expand Up @@ -283,6 +305,27 @@ protected boolean isSubtypeOfAbstractData(Type type) {
return type == RascalValueFactory.Tree;
}

@Override
public boolean match(Type matched, Map<Type, Type> bindings) throws FactTypeUseException {
if (matched instanceof NonTerminalType) {
IConstructor sym = ((NonTerminalType) matched).getSymbol();

if (SymbolAdapter.isLex(sym) || SymbolAdapter.isParameterizedLex(sym)) {
return arg.match(matched, bindings);
}
}
else if (matched.isBottom()) {
return arg.match(matched, bindings);
}

return false;
}

@Override
public Type instantiate(Map<Type, Type> bindings) {
return TF.modifyToLexical(arg.instantiate(bindings));
}

@Override
protected Type applyToRole(ModifySyntaxRole role) {
return TF.modifyToLexical(role.arg);
Expand Down Expand Up @@ -398,6 +441,27 @@ protected boolean isSubtypeOfAbstractData(Type type) {
return type == RascalValueFactory.Tree;
}

@Override
public boolean match(Type matched, Map<Type, Type> bindings) throws FactTypeUseException {
if (matched instanceof NonTerminalType) {
IConstructor sym = ((NonTerminalType) matched).getSymbol();

if (SymbolAdapter.isLayouts(sym)) {
return arg.match(matched, bindings);
}
}
else if (matched.isBottom()) {
return arg.match(matched, bindings);
}

return false;
}

@Override
public Type instantiate(Map<Type, Type> bindings) {
return TF.modifyToLayout(arg.instantiate(bindings));
}

@Override
protected Type applyToRole(ModifySyntaxRole role) {
return TF.modifyToLayout(role.arg);
Expand Down Expand Up @@ -511,6 +575,27 @@ protected boolean isSubtypeOfAbstractData(Type type) {
return type == RascalValueFactory.Tree;
}

@Override
public boolean match(Type matched, Map<Type, Type> bindings) throws FactTypeUseException {
if (matched instanceof NonTerminalType) {
IConstructor sym = ((NonTerminalType) matched).getSymbol();

if (SymbolAdapter.isKeyword(sym)) {
return arg.match(matched, bindings);
}
}
else if (matched.isBottom()) {
return arg.match(matched, bindings);
}

return false;
}

@Override
public Type instantiate(Map<Type, Type> bindings) {
return TF.modifyToKeyword(arg.instantiate(bindings));
}

@Override
protected Type applyToRole(ModifySyntaxRole role) {
return TF.modifyToKeyword(role.arg);
Expand Down Expand Up @@ -586,7 +671,7 @@ else if (type instanceof Data) {
return tf.nodeType();
}

@Override
@Override
protected Type glbWithModifySyntax(RascalType type) {
if (type instanceof Data) {
return this;
Expand All @@ -605,6 +690,7 @@ public IConstructor asSymbol(IValueFactory vf, TypeStore store, ISetWriter gramm
return vf.constructor(RascalValueFactory.Symbol_DataModifier, arg.asSymbol(vf, store, grammar, done));
}


@Override
protected Type applyToRole(ModifySyntaxRole role) {
return TF.modifyToData(role.arg);
Expand All @@ -616,6 +702,20 @@ protected boolean isSubtypeOfAbstractData(Type type) {
return true;
}

@Override
public boolean match(Type matched, Map<Type, Type> bindings) throws FactTypeUseException {
if (matched.isAbstractData() || matched.isBottom()) {
return arg.match(matched, bindings);
}

return false;
}

@Override
public Type instantiate(Map<Type, Type> bindings) {
return TF.modifyToData(arg.instantiate(bindings));
}

@Override
public Type applyToSyntax(NonTerminalType arg) {
String name = SymbolAdapter.getName(arg.getSymbol());
Expand Down Expand Up @@ -721,6 +821,12 @@ public Type asAbstractDataType() {
@Override
abstract public IConstructor asSymbol(IValueFactory vf, TypeStore store, ISetWriter grammar, Set<IConstructor> done);

@Override
abstract public Type instantiate(Map<Type, Type> bindings);

@Override
abstract public boolean match(Type matched, Map<Type, Type> bindings) throws FactTypeUseException;

@Override
public IValue randomValue(Random random, IValueFactory vf, TypeStore store, Map<Type, Type> typeParameters,
int maxDepth, int maxBreadth) {
Expand Down
71 changes: 3 additions & 68 deletions src/org/rascalmpl/types/RascalTypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import io.usethesource.vallang.IList;
import io.usethesource.vallang.type.Type;
import io.usethesource.vallang.type.TypeFactory;
import io.usethesource.vallang.type.TypeStore;

public class RascalTypeFactory {
private TypeFactory tf = TypeFactory.getInstance();
Expand All @@ -35,12 +34,8 @@ public static RascalTypeFactory getInstance() {
}

public Type nonTerminalType(IConstructor cons) {
if (SymbolAdapter.isADT(cons)) {
// TODO: what if the ADT has parameters?
return TypeFactory.getInstance().abstractDataType(
new TypeStore(),
SymbolAdapter.getName(cons));
}
assert !SymbolAdapter.isADT(cons) && !SymbolAdapter.isParameter(cons);

return tf.externalType(new NonTerminalType(cons));
}

Expand Down Expand Up @@ -139,67 +134,7 @@ public Type modifyToLexical(Type arg) {
return tf.externalType(new ModifySyntaxRole.Lexical(arg).apply());
}

// if (arg.isParameter()) {
// // lazy modification.. we keep the modifier and wait for instantiation of the parameter
// return modifySyntax(Role.Syntax, arg);
// }
// else if (arg.isAbstractData()) {
// if (arg.isParameterized()) {
// return tf.externalType(new NonTerminalType(
// syntax(
// arg.getName(),
// StreamSupport.stream(arg.getTypeParameters().spliterator(), false).toArray(Type[]::new)
// )
// ));
// }
// else {
// return tf.externalType(new NonTerminalType(syntax(arg.getName())));
// }
// }
// else if (arg.isExternalType()) {
// RascalType argType = (RascalType) arg;

// if (argType.isNonterminal()) {
// NonTerminalType nt = (NonTerminalType) argType;
// Type symbol = nt.getSymbol().getConstructorType();

// if (symbol == RascalValueFactory.Symbol_Adt) {
// // this should never happen, but for robustness' sake
// return arg;
// }
// else {
// IString name = (IString) nt.getSymbol().get(0);

// if (symbol == RascalValueFactory.Symbol_Keywords) {
// assert !symbol.hasField("parameters");
// return tf.abstractDataType(new TypeStore(), name.getValue());
// }
// else if (symbol == RascalValueFactory.Symbol_Layouts) {
// assert !symbol.hasField("parameters");
// return tf.abstractDataType(new TypeStore(), name.getValue());
// }
// else if (symbol == RascalValueFactory.Symbol_Sort) {
// assert !symbol.hasField("parameters");
// return tf.abstractDataType(new TypeStore(), name.getValue());
// }
// else if (symbol == RascalValueFactory.Symbol_Lex) {
// assert !symbol.hasField("parameters");
// return tf.abstractDataType(new TypeStore(), name.getValue());
// }
// else if (symbol == RascalValueFactory.Symbol_ParameterizedLex
// || symbol == RascalValueFactory.Symbol_ParameterizedSort) {
// assert symbol.hasField("parameters");
// Type[] args = SymbolAdapter.getParameters(nt.getSymbol()).stream()
// .map(s -> nonTerminalType((IConstructor) s)).toArray(Type[]::new);
// return tf.abstractDataType(new TypeStore(), name.getValue(), args);
// }
// }
// }
// }

// throw new IllegalArgumentException("Can not modify " + arg + " to a data type");
// }


/**
* Changes a keyword, a lexical, a syntax or a layout type to a data type.
* When the modified type is not a named entity type like a syntax or lexical sort,
Expand Down

0 comments on commit 366d3cc

Please sign in to comment.