Skip to content

Commit

Permalink
added support for TextBlock expressions in Java M3 ASTs, the yield st…
Browse files Browse the repository at this point in the history
…atement and lambda expressions.
  • Loading branch information
jurgenvinju committed Apr 15, 2024
1 parent a5f8f0f commit 7c102bd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 103 deletions.
6 changes: 5 additions & 1 deletion src/org/rascalmpl/library/lang/java/m3/AST.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ data Expression
| \null()
| \number(str numberValue)
| \booleanLiteral(bool boolValue)
| \stringLiteral(str stringValue)
| \stringLiteral(str stringValue, str literal=stringValue)
| \textBlock(str stringValue, str literal=stringValue)
| \type(Type \type)
| \variable(str name, int extraDimensions)
| \variable(str name, int extraDimensions, Expression \initializer)
Expand All @@ -78,6 +79,8 @@ data Expression
| \normalAnnotation(str typeName, list[Expression] memberValuePairs)
| \memberValuePair(str name, Expression \value)
| \singleMemberAnnotation(str typeName, Expression \value)
| \lambda(list[Declaration] parameters, Statement block)
| \lambda(list[Declaration] parameters, Expression body)
;

data Statement
Expand Down Expand Up @@ -111,6 +114,7 @@ data Statement
| \expressionStatement(Expression stmt)
| \constructorCall(bool isSuper, Expression expr, list[Expression] arguments)
| \constructorCall(bool isSuper, list[Expression] arguments)
| \yield(Expression argument)
;

data Type
Expand Down
139 changes: 37 additions & 102 deletions src/org/rascalmpl/library/lang/java/m3/internal/ASTConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,105 +3,11 @@
import java.util.Iterator;
import java.util.Map;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration;
import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
import org.eclipse.jdt.core.dom.ArrayAccess;
import org.eclipse.jdt.core.dom.ArrayCreation;
import org.eclipse.jdt.core.dom.ArrayInitializer;
import org.eclipse.jdt.core.dom.ArrayType;
import org.eclipse.jdt.core.dom.AssertStatement;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.BlockComment;
import org.eclipse.jdt.core.dom.BodyDeclaration;
import org.eclipse.jdt.core.dom.BooleanLiteral;
import org.eclipse.jdt.core.dom.BreakStatement;
import org.eclipse.jdt.core.dom.CastExpression;
import org.eclipse.jdt.core.dom.CatchClause;
import org.eclipse.jdt.core.dom.CharacterLiteral;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ConditionalExpression;
import org.eclipse.jdt.core.dom.ConstructorInvocation;
import org.eclipse.jdt.core.dom.ContinueStatement;
import org.eclipse.jdt.core.dom.DoStatement;
import org.eclipse.jdt.core.dom.EmptyStatement;
import org.eclipse.jdt.core.dom.EnhancedForStatement;
import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
import org.eclipse.jdt.core.dom.EnumDeclaration;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.FieldAccess;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.ForStatement;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.Initializer;
import org.eclipse.jdt.core.dom.InstanceofExpression;
import org.eclipse.jdt.core.dom.Javadoc;
import org.eclipse.jdt.core.dom.LabeledStatement;
import org.eclipse.jdt.core.dom.LineComment;
import org.eclipse.jdt.core.dom.MarkerAnnotation;
import org.eclipse.jdt.core.dom.MemberRef;
import org.eclipse.jdt.core.dom.MemberValuePair;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.MethodRef;
import org.eclipse.jdt.core.dom.MethodRefParameter;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.NormalAnnotation;
import org.eclipse.jdt.core.dom.NullLiteral;
import org.eclipse.jdt.core.dom.NumberLiteral;
import org.eclipse.jdt.core.dom.PackageDeclaration;
import org.eclipse.jdt.core.dom.ParameterizedType;
import org.eclipse.jdt.core.dom.ParenthesizedExpression;
import org.eclipse.jdt.core.dom.PostfixExpression;
import org.eclipse.jdt.core.dom.PrefixExpression;
import org.eclipse.jdt.core.dom.PrimitiveType;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.QualifiedType;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
import org.eclipse.jdt.core.dom.SuperFieldAccess;
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
import org.eclipse.jdt.core.dom.SwitchCase;
import org.eclipse.jdt.core.dom.SwitchStatement;
import org.eclipse.jdt.core.dom.SynchronizedStatement;
import org.eclipse.jdt.core.dom.TagElement;
import org.eclipse.jdt.core.dom.TextElement;
import org.eclipse.jdt.core.dom.ThisExpression;
import org.eclipse.jdt.core.dom.ThrowStatement;
import org.eclipse.jdt.core.dom.TryStatement;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclarationStatement;
import org.eclipse.jdt.core.dom.TypeLiteral;
import org.eclipse.jdt.core.dom.TypeParameter;
import org.eclipse.jdt.core.dom.UnionType;
import org.eclipse.jdt.core.dom.VariableDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
import org.eclipse.jdt.core.dom.WhileStatement;
import org.eclipse.jdt.core.dom.WildcardType;
import org.eclipse.jdt.core.dom.*;

