Skip to content

Commit 0f58857

Browse files
authored
Move compile-to-groovy visitors into nf-lang (#5910)
Signed-off-by: Ben Sherman <bentshermann@gmail.com>
1 parent 36153c6 commit 0f58857

File tree

10 files changed

+177
-192
lines changed

10 files changed

+177
-192
lines changed

modules/nextflow/src/main/groovy/nextflow/ast/GStringToLazyVisitor.groovy

Lines changed: 0 additions & 121 deletions
This file was deleted.

modules/nextflow/src/main/groovy/nextflow/ast/NextflowDSLImpl.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import nextflow.script.TokenStdoutCall
3636
import nextflow.script.TokenValCall
3737
import nextflow.script.TokenValRef
3838
import nextflow.script.TokenVar
39+
import nextflow.script.control.GStringToLazyVisitor
40+
import nextflow.script.control.TaskCmdXformVisitor
3941
import org.codehaus.groovy.ast.ASTNode
4042
import org.codehaus.groovy.ast.ClassCodeVisitorSupport
4143
import org.codehaus.groovy.ast.ClassNode

modules/nextflow/src/main/groovy/nextflow/ast/TaskCmdXform.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import java.lang.annotation.Target
2323

2424
import groovy.transform.CompileStatic
2525
import groovy.util.logging.Slf4j
26+
import nextflow.script.control.TaskCmdXformVisitor
2627
import org.codehaus.groovy.ast.ASTNode
2728
import org.codehaus.groovy.ast.ClassNode
2829
import org.codehaus.groovy.control.CompilePhase

modules/nextflow/src/main/groovy/nextflow/ast/TaskCmdXformVisitor.groovy

Lines changed: 0 additions & 62 deletions
This file was deleted.

modules/nextflow/src/main/groovy/nextflow/config/parser/v2/ConfigToGroovyXform.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import java.lang.annotation.RetentionPolicy
2222
import java.lang.annotation.Target
2323

2424
import groovy.transform.CompileStatic
25+
import nextflow.config.control.ConfigToGroovyVisitor
2526
import org.codehaus.groovy.ast.ASTNode
2627
import org.codehaus.groovy.control.CompilePhase
2728
import org.codehaus.groovy.control.SourceUnit

modules/nextflow/src/main/groovy/nextflow/script/parser/v2/ScriptCompiler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import nextflow.script.control.ModuleResolver;
3737
import nextflow.script.control.ResolveIncludeVisitor;
3838
import nextflow.script.control.ScriptResolveVisitor;
39+
import nextflow.script.control.ScriptToGroovyVisitor;
3940
import nextflow.script.control.TypeCheckingVisitor;
4041
import nextflow.script.parser.ScriptParserPluginFactory;
4142
import org.codehaus.groovy.ast.ASTNode;

modules/nextflow/src/main/groovy/nextflow/config/parser/v2/ConfigToGroovyVisitor.java renamed to modules/nf-lang/src/main/java/nextflow/config/control/ConfigToGroovyVisitor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package nextflow.config.parser.v2;
16+
package nextflow.config.control;
1717

1818
import java.util.ArrayList;
1919
import java.util.stream.Collectors;
@@ -34,8 +34,9 @@
3434
import static org.codehaus.groovy.ast.tools.GeneralUtils.*;
3535

3636
/**
37-
* Visitor to convert a Nextflow config AST into a
38-
* Groovy AST which is executed against {@link ConfigDsl}.
37+
* Transform a Nextflow config AST into a Groovy AST.
38+
*
39+
* @see nextflow.config.parser.v2.ConfigDsl
3940
*
4041
* @author Ben Sherman <bentshermann@gmail.com>
4142
*/
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2013-2024, Seqera Labs
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package nextflow.script.control;
18+
19+
import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
20+
import org.codehaus.groovy.ast.expr.ClosureExpression;
21+
import org.codehaus.groovy.ast.expr.Expression;
22+
import org.codehaus.groovy.ast.expr.GStringExpression;
23+
import org.codehaus.groovy.control.SourceUnit;
24+
25+
import static org.codehaus.groovy.ast.tools.GeneralUtils.*;
26+
27+
/**
28+
* Transform a GString to a Lazy GString.
29+
*
30+
* from
31+
* "${foo} ${bar}"
32+
* to
33+
* "${->foo} ${->bar}
34+
*
35+
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
36+
*/
37+
public class GStringToLazyVisitor extends ClassCodeVisitorSupport {
38+
39+
private SourceUnit sourceUnit;
40+
41+
private boolean inClosure;
42+
43+
public GStringToLazyVisitor(SourceUnit sourceUnit) {
44+
this.sourceUnit = sourceUnit;
45+
}
46+
47+
@Override
48+
protected SourceUnit getSourceUnit() {
49+
return sourceUnit;
50+
}
51+
52+
@Override
53+
public void visitClosureExpression(ClosureExpression node) {
54+
inClosure = true;
55+
try {
56+
super.visitClosureExpression(node);
57+
}
58+
finally {
59+
inClosure = false;
60+
}
61+
}
62+
63+
@Override
64+
public void visitGStringExpression(GStringExpression node) {
65+
// gstrings in a closure will be lazily evaluated and therefore
66+
// don't need to be lazy themselves
67+
if( !inClosure ) {
68+
transformToLazy(node);
69+
}
70+
}
71+
72+
private void transformToLazy(GStringExpression node) {
73+
var values = node.getValues();
74+
var lazyValues = new Expression[values.size()];
75+
76+
// wrap all non-closure expressions in a closure
77+
for( int i = 0; i < values.size(); i++ ) {
78+
var value = values.get(i);
79+
if( value instanceof ClosureExpression ) {
80+
// when the value is already a closure, skip the entire gstring
81+
// because it is assumed to already be lazy
82+
return;
83+
}
84+
lazyValues[i] = wrapExpressionInClosure(value);
85+
}
86+
87+
for( int i = 0; i < values.size(); i++ ) {
88+
values.set(i, lazyValues[i]);
89+
}
90+
}
91+
92+
protected ClosureExpression wrapExpressionInClosure(Expression node) {
93+
// note: the closure parameter argument must be *null* to force the creation of a closure like {-> something}
94+
// otherwise it creates a closure with an implicit parameter that is managed in a different manner by the
95+
// GString -- see http://docs.groovy-lang.org/latest/html/documentation/#_special_case_of_interpolating_closure_expressions
96+
return closureX(null, block(stmt(node)));
97+
}
98+
99+
}

modules/nextflow/src/main/groovy/nextflow/script/parser/v2/ScriptToGroovyVisitor.java renamed to modules/nf-lang/src/main/java/nextflow/script/control/ScriptToGroovyVisitor.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package nextflow.script.parser.v2;
16+
package nextflow.script.control;
1717

1818
import java.util.List;
1919
import java.util.Set;
2020
import java.util.stream.Collectors;
2121

22-
import nextflow.ast.GStringToLazyVisitor;
23-
import nextflow.ast.TaskCmdXformVisitor;
2422
import nextflow.script.ast.ASTNodeMarker;
2523
import nextflow.script.ast.AssignmentExpression;
2624
import nextflow.script.ast.FeatureFlagNode;
@@ -58,8 +56,9 @@
5856
import static org.codehaus.groovy.ast.tools.GeneralUtils.*;
5957

6058
/**
61-
* Visitor to convert a Nextflow script AST into a
62-
* Groovy AST which is executed against {@link BaseScript}.
59+
* Transform a Nextflow script AST into a Groovy AST.
60+
*
61+
* @see nextflow.script.BaseScript
6362
*
6463
* @author Ben Sherman <bentshermann@gmail.com>
6564
*/
@@ -349,7 +348,7 @@ private Expression varToStrX(Expression node) {
349348
}
350349

351350
protected ClosureExpression wrapExpressionInClosure(Expression node) {
352-
return closureX(block(new VariableScope(), stmt(node)));
351+
return closureX(block(stmt(node)));
353352
}
354353

355354
private Statement processWhen(Expression when) {

0 commit comments

Comments
 (0)