From 7c102bda52c67e1ac484b1c6aa3f839a6b36f842 Mon Sep 17 00:00:00 2001 From: "Jurgen J. Vinju" Date: Mon, 15 Apr 2024 16:34:52 +0200 Subject: [PATCH] added support for TextBlock expressions in Java M3 ASTs, the yield statement and lambda expressions. --- .../rascalmpl/library/lang/java/m3/AST.rsc | 6 +- .../lang/java/m3/internal/ASTConverter.java | 139 +++++------------- 2 files changed, 42 insertions(+), 103 deletions(-) diff --git a/src/org/rascalmpl/library/lang/java/m3/AST.rsc b/src/org/rascalmpl/library/lang/java/m3/AST.rsc index 48b768f55c4..58de33944a7 100644 --- a/src/org/rascalmpl/library/lang/java/m3/AST.rsc +++ b/src/org/rascalmpl/library/lang/java/m3/AST.rsc @@ -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) @@ -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 @@ -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 diff --git a/src/org/rascalmpl/library/lang/java/m3/internal/ASTConverter.java b/src/org/rascalmpl/library/lang/java/m3/internal/ASTConverter.java index 91638d7d228..c51a15b4703 100644 --- a/src/org/rascalmpl/library/lang/java/m3/internal/ASTConverter.java +++ b/src/org/rascalmpl/library/lang/java/m3/internal/ASTConverter.java @@ -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"}) @@ -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); @@ -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); @@ -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());