Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move compile-to-groovy visitors into nf-lang #5910

Merged
merged 1 commit into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import nextflow.script.TokenStdoutCall
import nextflow.script.TokenValCall
import nextflow.script.TokenValRef
import nextflow.script.TokenVar
import nextflow.script.control.GStringToLazyVisitor
import nextflow.script.control.TaskCmdXformVisitor
import org.codehaus.groovy.ast.ASTNode
import org.codehaus.groovy.ast.ClassCodeVisitorSupport
import org.codehaus.groovy.ast.ClassNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import java.lang.annotation.Target

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import nextflow.script.control.TaskCmdXformVisitor
import org.codehaus.groovy.ast.ASTNode
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.control.CompilePhase
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Target

import groovy.transform.CompileStatic
import nextflow.config.control.ConfigToGroovyVisitor
import org.codehaus.groovy.ast.ASTNode
import org.codehaus.groovy.control.CompilePhase
import org.codehaus.groovy.control.SourceUnit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import nextflow.script.control.ModuleResolver;
import nextflow.script.control.ResolveIncludeVisitor;
import nextflow.script.control.ScriptResolveVisitor;
import nextflow.script.control.ScriptToGroovyVisitor;
import nextflow.script.control.TypeCheckingVisitor;
import nextflow.script.parser.ScriptParserPluginFactory;
import org.codehaus.groovy.ast.ASTNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nextflow.config.parser.v2;
package nextflow.config.control;

import java.util.ArrayList;
import java.util.stream.Collectors;
Expand All @@ -34,8 +34,9 @@
import static org.codehaus.groovy.ast.tools.GeneralUtils.*;

/**
* Visitor to convert a Nextflow config AST into a
* Groovy AST which is executed against {@link ConfigDsl}.
* Transform a Nextflow config AST into a Groovy AST.
*
* @see nextflow.config.parser.v2.ConfigDsl
*
* @author Ben Sherman <bentshermann@gmail.com>
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2013-2024, Seqera Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package nextflow.script.control;

import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
import org.codehaus.groovy.ast.expr.ClosureExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.GStringExpression;
import org.codehaus.groovy.control.SourceUnit;

import static org.codehaus.groovy.ast.tools.GeneralUtils.*;

/**
* Transform a GString to a Lazy GString.
*
* from
* "${foo} ${bar}"
* to
* "${->foo} ${->bar}
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
public class GStringToLazyVisitor extends ClassCodeVisitorSupport {

private SourceUnit sourceUnit;

private boolean inClosure;

public GStringToLazyVisitor(SourceUnit sourceUnit) {
this.sourceUnit = sourceUnit;
}

@Override
protected SourceUnit getSourceUnit() {
return sourceUnit;
}

@Override
public void visitClosureExpression(ClosureExpression node) {
inClosure = true;
try {
super.visitClosureExpression(node);
}
finally {
inClosure = false;
}
}

@Override
public void visitGStringExpression(GStringExpression node) {
// gstrings in a closure will be lazily evaluated and therefore
// don't need to be lazy themselves
if( !inClosure ) {
transformToLazy(node);
}
}

private void transformToLazy(GStringExpression node) {
var values = node.getValues();
var lazyValues = new Expression[values.size()];

// wrap all non-closure expressions in a closure
for( int i = 0; i < values.size(); i++ ) {
var value = values.get(i);
if( value instanceof ClosureExpression ) {
// when the value is already a closure, skip the entire gstring
// because it is assumed to already be lazy
return;
}
lazyValues[i] = wrapExpressionInClosure(value);
}

for( int i = 0; i < values.size(); i++ ) {
values.set(i, lazyValues[i]);
}
}

protected ClosureExpression wrapExpressionInClosure(Expression node) {
// note: the closure parameter argument must be *null* to force the creation of a closure like {-> something}
// otherwise it creates a closure with an implicit parameter that is managed in a different manner by the
// GString -- see http://docs.groovy-lang.org/latest/html/documentation/#_special_case_of_interpolating_closure_expressions
return closureX(null, block(stmt(node)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nextflow.script.parser.v2;
package nextflow.script.control;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import nextflow.ast.GStringToLazyVisitor;
import nextflow.ast.TaskCmdXformVisitor;
import nextflow.script.ast.ASTNodeMarker;
import nextflow.script.ast.AssignmentExpression;
import nextflow.script.ast.FeatureFlagNode;
Expand Down Expand Up @@ -58,8 +56,9 @@
import static org.codehaus.groovy.ast.tools.GeneralUtils.*;

/**
* Visitor to convert a Nextflow script AST into a
* Groovy AST which is executed against {@link BaseScript}.
* Transform a Nextflow script AST into a Groovy AST.
*
* @see nextflow.script.BaseScript
*
* @author Ben Sherman <bentshermann@gmail.com>
*/
Expand Down Expand Up @@ -349,7 +348,7 @@ private Expression varToStrX(Expression node) {
}

protected ClosureExpression wrapExpressionInClosure(Expression node) {
return closureX(block(new VariableScope(), stmt(node)));
return closureX(block(stmt(node)));
}

private Statement processWhen(Expression when) {
Expand Down
Loading
Loading