From 1e7e1d0d7499d4b1dbbf3ad5892bb90bd86be008 Mon Sep 17 00:00:00 2001 From: "Jurgen J. Vinju" Date: Thu, 16 Nov 2023 16:10:42 +0100 Subject: [PATCH] implementing all the Type operations on Syntax modifiers --- src/org/rascalmpl/types/ModifySyntaxRole.java | 166 ++++++++++++++++-- src/org/rascalmpl/types/RascalType.java | 5 +- 2 files changed, 155 insertions(+), 16 deletions(-) diff --git a/src/org/rascalmpl/types/ModifySyntaxRole.java b/src/org/rascalmpl/types/ModifySyntaxRole.java index 9b543a95b78..53234ff26a7 100644 --- a/src/org/rascalmpl/types/ModifySyntaxRole.java +++ b/src/org/rascalmpl/types/ModifySyntaxRole.java @@ -9,12 +9,17 @@ import java.util.Map; import java.util.Random; +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; import io.usethesource.vallang.IValueFactory; import io.usethesource.vallang.type.Type; @@ -110,6 +115,34 @@ public Syntax(Type arg) { super(arg); } + @Override + public IConstructor asSymbol(IValueFactory vf, TypeStore store, ISetWriter grammar, Set done) { + return vf.constructor(RascalValueFactory.Symbol_SyntaxModifier, arg.asSymbol(vf, store, grammar, done)); + } + + + @Override + public boolean isSubtypeOfSyntaxModifier(RascalType type) { + assert isOpen(); + return type instanceof Syntax; + } + + @Override + public boolean isSubtypeOfNonTerminal(RascalType type) { + assert isOpen(); // otherwise we wouldn't be here + + NonTerminalType nt = (NonTerminalType) type; + IConstructor sym = nt.getSymbol(); + + return SymbolAdapter.isSort(sym) || SymbolAdapter.isParameterizedSort(sym); + } + + @Override + protected boolean isSubtypeOfAbstractData(Type type) { + // syntax[T] <: Tree + return type == RascalValueFactory.Tree; + } + @Override protected Type applyToRole(ModifySyntaxRole role) { return TF.modifyToSyntax(role.arg); @@ -168,6 +201,33 @@ public Lexical(Type arg) { super(arg); } + @Override + public boolean isSubtypeOfSyntaxModifier(RascalType type) { + assert isOpen(); + return type instanceof Lexical; + } + + @Override + public IConstructor asSymbol(IValueFactory vf, TypeStore store, ISetWriter grammar, Set done) { + return vf.constructor(RascalValueFactory.Symbol_LexicalModifier, arg.asSymbol(vf, store, grammar, done)); + } + + @Override + public boolean isSubtypeOfNonTerminal(RascalType type) { + assert isOpen(); // otherwise we wouldn't be here + + NonTerminalType nt = (NonTerminalType) type; + IConstructor sym = nt.getSymbol(); + + return SymbolAdapter.isLex(sym) || SymbolAdapter.isParameterizedLex(sym); + } + + @Override + protected boolean isSubtypeOfAbstractData(Type type) { + // syntax[T] <: Tree + return type == RascalValueFactory.Tree; + } + @Override protected Type applyToRole(ModifySyntaxRole role) { return TF.modifyToLexical(role.arg); @@ -224,6 +284,33 @@ public Layout(Type arg) { super(arg); } + @Override + public boolean isSubtypeOfSyntaxModifier(RascalType type) { + assert isOpen(); + return type instanceof Layout; + } + + @Override + public IConstructor asSymbol(IValueFactory vf, TypeStore store, ISetWriter grammar, Set done) { + return vf.constructor(RascalValueFactory.Symbol_LayoutModifier, arg.asSymbol(vf, store, grammar, done)); + } + + @Override + public boolean isSubtypeOfNonTerminal(RascalType type) { + assert isOpen(); // otherwise we wouldn't be here + + NonTerminalType nt = (NonTerminalType) type; + IConstructor sym = nt.getSymbol(); + + return SymbolAdapter.isLayouts(sym); + } + + @Override + protected boolean isSubtypeOfAbstractData(Type type) { + // syntax[T] <: Tree + return type == RascalValueFactory.Tree; + } + @Override protected Type applyToRole(ModifySyntaxRole role) { return TF.modifyToLayout(role.arg); @@ -277,7 +364,34 @@ public static class Keyword extends ModifySyntaxRole { public Keyword(Type arg) { super(arg); } - + + @Override + public IConstructor asSymbol(IValueFactory vf, TypeStore store, ISetWriter grammar, Set done) { + return vf.constructor(RascalValueFactory.Symbol_KeywordModifier, arg.asSymbol(vf, store, grammar, done)); + } + + @Override + public boolean isSubtypeOfSyntaxModifier(RascalType type) { + assert isOpen(); + return type instanceof Keyword; + } + + @Override + public boolean isSubtypeOfNonTerminal(RascalType type) { + assert isOpen(); // otherwise we wouldn't be here + + NonTerminalType nt = (NonTerminalType) type; + IConstructor sym = nt.getSymbol(); + + return SymbolAdapter.isKeyword(sym); + } + + @Override + protected boolean isSubtypeOfAbstractData(Type type) { + // syntax[T] <: Tree + return type == RascalValueFactory.Tree; + } + @Override protected Type applyToRole(ModifySyntaxRole role) { return TF.modifyToKeyword(role.arg); @@ -326,11 +440,33 @@ public Data(Type arg) { super(arg); } + @Override + public boolean isSubtypeOfSyntaxModifier(RascalType type) { + assert isOpen(); + return type instanceof Data; + } + + @Override + protected boolean isSupertypeOf(Type type) { + return type.isSubtypeOf(this); + } + + @Override + public IConstructor asSymbol(IValueFactory vf, TypeStore store, ISetWriter grammar, Set done) { + return vf.constructor(RascalValueFactory.Symbol_DataModifier, arg.asSymbol(vf, store, grammar, done)); + } + @Override protected Type applyToRole(ModifySyntaxRole role) { return TF.modifyToData(role.arg); } + @Override + protected boolean isSubtypeOfAbstractData(Type type) { + assert isOpen(); + return true; + } + @Override public Type applyToSyntax(NonTerminalType arg) { String name = SymbolAdapter.getName(arg.getSymbol()); @@ -367,6 +503,17 @@ public Type applyToData(Type arg) { } } + @Override + protected boolean isSupertypeOf(RascalType type) { + return type.isSubtypeOfSyntaxModifier(this); + } + + @Override + protected boolean isSubtypeOfNode(Type type) { + // all syntax roles are sub-type of node + return true; + } + @Override protected Type lub(RascalType type) { return null; @@ -385,23 +532,12 @@ protected boolean intersects(RascalType type) { } @Override - protected boolean isSupertypeOf(RascalType type) { - if (type.isRoleModifier()) { - - } - else if (isOpen()) { - - } - else { - - } + public Type asAbstractDataType() { + return RascalValueFactory.Symbol; } @Override - public Type asAbstractDataType() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'asAbstractDataType'"); - } + abstract public IConstructor asSymbol(IValueFactory vf, TypeStore store, ISetWriter grammar, Set done); @Override public IValue randomValue(Random random, IValueFactory vf, TypeStore store, Map typeParameters, diff --git a/src/org/rascalmpl/types/RascalType.java b/src/org/rascalmpl/types/RascalType.java index 78d4c58986b..69407d5c83f 100644 --- a/src/org/rascalmpl/types/RascalType.java +++ b/src/org/rascalmpl/types/RascalType.java @@ -22,7 +22,6 @@ protected Type glbWithExternal(Type type) { @Override protected boolean intersectsWithExternal(Type type) { assert type instanceof RascalType; - System.err.println(this.toString() + "Intersects with " + type); return intersects((RascalType) type); } @@ -42,6 +41,10 @@ public boolean isSubtypeOfNonTerminal(RascalType type) { return false; } + public boolean isSubtypeOfSyntaxModifier(RascalType type) { + return false; + } + protected boolean isSubtypeOfFunction(RascalType type) { return false; }