Skip to content

Commit

Permalink
implementing all the Type operations on Syntax modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenvinju committed Nov 16, 2023
1 parent 5fb57c8 commit 1e7e1d0
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 16 deletions.
166 changes: 151 additions & 15 deletions src/org/rascalmpl/types/ModifySyntaxRole.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -110,6 +115,34 @@ public Syntax(Type arg) {
super(arg);
}

@Override
public IConstructor asSymbol(IValueFactory vf, TypeStore store, ISetWriter grammar, Set<IConstructor> 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);
Expand Down Expand Up @@ -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<IConstructor> 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);
Expand Down Expand Up @@ -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<IConstructor> 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);
Expand Down Expand Up @@ -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<IConstructor> 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);
Expand Down Expand Up @@ -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<IConstructor> 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());
Expand Down Expand Up @@ -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;
Expand All @@ -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<IConstructor> done);

@Override
public IValue randomValue(Random random, IValueFactory vf, TypeStore store, Map<Type, Type> typeParameters,
Expand Down
5 changes: 4 additions & 1 deletion src/org/rascalmpl/types/RascalType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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;
}
Expand Down

0 comments on commit 1e7e1d0

Please sign in to comment.