import io.usethesource.vallang.IListWriter;
import io.usethesource.vallang.ISourceLocation;
import io.usethesource.vallang.IString;
import io.usethesource.vallang.IValue;

@SuppressWarnings({"rawtypes", "deprecation"})
Expand Down Expand Up @@ -692,10 +598,10 @@ public boolean visit(MethodDeclaration node) {

IValue name = values.string(node.getName().getFullyQualifiedName());

IValueList parameters = new IValueList(values);
IListWriter parameters = values.listWriter();
for (Iterator it = node.parameters().iterator(); it.hasNext();) {
SingleVariableDeclaration v = (SingleVariableDeclaration) it.next();
parameters.add(visitChild(v));
parameters.append(visitChild(v));
}

IValueList possibleExceptions = new IValueList(values);
Expand All @@ -712,7 +618,7 @@ public boolean visit(MethodDeclaration node) {
body = constructStatementNode("empty");
}

ownValue = constructDeclarationNode(constructorName, returnType, name, parameters.asList(), possibleExceptions.asList(), body);
ownValue = constructDeclarationNode(constructorName, returnType, name, parameters.done(), possibleExceptions.asList(), body);
setKeywordParameters("modifiers", extendedModifiers);
// FIXME: this doesn't seem to be in use anymore
//setKeywordParameters("typeParameters", genericTypes);
Expand Down Expand Up @@ -938,12 +844,41 @@ public boolean visit(SingleVariableDeclaration node) {

public boolean visit(StringLiteral node) {

IValue value = values.string(node.getEscapedValue());
ownValue = constructExpressionNode("stringLiteral", value);
IString escaped = values.string(node.getEscapedValue());
IString literal = values.string(node.getLiteralValue());
ownValue = constructExpressionNode("stringLiteral", escaped).asWithKeywordParameters().setParameter("literal", literal);

return false;
}

@Override
public boolean visit(TextBlock node) {
IString escaped = values.string(node.getEscapedValue());
IString literal = values.string(node.getLiteralValue());
ownValue = constructExpressionNode("textBlock", escaped).asWithKeywordParameters().setParameter("literal", literal);

return false;
}

@Override
public boolean visit(YieldStatement node) {
IValue exp = visitChild(node.getExpression());
ownValue = constructStatementNode("yield", exp);
return false;
}

@Override
public boolean visit(LambdaExpression node) {
IListWriter parameters = values.listWriter();
for (Iterator it = node.parameters().iterator(); it.hasNext();) {
SingleVariableDeclaration v = (SingleVariableDeclaration) it.next();
parameters.append(visitChild(v));
}

ownValue = constructExpressionNode("lambda", parameters.done(), visitChild(node.getBody()));
return false;
}

public boolean visit(SuperConstructorInvocation node) {

IValue expression = node.getExpression() == null ? null : visitChild(node.getExpression());
Expand Down

0 comments on commit 7c102bd

Please sign in to comment.