From 366d3cc7c22f6c638a9c271690c335a67db891cd Mon Sep 17 00:00:00 2001 From: "Jurgen J. Vinju" Date: Fri, 17 Nov 2023 10:14:32 +0100 Subject: [PATCH] added match and instantiate for SyntaxRole modifier types, and removed dead commented code --- src/org/rascalmpl/types/ModifySyntaxRole.java | 108 +++++++++++++++++- .../rascalmpl/types/RascalTypeFactory.java | 71 +----------- 2 files changed, 110 insertions(+), 69 deletions(-) diff --git a/src/org/rascalmpl/types/ModifySyntaxRole.java b/src/org/rascalmpl/types/ModifySyntaxRole.java index 6a83d5356de..fd99266ff41 100644 --- a/src/org/rascalmpl/types/ModifySyntaxRole.java +++ b/src/org/rascalmpl/types/ModifySyntaxRole.java @@ -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; @@ -162,6 +163,27 @@ public boolean isSubtypeOfNonTerminal(RascalType type) { return SymbolAdapter.isSort(sym) || SymbolAdapter.isParameterizedSort(sym); } + @Override + public boolean match(Type matched, Map 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 bindings) { + return TF.modifyToSyntax(arg.instantiate(bindings)); + } + @Override protected boolean isSubtypeOfAbstractData(Type type) { // syntax[T] <: Tree @@ -283,6 +305,27 @@ protected boolean isSubtypeOfAbstractData(Type type) { return type == RascalValueFactory.Tree; } + @Override + public boolean match(Type matched, Map 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 bindings) { + return TF.modifyToLexical(arg.instantiate(bindings)); + } + @Override protected Type applyToRole(ModifySyntaxRole role) { return TF.modifyToLexical(role.arg); @@ -398,6 +441,27 @@ protected boolean isSubtypeOfAbstractData(Type type) { return type == RascalValueFactory.Tree; } + @Override + public boolean match(Type matched, Map 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 bindings) { + return TF.modifyToLayout(arg.instantiate(bindings)); + } + @Override protected Type applyToRole(ModifySyntaxRole role) { return TF.modifyToLayout(role.arg); @@ -511,6 +575,27 @@ protected boolean isSubtypeOfAbstractData(Type type) { return type == RascalValueFactory.Tree; } + @Override + public boolean match(Type matched, Map 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 bindings) { + return TF.modifyToKeyword(arg.instantiate(bindings)); + } + @Override protected Type applyToRole(ModifySyntaxRole role) { return TF.modifyToKeyword(role.arg); @@ -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; @@ -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); @@ -616,6 +702,20 @@ protected boolean isSubtypeOfAbstractData(Type type) { return true; } + @Override + public boolean match(Type matched, Map bindings) throws FactTypeUseException { + if (matched.isAbstractData() || matched.isBottom()) { + return arg.match(matched, bindings); + } + + return false; + } + + @Override + public Type instantiate(Map bindings) { + return TF.modifyToData(arg.instantiate(bindings)); + } + @Override public Type applyToSyntax(NonTerminalType arg) { String name = SymbolAdapter.getName(arg.getSymbol()); @@ -721,6 +821,12 @@ public Type asAbstractDataType() { @Override abstract public IConstructor asSymbol(IValueFactory vf, TypeStore store, ISetWriter grammar, Set done); + @Override + abstract public Type instantiate(Map bindings); + + @Override + abstract public boolean match(Type matched, Map bindings) throws FactTypeUseException; + @Override public IValue randomValue(Random random, IValueFactory vf, TypeStore store, Map typeParameters, int maxDepth, int maxBreadth) { diff --git a/src/org/rascalmpl/types/RascalTypeFactory.java b/src/org/rascalmpl/types/RascalTypeFactory.java index 8422b55e58d..994816d119f 100644 --- a/src/org/rascalmpl/types/RascalTypeFactory.java +++ b/src/org/rascalmpl/types/RascalTypeFactory.java @@ -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(); @@ -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)); } @@ -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,